public void AcknowledgeMessageEncoding()
        {
            AcknowledgeMessage originalMessage = new AcknowledgeMessage();

            originalMessage.AddPacketId(1);
            originalMessage.AddPacketId(2);
            originalMessage.AddPacketId(3);
            originalMessage.AddPacketId(4);
            originalMessage.AddPacketId(5);
            Assert.AreEqual(5, originalMessage.PacketIdCount);

            byte[] encodedBytes = new byte[256];

            int dataLength = originalMessage.EncodeFrameData(0, encodedBytes, 0);

            AcknowledgeMessage decodedMessage = new AcknowledgeMessage();

            decodedMessage.DecodeFrameData(0, encodedBytes, 0, dataLength);

            decodedMessage.MessageId = originalMessage.MessageId;

            String originalMessageString = originalMessage.ToString();
            String decodedMessageString  = decodedMessage.ToString();

            Assert.AreEqual(originalMessageString, decodedMessageString);

            Assert.AreEqual((uint)1, decodedMessage.GetPacketId(0));
            Assert.AreEqual((uint)2, decodedMessage.GetPacketId(1));
            Assert.AreEqual((uint)3, decodedMessage.GetPacketId(2));
            Assert.AreEqual((uint)4, decodedMessage.GetPacketId(3));
            Assert.AreEqual((uint)5, decodedMessage.GetPacketId(4));
        }
        public void AcknowledgeMessageClear()
        {
            AcknowledgeMessage originalMessage = new AcknowledgeMessage();

            originalMessage.AddPacketId(1);
            originalMessage.AddPacketId(2);
            originalMessage.AddPacketId(3);
            originalMessage.AddPacketId(4);
            originalMessage.AddPacketId(5);

            originalMessage.Clear();
            AcknowledgeMessage emptyMessage = new AcknowledgeMessage();

            emptyMessage.MessageId = originalMessage.MessageId;
            Assert.AreEqual(originalMessage.ToString(), emptyMessage.ToString());
        }
示例#3
0
        public override IMessage ServiceMessage(IMessage message)
        {
            if (FluorineContext.Current.Client != null)
            {
                FluorineContext.Current.Client.Renew();
            }

            if (message is CommandMessage)
            {
                CommandMessage commandMessage = message as CommandMessage;
                switch (commandMessage.operation)
                {
                case CommandMessage.PollOperation:
                {
                    if (log.IsDebugEnabled)
                    {
                        log.Debug(__Res.GetString(__Res.Endpoint_HandleMessage, this.Id, message.ToString()));
                    }

                    if (FluorineContext.Current.Client != null)
                    {
                        FluorineContext.Current.Client.Renew();
                    }

                    //IMessage[] messages = null;
                    IList messages = null;
                    _waitingPollRequests.Increment();
                    int waitIntervalMillis = this.ChannelDefinition.Properties.WaitIntervalMillis != -1 ? this.ChannelDefinition.Properties.WaitIntervalMillis : 60000;        // int.MaxValue;

                    if (commandMessage.HeaderExists(CommandMessage.FluorineSuppressPollWaitHeader))
                    {
                        waitIntervalMillis = 0;
                    }
                    //If async handling was not set long polling is not supported
                    if (!FluorineConfiguration.Instance.FluorineSettings.Runtime.AsyncHandler)
                    {
                        waitIntervalMillis = 0;
                    }
                    if (this.ChannelDefinition.Properties.MaxWaitingPollRequests <= 0 || _waitingPollRequests.Value >= this.ChannelDefinition.Properties.MaxWaitingPollRequests)
                    {
                        waitIntervalMillis = 0;
                    }

                    if (message.destination != null && message.destination != string.Empty)
                    {
                        string             clientId           = commandMessage.clientId as string;
                        MessageDestination messageDestination = this.GetMessageBroker().GetDestination(message.destination) as MessageDestination;
                        MessageClient      client             = messageDestination.SubscriptionManager.GetSubscriber(clientId);
                        client.Renew();
                        //messages = client.GetPendingMessages();
                    }
                    else
                    {
                        //if (FluorineContext.Current.Client != null)
                        //    messages = FluorineContext.Current.Client.GetPendingMessages(waitIntervalMillis);
                    }

                    if (FluorineContext.Current.Client != null)
                    {
                        IEndpointPushHandler handler = FluorineContext.Current.Client.GetEndpointPushHandler(this.Id);
                        if (handler != null)
                        {
                            messages = handler.GetPendingMessages();
                        }
                        if (messages == null)
                        {
                            lock (handler.SyncRoot)
                            {
                                Monitor.Wait(handler.SyncRoot, waitIntervalMillis);
                            }
                            messages = handler.GetPendingMessages();
                        }
                    }

                    _waitingPollRequests.Decrement();
                    IMessage response = null;
                    if (messages == null || messages.Count == 0)
                    {
                        response = new AcknowledgeMessage();
                    }
                    else
                    {
                        CommandMessage resultMessage = new CommandMessage();
                        resultMessage.operation = CommandMessage.ClientSyncOperation;
                        resultMessage.body      = messages;
                        response = resultMessage;
                    }
                    if (log.IsDebugEnabled)
                    {
                        log.Debug(__Res.GetString(__Res.Endpoint_Response, this.Id, response.ToString()));
                    }
                    return(response);
                }

                case CommandMessage.SubscribeOperation:
                {
                    /*
                     * if (FluorineContext.Current.Client == null)
                     *  FluorineContext.Current.SetCurrentClient(this.GetMessageBroker().ClientRegistry.GetClient(message));
                     * RemotingConnection remotingConnection = null;
                     * foreach (IConnection connection in FluorineContext.Current.Client.Connections)
                     * {
                     *  if (connection is RemotingConnection)
                     *  {
                     *      remotingConnection = connection as RemotingConnection;
                     *      break;
                     *  }
                     * }
                     * if (remotingConnection == null)
                     * {
                     *  remotingConnection = new RemotingConnection(this, null, FluorineContext.Current.Client.Id, null);
                     *  FluorineContext.Current.Client.Renew(this.ClientLeaseTime);
                     *  remotingConnection.Initialize(FluorineContext.Current.Client);
                     * }
                     * FluorineWebContext webContext = FluorineContext.Current as FluorineWebContext;
                     * webContext.SetConnection(remotingConnection);
                     */

                    if (this.ChannelDefinition.Properties.IsPollingEnabled)
                    {
                        //Create and forget, client will close the notifier
                        IEndpointPushHandler handler = FluorineContext.Current.Client.GetEndpointPushHandler(this.Id);
                        if (handler == null)
                        {
                            handler = new EndpointPushNotifier(this, FluorineContext.Current.Client);
                        }

                        /*
                         * lock (_endpointPushHandlers.SyncRoot)
                         * {
                         *  _endpointPushHandlers.Add(notifier.Id, notifier);
                         * }
                         */
                    }
                }
                break;

                case CommandMessage.DisconnectOperation:
                {
                    if (log.IsDebugEnabled)
                    {
                        log.Debug(__Res.GetString(__Res.Endpoint_HandleMessage, this.Id, message.ToString()));
                    }

                    if (FluorineContext.Current.Client != null && FluorineContext.Current.Client.IsValid)
                    {
                        IList messageClients = FluorineContext.Current.Client.MessageClients;
                        if (messageClients != null)
                        {
                            foreach (MessageClient messageClient in messageClients)
                            {
                                messageClient.Invalidate();
                            }
                        }
                        FluorineContext.Current.Client.Invalidate();
                    }
                    if (FluorineContext.Current.Session != null)
                    {
                        FluorineContext.Current.Session.Invalidate();
                    }
                    //Disconnect command is received from a client channel.
                    //The response returned by this method is not guaranteed to get to the client, which is free to terminate its physical connection at any point.
                    IMessage response = new AcknowledgeMessage();

                    if (log.IsDebugEnabled)
                    {
                        log.Debug(__Res.GetString(__Res.Endpoint_Response, this.Id, response.ToString()));
                    }
                    return(response);
                }
                }
            }
            return(base.ServiceMessage(message));
        }