private async Task ProcessRequestUpdates(JObject response, int rid) { var request = RequestManager.GetRequest(rid); switch (request) { case ListRequest _: var listRequest = (ListRequest)request; var name = listRequest.Path.Split('/').Last(); var node = new RemoteNode(name, null, listRequest.Path); node.FromSerialized(response["updates"].Value <JArray>()); await Task.Run(() => listRequest.Callback(new ListResponse(_link, rid, listRequest.Path, node))); break; case SetRequest _: RequestManager.StopRequest(rid); break; case RemoveRequest _: RequestManager.StopRequest(rid); break; case InvokeRequest _: var invokeRequest = (InvokeRequest)request; var path = invokeRequest.Path; var columns = response.GetValue("columns") != null ? response["columns"].Value <JArray>() : new JArray(); var updates = response.GetValue("updates") != null ? response["updates"].Value <JArray>() : new JArray(); var meta = response.GetValue("meta") != null ? response["meta"].Value <JObject>() : new JObject(); var error = response.GetValue("error") != null ? response["error"].Value <JObject>() : new JObject(); await Task.Run(() => { invokeRequest?.Callback(new InvokeResponse(_link, rid, path, columns, updates, meta, error)); }); break; } }
/// <summary> /// Processes incoming responses. /// </summary> /// <param name="responses">Responses</param> /// <returns>Requests</returns> internal async Task <JArray> ProcessResponses(JArray responses) { var requests = new JArray(); foreach (JObject response in responses) { if (response["rid"].Type == JTokenType.Integer && response["rid"].Value <int>() == 0) { foreach (dynamic update in response["updates"]) { if (update is JArray) { JArray arrayUpdate = update; int sid = arrayUpdate[0].Value <int>(); JToken value = arrayUpdate[1]; string dt = arrayUpdate[2].Value <string>(); _subscriptionManager.InvokeSubscriptionUpdate(sid, new SubscriptionUpdate(sid, value, dt)); } else if (update is JObject) { JObject objectUpdate = update; int sid = objectUpdate["sid"].Value <int>(); JToken value = objectUpdate["value"]; string ts = objectUpdate["ts"].Value <string>(); int count = objectUpdate["count"].Value <int>(); int sum = objectUpdate["sum"].Value <int>(); int min = objectUpdate["min"].Value <int>(); int max = objectUpdate["max"].Value <int>(); _subscriptionManager.InvokeSubscriptionUpdate(sid, new SubscriptionUpdate(sid, value, ts, count, sum, min, max)); } } } else if (response["rid"].Type == JTokenType.Integer && _requestManager.RequestPending(response["rid"].Value <int>())) { var request = _requestManager.GetRequest(response["rid"].Value <int>()); if (request is ListRequest) { var listRequest = request as ListRequest; string name = listRequest.Path.Split('/').Last(); var node = new RemoteNode(name, null, listRequest.Path); node.FromSerialized(response["updates"].Value <JArray>()); await Task.Run(() => listRequest.Callback(new ListResponse(_link, listRequest.RequestID, listRequest.Path, node))); } else if (request is SetRequest) { _requestManager.StopRequest(request.RequestID); } else if (request is RemoveRequest) { _requestManager.StopRequest(request.RequestID); } else if (request is InvokeRequest) { var invokeRequest = request as InvokeRequest; await Task.Run(() => invokeRequest.Callback(new InvokeResponse(_link, invokeRequest.RequestID, invokeRequest.Path, response["columns"].Value <JArray>(), response["updates"].Value <JArray>()))); } } else if (response["rid"].Type == JTokenType.Null) { _link.Logger.Warning("Incoming request has null request ID."); } } return(requests); }