Skip to content

Commit

Permalink
Extract now-identical handle_request from sidecar/gimlet
Browse files Browse the repository at this point in the history
  • Loading branch information
jgallagher committed Apr 21, 2022
1 parent 2fd52c7 commit a089bb4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 63 deletions.
30 changes: 3 additions & 27 deletions sp-sim/src/gimlet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use crate::config::GimletConfig;
use crate::server;
use crate::server::UdpServer;
use crate::{Responsiveness, SimulatedSp};
use anyhow::{anyhow, Context, Result};
Expand Down Expand Up @@ -370,7 +371,7 @@ impl UdpTask {
loop {
select! {
recv0 = self.udp0.recv_from() => {
if let Some((resp, addr)) = handle_request(
if let Some((resp, addr)) = server::handle_request(
&mut self.handler,
recv0,
&mut server,
Expand All @@ -382,7 +383,7 @@ impl UdpTask {
}

recv1 = self.udp1.recv_from() => {
if let Some((resp, addr)) = handle_request(
if let Some((resp, addr)) = server::handle_request(
&mut self.handler,
recv1,
&mut server,
Expand Down Expand Up @@ -413,31 +414,6 @@ impl UdpTask {
}
}

async fn handle_request<'a>(
handler: &mut Handler,
recv: Result<(&[u8], SocketAddr)>,
server: &'a mut SpServer,
responsiveness: Responsiveness,
port_num: SpPort,
) -> Result<Option<(&'a [u8], SocketAddr)>> {
match responsiveness {
Responsiveness::Responsive => (), // proceed
Responsiveness::Unresponsive => {
// pretend to be unresponsive - drop this packet
return Ok(None);
}
}

let (data, addr) =
recv.with_context(|| format!("recv on {:?}", port_num))?;

let resp = server
.dispatch(addr, port_num, data, handler)
.map_err(|err| anyhow!("dispatching message failed: {:?}", err))?;

Ok(Some((resp, addr)))
}

struct Handler {
log: Logger,
serial_number: SerialNumber,
Expand Down
48 changes: 41 additions & 7 deletions sp-sim/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use crate::config::Config;
use anyhow::{bail, Context, Result};
use gateway_messages::{Request, SerializedSize};
use slog::{debug, error, Logger};
use std::{
net::{Ipv6Addr, SocketAddr},
sync::Arc,
};
use crate::Responsiveness;
use anyhow::anyhow;
use anyhow::bail;
use anyhow::Context;
use anyhow::Result;
use gateway_messages::sp_impl::SpHandler;
use gateway_messages::sp_impl::SpServer;
use gateway_messages::Request;
use gateway_messages::SerializedSize;
use gateway_messages::SpPort;
use slog::debug;
use slog::error;
use slog::Logger;
use std::net::Ipv6Addr;
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::net::UdpSocket;

/// Thin wrapper pairing a [`UdpSocket`] with a buffer sized for [`Request`]s.
Expand Down Expand Up @@ -91,3 +100,28 @@ pub fn logger(config: &Config) -> Result<Logger> {
}
Ok(log)
}

pub(crate) async fn handle_request<'a, H: SpHandler>(
handler: &mut H,
recv: Result<(&[u8], SocketAddr)>,
server: &'a mut SpServer,
responsiveness: Responsiveness,
port_num: SpPort,
) -> Result<Option<(&'a [u8], SocketAddr)>> {
match responsiveness {
Responsiveness::Responsive => (), // proceed
Responsiveness::Unresponsive => {
// pretend to be unresponsive - drop this packet
return Ok(None);
}
}

let (data, addr) =
recv.with_context(|| format!("recv on {:?}", port_num))?;

let resp = server
.dispatch(addr, port_num, data, handler)
.map_err(|err| anyhow!("dispatching message failed: {:?}", err))?;

Ok(Some((resp, addr)))
}
32 changes: 3 additions & 29 deletions sp-sim/src/sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ use std::net::SocketAddr;
use crate::config::Config;
use crate::config::SidecarConfig;
use crate::ignition_id;
use crate::server;
use crate::server::UdpServer;
use crate::Responsiveness;
use crate::SimulatedSp;
use anyhow::anyhow;
use anyhow::Context;
use anyhow::Result;
use async_trait::async_trait;
use futures::future;
Expand Down Expand Up @@ -177,7 +176,7 @@ impl Inner {
loop {
select! {
recv0 = self.udp0.recv_from() => {
if let Some((resp, addr)) = handle_request(
if let Some((resp, addr)) = server::handle_request(
&mut self.handler,
recv0,
&mut server,
Expand All @@ -189,7 +188,7 @@ impl Inner {
}

recv1 = self.udp1.recv_from() => {
if let Some((resp, addr)) = handle_request(
if let Some((resp, addr)) = server::handle_request(
&mut self.handler,
recv1,
&mut server,
Expand Down Expand Up @@ -225,31 +224,6 @@ impl Inner {
}
}

async fn handle_request<'a>(
handler: &mut Handler,
recv: Result<(&[u8], SocketAddr)>,
server: &'a mut SpServer,
responsiveness: Responsiveness,
port_num: SpPort,
) -> Result<Option<(&'a [u8], SocketAddr)>> {
match responsiveness {
Responsiveness::Responsive => (), // proceed
Responsiveness::Unresponsive => {
// pretend to be unresponsive - drop this packet
return Ok(None);
}
}

let (data, addr) =
recv.with_context(|| format!("recv on {:?}", port_num))?;

let resp = server
.dispatch(addr, port_num, data, handler)
.map_err(|err| anyhow!("dispatching message failed: {:?}", err))?;

Ok(Some((resp, addr)))
}

struct Handler {
log: Logger,
serial_number: SerialNumber,
Expand Down

0 comments on commit a089bb4

Please sign in to comment.