1 // Copyright 2024 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 use crate::devices::devices_handler; 16 use futures_util::{FutureExt as _, TryFutureExt as _}; 17 use grpcio::{RpcContext, RpcStatus, RpcStatusCode, UnarySink}; 18 use log::warn; 19 use netsim_proto::frontend::VersionResponse; 20 use netsim_proto::frontend_grpc::FrontendService; 21 use protobuf::well_known_types::empty::Empty; 22 23 #[derive(Clone)] 24 pub struct FrontendClient; 25 26 impl FrontendService for FrontendClient { get_version(&mut self, ctx: RpcContext<'_>, req: Empty, sink: UnarySink<VersionResponse>)27 fn get_version(&mut self, ctx: RpcContext<'_>, req: Empty, sink: UnarySink<VersionResponse>) { 28 let response = 29 VersionResponse { version: crate::version::get_version(), ..Default::default() }; 30 let f = sink 31 .success(response) 32 .map_err(move |e| eprintln!("client error {:?}: {:?}", req, e)) 33 .map(|_| ()); 34 ctx.spawn(f) 35 } 36 list_device( &mut self, ctx: grpcio::RpcContext, req: Empty, sink: grpcio::UnarySink<netsim_proto::frontend::ListDeviceResponse>, )37 fn list_device( 38 &mut self, 39 ctx: grpcio::RpcContext, 40 req: Empty, 41 sink: grpcio::UnarySink<netsim_proto::frontend::ListDeviceResponse>, 42 ) { 43 let response = match devices_handler::list_device() { 44 Ok(response) => sink.success(response), 45 Err(e) => { 46 warn!("failed to list device: {}", e); 47 sink.fail(RpcStatus::with_message(RpcStatusCode::INTERNAL, e)) 48 } 49 }; 50 51 ctx.spawn(response.map_err(move |e| warn!("client error {:?}: {:?}", req, e)).map(|_| ())) 52 } 53 patch_device( &mut self, _ctx: grpcio::RpcContext, _req: netsim_proto::frontend::PatchDeviceRequest, _sink: grpcio::UnarySink<Empty>, )54 fn patch_device( 55 &mut self, 56 _ctx: grpcio::RpcContext, 57 _req: netsim_proto::frontend::PatchDeviceRequest, 58 _sink: grpcio::UnarySink<Empty>, 59 ) { 60 todo!() 61 } 62 reset(&mut self, ctx: grpcio::RpcContext, _req: Empty, sink: grpcio::UnarySink<Empty>)63 fn reset(&mut self, ctx: grpcio::RpcContext, _req: Empty, sink: grpcio::UnarySink<Empty>) { 64 let response = match devices_handler::reset_all() { 65 Ok(_) => sink.success(Empty::new()), 66 Err(e) => { 67 warn!("failed to reset: {}", e); 68 sink.fail(RpcStatus::with_message(RpcStatusCode::INTERNAL, e)) 69 } 70 }; 71 72 ctx.spawn(response.map_err(move |e| warn!("client error: {:?}", e)).map(|_| ())) 73 } 74 patch_capture( &mut self, _ctx: grpcio::RpcContext, _req: netsim_proto::frontend::PatchCaptureRequest, _sink: grpcio::UnarySink<Empty>, )75 fn patch_capture( 76 &mut self, 77 _ctx: grpcio::RpcContext, 78 _req: netsim_proto::frontend::PatchCaptureRequest, 79 _sink: grpcio::UnarySink<Empty>, 80 ) { 81 todo!() 82 } 83 list_capture( &mut self, _ctx: grpcio::RpcContext, _req: Empty, _sink: grpcio::UnarySink<netsim_proto::frontend::ListCaptureResponse>, )84 fn list_capture( 85 &mut self, 86 _ctx: grpcio::RpcContext, 87 _req: Empty, 88 _sink: grpcio::UnarySink<netsim_proto::frontend::ListCaptureResponse>, 89 ) { 90 todo!() 91 } 92 get_capture( &mut self, _ctx: grpcio::RpcContext, _req: netsim_proto::frontend::GetCaptureRequest, _sink: grpcio::ServerStreamingSink<netsim_proto::frontend::GetCaptureResponse>, )93 fn get_capture( 94 &mut self, 95 _ctx: grpcio::RpcContext, 96 _req: netsim_proto::frontend::GetCaptureRequest, 97 _sink: grpcio::ServerStreamingSink<netsim_proto::frontend::GetCaptureResponse>, 98 ) { 99 todo!() 100 } 101 } 102