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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
extern crate rand;

use commands::RedisCommand;
use errors::RedisError;
use reader::Reader;
use results::RedisResult;
use self::rand::Rng;
use std::collections::HashMap;
use std::fmt;
use std::io::BufReader;
use std::io::prelude::*;
use std::net::TcpStream;
use std::sync::mpsc::*;
use std::time::Duration;
use std::thread;
use std::u32;
use types::{PubSubType, SenderType};

pub struct RedisClient {
    port: &'static str,
    host: &'static str,
    buffer: BufReader<TcpStream>,
}

pub struct RedisClientAsync {
    port: &'static str,
    host: &'static str,
    sender: Sender<(SenderType, u32, Vec<u8>)>,
    callbacks: HashMap<u32, Box<Fn(Result<RedisResult, RedisError>)>>,
    receiver: Receiver<(u32, Result<RedisResult, RedisError>)>,
    pipe_callbacks: HashMap<u32, Box<Fn(Result<Vec<RedisResult>, RedisError>)>>,
    pipe_receiver: Receiver<(u32, Result<Vec<RedisResult>, RedisError>)>
}

pub struct PubSubArg {
    pub pubsub_type: PubSubType,
    pub callback: Option<Box<Fn(RedisResult)>>
}

pub struct PubSubClientAsync {
    port: &'static str,
    host: &'static str,
    cmd_sender: Sender<(PubSubType, u32, Vec<u8>)>,
    receiver: Receiver<(u32, Result<RedisResult, RedisError>)>,
    cmd_callbacks: HashMap<u32, Box<Fn(Result<RedisResult, RedisError>)>>,
    channel_callbacks: HashMap<String, Box<Fn(RedisResult)>>,
    pattern_callbacks: HashMap<String, Box<Fn(RedisResult)>>
}

/// A RedisClient is a structure to send command to redis and receive the response.
/// All RedisClient's methods are performed synchronously.
/// 
/// When creating a RedisClient it will automatically create a connection. Therefore when
/// it is created it uses the host and the port.
///
/// Example:
///
/// ```
/// # fn function() -> Result<(), redis_client::errors::RedisError> {
/// let mut client = try!(redis_client::RedisClient::new("127.0.0.1", "6379"));
/// # Ok(())}
/// ```
impl RedisClient {
    pub fn new(host: &'static str, port: &'static str) -> Result<RedisClient, RedisError> {
        TcpStream::connect(&*format!("{}:{}", host, port))
            .map(|tcp_stream| {
                    // TODO better timeout init
                    let _res_write = tcp_stream.set_write_timeout(Some(Duration::new(5, 0)));
                    let _res_read = tcp_stream.set_read_timeout(Some(Duration::new(1, 0)));
                    RedisClient {
                        port: port,
                        host: host,
                        buffer: BufReader::new(tcp_stream),
                }
            })
            .map_err(|err| RedisError::Io(err))
    }

    /// write a command to the stream
    fn write_command(&mut self, buf_to_send: &[u8]) -> Result<usize, RedisError> {
        let mut writer = self.buffer.get_mut() as &mut Write;
        let size = try!(writer.write(buf_to_send));
        Ok(size)
    }

    /// Execute a command received as an array of bytes
    fn exec_command(&mut self, buf_to_send: &[u8]) -> Result<RedisResult, RedisError> {
        try!(self.write_command(buf_to_send));
        
        Reader::read(&mut self.buffer)
    }

    /// Execute a pipeline command received as an array of bytes
    fn exec_pipeline_command(&mut self, buf_to_send: &[u8], cmd_nb: usize) -> Result<Vec<RedisResult>, RedisError> {
        try!(self.write_command(buf_to_send));

        Reader::read_pipeline(&mut self.buffer, cmd_nb)
    }

    /// Execute a RedisCommand
    pub fn exec_redis_command(&mut self, redis_command: &mut RedisCommand) -> Result<RedisResult, RedisError> {
        self.exec_command(redis_command.into())
    }

    /// Execute a pipeline of RedisCommand
    pub fn exec_redis_pipeline_command(&mut self, redis_command: &mut RedisCommand) -> Result<Vec<RedisResult>, RedisError> {
        let cmd_nb: usize;
        {
            cmd_nb = redis_command.get_command_nb();
        }
        self.exec_pipeline_command(redis_command.into(), cmd_nb)
    }

}

impl fmt::Debug for RedisClient {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Redis Client - HOST = {} : PORT + {}", self.host, self.port)
    }
}

impl fmt::Display for RedisClient {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Redis Client - HOST = {} : PORT + {}", self.host, self.port)
    }
}

/// A RedisClientAsync is a structure to send command to redis and receive the response asynchronously.
/// 
/// When creating a RedisClientAsync it will automatically create a connection. Therefore when
/// it is created it uses the host and the port.
///
/// Example:
///
/// ```
/// # fn function() -> Result<(), redis_client::errors::RedisError> {
/// let mut client = try!(redis_client::RedisClientAsync::new("127.0.0.1", "6379"));
/// # Ok(())}
/// ```
impl RedisClientAsync {
    pub fn new(host: &'static str, port: &'static str) -> Result<RedisClientAsync, RedisError> {
        let (sender_tx, sender_rx) = channel::<(SenderType, u32, Vec<u8>)>();
        let (init_tx, init_rx) = channel::<Option<RedisError>>();
        let (receiver_tx, receiver_rx) = channel::<(u32, Result<RedisResult, RedisError>)>();
        let (pipe_receiver_tx, pipe_receiver_rx) = channel::<(u32, Result<Vec<RedisResult>, RedisError>)>();

        thread::spawn(move || {
            let _client = RedisClient::new(host, port)
            .map(|mut redis_client| {
                init_tx.send(None)
                .map(|_| {
                    loop {
                        match sender_rx.recv() {
                            Ok(value) => {
                                match value.0 {
                                    SenderType::Simple => {
                                        let _res = receiver_tx.send((value.1, redis_client.exec_command(&value.2[..])));
                                    },
                                    SenderType::Pipe(cmd_nb) => {
                                        let _res = pipe_receiver_tx.send((value.1, redis_client.exec_pipeline_command(&value.2[..], cmd_nb)));
                                    },
                                };
                            },
                            Err(_) => break,
                        };
                    }
                })
            })
            .map_err(|error| {
                let _res = init_tx.send(Some(error));
            });
        });

        match init_rx.recv() {
            Ok(None) => {
                Ok(RedisClientAsync {
                    port: port,
                    host: host,
                    sender: sender_tx,
                    receiver: receiver_rx,
                    callbacks: HashMap::new(),
                    pipe_receiver: pipe_receiver_rx,
                    pipe_callbacks: HashMap::new()
                })
            },
            Ok(Some(err)) =>  Err(err),
            Err(err) => Err(RedisError::MpscRecv(err)),
        }
    }

    /// Execute a redis pipeline command. The callback will be called once the command execution is over and the pump method is called.
    /// The return value indicates if the command was successfully launched.
    pub fn exec_redis_pipeline_command_async<F>(&mut self, redis_command: &mut RedisCommand, callback: F) 
        -> Result<(), RedisError> where F: Fn(Result<Vec<RedisResult>, RedisError>), F: Send + 'static
    {
        let mut rng = rand::thread_rng();
        let key = rng.gen::<u32>();
        try!(self.sender.send((SenderType::Pipe(redis_command.get_command_nb()), key, redis_command.into())));
        self.pipe_callbacks.insert(key, Box::new(callback));
        Ok(())
    }

    /// Execute a redis command. The callback will be called once the command execution is over and the pump method is called.
    /// The return value indicates if the command was successfully launched.
    pub fn exec_redis_command_async<F>(&mut self, redis_command: &mut RedisCommand, callback: F) 
        -> Result<(), RedisError> where F: Fn(Result<RedisResult, RedisError>), F: Send + 'static
    {
        let mut rng = rand::thread_rng();
        let key = rng.gen::<u32>();
        try!(self.sender.send((SenderType::Simple, key, redis_command.into())));
        self.callbacks.insert(key, Box::new(callback));
        Ok(())
    }

    /// Pump the result and execute the callbacks with them. If no result are ready this function will return.
    pub fn pump(&mut self) -> Result<(), RedisError> {
        loop {
            match self.receiver.try_recv() {
                Ok(result) => {
                    self.callbacks.remove(&result.0) 
                        .map(|callback| {
                            if result.1.is_ok() {

                                callback(result.1.clone());
                            }
                        });
                },
                Err(TryRecvError::Empty) => {
                    match self.pipe_receiver.try_recv() {
                        Ok(result) => {
                            self.pipe_callbacks.remove(&result.0) 
                                .map(|callback| {
                                    if result.1.is_ok() {

                                        callback(result.1.clone());
                                    }
                                });
                        },
                        Err(TryRecvError::Empty) => return Ok(()),
                        Err(err) => return Err(RedisError::MpscTryRecv(err))
                    };
                },
                Err(err) => return Err(RedisError::MpscTryRecv(err))
            };
        }
    }
}

impl fmt::Debug for RedisClientAsync {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Redis Client Async - HOST = {} : PORT + {}", self.host, self.port)
    }
}

impl fmt::Display for RedisClientAsync {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Redis Client Async - HOST = {} : PORT + {}", self.host, self.port)
    }
}


/// A PubSubClientAsync is a structure to use redis publish/subscribe functionnality.
/// 
/// When creating a PubSubClientAsync it will automatically create a connection. Therefore when
/// it is created it uses the host and the port.
///
/// Example:
///
/// ```
/// # fn function() -> Result<(), redis_client::errors::RedisError> {
/// let mut client = try!(redis_client::PubSubClientAsync::new("127.0.0.1", "6379"));
/// # Ok(())}
/// ```
impl PubSubClientAsync {
    pub fn new(host: &'static str, port: &'static str) -> Result<PubSubClientAsync, RedisError> {
        let (init_tx, init_rx) = channel::<Option<RedisError>>();
        let (sender_tx, sender_rx) = channel::<(PubSubType, u32, Vec<u8>)>();
        let (receiver_tx, receiver_rx) = channel::<(u32, Result<RedisResult, RedisError>)>();

        thread::spawn(move || {
            let _client = RedisClient::new(host, port)
            .map(|mut redis_client| {
                init_tx.send(None)
                .map(|_| {
                    loop {
                        match sender_rx.try_recv() {
                            Ok(value) => {
                                let _res = receiver_tx.send((value.1, redis_client.exec_command(&value.2[..])));
                            },
                            Err(_) => {
                                if let Ok(res) = Reader::read(&mut redis_client.buffer) {
                                    let _res = receiver_tx.send((0, Ok(res)));
                                }
                            }
                        };
                    }
                })
            })
            .map_err(|error| {
                let _res = init_tx.send(Some(error));
            });
        });

        match init_rx.recv() {
            Ok(None) => {
                Ok(PubSubClientAsync {
                    port: port,
                    host: host,
                    cmd_sender: sender_tx,
                    receiver: receiver_rx,
                    cmd_callbacks: HashMap::new(),
                    channel_callbacks: HashMap::new(),
                    pattern_callbacks: HashMap::new()
                })
            },
            Ok(Some(err)) =>  Err(err),
            Err(err) => Err(RedisError::MpscRecv(err)),
        }
    }

    /// Execute a redis command. The cmd_callback will be called once the command execution is over and the pump method is called.
    /// The return value indicates if the command was successfully launched.
    pub fn exec_redis_command_async<F>(&mut self, redis_command: &mut RedisCommand, cmd_callback: F, pubsub_arg: PubSubArg) 
        -> Result<(), RedisError> where F: Fn(Result<RedisResult, RedisError>), F: Send + 'static
    {
        let mut rng = rand::thread_rng();
        let key: u32 = rng.gen_range(1, u32::MAX);

        let pubsub_type = pubsub_arg.pubsub_type.clone();
        try!(self.cmd_sender.send((pubsub_type, key, redis_command.into())));
        self.cmd_callbacks.insert(key, Box::new(cmd_callback));

        if let Some(callback) = pubsub_arg.callback {
            if let PubSubType::Channel(value) = pubsub_arg.pubsub_type {
                self.channel_callbacks.insert(value, callback);
            }
            else if let PubSubType::Pattern(value) = pubsub_arg.pubsub_type {
                self.pattern_callbacks.insert(value, callback);
            }          
        }

        Ok(())
    }

    /// Pump the result and the received value and execute the callbacks with them. If no result or received value are ready this function will return.
    pub fn pump(&mut self) -> Result<(), RedisError> {
        loop {
            match self.receiver.try_recv() {
                Ok(result) => {
                    if result.0 == 0 {
                        if let Ok(res) = result.1 {
                            let array = res.convert::<Vec<String>>();
                            if !array.is_empty() {
                                if array[0] == "message" && array.len() == 3 && self.channel_callbacks.contains_key(&array[1]) {
                                    self.channel_callbacks[&array[1]](RedisResult::String(array[2].clone()));
                                } else if array[0] == "pmessage" && array.len() == 4 && self.pattern_callbacks.contains_key(&array[1]) {
                                    self.pattern_callbacks[&array[1]](RedisResult::String(array[3].clone()));
                                }
                            }
                        } 
                    } else {
                        self.cmd_callbacks.remove(&result.0) 
                            .map(|callback| {
                                if result.1.is_ok() {

                                    callback(result.1.clone());
                                }
                            });
                    }
                },
                Err(TryRecvError::Empty) => return Ok(()),
                Err(err) => return Err(RedisError::MpscTryRecv(err))
            };
        }
    }
}

impl fmt::Debug for PubSubClientAsync {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Redis Client Async - HOST = {} : PORT + {}", self.host, self.port)
    }
}

impl fmt::Display for PubSubClientAsync {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Redis Client Async - HOST = {} : PORT + {}", self.host, self.port)
    }
}