Add Context to Rust service
This commit is contained in:
@ -105,21 +105,27 @@ impl JRPCClient {
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
pub trait JRPCServerService: Send + Sync + 'static {
|
||||
pub trait JRPCServerService: Send + Sync {
|
||||
type Context;
|
||||
fn get_id(&self) -> String;
|
||||
async fn handle(&self, request: &JRPCRequest, function: &str) -> Result<(bool, Value)>;
|
||||
async fn handle(
|
||||
&self,
|
||||
request: &JRPCRequest,
|
||||
function: &str,
|
||||
ctx: Self::Context,
|
||||
) -> Result<(bool, Value)>;
|
||||
}
|
||||
|
||||
pub type JRPCServiceHandle = Arc<dyn JRPCServerService>;
|
||||
pub type JRPCServiceHandle<Context> = Arc<dyn JRPCServerService<Context = Context>>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct JRPCSession {
|
||||
server: JRPCServer,
|
||||
pub struct JRPCSession<Context> {
|
||||
server: JRPCServer<Context>,
|
||||
message_sender: Sender<JRPCResult>,
|
||||
}
|
||||
|
||||
impl JRPCSession {
|
||||
pub fn new(server: JRPCServer, sender: Sender<JRPCResult>) -> JRPCSession {
|
||||
impl<Context: Clone + Send + Sync + 'static> JRPCSession<Context> {
|
||||
pub fn new(server: JRPCServer<Context>, sender: Sender<JRPCResult>) -> Self {
|
||||
JRPCSession {
|
||||
server,
|
||||
message_sender: sender,
|
||||
@ -148,7 +154,7 @@ impl JRPCSession {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_request(&self, request: JRPCRequest) -> () {
|
||||
pub fn handle_request(&self, request: JRPCRequest, ctx: Context) -> () {
|
||||
let session = self.clone();
|
||||
tokio::task::spawn(async move {
|
||||
info!("Received request: {}", request.method);
|
||||
@ -163,7 +169,7 @@ impl JRPCSession {
|
||||
|
||||
let service = session.server.services.get(service);
|
||||
if let Some(service) = service {
|
||||
let result = service.handle(&request, function).await;
|
||||
let result = service.handle(&request, function, ctx).await;
|
||||
match result {
|
||||
Ok((is_send, result)) => {
|
||||
if is_send && request.id.is_some() {
|
||||
@ -204,23 +210,23 @@ impl JRPCSession {
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct JRPCServer {
|
||||
services: HashMap<String, JRPCServiceHandle>,
|
||||
pub struct JRPCServer<Context> {
|
||||
services: HashMap<String, JRPCServiceHandle<Context>>,
|
||||
}
|
||||
|
||||
impl JRPCServer {
|
||||
pub fn new() -> JRPCServer {
|
||||
impl<Context: Clone + Send + Sync + 'static> JRPCServer<Context> {
|
||||
pub fn new() -> Self {
|
||||
JRPCServer {
|
||||
services: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_service(&mut self, service: JRPCServiceHandle) -> () {
|
||||
pub fn add_service(&mut self, service: JRPCServiceHandle<Context>) -> () {
|
||||
let id = service.get_id();
|
||||
self.services.insert(id, service);
|
||||
}
|
||||
|
||||
pub fn get_session(&self, sender: Sender<JRPCResult>) -> JRPCSession {
|
||||
pub fn get_session(&self, sender: Sender<JRPCResult>) -> JRPCSession<Context> {
|
||||
JRPCSession::new(self.clone(), sender)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user