示例#1
0
        private void handleSubscribed(JsonObject message)
        {
            String subscriptionId = (String)message[ProtocolConstants.ID_FIELD];

            // A subscription request has succeeded.
            BasicSubscription subscription = subscriptions[subscriptionId];

            if (subscription != null && subscription.isPending())
            {
                subscription.setPending(false);
                subscription.getListener().OnSubscribe(subscriptionId);
            }
        }
示例#2
0
        private void handleMessage(JsonObject envelope)
        {
            lock (processLock)
            {
                long seqNum = 0;

                String     to      = (String)envelope[ProtocolConstants.TO_FIELD];
                JsonObject message = (JsonObject)envelope[ProtocolConstants.BODY_FIELD];

                object seqNumObj;

                if (envelope.TryGetValue(ProtocolConstants.SEQ_NUM_FIELD, out seqNumObj))
                {
                    seqNum = Convert.ToInt64(seqNumObj);
                }

                // The message will be processed if qos is not enabled, there is no
                // sequence number or if the sequence number is greater than the last
                // received sequence number.

                if (!qos || seqNum == 0 || seqNum > lastSequenceNumber)
                {
                    BasicSubscription subscription = null;

                    bool exists = subscriptions.TryGetValue(to, out subscription);
                    if (exists && subscription != null && !subscription.isPending())
                    {
                        IMessage[] messages = { new JSONMessage(message) };
                        try
                        {
                            subscription.getListener().OnMessages(messages);
                        }
                        catch (Exception)
                        {
                            // catch and discard exceptions thrown by the listener
                        }
                    }

                    // track the last received sequence number only if qos is enabled
                    if (qos && seqNum > 0)
                    {
                        lastSequenceNumber = seqNum;
                    }
                }

                if (seqNum > 0)
                {
                    ack(seqNum);
                }
            }
        }
示例#3
0
        private void handleMessages(JsonArray array)
        {
            lock (processLock)
            {
                ArrayList messages = new ArrayList(array.Count);

                BasicSubscription currentSubscription = null;
                long seqNum     = 0;
                long lastSeqNum = 0;
                int  max        = array.Count;

                for (int i = 0; i < max; i++)
                {
                    JsonObject envelope = (JsonObject)array[i];

                    String     to      = (string)envelope[ProtocolConstants.TO_FIELD];
                    JsonObject message = (JsonObject)envelope[ProtocolConstants.BODY_FIELD];

                    object seqNumObj;

                    if (envelope.TryGetValue(ProtocolConstants.SEQ_NUM_FIELD, out seqNumObj))
                    {
                        seqNum = Convert.ToInt64(seqNumObj);
                    }

                    // The message will be processed if qos is not enabled, there is no
                    // sequence number or if the sequence number is greater than the last
                    // received sequence number.

                    if (!qos || seqNum == 0 || seqNum > lastSequenceNumber)
                    {
                        BasicSubscription subscription = null;
                        bool exists = subscriptions.TryGetValue(to, out subscription);

                        if (exists && subscription != null && !subscription.isPending())
                        {
                            if (currentSubscription != null && currentSubscription != subscription)
                            {
                                try
                                {
                                    IMessage[] arr = new IMessage[messages.Count];

                                    for (int j = 0; j < messages.Count; j++)
                                    {
                                        arr[j] = (JSONMessage)messages[j];
                                    }

                                    currentSubscription.getListener().OnMessages(arr);
                                }
                                catch (Exception)
                                {
                                    // catch and discard exceptions thrown by the listener
                                }
                                messages.Clear();
                            }

                            currentSubscription = subscription;

                            messages.Add(new JSONMessage(message));
                        }

                        // track the last received sequence number only if qos is enabled
                        if (qos && seqNum > 0)
                        {
                            lastSequenceNumber = seqNum;
                        }
                    }

                    if (seqNum > 0)
                    {
                        lastSeqNum = seqNum;
                    }
                }

                if (currentSubscription != null && messages.Count > 0)
                {
                    try
                    {
                        IMessage[] arr = new IMessage[messages.Count];

                        for (int i = 0; i < messages.Count; i++)
                        {
                            arr[i] = (JSONMessage)messages[i];
                        }

                        currentSubscription.getListener().OnMessages(arr);
                    }
                    catch (Exception e)
                    {
                        // catch and discard exceptions thrown by the listener
                        Console.WriteLine(e.Message);
                    }
                    messages.Clear();
                }

                // Send an acknowledgment for the last sequence number in the array.
                // The server will acknowledge all messages less than or equal to
                // this sequence number.

                if (lastSeqNum > 0)
                {
                    ack(lastSeqNum);
                }
            }
        }