Example #1
0
        private void ProcessInThread(IMessage processor, string replyTo, string method, object args)
        {
            try
            {
                // TODO: vladi: implement the long running part
                //if (processor.GetType().GetMethod(method) != null)
                if (processor != null)
                {
                    if (processor.IsLongRunning())
                    {
                        if (this.RestartingAgent)
                        {
                            RemoteException exception = new RemoteException("restarting agent");
                            // todo: vlad: fix the hash here
                            this.Publish(replyTo, exception.ToHash().ToString());
                        }
                        else
                        {
                            this.Lock.WaitOne();
                            try
                            {
                                if (this.LongRunningAgentTask.Count == 0)
                                {
                                    this.ProcessLongRunning(replyTo, processor, args);
                                }
                                else
                                {
                                    RemoteException exception = new RemoteException("already running long running task");
                                    // todo: vlad: fix the hash here
                                    this.Publish(replyTo, exception.ToHash().ToString());
                                }
                            }
                            finally
                            {
                                this.Lock.ReleaseMutex();
                            }
                        }
                    }
                    else
                    {
                        string payload = this.Process(processor, args);

                        if (Config.Configure != null && (method == "prepare_network_change"))
                        {
                            // todo: vladi: fix payload
                            this.Publish(replyTo, payload, () => PostPrepareNetworkChange());
                        }
                        else
                        {
                            // todo: vladi: fix payload
                            this.Publish(replyTo, payload.ToString());
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                // since this is running in a thread we're going to be nice and
                // log an error as this would otherwise be lost
                Logger.Error("{0}: {1)", processor.ToString(), ex.ToString());
            }
        }
Example #2
0
        public void HandleMessage(string json)
        {
            try
            {
                dynamic msg = JsonConvert.DeserializeObject(json);

                if (String.IsNullOrEmpty(msg["reply_to"].ToString()))
                {
                    Logger.Info("Missing reply_to in: {0}", json);
                    return;
                }

                Logger.Info("Message: {0}", json);
                msg = CheckCredentials(msg);
                if (msg == null)
                {
                    return;
                }

                string replyTo = msg["reply_to"].Value;
                string method = msg["method"].Value;
                dynamic args = msg["arguments"];

                if (method.Equals("get_state", StringComparison.OrdinalIgnoreCase))
                {
                    method = "state";
                }

                IMessage processor = Lookup(method);

                if (processor != null)
                {
                    ThreadPool.QueueUserWorkItem((data) =>
                        {
                            ProcessInThread(processor, replyTo, method, args);
                        });
                }
                else if (method.Equals("get_task", StringComparison.OrdinalIgnoreCase))
                {
                    HandleGetTask(replyTo, (JsonConvert.DeserializeObject(args.ToString(), typeof(string[])) as string[])[0]);
                }
                else if (method.Equals("shutdown", StringComparison.OrdinalIgnoreCase))
                {
                    HandleShutdown(replyTo);
                }
                else
                {
                    RemoteException re = new RemoteException(string.Format(CultureInfo.InvariantCulture, "Unknown message {0}", json));
                    // todo: vlad: fix the hash here
                    this.Publish(replyTo, re.ToHash().ToString());
                }
            }
            catch (Exception ex)
            {
                Logger.Warning("Failed to parse message: {0}: {1}", json, ex.ToString());
            }
        }