Пример #1
0
        private HMessage InnerBuildMessage(string actor, string type, JToken payload, HMessageOptions mOptions)
        {
            if (actor == null || actor.Length <= 0)
                throw new MissingAttrException("actor");

            HMessage message = new HMessage();
            message.SetActor(actor);
            message.SetType(type);
            if (mOptions != null)
            {
                message.SetRef(mOptions.Ref);
                message.SetConvid(mOptions.Convid);
                message.SetPriority(mOptions.Priority);
                message.SetAuthor(mOptions.Author);
                message.SetHeaders(mOptions.Headers);
                message.SetLocation(mOptions.Location);
                message.SetPublished(mOptions.Published);
                message.SetPersistent(mOptions.Persistent);
                message.SetTimeout(mOptions.Timeout);
                if (mOptions.RelevanceOffset != null)
                {
                    Debug.WriteLine("----   " + mOptions.RelevanceOffset);
                    message.SetRelevance((DateTime.UtcNow).AddMilliseconds(mOptions.RelevanceOffset.Value));
                    Debug.WriteLine("++++   " + message.GetRelevance());
                }
                else
                    message.SetRelevance(mOptions.Relevance);
            }
            if (transportOptions != null && transportOptions.Login != null)
                message.SetPublisher(transportOptions.FullUrn);
            else
                message.SetPublisher(null);
            message.SetPayload(payload);
            return message;
        }
Пример #2
0
        /// <summary>
        /// removed since v0.5
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="messageDelegate"></param>
        public void Send(HMessage message, Action<HMessage> messageDelegate)
        {
            if (this.connectionStatus != ConnectionStatus.CONNECTED)
            {
                notifyResultError(message.GetMsgid(), ResultStatus.NOT_CONNECTED, "Not connected.", messageDelegate);
                return;
            }
            if (message == null)
            {
                notifyResultError(null, ResultStatus.MISSING_ATTR, "Provided message is null.", messageDelegate);
                return;
            }
            if (message.GetActor() == null)
            {
                notifyResultError(message.GetMsgid(), ResultStatus.MISSING_ATTR, "Actor is missing.", messageDelegate);
                return;
            }

            message.SetSent(DateTime.UtcNow);
            message.SetPublished(DateTime.UtcNow);
            message.SetMsgid(Guid.NewGuid().ToString());
            message.SetPublisher(transportOptions.FullUrn);

            if (message.GetTimeout() > 0)
            {
                // hAPI will do correlation. If no answer within the
                // timeout, a timeout error will be sent.
                if (messageDelegate != null)
                {
                    messageDelegates.Add(message.GetMsgid(), messageDelegate);

                    ThreadPoolTimer timeOutTimer = ThreadPoolTimer.CreateTimer((obj) =>
                    {
                        notifyResultError(message.GetMsgid(), ResultStatus.EXEC_TIMEOUT, "The response of message is time out.", null);
                    }, new TimeSpan(0, 0, 0, 0, message.GetTimeout()));

                    timerOutDictionary.Add(message.GetMsgid(), timeOutTimer);
                }
                else
                {
                    //when there is no callback, timeout has no sense. delete timeout.
                    message.SetTimeout(0);
                }
            }
            transportManager.SendObject(message);
        }