Dispatch one server-side event and then return. Just so we have a nicer looking `select` statement in `run` :-)
(
&self,
input: &mut FramedRead<I, JsonRpcCodec>,
plugin: &Plugin<S>,
)
| 911 | /// Dispatch one server-side event and then return. Just so we |
| 912 | /// have a nicer looking `select` statement in `run` :-) |
| 913 | async fn dispatch_one<I>( |
| 914 | &self, |
| 915 | input: &mut FramedRead<I, JsonRpcCodec>, |
| 916 | plugin: &Plugin<S>, |
| 917 | ) -> Result<(), Error> |
| 918 | where |
| 919 | I: Send + AsyncReadExt + Unpin, |
| 920 | { |
| 921 | match input.next().await { |
| 922 | Some(Ok(msg)) => { |
| 923 | trace!("Received a message: {:?}", msg); |
| 924 | match msg { |
| 925 | messages::JsonRpc::Request(_id, _p) => { |
| 926 | todo!( |
| 927 | "This is unreachable until we start filling in messages:Request. Until then the custom dispatcher below is used exclusively." |
| 928 | ); |
| 929 | } |
| 930 | messages::JsonRpc::Notification(_n) => { |
| 931 | todo!( |
| 932 | "As soon as we define the full structure of the messages::Notification we'll get here. Until then the custom dispatcher below is used." |
| 933 | ) |
| 934 | } |
| 935 | messages::JsonRpc::CustomRequest(id, request) => { |
| 936 | trace!("Dispatching custom method {:?}", request); |
| 937 | let method = request |
| 938 | .get("method") |
| 939 | .context("Missing 'method' in request")? |
| 940 | .as_str() |
| 941 | .context("'method' is not a string")?; |
| 942 | let callback = match method { |
| 943 | name if name.eq("setconfig") => { |
| 944 | self.setconfig_callback.as_ref().ok_or_else(|| { |
| 945 | anyhow!("No handler for method '{}' registered", method) |
| 946 | })? |
| 947 | } |
| 948 | _ => self.rpcmethods.get(method).with_context(|| { |
| 949 | anyhow!("No handler for method '{}' registered", method) |
| 950 | })?, |
| 951 | }; |
| 952 | let params = request |
| 953 | .get("params") |
| 954 | .context("Missing 'params' field in request")? |
| 955 | .clone(); |
| 956 | |
| 957 | let plugin = plugin.clone(); |
| 958 | let call = callback(plugin.clone(), params); |
| 959 | |
| 960 | tokio::spawn(async move { |
| 961 | match call.await { |
| 962 | Ok(v) => plugin |
| 963 | .sender |
| 964 | .send(json!({ |
| 965 | "jsonrpc": "2.0", |
| 966 | "id": id, |
| 967 | "result": v |
| 968 | })) |
| 969 | .await |
| 970 | .context("returning custom response"), |