Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
codekansas committed Nov 14, 2024
1 parent 0e01da2 commit 65b4aab
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 86 deletions.
37 changes: 37 additions & 0 deletions actuator/actuator/src/server/actuator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::grpc::actuator::common::{
actuator_controller_server::ActuatorController, ActuatorStatus, CommandActuatorsRequest,
CommandActuatorsResponse, ResponseCode,
};
use tonic::{Request, Response, Status};

#[derive(Default, Clone, Copy)]
pub struct ActuatorService {}

#[tonic::async_trait]
impl ActuatorController for ActuatorService {
async fn command_actuators(
&self,
request: Request<CommandActuatorsRequest>,
) -> Result<Response<CommandActuatorsResponse>, Status> {
let cmd = request.into_inner();
println!("Received CommandActuatorsRequest: {:?}", cmd);

// Create a response for each actuator in the request
let statuses: Vec<ActuatorStatus> = cmd
.commands
.iter()
.map(|cmd| ActuatorStatus {
actuator_id: cmd.actuator_id,
position: cmd.position,
velocity: cmd.velocity,
})
.collect();

let response = CommandActuatorsResponse {
statuses,
response_code: ResponseCode::Ok as i32,
};

Ok(Response::new(response))
}
}
96 changes: 10 additions & 86 deletions actuator/actuator/src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,98 +1,22 @@
use crate::grpc::actuator::common::{
actuator_controller_server::{ActuatorController, ActuatorControllerServer},
ActuatorStatus, CommandActuatorsRequest, CommandActuatorsResponse, ResponseCode,
};
use crate::grpc::google::longrunning::{
operations_server::{Operations, OperationsServer},
CancelOperationRequest, DeleteOperationRequest, GetOperationRequest, ListOperationsRequest,
ListOperationsResponse, Operation, WaitOperationRequest,
};
use std::net::SocketAddr;
use tonic::{transport::Server, Request, Response, Status};

#[derive(Default, Clone, Copy)]
pub struct ActuatorService {}

#[tonic::async_trait]
impl Operations for ActuatorService {
async fn list_operations(
&self,
request: Request<ListOperationsRequest>,
) -> Result<Response<ListOperationsResponse>, Status> {
// Implement your logic here
Ok(Response::new(ListOperationsResponse {
operations: vec![],
next_page_token: String::new(),
}))
}

async fn get_operation(
&self,
request: Request<GetOperationRequest>,
) -> Result<Response<Operation>, Status> {
Err(Status::unimplemented("Not yet implemented"))
}

async fn delete_operation(
&self,
request: Request<DeleteOperationRequest>,
) -> Result<Response<()>, Status> {
Err(Status::unimplemented("Not yet implemented"))
}

async fn cancel_operation(
&self,
request: Request<CancelOperationRequest>,
) -> Result<Response<()>, Status> {
Err(Status::unimplemented("Not yet implemented"))
}
mod actuator;
mod operations;

async fn wait_operation(
&self,
request: Request<WaitOperationRequest>,
) -> Result<Response<Operation>, Status> {
Err(Status::unimplemented("Not yet implemented"))
}
}

#[tonic::async_trait]
impl ActuatorController for ActuatorService {
async fn command_actuators(
&self,
request: Request<CommandActuatorsRequest>,
) -> Result<Response<CommandActuatorsResponse>, Status> {
let cmd = request.into_inner();
println!("Received CommandActuatorsRequest: {:?}", cmd);

// Create a response for each actuator in the request
let statuses: Vec<ActuatorStatus> = cmd
.commands
.iter()
.map(|cmd| ActuatorStatus {
actuator_id: cmd.actuator_id,
position: cmd.position,
velocity: cmd.velocity,
})
.collect();
pub use actuator::ActuatorService;
pub use operations::OperationsService;

let response = CommandActuatorsResponse {
statuses,
response_code: ResponseCode::Ok as i32,
};

Ok(Response::new(response))
}
}
use crate::grpc::actuator::common::actuator_controller_server::ActuatorControllerServer;
use crate::grpc::google::longrunning::operations_server::OperationsServer;
use std::net::SocketAddr;
use tonic::transport::Server;

pub async fn run_server() -> Result<(), Box<dyn std::error::Error>> {
let addr: SocketAddr = "[::1]:50051".parse()?;
let service = ActuatorService::default();

println!("Starting actuator server on {}", addr);

Server::builder()
.add_service(ActuatorControllerServer::new(service.clone()))
.add_service(OperationsServer::new(service))
.add_service(ActuatorControllerServer::new(ActuatorService::default()))
.add_service(OperationsServer::new(OperationsService::default()))
.serve(addr)
.await?;

Expand Down
51 changes: 51 additions & 0 deletions actuator/actuator/src/server/operations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use crate::grpc::google::longrunning::{
operations_server::Operations, CancelOperationRequest, DeleteOperationRequest,
GetOperationRequest, ListOperationsRequest, ListOperationsResponse, Operation,
WaitOperationRequest,
};
use tonic::{Request, Response, Status};

#[derive(Default, Clone, Copy)]
pub struct OperationsService {}

#[tonic::async_trait]
impl Operations for OperationsService {
async fn list_operations(
&self,
request: Request<ListOperationsRequest>,
) -> Result<Response<ListOperationsResponse>, Status> {
// Implement your logic here
Ok(Response::new(ListOperationsResponse {
operations: vec![],
next_page_token: String::new(),
}))
}

async fn get_operation(
&self,
request: Request<GetOperationRequest>,
) -> Result<Response<Operation>, Status> {
Err(Status::unimplemented("Not yet implemented"))
}

async fn delete_operation(
&self,
request: Request<DeleteOperationRequest>,
) -> Result<Response<()>, Status> {
Err(Status::unimplemented("Not yet implemented"))
}

async fn cancel_operation(
&self,
request: Request<CancelOperationRequest>,
) -> Result<Response<()>, Status> {
Err(Status::unimplemented("Not yet implemented"))
}

async fn wait_operation(
&self,
request: Request<WaitOperationRequest>,
) -> Result<Response<Operation>, Status> {
Err(Status::unimplemented("Not yet implemented"))
}
}

0 comments on commit 65b4aab

Please sign in to comment.