Skip to content

Commit

Permalink
Adjust logging to be less verbose
Browse files Browse the repository at this point in the history
  • Loading branch information
r-n-o committed Jun 27, 2024
1 parent 61fbca6 commit 89e3535
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 49 deletions.
44 changes: 19 additions & 25 deletions src/qos_net/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,19 @@ impl Proxy {
Ok(conn) => {
let connection_id = conn.id;
let remote_ip = conn.ip.clone();
println!("called new_from_name successfully. Saving connection ID {connection_id}...");
match self.save_connection(conn) {
Ok(()) => {
println!("Connection established and saved. Returning ConnectResponse to client");
println!("Connection to {hostname} established and saved as ID {connection_id}");
ProxyMsg::ConnectResponse { connection_id, remote_ip }
}
Err(e) => {
println!("error saving connection.");
println!("error saving connection: {e:?}");
ProxyMsg::ProxyError(e)
}
}
}
Err(e) => {
println!("error calling new_from_name");
println!("error while establishing connection: {e:?}");
ProxyMsg::ProxyError(e)
}
}
Expand All @@ -126,18 +125,25 @@ impl Proxy {
/// Create a new connection, targeting an IP address directly.
/// address. The TCP connection is opened and saved in internal state.
pub fn connect_by_ip(&mut self, ip: String, port: u16) -> ProxyMsg {
match proxy_connection::ProxyConnection::new_from_ip(ip, port) {
match proxy_connection::ProxyConnection::new_from_ip(ip.clone(), port) {
Ok(conn) => {
let connection_id = conn.id;
let remote_ip = conn.ip.clone();
match self.save_connection(conn) {
Ok(()) => {
println!("Connection to {ip} established and saved as ID {connection_id}");
ProxyMsg::ConnectResponse { connection_id, remote_ip }
}
Err(e) => ProxyMsg::ProxyError(e),
Err(e) => {
println!("error saving connection: {e:?}");
ProxyMsg::ProxyError(e)
}
}
}
Err(e) => ProxyMsg::ProxyError(e),
Err(e) => {
println!("error while establishing connection: {e:?}");
ProxyMsg::ProxyError(e)
}
}
}

Expand Down Expand Up @@ -207,7 +213,6 @@ impl Proxy {

impl server::RequestProcessor for Proxy {
fn process(&mut self, req_bytes: Vec<u8>) -> Vec<u8> {
println!("Proxy processing request");
if req_bytes.len() > MAX_ENCODED_MSG_LEN {
return ProxyMsg::ProxyError(QosNetError::OversizedPayload)
.try_to_vec()
Expand All @@ -217,43 +222,32 @@ impl server::RequestProcessor for Proxy {
let resp = match ProxyMsg::try_from_slice(&req_bytes) {
Ok(req) => match req {
ProxyMsg::StatusRequest => {
println!("Proxy processing StatusRequest");
ProxyMsg::StatusResponse(self.connections.len())
}
ProxyMsg::ConnectByNameRequest {
hostname,
port,
dns_resolvers,
dns_port,
} => {
println!("Proxy connecting to {hostname}:{port}");
let resp = self.connect_by_name(
hostname.clone(),
port,
dns_resolvers,
dns_port,
);
println!("Proxy connected to {hostname}:{port}");
resp
}
} => self.connect_by_name(
hostname.clone(),
port,
dns_resolvers,
dns_port,
),
ProxyMsg::ConnectByIpRequest { ip, port } => {
println!("Proxy connecting to {ip}:{port}");
self.connect_by_ip(ip, port)
}
ProxyMsg::CloseRequest { connection_id } => {
println!("Proxy closing connection {connection_id}");
self.close(connection_id)
}
ProxyMsg::ReadRequest { connection_id, size } => {
println!("Proxy reading {size} bytes from connection {connection_id}");
self.read(connection_id, size)
}
ProxyMsg::WriteRequest { connection_id, data } => {
println!("Proxy writing to connection {connection_id}");
self.write(connection_id, data)
}
ProxyMsg::FlushRequest { connection_id } => {
println!("Proxy flushing connection {connection_id}");
self.flush(connection_id)
}
ProxyMsg::ProxyError(_) => {
Expand Down
14 changes: 2 additions & 12 deletions src/qos_net/src/proxy_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,15 @@ impl ProxyConnection {
dns_resolvers: Vec<String>,
dns_port: u16,
) -> Result<ProxyConnection, QosNetError> {
println!("new_from_name invoked");
let ip = resolve_hostname(hostname, dns_resolvers, dns_port)?;
println!("resolved name into an IP");

// Generate a new random u32 to get an ID. We'll use it to name our
// socket. This will be our connection ID.
let mut rng = rand::thread_rng();
let connection_id: u32 = rng.gen::<u32>();
println!("Random connection ID generated");

let tcp_addr = SocketAddr::new(ip, port);
println!("TCP addr created. Connecting...");
let tcp_stream = TcpStream::connect(tcp_addr)?;
println!("Connected");
Ok(ProxyConnection {
id: connection_id,
ip: ip.to_string(),
Expand Down Expand Up @@ -93,7 +88,6 @@ fn resolve_hostname(
resolver_addrs: Vec<String>,
port: u16,
) -> Result<IpAddr, QosNetError> {
println!("resolving hostname...");
let resolver_parsed_addrs = resolver_addrs
.iter()
.map(|resolver_address| {
Expand All @@ -113,12 +107,8 @@ fn resolve_hostname(
),
);
let resolver = Resolver::new(resolver_config, ResolverOpts::default())?;
println!("resolver ready");
let response = resolver.lookup_ip(hostname.clone()).map_err(|e| {
println!("error invoking resolver: {:?}", e);
QosNetError::from(e)
})?;
println!("resolver successfully invoked");
let response =
resolver.lookup_ip(hostname.clone()).map_err(QosNetError::from)?;
response.iter().next().ok_or_else(|| {
QosNetError::DNSResolutionError(format!(
"Empty response when querying for host {hostname}"
Expand Down
14 changes: 2 additions & 12 deletions src/qos_net/src/proxy_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ impl Read for ProxyStream {
for (i, b) in data.iter().enumerate() {
buf[i] = *b
}
println!("READ {}: read {} bytes", buf.len(), size);
Ok(size)
}
ProxyMsg::ProxyError(e) => Err(std::io::Error::new(
Expand Down Expand Up @@ -240,7 +239,6 @@ impl Write for ProxyStream {
"Write failed: 0 bytes written",
));
}
println!("WRITE {}: sent buf of {} bytes", buf.len(), size);
Ok(size)
}
_ => Err(std::io::Error::new(
Expand Down Expand Up @@ -284,10 +282,7 @@ impl Write for ProxyStream {

match ProxyMsg::try_from_slice(&resp_bytes) {
Ok(resp) => match resp {
ProxyMsg::FlushResponse { connection_id: _ } => {
println!("FLUSH OK");
Ok(())
}
ProxyMsg::FlushResponse { connection_id: _ } => Ok(()),
_ => Err(std::io::Error::new(
ErrorKind::InvalidData,
"unexpected response",
Expand Down Expand Up @@ -459,7 +454,6 @@ mod test {
for (i, b) in data.iter().enumerate() {
buf[i] = *b
}
println!("READ {}: read {} bytes", buf.len(), size);
Ok(size)
}
ProxyMsg::ProxyError(e) => Err(std::io::Error::new(
Expand Down Expand Up @@ -498,7 +492,6 @@ mod test {
"failed Write",
));
}
println!("WRITE {}: sent {} bytes", buf.len(), size,);
Ok(size)
}
_ => Err(std::io::Error::new(
Expand All @@ -522,10 +515,7 @@ mod test {

match ProxyMsg::try_from_slice(&resp_bytes) {
Ok(resp) => match resp {
ProxyMsg::FlushResponse { connection_id: _ } => {
println!("FLUSH OK");
Ok(())
}
ProxyMsg::FlushResponse { connection_id: _ } => Ok(()),
_ => Err(std::io::Error::new(
ErrorKind::InvalidData,
"unexpected response",
Expand Down

0 comments on commit 89e3535

Please sign in to comment.