Example #1
0
        public IMessage[] GetMessageArray(String fieldName)
        {
            object obj = null;

            if (String.IsNullOrEmpty(fieldName))
            {
                throw new System.ArgumentException("field name is null or empty");
            }

            if (!data.TryGetValue(fieldName, out obj))
            {
                throw new System.Exception("field " + fieldName + " does not exist");
            }

            if (obj is JsonArray)
            {
                JsonArray arr = (JsonArray)obj;

                if (arr.Count == 0)
                {
                    return(new IMessage[0]);
                }
                else if (arr[0] is JsonObject)
                {
                    IMessage[] retVal = new IMessage[arr.Count];

                    for (int i = 0, max = arr.Count; i < max; i++)
                    {
                        retVal[i] = new JSONMessage((JsonObject)arr[i]);
                    }

                    return(retVal);
                }
                else
                {
                    throw new System.Exception("field " + fieldName + " is not of type MESSAGE_ARRAY");
                }
            }

            return(null);
        }
        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);
                    }
                }
            }
        }