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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

use anyhow::anyhow;
use bytes::BytesMut;

use crate::platform::Platform;
use crate::request::Request;
use crate::response::{OutputError, Response};
use crate::watch::{Average, Watch};

#[derive(Debug)]
pub enum CommandError {
    OutputError(OutputError),
    ClientError(anyhow::Error),
    ServerError(anyhow::Error),
}

#[macro_export]
macro_rules! server_error {
    ($err:expr $(,)?) => ({
        use anyhow::anyhow;
        jupiter::commands::CommandError::ServerError(anyhow!($err))
    });
    ($fmt:expr, $($arg:tt)*) => {
        use anyhow::anyhow;
        jupiter::commands::CommandError::ServerError(anyhow!($fmt, $($arg)*))
    };
}

#[macro_export]
macro_rules! client_error {
    ($err:expr $(,)?) => ({
        use anyhow::anyhow;
        jupiter::commands::CommandError::ClientError(anyhow!($err))
    });
    ($fmt:expr, $($arg:tt)*) => {
        use anyhow::anyhow;
        jupiter::commands::CommandError::ClientError(anyhow!($fmt, $($arg)*))
    };
}

impl From<OutputError> for CommandError {
    fn from(output_error: OutputError) -> Self {
        CommandError::OutputError(output_error)
    }
}

impl From<anyhow::Error> for CommandError {
    fn from(error: anyhow::Error) -> Self {
        CommandError::ClientError(error)
    }
}

pub type CommandResult = std::result::Result<(), CommandError>;

pub trait ResultExt {
    fn complete(self, call: Call);
}

impl ResultExt for CommandResult {
    fn complete(self, call: Call) {
        call.complete(self);
    }
}

pub struct Call {
    pub request: Request,
    pub response: Response,
    pub token: usize,
    callback: tokio::sync::oneshot::Sender<Result<BytesMut, OutputError>>,
}

impl Call {
    pub fn complete(mut self, result: CommandResult) {
        let result = match result {
            Ok(_) => self.response.complete(),
            Err(CommandError::OutputError(error)) => Err(error),
            Err(CommandError::ClientError(error)) => {
                if let Err(error) = self.response.error(&format!("CLIENT: {}", error)) {
                    Err(error)
                } else {
                    self.response.complete()
                }
            }
            Err(CommandError::ServerError(error)) => {
                if let Err(error) = self.response.error(&format!("SERVER: {}", error)) {
                    Err(error)
                } else {
                    self.response.complete()
                }
            }
        };

        if let Err(_) = self.callback.send(result) {
            log::error!("Failed to submit a result to a oneshot callback channel!");
        }
    }
}

pub type Queue = tokio::sync::mpsc::Sender<Call>;
pub type Endpoint = tokio::sync::mpsc::Receiver<Call>;

pub fn queue() -> (Queue, Endpoint) {
    tokio::sync::mpsc::channel(1024)
}

pub struct Command {
    pub name: &'static str,
    queue: Queue,
    token: usize,
    call_metrics: Average,
}

impl Command {
    pub fn call_count(&self) -> i32 {
        self.call_metrics.count() as i32
    }

    pub fn avg_duration(&self) -> i32 {
        self.call_metrics.avg() as i32
    }
}

pub struct CommandDictionary {
    commands: Mutex<HashMap<&'static str, Arc<Command>>>,
}

pub struct Dispatcher {
    commands: HashMap<&'static str, (Arc<Command>, Queue)>,
}

impl CommandDictionary {
    pub fn new() -> Self {
        CommandDictionary {
            commands: Mutex::new(HashMap::default()),
        }
    }

    pub fn install(platform: &Arc<Platform>) -> Arc<Self> {
        let commands = Arc::new(CommandDictionary::new());
        platform.register::<CommandDictionary>(commands.clone());

        commands
    }

    pub fn register_command(&self, name: &'static str, queue: Queue, token: usize) {
        let mut commands = self.commands.lock().unwrap();
        if commands.get(name).is_some() {
            log::error!("Not going to register command {} as there is already a command present for this name",
                   name);
        } else {
            log::debug!("Registering command {}...", name);
            commands.insert(
                name,
                Arc::new(Command {
                    name,
                    queue,
                    token,
                    call_metrics: Average::new(),
                }),
            );
        }
    }

    pub fn commands(&self) -> Vec<Arc<Command>> {
        let mut result = Vec::new();
        for command in self.commands.lock().unwrap().values() {
            result.push(command.clone());
        }

        return result;
    }

    pub fn dispatcher(&self) -> Dispatcher {
        let commands = self.commands.lock().unwrap();
        let mut cloned_commands = HashMap::with_capacity(commands.len());
        for command in commands.values() {
            cloned_commands.insert(command.name, (command.clone(), command.queue.clone()));
        }

        Dispatcher {
            commands: cloned_commands,
        }
    }
}

impl Dispatcher {
    pub async fn invoke(&mut self, request: Request) -> Result<BytesMut, OutputError> {
        let mut response = Response::new();
        match self.commands.get_mut(request.command()) {
            Some((command, queue)) => {
                Dispatcher::invoke_command(command, queue, request, response).await
            }
            _ => {
                response.error(&format!("CLIENT: Unknown command: {}", request.command()))?;
                Ok(response.complete()?)
            }
        }
    }

    async fn invoke_command(
        command: &Arc<Command>,
        queue: &mut Queue,
        request: Request,
        response: Response,
    ) -> Result<BytesMut, OutputError> {
        let (callback, promise) = tokio::sync::oneshot::channel();
        let task = Call {
            request,
            response,
            callback,
            token: command.token,
        };

        let watch = Watch::start();
        if let Err(_) = queue.send(task).await {
            Err(OutputError::ProtocolError(anyhow!(
                "Failed to submit command into queue!"
            )))
        } else {
            match promise.await {
                Ok(result) => {
                    command.call_metrics.add(watch.micros());
                    result
                }
                _ => Err(OutputError::ProtocolError(anyhow!(
                    "Command {} did not yield any result!",
                    command.name
                ))),
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use bytes::BytesMut;
    use num_derive::FromPrimitive;
    use num_traits::FromPrimitive;

    use crate::commands::{queue, Call, CommandDictionary, CommandError, CommandResult, ResultExt};
    use crate::request::Request;

    fn ping(task: &mut Call) -> CommandResult {
        task.response.simple("PONG")?;
        Ok(())
    }

    fn test(task: &mut Call) -> CommandResult {
        task.response.simple("OK")?;
        Ok(())
    }

    #[derive(FromPrimitive)]
    enum TestCommands {
        Ping,
        Test,
    }

    #[test]
    fn a_command_can_be_executed() {
        tokio_test::block_on(async {
            let (queue, mut endpoint) = queue();
            tokio::spawn(async move {
                loop {
                    match endpoint.recv().await {
                        Some(mut call) => match TestCommands::from_usize(call.token) {
                            Some(TestCommands::Ping) => ping(&mut call).complete(call),
                            Some(TestCommands::Test) => test(&mut call).complete(call),
                            _ => call.complete(Err(CommandError::ServerError(anyhow::anyhow!(
                                "Unknown token received!"
                            )))),
                        },
                        _ => return,
                    }
                }
            });

            let commands = CommandDictionary::new();
            commands.register_command("PING", queue.clone(), TestCommands::Ping as usize);
            commands.register_command("TEST", queue.clone(), TestCommands::Test as usize);
            let mut dispatcher = commands.dispatcher();

            let request = Request::parse(&mut BytesMut::from("*1\r\n$4\r\nPING\r\n"))
                .unwrap()
                .unwrap();
            let result = dispatcher.invoke(request).await.unwrap();
            assert_eq!(std::str::from_utf8(&result[..]).unwrap(), "+PONG\r\n");

            let request = Request::parse(&mut BytesMut::from("*1\r\n$4\r\nTEST\r\n"))
                .unwrap()
                .unwrap();
            let result = dispatcher.invoke(request).await.unwrap();
            assert_eq!(std::str::from_utf8(&result[..]).unwrap(), "+OK\r\n");
        });
    }
}