Ejemplo n.º 1
0
        override protected void HandleReceivedMessage(Message message)
        {
            base.HandleReceivedMessage(message);

            Message response = null;

            switch (message.Type)
            {
            case MessageType.TRACE:
                _tracing2Client = true;
                HandleMessage?.Invoke(this, message);
                break;

            case MessageType.PING:
                response = CreatePingResponse(message);
                SendMessage(response);
                break;

            case MessageType.STATUS_REQUEST:
                response = CreateStatusResponse(message);
                SendMessage(response);
                break;

            case MessageType.SUBSCRIBE:
                if (message.HasValue("Subscriber"))
                {
                    try
                    {
                        AddSubscriber(ConnectionManager.Subscriber.Parse(message.GetString("Subscriber")));
                    }
                    catch (Exception e)
                    {
                        Tracing?.TraceEvent(TraceEventType.Error, 2000, e.Message);
                    }
                }
                break;

            case MessageType.UNSUBSCRIBE:
                if (message.HasValue("Subscriber"))
                {
                    RemoveSubscriber(message.GetString("Subscriber"));
                }
                break;

            default:
                int priorSize = ThreadExecutionManager.MaxQueueSize;
                ThreadExecutionManager.MaxQueueSize = 256;
                ThreadExecutionManager.Execute <Message>("HandleMessage-" + ID, HandleMessageDelegateWrapper, message);
                ThreadExecutionManager.MaxQueueSize = priorSize;
                break;
            }
        }
        public void HandleClientMessage(Connection cnn, Chetch.Messaging.Message message)
        {
            //record this
            LogMessage(CMDataSource.MessageDirection.INBOUND, message);

            switch (message.Type)
            {
            case MessageType.SHUTDOWN:
                break;

            case MessageType.STATUS_RESPONSE:
                if (message.HasValue("ServerID"))
                {
                    //send status request to all connected clients
                    var clients = message.GetList <String>("Connections");
                    System.Diagnostics.Trace.WriteLine(String.Format("Server status response shows {0} client connections ...", clients.Count));
                    foreach (var cs in clients)
                    {
                        var data       = cs.Split(' ');
                        var clientName = data[1];
                        if (clientName != null && clientName != String.Empty)
                        {
                            CurrentClient.RequestClientConnectionStatus(clientName);
                            System.Diagnostics.Trace.WriteLine("Requesting status of " + clientName);
                            //System.Threading.Thread.Sleep(100);
                        }
                    }
                }
                break;

            default:
                break;
            } //ehd switch
        }
        virtual protected bool Matches(Message message)
        {
            bool matched = true;

            if (Sender != null && message.Sender != null)
            {
                matched = Sender.Equals(message.Sender);
                if (!matched)
                {
                    return(false);
                }
            }

            if (_types != null && _types.Count > 0)
            {
                matched = _types.Contains(message.Type);
                if (!matched)
                {
                    return(false);
                }
            }

            if (_requiredKeys.Count > 0)
            {
                foreach (String k in _requiredKeys)
                {
                    if (!message.HasValue(k))
                    {
                        return(false);
                    }
                }
            }

            if (_requiredVals.Count == _requiredKeys.Count)
            {
                for (int i = 0; i < _requiredVals.Count; i++)
                {
                    String k  = _requiredKeys[i];
                    Object rv = _requiredVals[i];
                    Object v  = message.GetValue(k);
                    if ((v == null && rv != null) || (v != null && !v.Equals(rv)))
                    {
                        return(false);
                    }
                }
            }

            return(matched);
        }