예제 #1
0
        public async Task <IList <OutgoingMessage> > HandleAsync(IncomingMessage request, CancellationToken cToken)
        {
            if (request.GetType() != typeof(T))
            {
                throw new ArgumentException($"request of type {request.GetType().Name} cannot be processed by {this.GetType().Name}");
            }
            var outgoigMessages = new List <OutgoingMessage>();

            await HandleAsync((T)request, outgoigMessages, cToken);

            await _dbCtx.CommitAsync();

            return(outgoigMessages);
        }
 public override void HandleIncomingMessage(IncomingMessage message)
 {
     if (message is CredentialsMessage)
     {
         CredentialsMessage cm = message as CredentialsMessage;
         if (AuthenticationHandler.Authenticate(cm.Credentials))
         {
             _session.ClientId.Credentials = cm.Credentials;
             _session.Communication.SendAuthenticationAccepted();
             _session.State = new PairingState(_session);
         }
         else
         {
             _session.Communication.SendAuthenticationRejected();
         }
     }
     else if (message is ClientPausedMessage ||
          message is ClientResumedMessage)
     {
         // Ignore, but not reject!
     }
     else
     {
         string errorMsg = string.Format("Message type '{0}' is not supported at this point", message.GetType().Name);
         Debug.WriteLine(errorMsg);
         throw new NotSupportedException(errorMsg);
     }
 }
예제 #3
0
        public MessageHandlerScope BuildInNestedScope(IncomingMessage message)
        {
            var reqType = message.GetType();

            if (_typeMap.TryGetValue(reqType, out var handlerType))
            {
                return(new MessageHandlerScope(_container.GetNestedContainer(), handlerType));
            }
            throw new Exception($"Handler not found for {reqType.Name}");
        }
예제 #4
0
        public IMessageHandler Build(IncomingMessage message)
        {
            var reqType = message.GetType();

            if (_typeMap.TryGetValue(reqType, out var handlerType))
            {
                return((IMessageHandler)_container.GetInstance(handlerType));
            }
            throw new Exception($"Handler not found for {reqType.Name}");
        }
예제 #5
0
 public override void HandleIncomingMessage(IncomingMessage message)
 {
     if (message is PinCodeMessage)
     {
         PinCodeMessage pincodeMessage = (message as PinCodeMessage);
         ClientTagVisualization visualizationMatch = ClientSessionsController.Instance.GetMatchForPinCode(pincodeMessage.PinCode);
         if (visualizationMatch != null)
         {
             ClientSessionsController.Instance.UnregisterPairingCodes(visualizationMatch);
             _session.SetClientTag(visualizationMatch.VisualizedTag);
             _session.Communication.SendPairingCodeAccepted();
             if (ClientDataStorage.Instance.GetClientCalibration(visualizationMatch.VisualizedTag) == null)
             {
                 _session.State = new CalibrationState(_session, visualizationMatch);
             }
             else
             {
                 _session.State = new StreamingState(_session, visualizationMatch);
             }
             visualizationMatch.AssociateClient(_session);
         }
         else
         {
             _session.Communication.SendPincodeRejected();
         }
     }
     else if (message is ColorCodeMessage)
     {
         throw new NotSupportedException("COLOR_CODE not supported yet");
     }
     else if (message is ClientPausedMessage ||
             message is ClientResumedMessage)
     {
         // Ignore, but not reject!
     }
     else
     {
         string errorMsg = string.Format("Message type '{0}' is not supported at this point", message.GetType().Name);
         Debug.WriteLine(errorMsg);
         throw new NotSupportedException(errorMsg);
     }
 }
 public override void HandleIncomingMessage(IncomingMessage message)
 {
     if (message is CalibrationAcceptedMessage)
     {
         Visualization.CalibrationAccepted();
     }
     else if (message is TouchEventMessage ||
              message is ColorCodeMessage ||
              message is PinCodeMessage ||
              message is ClientPausedMessage ||
              message is ClientResumedMessage)
     {
         // Ignored messages, but not rejected...
     }
     else
     {
         Debug.WriteLine(string.Format("Message type '{0}' is not supported at this point", message.GetType().Name));
         throw new NotSupportedException();
     }
 }
 public override void HandleIncomingMessage(IncomingMessage message)
 {
     if (message is TouchEventMessage)
     {
         if (Visualization != null)
         {
             Visualization.TouchEvent(message as TouchEventMessage);
         }
     }
     else if (message is CalibrationAcceptedMessage)
     {
         if (Visualization != null)
         {
             Visualization.CalibrationAccepted();
         }
     }
     else if (message is ClientPausedMessage)
     {
         StopStreaming();
     }
     else if (message is ClientResumedMessage)
     {
         StartStreaming();
     }
     else if (message is RequestCalibrationMessage)
     {
         Debug.WriteLineIf(DebugSettings.DEBUG_CALIBRATION, "RequestCalibrationMessage");
         StopStreaming();
         CalibrationState.SetAsState(_session, Visualization);
     }
     else if (message is ColorCodeMessage ||
              message is PinCodeMessage)
     {
         // Ignored messages, but not rejected...
     }
     else
     {
         Debug.WriteLine(string.Format("Message type '{0}' is not supported at this point", message.GetType().Name));
         throw new NotSupportedException();
     }
 }
예제 #8
0
        private async Task ProcessAsync(IncomingMessage msg, CancellationToken cToken)
        {
            IMessageHandler handler = null;

            try
            {
                handler = _handlerFactory.Create(msg);
                Logger.Debug("Handler {0} found for request {1}", handler.GetType().Name, msg.GetType().Name);
                var responses = await handler.HandleAsync(msg, cToken);

                if (responses == null)
                {
                    return;
                }

                Logger.Debug("{0} messages to send back", responses.Count);
                foreach (var r in responses)
                {
                    await _nodeMessageChannel.SendAsync(r, cToken);

                    MessageLog.Debug("OUT:{0}", r);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
            }
            finally
            {
                if (handler != null)
                {
                    _handlerFactory.Release(handler);
                }
            }
        }
예제 #9
0
        public async Task <IList <OutgoingMessage> > ProcessOne(IncomingMessage msg, CancellationToken token)
        {
            var ts = Stopwatch.StartNew();

            using var scopedHandler = _messageHandlerFactory.BuildInNestedScope(msg);
            Logger.Debug(() => $"{scopedHandler.Handler.GetType().Name} will be used to handle {msg.GetType().Name}");
            var responses = await scopedHandler.Handler.HandleAsync(msg, token);

            ts.Stop();
            Logger.Debug(() =>
                         $"{scopedHandler.Handler.GetType().Name} has finnish handling {msg.GetType().Name} in {ts.ElapsedMilliseconds} ms.");

            return(responses);
        }