コード例 #1
0
ファイル: Handler.cs プロジェクト: mrdavidlaing/bosh-dotnet
        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());
            }
        }
コード例 #2
0
        public void TC001_OnlyMessage()
        {
            //Arrange
            string message = "this is a message";

            //Act
            RemoteException remoteException = new RemoteException(message);

            //Assert
            Assert.IsFalse(string.IsNullOrEmpty(remoteException.Backtrace));
        }
コード例 #3
0
        public void TC002_TestException()
        {
            //Arrange
            string message = "this is a message";
            string blob = "blob";
            string stacktrace = "this is a stacktrace";

            //Act
            RemoteException remoteException = new RemoteException(message, stacktrace, blob);

            //Assert
            Assert.AreEqual(message, remoteException.Message);
            Assert.AreEqual(blob, remoteException.Blob);
            Assert.AreEqual(stacktrace, remoteException.Backtrace);
        }
コード例 #4
0
ファイル: Handler.cs プロジェクト: mrdavidlaing/bosh-dotnet
        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());
            }
        }
コード例 #5
0
ファイル: Handler.cs プロジェクト: UhuruSoftware/bosh-dotnet
        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());
            }
        }
コード例 #6
0
ファイル: Handler.cs プロジェクト: UhuruSoftware/bosh-dotnet
        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());
            }
        }