anor_api/client/
api_client.rs1use anor_storage::storage::storage_item::StorageItem;
2use anor_utils::config::Config;
3use std::io::prelude::*;
4use std::net::TcpStream;
5use std::sync::Arc;
6
7pub trait SocketClient {
8 fn with_config(config: Arc<Config>) -> Self;
9 fn connect(&mut self) -> std::io::Result<()>;
10 fn disconnect(&mut self) -> std::io::Result<()>;
11 fn insert(&self, storage_item: StorageItem);
12 fn update(&mut self, key: &str, storage_item: StorageItem) -> std::io::Result<()>;
13 fn get(&mut self, key: &str) -> std::io::Result<StorageItem>;
14 fn remove(&self, key: &str) -> bool;
15 fn keys(&self) -> Vec<String>;
16 fn clear(&self);
17 fn flush(&self);
18}
19pub struct StorageApiClient {
20 stream: Option<TcpStream>,
21 config: Arc<Config>,
22}
23
24impl SocketClient for StorageApiClient {
25 fn with_config(config: Arc<Config>) -> Self {
26 StorageApiClient {
27 stream: None,
28 config,
29 }
30 }
31
32 fn connect(&mut self) -> std::io::Result<()> {
33 assert!(self.config.remote.is_some());
34 let config_remote = self.config.remote.as_ref().unwrap();
35 assert!(!config_remote.nodes.is_empty());
36 let remote_address = config_remote.nodes[0];
37
38 let stream = TcpStream::connect(remote_address)?;
39
40 let local_addr = stream.local_addr().unwrap();
41 log::info!("connected to {} as {}", remote_address, local_addr);
42
43 stream.set_nodelay(true).expect("set_nodelay call failed");
44
45 self.stream = Some(stream);
46 Ok(())
47 }
48
49 fn disconnect(&mut self) -> std::io::Result<()> {
50 let stream = self.stream.as_mut().unwrap();
51 stream.flush()?;
52 self.stream = None;
53 Ok(())
54 }
55
56fn insert(&self, storage_item: StorageItem) {
75 todo!()
76 }
77
78 fn update(&mut self, key: &str, storage_item: StorageItem) -> std::io::Result<()> {
79 todo!()
80 }
81
82 fn get(&mut self, key: &str) -> std::io::Result<StorageItem> {
83 todo!()
84 }
85
86 fn remove(&self, key: &str) -> bool {
87 todo!()
88 }
89
90 fn keys(&self) -> Vec<String> {
91 vec![]
92 }
93
94 fn clear(&self) {
95 todo!()
96 }
97
98 fn flush(&self) {
99 todo!()
100 }
101}