1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use anyhow::Result;
use termusiclib::player::playlist_helpers::PlaylistRemoveTrackType;
use tokio::sync::mpsc::UnboundedReceiver;
use crate::ui::{
model::TxToMain,
msg::{Msg, ServerReqResponse},
music_player_client::Playback,
tui_cmd::{PlaylistCmd, TuiCmd},
};
/// Actor that handles all requests to the Server via GRPC.
///
/// This actor can be given commands via [`TuiCmd`] and responds on [`TxToMain`].
pub struct ServerRequestActor {
client_handle: Playback,
rx_cmd: UnboundedReceiver<TuiCmd>,
tx_main: TxToMain,
}
impl ServerRequestActor {
/// Create and start a new [`ServerRequestActor`].
///
/// To shutdown this actor, close `rx_cmd` channel.
pub fn start_actor(
client_handle: Playback,
rx_cmd: UnboundedReceiver<TuiCmd>,
tx_main: TxToMain,
) {
let obj = Self {
client_handle,
rx_cmd,
tx_main,
};
// clippy suggests manually dropping instead of "let _ =" on a future, which "JoinHandle" is
let jh = tokio::spawn(Self::run(obj));
drop(jh);
}
/// The actor loop.
async fn run(mut actor: Self) {
while let Some(cmd) = actor.rx_cmd.recv().await {
if let Err(err) = actor.handle_cmd(cmd).await {
error!("Error processing command to server: {err:#?}");
}
}
}
/// Handle all commands to the server and their responses.
async fn handle_cmd(&mut self, cmd: TuiCmd) -> Result<()> {
match cmd {
TuiCmd::TogglePause => {
// result will be populated back via UpdateStream
let _ = self.client_handle.toggle_pause().await?;
}
TuiCmd::SeekForward => {
// result will be populated back via UpdateStream
let _ = self.client_handle.seek_forward().await?;
}
TuiCmd::SeekBackward => {
// result will be populated back via UpdateStream
let _ = self.client_handle.seek_backward().await?;
}
TuiCmd::VolumeUp => {
// result will be populated back via UpdateStream
let _ = self.client_handle.volume_up().await?;
}
TuiCmd::VolumeDown => {
// result will be populated back via UpdateStream
let _ = self.client_handle.volume_down().await?;
}
TuiCmd::SpeedUp => {
// result will be populated back via UpdateStream
let _ = self.client_handle.speed_up().await?;
}
TuiCmd::SpeedDown => {
// result will be populated back via UpdateStream
let _ = self.client_handle.speed_down().await?;
}
TuiCmd::SkipNext => {
// result will be populated back via UpdateStream
self.client_handle.skip_next().await?;
}
TuiCmd::SkipPrevious => {
// result will be populated back via UpdateStream
self.client_handle.skip_previous().await?;
}
TuiCmd::ToggleGapless => {
// result will be populated back via UpdateStream
let _ = self.client_handle.toggle_gapless().await?;
}
TuiCmd::CycleLoop => {
// result will be populated back via UpdateStream
let _ = self.client_handle.cycle_loop().await?;
}
TuiCmd::GetProgress => {
let res = self.client_handle.get_progress().await?;
self.send_response(Msg::ServerReqResponse(ServerReqResponse::GetProgress(res)));
}
TuiCmd::ReloadConfig => {
self.client_handle.reload_config().await?;
}
TuiCmd::Playlist(playlist_cmd) => self.handle_playlist_cmd(playlist_cmd).await?,
TuiCmd::QuitServer => {
let () = self.client_handle.quit_server().await?;
}
}
Ok(())
}
/// Handle Playlist requests.
async fn handle_playlist_cmd(&mut self, cmd: PlaylistCmd) -> Result<()> {
match cmd {
PlaylistCmd::PlaySpecific(playlist_play_specific) => {
// result will be populated back via UpdateStream
self.client_handle
.play_specific(playlist_play_specific)
.await?;
}
PlaylistCmd::AddTrack(playlist_add_track) => {
// result will be populated back via UpdateStream
self.client_handle
.add_to_playlist(playlist_add_track)
.await?;
}
PlaylistCmd::RemoveTrack(playlist_remove_track_indexed) => {
// result will be populated back via UpdateStream
self.client_handle
.remove_from_playlist(PlaylistRemoveTrackType::Indexed(
playlist_remove_track_indexed,
))
.await?;
}
PlaylistCmd::Clear => {
// result will be populated back via UpdateStream
self.client_handle
.remove_from_playlist(PlaylistRemoveTrackType::Clear)
.await?;
}
PlaylistCmd::SwapTrack(playlist_swap_track) => {
// result will be populated back via UpdateStream
self.client_handle.swap_tracks(playlist_swap_track).await?;
}
PlaylistCmd::Shuffle => {
// result will be populated back via UpdateStream
self.client_handle.shuffle_playlist().await?;
}
PlaylistCmd::RemoveDeletedItems => {
// result will be populated back via UpdateStream
self.client_handle.remove_deleted_tracks().await?;
}
PlaylistCmd::SelfReloadPlaylist => {
let tracks = self.client_handle.get_playlist().await?;
self.send_response(Msg::ServerReqResponse(ServerReqResponse::FullPlaylist(
tracks,
)));
}
}
Ok(())
}
#[inline]
fn send_response(&self, msg: Msg) {
let _ = self.tx_main.send(msg);
}
}