Example #1
0
        /// <summary>
        /// Invoke remote procedure via zbus using JSON data format
        /// </summary>
        /// <param name="method">method name to invoke</param>
        /// <param name="args">method arguments</param>
        /// <returns>primitive type or Dictionary<string,object> instance</returns>
        public object Invoke(string method, params object[] args)
        {
            Dictionary <string, object> req = new Dictionary <string, object>();

            req["id"]     = "jsonrpc";
            req["method"] = method;
            req["params"] = args;

            string json = JSON.Instance.ToJSON(req);

            ZMsg request = new ZMsg();

            request.PushBack(this.Encoding.GetBytes(json));

            ZMsg result = this.client.Request(this.Service, this.Token, request, this.timeout);

            if (result == null)
            {
                throw new ZBusException("json rpc request timeout");
            }
            if (result.FrameSize != 2)
            {
                throw new ZBusException("json rpc result format error");
            }

            Dictionary <string, object> res = null;
            string status = result.PopFrontStr();

            if (status.Equals("200"))
            {
                try {
                    json = result.PopBackStr(this.Encoding);
                    res  = (Dictionary <string, object>)JSON.Instance.Parse(json);
                    if (res.ContainsKey("result"))
                    {
                        return(res["result"]);
                    }
                    else
                    {
                        throw new ZBusException((string)res["error"]);
                    }
                } catch (System.Exception ex) {
                    throw new ZBusException(ex.Message);
                }
            }
            else
            {
                throw new ZBusException(result.PopBackStr(this.Encoding));
            }
        }
Example #2
0
        public bool Publish(string service, string token, string topic, ZMsg msg, int timeout = 2500)
        {
            AsynCtrl ctrl = new AsynCtrl();

            ctrl.Service = service;
            ctrl.Token   = token;
            ctrl.Timeout = timeout;

            msg.PushFront(topic);

            ZMsg   res    = this.Send(ctrl, msg);
            string status = res.PopFrontStr();

            if (status != null && status.Equals("200"))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }