private void handleUnsubscribed(JsonObject message)
        {
            String subscriptionId = (String)message[ProtocolConstants.ID_FIELD];
            long   code           = (long)message[ProtocolConstants.ERR_CODE_FIELD];
            String reason         = (String)message[ProtocolConstants.REASON_FIELD];

            // A subscription request has failed.
            // Possible errors include:
            //   LISTENS_DISABLED listens are disabled
            //   LISTENS_DISALLOWED listens are disabled for this user
            //   SUBSCRIPTION_FAILED an internal error occurred

            BasicSubscription subscription = subscriptions[subscriptionId];

            // remove the subscription if not retryable
            if (code == SubscriptionConstants.SUBSCRIPTION_INVALID)
            {
                subscriptions.TryRemove(subscriptionId, out subscription);
            }

            if (subscription != null)
            {
                subscription.Pending = true;
                subscription.getListener().OnError(subscriptionId, Convert.ToInt32(code), reason);
            }
        }
        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.Pending)
            {
                subscription.Pending = false;
                subscription.getListener().OnSubscribe(subscriptionId);
            }
        }
Beispiel #3
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)
                    {
                        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);
                }
            }
        }
        private void handleMessage(JsonObject envelope)
        {
            lock (processLock)
            {
                long   seqNum        = 0;
                long   reqId         = 0;
                long   msgId         = 0;
                long   deliveryCount = 0;
                String replyTo       = null;

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

                object fieldObj;

                if (envelope.TryGetValue(ProtocolConstants.SEQ_NUM_FIELD, out fieldObj))
                {
                    seqNum = Convert.ToInt64(fieldObj);
                }
                if (envelope.TryGetValue(ProtocolConstants.REQ_ID_FIELD, out fieldObj))
                {
                    reqId = Convert.ToInt64(fieldObj);
                }
                if (envelope.TryGetValue(ProtocolConstants.STORE_MSG_ID_FIELD, out fieldObj))
                {
                    msgId = Convert.ToInt64(fieldObj);
                }
                if (envelope.TryGetValue(ProtocolConstants.DELIVERY_COUNT_FIELD, out fieldObj))
                {
                    deliveryCount = Convert.ToInt64(fieldObj);
                }
                if (envelope.TryGetValue(ProtocolConstants.REPLY_TO_FIELD, out fieldObj))
                {
                    replyTo = Convert.ToString(fieldObj);
                }

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

                BasicSubscription subscription = null;

                bool exists = subscriptions.TryGetValue(to, out subscription);
                if (exists && subscription != null)
                {
                    if (seqNum == 0 || seqNum > subscription.LastSeqNum)
                    {
                        IMessage message = new JSONMessage(body);

                        ((JSONMessage)message).SeqNum         = seqNum;
                        ((JSONMessage)message).SubId          = to;
                        ((JSONMessage)message).ReplyTo        = replyTo;
                        ((JSONMessage)message).ReqId          = reqId;
                        ((JSONMessage)message).StoreMessageId = msgId;
                        ((JSONMessage)message).DeliveryCount  = deliveryCount;

                        try
                        {
                            subscription.getListener().OnMessages(new IMessage[] { message });
                        }
                        catch (Exception)
                        {
                            // catch and discard exceptions thrown by the listener
                        }

                        // track the last received sequence number
                        if (subscription.AutoAck && seqNum != 0)
                        {
                            subscription.LastSeqNum = seqNum;
                        }
                    }

                    // auto-acknowledge the message
                    if (subscription.AutoAck && seqNum != 0)
                    {
                        acknowledge(seqNum, subscription.Id);
                    }
                }
            }
        }
Beispiel #5
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)
                        {
                            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)
                    {
                        // 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);
                }
            }
        }