Example #1
0
        public ServiceMessage ExecuteMessage(ServiceMessage message)
        {
            ServiceMessage result = null;

            if (this.Service != null) {
                var polled = this.Service.ExecuteMessage(message);

                // Clone the message so we have no proxy to the other side.
                result = new ServiceMessage() {
                    Name = polled.Name,
                    Arguments = polled.Arguments,
                    Stamp = polled.Stamp
                };
            }

            return result;
        }
Example #2
0
        /// <summary>
        /// Executes very simple commands originating from the service controller from a local origin.
        /// </summary>
        public ServiceMessage ExecuteMessage(ServiceMessage message) {
            // A request from the service controller to run a command locally.
            ICommandResult result = this.Tunnel(new Command() {
                Name = message.Name,
                Origin = CommandOrigin.Local,
                // This is possible because of the SortedDictionary used in Potato.Service.Shared.ArgumentsHelper
                // This method will always assume that Arguments.Values will result in the order of execution. 
                Parameters = message.Arguments.Values.Select(value => new CommandParameter() {
                    Data = {
                        Content = new List<String>() {
                            value
                        }
                    }
                }).Cast<ICommandParameter>().ToList()
            });

            // Format and return the service message.
            return new ServiceMessage() {
                Name = "result",
                Arguments = new Dictionary<string, string>() {
                    { "Command", message.Name },
                    { "Success", result.Success.ToString() },
                    { "Status", result.CommandResultType.ToString() },
                    { "Message", result.Message }
                }
            };
        }
Example #3
0
        /// <summary>
        /// Fires a message across the appdomain back to the instance controller, provided 
        /// a callback has been setup.
        /// </summary>
        /// <remarks>
        /// If the main thread is tied up then "nop" won't be sent back and the service 
        /// will be shut down. We may expand on this in the future to check extra threads, 
        /// polling all the plugin appdomains and such.
        /// </remarks>
        public ServiceMessage PollService() {
            ServiceMessage message = null;
            
            if (this.ServiceMessage != null) {
                message = this.ServiceMessage;

                // clear it for the next poll.
                this.ServiceMessage = null;
            }
            else {
                message = new ServiceMessage() {
                    Name = "nop"
                };
            }
            
            return message;
        }
Example #4
0
        /// <summary>
        /// Called when a message processing is completed.
        /// </summary>
        /// <param name="message">The message being processed</param>
        /// <param name="seconds">The time in seconds it took to process the signal</param>
        private void OnSignalEnd(ServiceMessage message, Double seconds)
        {
            var handler = this.SignalEnd;

            if (handler != null) {
                handler(this, message, seconds);
            }
        }
Example #5
0
        /// <summary>
        /// Called before processing a signal begins
        /// </summary>
        /// <param name="message">The message being processed</param>
        private void OnSignalBegin(ServiceMessage message)
        {
            var handler = this.SignalBegin;

            if (handler != null) {
                handler(this, message);
            }
        }
Example #6
0
        /// <summary>
        /// Called when a result signal is processed
        /// </summary>
        /// <param name="message">The service message containing further details about the result</param>
        private void OnResult(ServiceMessage message)
        {
            var handler = this.SignalResult;

            if (handler != null) {
                handler(this, message);
            }
        }
Example #7
0
        /// <summary>
        /// Parse a basic message to the service controller
        /// </summary>
        /// <param name="message">The message to dispatch</param>
        /// <returns>True if the message was processed correctly, false otherwise</returns>
        public bool SignalMessage(ServiceMessage message)
        {
            bool processed = true;

            if (message != null) {
                // Record the current time for statistics output.
                DateTime begin = DateTime.Now;

                // Ignore "nop" messages
                if (String.Compare(message.Name, "nop", StringComparison.OrdinalIgnoreCase) != 0) {
                    this.OnSignalBegin(message);

                    if (String.Compare(message.Name, "start", StringComparison.OrdinalIgnoreCase) == 0) {
                        this.Start();
                    }
                    else if (String.Compare(message.Name, "stop", StringComparison.OrdinalIgnoreCase) == 0) {
                        this.Stop();
                    }
                    else if (String.Compare(message.Name, "restart", StringComparison.OrdinalIgnoreCase) == 0) {
                        this.Restart();
                    }
                    else if (String.Compare(message.Name, "merge", StringComparison.OrdinalIgnoreCase) == 0) {
                        if (message.Arguments.ContainsKey("uri") == true && message.Arguments.ContainsKey("packageid") == true) {
                            this.MergePackage(message.Arguments["uri"], message.Arguments["packageid"]);
                        }
                        else {
                            this.OnSignalParameterError(new List<String>() {
                                "uri",
                                "packageId"
                            });
                        }
                    }
                    else if (String.Compare(message.Name, "uninstall", StringComparison.OrdinalIgnoreCase) == 0) {
                        if (message.Arguments.ContainsKey("packageid") == true) {
                            this.UninstallPackage(message.Arguments["packageid"]);
                        }
                        else {
                            this.OnSignalParameterError(new List<String>() {
                                "packageId"
                            });
                        }
                    }
                    else if (String.Compare(message.Name, "statistics", StringComparison.OrdinalIgnoreCase) == 0 || String.Compare(message.Name, "stats", StringComparison.OrdinalIgnoreCase) == 0) {
                        this.OnStatistics();
                    }
                    else if (String.Compare(message.Name, "help", StringComparison.OrdinalIgnoreCase) == 0) {
                        this.OnHelp();
                    }
                    else if (String.Compare(message.Name, "result", StringComparison.OrdinalIgnoreCase) == 0) {
                        this.OnResult(message);
                    }
                    else if (String.Compare(message.Name, "write", StringComparison.OrdinalIgnoreCase) == 0) {
                        this.WriteServiceConfig();
                    }
                    else {
                        processed = false;
                    }

                    this.OnSignalEnd(message, (DateTime.Now - begin).TotalMilliseconds / 1000);
                }
                // else do nothing for nop messages, the message was to do nothing.

                message.Dispose();
            }
            else {
                processed = false;
            }

            return processed;
        }
Example #8
0
        /// <summary>
        /// Runs a command on the instance, returning a signal message. The signal message is
        /// processed the same as if it was found while polling the instance.
        /// </summary>
        /// <param name="message">The message to send to the instance.</param>
        public void ExecuteMessage(ServiceMessage message)
        {
            this.Start();

            if (this.Observer.Status == ServiceStatusType.Started) {
                this.SignalMessage(this.ServiceLoaderProxy.ExecuteMessage(message));
            }
        }
Example #9
0
        public ServiceMessage PollService()
        {
            ServiceMessage message = null;

            if (this.Service != null) {
                var polled = this.Service.PollService();

                // Clone the message so we have no proxy to the other side.
                message = new ServiceMessage() {
                    Name = polled.Name,
                    Arguments = polled.Arguments,
                    Stamp = polled.Stamp
                };
            }

            return message;
        }