v_storage/
remote_storage_client.rs1use v_individual_model::onto::individual::Individual;
2use v_individual_model::onto::parser::parse_raw;
3use crate::common::{Storage, StorageId, StorageResult};
4use nng::{Message, Protocol, Socket};
5use std::str;
6
7pub struct StorageROClient {
10 pub soc: Socket,
11 pub addr: String,
12 pub is_ready: bool,
13}
14
15impl Default for StorageROClient {
16 fn default() -> Self {
17 StorageROClient {
18 soc: Socket::new(Protocol::Req0).unwrap(),
19 addr: "".to_owned(),
20 is_ready: false,
21 }
22 }
23}
24
25impl StorageROClient {
26 pub fn new(addr: &str) -> Self {
27 StorageROClient {
28 soc: Socket::new(Protocol::Req0).unwrap(),
29 addr: addr.to_string(),
30 is_ready: false,
31 }
32 }
33
34 pub fn connect(&mut self) -> bool {
35 if let Err(e) = self.soc.dial(&self.addr) {
36 error!("fail connect to storage_manager ({}), err={:?}", self.addr, e);
37 self.is_ready = false;
38 } else {
39 info!("success connect connect to storage_manager ({})", self.addr);
40 self.is_ready = true;
41 }
42 self.is_ready
43 }
44
45 pub fn get_individual_from_db(&mut self, db_id: StorageId, id: &str, iraw: &mut Individual) -> StorageResult<()> {
46 if !self.is_ready && !self.connect() {
47 error!("REMOTE STORAGE: fail send to storage_manager, not ready");
48 return StorageResult::NotReady;
49 }
50
51 let req = if db_id == StorageId::Tickets {
52 Message::from(("t,".to_string() + id).as_bytes())
53 } else {
54 Message::from(("i,".to_string() + id).as_bytes())
55 };
56
57 if let Err(e) = self.soc.send(req) {
58 error!("REMOTE STORAGE: fail send to storage_manager, err={:?}", e);
59 return StorageResult::NotReady;
60 }
61
62 match self.soc.recv() {
64 Err(e) => {
65 error!("REMOTE STORAGE: fail recv from main module, err={:?}", e);
66 StorageResult::NotReady
67 },
68
69 Ok(msg) => {
70 let data = msg.as_slice();
71 if data == b"[]" {
72 return StorageResult::NotFound;
73 }
74
75 iraw.set_raw(data);
76
77 if parse_raw(iraw).is_ok() {
78 StorageResult::Ok(())
79 } else {
80 error!("REMOTE STORAGE: fail parse binobj, len={}, uri=[{}]", iraw.get_raw_len(), id);
81 StorageResult::UnprocessableEntity
82 }
83 },
84 }
85 }
86
87 pub fn count(&mut self, _storage: StorageId) -> StorageResult<usize> {
88 StorageResult::Error("Remote storage does not support count".to_string())
89 }
90}
91
92impl Storage for StorageROClient {
93 fn get_individual(&mut self, storage: StorageId, id: &str, iraw: &mut Individual) -> StorageResult<()> {
94 self.get_individual_from_db(storage, id, iraw)
95 }
96
97 fn get_value(&mut self, _storage: StorageId, _key: &str) -> StorageResult<String> {
98 StorageResult::Error("Remote storage does not support get_value".to_string())
100 }
101
102 fn get_raw_value(&mut self, _storage: StorageId, _key: &str) -> StorageResult<Vec<u8>> {
103 StorageResult::Error("Remote storage does not support get_raw_value".to_string())
105 }
106
107 fn put_value(&mut self, _storage: StorageId, _key: &str, _val: &str) -> StorageResult<()> {
108 StorageResult::Error("Remote storage is read-only".to_string())
110 }
111
112 fn put_raw_value(&mut self, _storage: StorageId, _key: &str, _val: Vec<u8>) -> StorageResult<()> {
113 StorageResult::Error("Remote storage is read-only".to_string())
115 }
116
117 fn remove_value(&mut self, _storage: StorageId, _key: &str) -> StorageResult<()> {
118 StorageResult::Error("Remote storage is read-only".to_string())
120 }
121
122 fn count(&mut self, _storage: StorageId) -> StorageResult<usize> {
123 StorageResult::Error("Remote storage does not support count".to_string())
125 }
126}