Пример #1
0
        public JObject Call(string path, JToken args, Action <bool, JToken> responseCallback, double responseTimeoutMs, double methodCallTimeoutMs)
        {
            if (path == null)
            {
                throw new ArgumentNullException();
            }

            lock (this.methodCallbacks)
            {
                if (this.methodCallbacks.ContainsKey(path))
                {
                    throw new ArgumentException("You can't call Call() on a method you don't own!", "path");
                }
            }

            JObject parameters = new JObject();

            parameters["path"] = path;
            if (methodCallTimeoutMs > 0.1)
            {
                parameters["timeout"] = methodCallTimeoutMs / 1000.0;
            }

            if (args.Type != JTokenType.Null)
            {
                parameters["args"] = args;
            }

            JetMethod call = new JetMethod(JetMethod.Call, parameters, responseCallback, responseTimeoutMs);

            return(this.ExecuteMethod(call));
        }
Пример #2
0
        public JObject Fetch(out FetchId id, Matcher matcher, Action <JToken> fetchCallback, Action <bool, JToken> responseCallback, double responseTimeoutMs)
        {
            int        fetchId = Interlocked.Increment(ref this.fetchIdCounter);
            JetFetcher fetcher = new JetFetcher(fetchCallback);

            this.RegisterFetcher(fetchId, fetcher);

            JObject parameters = new JObject();
            JObject path       = this.FillPath(matcher);

            if (path != null)
            {
                parameters["path"] = path;
            }

            parameters["caseInsensitive"] = matcher.CaseInsensitive;
            parameters["id"] = fetchId;
            JetMethod fetch = new JetMethod(JetMethod.Fetch, parameters, responseCallback, responseTimeoutMs);

            id = new FetchId(fetchId);
            lock (allFetches)
            {
                allFetches.Add(id);
            }

            return(this.ExecuteMethod(fetch));
        }
Пример #3
0
        public void info(Action <JToken> responseCallback)
        {
            int       id   = Interlocked.Increment(ref requestIdCounter);
            JetMethod info = new JetMethod(JetMethod.INFO, null, id, responseCallback);

            executeMethod(info, id);
        }
Пример #4
0
        private JObject ExecuteMethod(JetMethod method)
        {
            double timeoutMs = method.getTimeoutMs();

            if (timeoutMs < 0.0)
            {
                throw new ArgumentException("timeoutMs");
            }

            if (method.HasResponseCallback())
            {
                int id = method.GetRequestId();
                lock (this.openRequests)
                {
                    method.RequestTimer.Interval = timeoutMs;
                    method.RequestTimer.Elapsed += (sender, e) => RequestTimer_Elapsed(this, e, method);
                    method.RequestTimer.Start();

                    this.openRequests.Add(id, method);
                }
            }

            JObject request = method.GetJson();

            this.connection.SendMessage(JsonConvert.SerializeObject(request));
            return(request);
        }
Пример #5
0
        public JObject Set(string path, JToken value, Action <bool, JToken> responseCallback, double responseTimeoutMs, double stateSetTimeoutMs)
        {
            if (String.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }

            lock (this.stateCallbacks)
            {
                if (this.stateCallbacks.ContainsKey(path))
                {
                    throw new ArgumentException("Don't call Set() on a state you own, use Change() instead!", "path");
                }
            }

            JObject parameters = new JObject();

            parameters["path"]  = path;
            parameters["value"] = value;
            if (stateSetTimeoutMs > 0.1)
            {
                parameters["timeout"] = stateSetTimeoutMs / 1000.0;
            }

            JetMethod set = new JetMethod(JetMethod.Set, parameters, responseCallback, responseTimeoutMs);

            return(this.ExecuteMethod(set));
        }
Пример #6
0
 private void executeMethod(JetMethod method, int id)
 {
     lock (openRequests)
     {
         openRequests.Add(id, method);
     }
     io.sendMessage(Encoding.UTF8.GetBytes(method.getJson()));
 }
Пример #7
0
        public JObject Passwd(string user, string password, Action <bool, JToken> responseCallback, double responseTimeoutMs)
        {
            JObject credentials = new JObject();

            credentials["user"]     = user;
            credentials["password"] = password;
            JetMethod passwd = new JetMethod(JetMethod.Passwd, credentials, responseCallback, responseTimeoutMs);

            return(this.ExecuteMethod(passwd));
        }
Пример #8
0
        public JObject Authenticate(string user, string password, Action <bool, JToken> responseCallback, double responseTimeoutMs)
        {
            JObject credentials = new JObject();

            credentials["user"]     = user;
            credentials["password"] = password;
            JetMethod authenticate = new JetMethod(JetMethod.Authenticate, credentials, responseCallback, responseTimeoutMs);

            return(this.ExecuteMethod(authenticate));
        }
Пример #9
0
        public void set(string path, JToken value, Action <JToken> responseCallback)
        {
            JObject parameters = new JObject();

            parameters["path"]  = path;
            parameters["value"] = value;
            int       requestId = Interlocked.Increment(ref requestIdCounter);
            JetMethod set       = new JetMethod(JetMethod.SET, parameters, requestId, responseCallback);

            executeMethod(set, requestId);
        }
Пример #10
0
        public void unfetch(FetchId fetchId, Action <JToken> responseCallback)
        {
            unregisterFetcher(fetchId.getId());

            JObject parameters = new JObject();

            parameters["id"] = fetchId.getId();
            int       requestId = Interlocked.Increment(ref requestIdCounter);
            JetMethod unfetch   = new JetMethod(JetMethod.UNFETCH, parameters, requestId, responseCallback);

            executeMethod(unfetch, requestId);
        }
Пример #11
0
        /// <summary>
        /// Configures a connection.
        /// </summary>
        /// <param name="name">A name for the connection. This name is mainly used on the daemon for better logging.</param>
        /// <param name="responseCallback">A callback method that will be called if this method succeeds or fails.</param>
        /// <param name="responseTimeoutMilliseconds">The timeout how long the operation might take before failing.</param>
        /// <returns>A JObject representing the Config message send to the Jet daemon.</returns>
        public JObject Config(String name, Action <bool, JToken> responseCallback, double responseTimeoutMs)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            JObject parameters = new JObject();

            parameters["name"] = name;
            JetMethod config = new JetMethod(JetMethod.Config, parameters, responseCallback, responseTimeoutMs);

            return(this.ExecuteMethod(config));
        }
Пример #12
0
        public JObject Unfetch(FetchId fetchId, Action <bool, JToken> responseCallback, double responseTimeoutMs)
        {
            this.UnregisterFetcher(fetchId.GetId());

            JObject parameters = new JObject();

            parameters["id"] = fetchId.GetId();
            JetMethod unfetch = new JetMethod(JetMethod.Unfetch, parameters, responseCallback, responseTimeoutMs);

            lock (allFetches)
            {
                allFetches.Remove(fetchId);
            }

            return(this.ExecuteMethod(unfetch));
        }
Пример #13
0
        public JObject Get(Matcher matcher, Action <bool, JToken> responseCallback, double responseTimeoutMs)
        {
            JObject parameters = new JObject();
            JObject path       = this.FillPath(matcher);

            if (path != null)
            {
                parameters["path"] = path;
            }

            parameters["caseInsensitive"] = matcher.CaseInsensitive;

            JetMethod get = new JetMethod(JetMethod.Get, parameters, responseCallback, responseTimeoutMs);


            return(this.ExecuteMethod(get));
        }
Пример #14
0
        public FetchId fetch(Matcher matcher, Action <JToken> fetchCallback, Action <JToken> responseCallback)
        {
            int        fetchId = Interlocked.Increment(ref fetchIdCounter);
            JetFetcher fetcher = new JetFetcher(fetchCallback);

            registerFetcher(fetchId, fetcher);

            JObject parameters = new JObject();

            parameters["path"]            = fillPath(matcher);
            parameters["caseInsensitive"] = matcher.caseInsensitive;
            parameters["id"] = fetchId;
            int       requestId = Interlocked.Increment(ref requestIdCounter);
            JetMethod fetch     = new JetMethod(JetMethod.FETCH, parameters, requestId, responseCallback);

            executeMethod(fetch, requestId);
            return(new FetchId(fetchId));
        }
Пример #15
0
        public JObject AddState(
            string path,
            JToken value,
            string[] setGroups,
            string[] fetchGroups,
            Func <string, JToken, JToken> stateCallback,
            Action <bool, JToken> responseCallback,
            double responseTimeoutMs,
            double stateSetTimeoutMs = DefaultRoutingTimeout)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            JObject parameters = new JObject();

            parameters["path"]    = path;
            parameters["value"]   = value;
            parameters["timeout"] = stateSetTimeoutMs / 1000.0;
            if (stateCallback == null)
            {
                parameters["fetchOnly"] = true;
            }
            else
            {
                this.RegisterStateCallback(path, stateCallback);
            }

            if (((setGroups != null) && (setGroups.Length > 0)) || ((fetchGroups != null) && (fetchGroups.Length > 0)))
            {
                JObject access = new JObject();
                parameters["access"]  = access;
                access["setGroups"]   = new JArray(setGroups);
                access["fetchGroups"] = new JArray(fetchGroups);
            }

            JetMethod add = new JetMethod(JetMethod.Add, parameters, responseCallback, responseTimeoutMs);

            return(this.ExecuteMethod(add));
        }
Пример #16
0
        private void handleResponse(JObject json)
        {
            JToken token = json["id"];

            if (token != null)
            {
                int       id     = token.ToObject <int>();
                JetMethod method = null;
                lock (openRequests)
                {
                    if (openRequests.ContainsKey(id))
                    {
                        method = openRequests[id];
                        openRequests.Remove(id);
                    }
                }
                if (method != null)
                {
                    method.callResponseCallback(json);
                }
            }
        }
Пример #17
0
        private void HandleResponse(JObject json)
        {
            JToken token = json["id"];

            if (token != null)
            {
                int       id     = token.ToObject <int>();
                JetMethod method = null;
                lock (this.openRequests)
                {
                    if (this.openRequests.ContainsKey(id))
                    {
                        method = this.openRequests[id];
                        this.openRequests.Remove(id);
                        method.RequestTimer.Stop();
                        lock (method)
                        {
                            method.CallResponseCallback(true, json);
                        }
                    }
                }
            }
        }
Пример #18
0
        private JObject RemoveStateOrMethod(bool isState, string path, Action <bool, JToken> responseCallback, double responseTimeoutMs)
        {
            if (path == null)
            {
                throw new ArgumentNullException();
            }

            if (isState)
            {
                this.UnregisterStateCallback(path);
            }
            else
            {
                this.UnregisterMethodCallback(path);
            }

            JObject parameters = new JObject();

            parameters["path"] = path;
            JetMethod remove = new JetMethod(JetMethod.Remove, parameters, responseCallback, responseTimeoutMs);

            return(this.ExecuteMethod(remove));
        }
Пример #19
0
        public JObject Change(string path, JToken value, Action <bool, JToken> responseCallback, double responseTimeoutMs)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            lock (this.stateCallbacks)
            {
                if (!this.stateCallbacks.ContainsKey(path))
                {
                    throw new ArgumentException("You can't call Change() on a state you don't own!", "path");
                }
            }

            JObject parameters = new JObject();

            parameters["path"]  = path;
            parameters["value"] = value;
            JetMethod change = new JetMethod(JetMethod.Change, parameters, responseCallback, responseTimeoutMs);

            return(this.ExecuteMethod(change));
        }
Пример #20
0
        public JObject AddMethod(
            string path,
            string[] callGroups,
            string[] fetchGroups,
            Func <string, JToken, JToken> methodCallback,
            Action <bool, JToken> responseCallback,
            double responseTimeoutMs,
            double methodCallTimeoutMs = DefaultRoutingTimeout)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            if (methodCallback == null)
            {
                throw new ArgumentNullException("methodCallback");
            }

            JObject parameters = new JObject();

            parameters["path"]    = path;
            parameters["timeout"] = methodCallTimeoutMs / 1000.0;
            if (((callGroups != null) && (callGroups.Length > 0)) || ((fetchGroups != null) && (fetchGroups.Length > 0)))
            {
                JObject access = new JObject();
                parameters["access"]  = access;
                access["callGroups"]  = new JArray(callGroups);
                access["fetchGroups"] = new JArray(fetchGroups);
            }

            this.RegisterMethodCallback(path, methodCallback);
            JetMethod add = new JetMethod(JetMethod.Add, parameters, responseCallback, responseTimeoutMs);

            return(this.ExecuteMethod(add));
        }
Пример #21
0
        public JObject Info(Action <bool, JToken> responseCallback, double responseTimeoutMs)
        {
            JetMethod info = new JetMethod(JetMethod.Info, null, responseCallback, responseTimeoutMs);

            return(this.ExecuteMethod(info));
        }
Пример #22
0
        private static void RequestTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e, JetMethod method)
        {
            JetPeer peer = (JetPeer)sender;

            lock (peer.openRequests)
            {
                method.RequestTimer.Stop();
                int id = method.GetRequestId();

                if (peer.openRequests.ContainsKey(id))
                {
                    method = peer.openRequests[id];
                    peer.openRequests.Remove(id);

                    JObject response = new JObject();
                    response["jsonrpc"] = "2.0";
                    response["id"]      = id;
                    JObject error = new JObject();
                    error["code"]     = -32100;
                    error["message"]  = "timeout while waiting for response";
                    response["error"] = error;
                    lock (method)
                    {
                        method.CallResponseCallback(false, response);
                    }
                }
            }
        }