/// <summary> /// Identifies all RPC methods and adds to the methodDict. /// </summary> private void IdentifyRpcMethods(RpcDispatchInfo rpcDispatchInfo) { log.Debug("Begin Identify Rpc Methods"); Type t = rpcDispatchInfo.RpcDispatchedObject.GetType(); foreach(MethodInfo m in t.GetMethodsWithAttribute<RpcAttribute>()) { RpcAttribute[] attrs = m.GetCustomAttributes<RpcAttribute>(); Preconditions.IsTrue(1==attrs.Length, string.Format("Only expecting one RpcAttribute. Bad Method: {0}",m.ToString())); RpcAttribute rpc = attrs[0]; RpcInfo rpcInfo = new RpcInfo(rpc.Id, rpc.ChunkSize, rpc.IsBroadcast, m); log.Debug(rpcInfo); rpcDispatchInfo.Add(rpcInfo); } log.Debug("End Identify Rpc Methods"); }
/// <summary> /// Sends a response with the specified return value. /// </summary> private void SendResponse(IMapMessage msg, object returnValue, RpcInfo info) { log.Debug("SendResponse with return value"); string json = null; if(info.IsResponseChunked) { log.Debug("response is chunked"); SendChunkedResponses(msg, returnValue, info); return; } log.Debug("response is not chunked"); json = Json.ToJson(returnValue); log.Debug("json==> " + json); nmsTemplate.SendWithDelegate(msg.NMSReplyTo, delegate(ISession session) { IMapMessage response = session.CreateMapMessage(); response.Body.SetString(ProtocolParams.ReturnValue, json); response.Body.SetBool(ProtocolParams.IsException,false); response.Body.SetBool(ProtocolParams.IsChunked, false); response.Body.SetBool(ProtocolParams.IsBroadcast, info.IsBroadcast); response.Body.SetBool(ProtocolParams.IsResponseComplete, true); response.Body.SetString(ProtocolParams.SessionId, msg.Body.GetString(ProtocolParams.SessionId)); response.NMSCorrelationID = msg.NMSCorrelationID; return response; }); log.Debug("After using nmsTemplate"); }
/// <summary> /// Adds the Rpc info to this instance. /// </summary> internal void Add(RpcInfo rpcInfo) { Preconditions.IsFalse(methodDict.ContainsKey(rpcInfo.Id), string.Format("Already encountered methodId {0} on rpc objectId {1}", rpcInfo.Id, id)); methodDict.Add(rpcInfo.Id,rpcInfo); }
/// <summary> /// Sends data in a series of chunked messages. /// </summary> private void SendChunkedResponses(IMapMessage msg, object returnValue, RpcInfo rpcInfo) { Preconditions.NotNull(returnValue,"Response marked as chunked but is null."); Preconditions.IsTrue(returnValue.GetType().IsArray, "Response is to be chunked but return value is not an array."); object[] wholeArray = (object[]) returnValue; IList<object[]> list = wholeArray.Split<object>(rpcInfo.ChunkSize); for(int i=0; i<list.Count; i++) { string json = Json.ToJson(list[i]); // convert only the part array to JSON to save memory bool isFirstChunk = 0==i; bool isResponseComplete = i==list.Count-1; nmsTemplate.SendWithDelegate(msg.NMSReplyTo, delegate(ISession session) { IMapMessage response = session.CreateMapMessage(); response.Body.SetString(ProtocolParams.ReturnValue,json); response.Body.SetBool(ProtocolParams.IsException,false); response.Body.SetBool(ProtocolParams.IsBroadcast, rpcInfo.IsBroadcast); response.Body.SetBool(ProtocolParams.IsChunked, true); response.Body.SetBool(ProtocolParams.IsFirstChunk, isFirstChunk); if(isFirstChunk) { response.Body.SetInt(ProtocolParams.TotalItemCount, wholeArray.Length); } response.Body.SetBool(ProtocolParams.IsResponseComplete, isResponseComplete); response.Body.SetString(ProtocolParams.SessionId, msg.Body.GetString(ProtocolParams.SessionId)); response.NMSCorrelationID = msg.NMSCorrelationID; return response; }); } }