intel_cache_lib/
ic_client.rs

1use std::net::TcpStream;
2use std::io::{ErrorKind,Error};
3use crate::ic_types::{IcConnection,IcPacket};
4
5/// The Client interface struct for IntelCache. Used to interact with the server.
6pub struct IcClient { 
7	///Underlying connection object for both the client and backend.
8	pub con: IcConnection 
9}
10impl IcClient {
11	/// Connect to `ip` address
12	///
13	/// Note: the address is in ipv4 format. No ports.
14	///
15	/// Returns a client or an error (if couldn't connect).
16	pub fn connect(ip: &str,testing: bool) -> Result<IcClient,Error> {
17		if ! testing {
18			let con = TcpStream::connect(ip.to_owned()+":64209");
19			if let Ok(c) = con {
20				return Ok(IcClient { con: IcConnection::new(c,testing) });
21			} else {
22				return Err(Error::new(ErrorKind::Other,"Failed to connect."));
23			}
24		} else {
25			let con = TcpStream::connect(ip.to_owned()+":46290");
26			if let Ok(c) = con {
27				return Ok(IcClient { con: IcConnection::new(c,testing) });
28			} else {
29				return Err(Error::new(ErrorKind::Other,"Failed to connect."));
30			}
31		}
32	}
33
34	/// `send_cmd` will send a command to the server
35	///
36	/// Returns an [`IcPacket`] from the server
37	pub fn send_cmd(&mut self,c: &mut IcPacket) -> IcPacket {
38		//if self.con.check_connection() {
39		//	self.con.send_packet(c).unwrap(); 
40		//	let retp = self.con.get_packet().unwrap_or(IcPacket::new_empty());
41		//	return retp;
42		//} else {
43		//	return IcPacket::new_empty();
44		//}
45		self.con.send_packet(c).unwrap(); 
46		let retp = self.con.get_packet().unwrap_or(IcPacket::new_empty());
47		return retp;
48	}
49}
50