private void notifyResultError(string @ref, ResultStatus resultStatus, string errorMsg, Action<HMessage> messageDelegate) { JObject obj = new JObject(); obj["errorMsg"] = errorMsg; HResult result = new HResult(); result.SetStatus(resultStatus); result.SetResult(obj); HMessage message = new HMessage(); message.SetRef(@ref); message.SetType("hResult"); message.SetPayload(result); this.notifyMessage(message, messageDelegate); }
private void notifyMessage(HMessage message, Action<HMessage> messageDelegate) { // 1 - we search the delegate with the ref if any in delegate dictionnary. if(messageDelegates.Count >0 && message.GetRef() != null && messageDelegates.ContainsKey(HUtil.GetApiRef(message.GetRef()))) { string msgRef = HUtil.GetApiRef(message.GetRef()); if (timerOutDictionary.ContainsKey(msgRef)) { ThreadPoolTimer timer = timerOutDictionary[msgRef]; timerOutDictionary.Remove(msgRef); if (timer != null) timer.Cancel(); } Action<HMessage> action = messageDelegates[msgRef]; messageDelegates.Remove(msgRef); if (action != null) { IAsyncAction threadPoolWorkItem = ThreadPool.RunAsync( (source) => { action(message); } ); } } // 2 - if the ref can not provide a delegate, we try the parameter sent else if (messageDelegate != null) { IAsyncAction threadPoolWorkItem = ThreadPool.RunAsync( (source) => { messageDelegate(message); } ); } else { // 3 - in other case, try the default message delegate onMessage. if (this.onMessage != null) { IAsyncAction threadPoolWorkItem = ThreadPool.RunAsync( (source) => { this.onMessage(message); } ); } } }
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; }
/// <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.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) { message.SetMsgid(Guid.NewGuid().ToString()); 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); }
private void callback(HMessage msg) { Debug.WriteLine(">>>[Callback]<<< \n " + msg.ToString() + "\n"); Update_TextBlock_UI(msgScreen, ">>>CallBack<<< \n" + msg.ToString()); }
void client_onMessage(HMessage message) { Debug.WriteLine(">>>client_onMessage: " + message.ToString()); Update_TextBlock_UI(msgScreen, ">>>onMessage<<< \n" + message.ToString()); }