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
use crate::error::Result;
use crate::tcp::protocol::{DBCommands, ServerResponse};
use std::io::Write;
use std::net::TcpStream;
pub struct KVSClient {
stream: TcpStream,
}
impl KVSClient {
pub fn new(addr: String) -> Result<Self> {
let stream = TcpStream::connect(addr)?;
Ok(KVSClient { stream })
}
pub fn send_cmd(&mut self, command: DBCommands) -> Result<ServerResponse> {
let cmd_packet = command.to_packet()?;
let _ = &self.stream.write_all(&cmd_packet)?;
let _ = &self.stream.flush()?;
ServerResponse::from_stream(&mut self.stream)
}
}