void ServiceRequestReceived(ObjectBusMessage message) { #if TRACE Console.WriteLine(new System.Diagnostics.StackTrace(true).GetFrame(0)); #endif if (message == null) { throw new ArgumentNullException("message"); } if (!(message is ServiceRequestMessage)) { throw new ArgumentException(string.Format("message type is not valid, must be of type {0}", typeof(ServiceRequestMessage).FullName)); } ServiceRequestMessage serviceRequest = (ServiceRequestMessage)message; Func <ServiceAgentMode, ObjectBusSession, Action, Byte[], ServiceAgent> agentFunc; lock (localServices) lock (localServiceAgents) agentFunc = localServiceAgents [localServices [serviceRequest.ServiceID]]; Guid responseID = Guid.NewGuid(); ObjectBusSession session = objectBus.CreateSession(responseID, SessionDisconnected); ServiceResponseMessage response = new ServiceResponseMessage(responseID, serviceRequest.ID, ServiceResponseStatus.Accepted); ServiceAgent agent = agentFunc.Invoke(ServiceAgentMode.Server, session, objectBus.Flush, serviceRequest.Parameters); lock (sessionAgents) sessionAgents.Add(session.SessionID, agent); objectBus.SendMessage(response); }
public ServiceAgent RequestService(ServiceAnnounceMessage remoteServiceAnnouncement, byte[] parameters, Func <ServiceAgentMode, ObjectBusSession, Action, Byte[], ServiceAgent> func, Byte[] localAgentParameters) { #if TRACE Console.WriteLine(new System.Diagnostics.StackTrace(true).GetFrame(0)); #endif lock (remoteServices) if (!remoteServices.Contains(remoteServiceAnnouncement)) { throw new InvalidOperationException("The provided remoteServiceAnnouncement is not valid."); } ServiceRequestMessage request = new ServiceRequestMessage(Guid.NewGuid(), remoteServiceAnnouncement.ID, parameters); System.Threading.ManualResetEvent mre = new System.Threading.ManualResetEvent(false); System.Threading.ManualResetEvent mre_done = new System.Threading.ManualResetEvent(false); lock (requests) requests.Add(request.ID, new Tuple <ServiceRequestMessage, System.Threading.ManualResetEvent, System.Threading.ManualResetEvent> (request, mre, mre_done)); objectBus.SendMessage(request); mre.WaitOne(); //todo: add exception handling here ServiceResponseMessage response = pendingResponses [request.ID]; lock (pendingResponses) pendingResponses.Remove(response.RequestID); Console.WriteLine("Service request accepted, remote created a session with ID {0}", response.ID); ServiceAgent agent = func(ServiceAgentMode.Client, objectBus.CreateSession(response.ID, SessionDisconnected), objectBus.Flush, localAgentParameters); sessionAgents.Add(response.ID, agent); mre_done.Set(); return(agent); }
public ServiceAgent RequestService(ServiceAnnounceMessage remoteServiceAnnouncement, byte[] parameters, Func<ServiceAgentMode , ObjectBusSession, Action, Byte[], ServiceAgent> func, Byte[] localAgentParameters) { #if TRACE Console.WriteLine (new System.Diagnostics.StackTrace (true).GetFrame (0)); #endif lock (remoteServices) if (!remoteServices.Contains (remoteServiceAnnouncement)) throw new InvalidOperationException ("The provided remoteServiceAnnouncement is not valid."); ServiceRequestMessage request = new ServiceRequestMessage (Guid.NewGuid (), remoteServiceAnnouncement.ID, parameters); System.Threading.ManualResetEvent mre = new System.Threading.ManualResetEvent (false); System.Threading.ManualResetEvent mre_done = new System.Threading.ManualResetEvent (false); lock (requests) requests.Add (request.ID, new Tuple<ServiceRequestMessage, System.Threading.ManualResetEvent, System.Threading.ManualResetEvent> (request, mre, mre_done)); objectBus.SendMessage (request); mre.WaitOne (); //todo: add exception handling here ServiceResponseMessage response = pendingResponses [request.ID]; lock (pendingResponses) pendingResponses.Remove (response.RequestID); Console.WriteLine ("Service request accepted, remote created a session with ID {0}", response.ID); ServiceAgent agent = func (ServiceAgentMode.Client, objectBus.CreateSession (response.ID, SessionDisconnected), objectBus.Flush, localAgentParameters); sessionAgents.Add (response.ID, agent); mre_done.Set (); return agent; }