private void Subscribe(BasicSubscription subscription)
        {
            JsonObject message = new JsonObject();

            message[ProtocolConstants.OP_FIELD] = ProtocolOpConstants.OP_SUBSCRIBE;
            message[ProtocolConstants.ID_FIELD] = subscription.getSubscriptionId();
            if (subscription.getContentMatcher() != null)
            {
                message[ProtocolConstants.MATCHER_FIELD] = subscription.getContentMatcher();
            }
            if (subscription.getDurable() != null)
            {
                message[ProtocolConstants.DURABLE_FIELD] = subscription.getDurable();
            }
            if (subscription.getProperties() != null)
            {
                foreach (String key in subscription.getProperties().Keys)
                {
                    message[key] = subscription.getProperties() [key];
                }
            }

            Queue(message.ToString());
        }
        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);
                    }
                }
            }
        }
Пример #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)
                        {
                            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);
                }
            }
        }