public string Evaluate(Context simlContext)
        {
            string input = simlContext.Element.Value;

            MachineMessage request = new MachineMessage(input);

            hub.Publish <MachineMessage>(request);

            return(string.Empty);
        }
        // GET MachineImageMessageApi
        public HttpResponseMessage Get(string id)
        {
            string uploadsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, uploadRelativeDirectory);

            if (Directory.Exists(uploadsDirectory))
            {
                foreach (var file in Directory.GetFiles(uploadsDirectory))
                {
                    string guid = Path.GetFileNameWithoutExtension(file);
                    if (guid.LastIndexOf('_') >= 0 && guid.LastIndexOf('_') < guid.Length)
                    {
                        guid = guid.Substring(guid.LastIndexOf('_') + 1);
                    }
                    else
                    {
                        guid = null;
                    }


                    if (Path.GetFileName(file) == id ||
                        Path.GetFileNameWithoutExtension(file) == id ||
                        (guid != null && guid == id))
                    {
                        byte[] fileBytes = File.ReadAllBytes(file);
                        string ext       = Path.GetExtension(file);

                        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
                        result.Content = new ByteArrayContent(fileBytes);
                        result.Content.Headers.ContentType = new MediaTypeHeaderValue(MimeTypeMap.GetMimeType(ext));

                        string info = string.Format("MachineImageMessageApiController: Serving file \"{0}\"", file);
                        Console.WriteLine("[INFO] " + info);
                        IMachineMessage machineMsg = new MachineMessage(info);
                        hub.Publish <IMachineMessage>(machineMsg);

                        return(result);
                    }
                }
            }

            HttpResponseMessage errorMessage = new HttpResponseMessage(HttpStatusCode.OK);

            errorMessage.Content = new ByteArrayContent(Encoding.ASCII.GetBytes("No image found"));
            errorMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("text/json");

            string information = string.Format("MachineImageMessageApiController: No file found");

            Console.WriteLine("[INFO] " + information);
            IMachineMessage machineMessage = new MachineMessage(information);

            hub.Publish <IMachineMessage>(machineMessage);

            return(errorMessage);
        }
Exemple #3
0
        public void AddSubscriber(string ip, string port)
        {
            var stateVal = new StateValue(ip, int.Parse(port));

            subscribers[stateVal.GenerateId()] = new StateValue(ip, int.Parse(port));

            utility.SaveState(configFilename, subscribers);

            Console.WriteLine("[ICANPASSMESSAGE] Added Subscriber " + stateVal.GenerateId());
            IMachineMessage message = new MachineMessage("[ICANPASSMESSAGE] Added Subscriber " + stateVal.GenerateId());

            hub.Publish(message);
        }
Exemple #4
0
        private bool ResponseRegister(HttpWebResponse httpWebResponse)
        {
            string json = Response(httpWebResponse.GetResponseStream());

            if (json != null && httpWebResponse.StatusCode == HttpStatusCode.OK)
            {
                MachineMessage message = serializer.Deserialize <MachineMessage>(json);
                serverClientId = message.Id;
                return(true);
            }

            return(false);
        }
        // POST MachineMessageApi
        public string Post([FromBody] string value)
        {
            if (hub == null)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("MachineMessageApiController error, hub is not available. IMachineMessage not pushed with message: {0}", value);
                Console.ResetColor();
                return("Server Api Error");
            }

            IMachineMessage machineMessage = new MachineMessage(value);

            hub.Publish <IMachineMessage>(machineMessage);

            Console.WriteLine("[INFO] /MachineMessageApi: MachineMessage published: {0}", value);

            return("Acknowledged");
        }
Exemple #6
0
        public ActionResult HostInfo()
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>OSVersion</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Environment.OSVersion);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>MachineName</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Environment.MachineName);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>ProcessorCount</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Environment.ProcessorCount);
            stringBuilder.Append("</tr>");
            string arg = (Environment.TickCount / 3600000.0).ToString("N2");

            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>TickCount</td>");
            stringBuilder.AppendFormat("<td>{0}-{1}:小时</td>", Environment.TickCount, arg);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>UserDomainName:</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Environment.UserDomainName);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>UserInteractive</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Environment.UserInteractive);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>UserName:</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Environment.UserName);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Version</td>");
            stringBuilder.AppendFormat("<td>.NETCLR{0}</td>", Environment.Version);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>EnvironmentVariables.Key</td>");
            stringBuilder.AppendFormat("<td>EnvironmentVariables.Value</td>");
            stringBuilder.Append("</tr>");
            IDictionary environmentVariables = Environment.GetEnvironmentVariables();

            foreach (DictionaryEntry dictionaryEntry in environmentVariables)
            {
                stringBuilder.Append("<trclass='active'>");
                stringBuilder.AppendFormat("<td>{0}</td>", dictionaryEntry.Key);
                stringBuilder.AppendFormat("<td>{0}</td>", dictionaryEntry.Value);
                stringBuilder.Append("</tr>");
            }
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Request.HeaServerVariablesders.Key</td>");
            stringBuilder.AppendFormat("<td>Request.ServerVariables.Key</td>");
            stringBuilder.Append("</tr>");
            NameValueCollection serverVariables = Request.ServerVariables;

            foreach (string text in serverVariables)
            {
                stringBuilder.Append("<tr>");
                stringBuilder.AppendFormat("<td>{0}</td>", text);
                stringBuilder.AppendFormat("<td>{0}</td>", serverVariables[text]);
                stringBuilder.Append("</tr>");
            }
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Request.Headers.Key</td>");
            stringBuilder.AppendFormat("<td>Request.Headers.Key</td>");
            stringBuilder.Append("</tr>");
            NameValueCollection headers = Request.Headers;

            foreach (string text2 in headers)
            {
                stringBuilder.Append("<tr>");
                stringBuilder.AppendFormat("<td>{0}</td>", text2);
                stringBuilder.AppendFormat("<td>{0}</td>", headers.Get(text2));
                stringBuilder.Append("</tr>");
            }
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Request.Url</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Request.Url);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Request.Url.AbsolutePath</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Request.Url.AbsolutePath);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Request.Url.AbsoluteUri</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Request.Url.AbsoluteUri);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Request.Url.Authority</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Request.Url.Authority);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Request.Url.IsAbsoluteUri</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Request.Url.IsAbsoluteUri);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Request.Url.PathAndQuery</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Request.Url.PathAndQuery);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Request.Url.DnsSafeHost</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Request.Url.DnsSafeHost);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Request.Url.Host</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Request.Url.Host);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Request.Url.HostNameType</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Request.Url.HostNameType);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Request.Url.LocalPath</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Request.Url.LocalPath);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Request.Url.UserInfo</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Request.Url.UserInfo);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Request.UserHostName</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Request.UserHostName);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Request.UserHostAddress</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Request.UserHostAddress);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Request.Path</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Request.Path);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Request.PathInfo</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Request.PathInfo);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Request.PhysicalApplicationPath</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Request.PhysicalApplicationPath);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Request.PhysicalPath</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Request.PhysicalPath);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Request.AppRelativeCurrentExecutionFilePath</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Request.AppRelativeCurrentExecutionFilePath);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Request.ApplicationPath</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Request.ApplicationPath);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Server.MachineName</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Server.MachineName);
            stringBuilder.Append("</tr>");
            stringBuilder.Append("<tr>");
            stringBuilder.AppendFormat("<td>Server.ScriptTimeout</td>");
            stringBuilder.AppendFormat("<td>{0}</td>", Server.ScriptTimeout);
            stringBuilder.Append("</tr>");

            MachineMessage model = new MachineMessage
            {
                Message = stringBuilder.ToString()
            };

            return(View(model));
        }
Exemple #7
0
        public void PublishMessageToSubscribers(MessageType machineMessage, string content)
        {
            List <Task <bool> > taskList = new List <Task <bool> >();

            foreach (var subscriber in subscribers)
            {
                string formattedContent = machineMessage.Value + " " + content;
                var    task             = utility.PostMessageAsync(subscriber.Value.ip, subscriber.Value.port, formattedContent);
                task.Start();

                taskList.Add(task);
            }

            int i;

            //File.WriteAllText("hello.txt", "Hi");

            List <bool> taskSuccess = new List <bool>();

            for (i = 0; i < subscribers.Count; i++)
            {
                taskList[i].Wait();

                if (taskList[i].Result)
                {
                    taskSuccess.Add(true);
                }
                else
                {
                    taskSuccess.Add(false);
                }
            }

            List <string> purgeKeys = new List <string>();

            i = 0;
            foreach (var subscriber in subscribers)
            {
                if (!taskSuccess[i++])
                {
                    subscriber.Value.FailureCount++;
                    if (subscriber.Value.FailureCount > 3)
                    {
                        purgeKeys.Add(subscriber.Key);
                    }
                }
                else
                {
                    subscriber.Value.FailureCount = 0;
                }
            }

            foreach (var key in purgeKeys)
            {
                subscribers.Remove(key);

                Console.WriteLine("[ICANPASSMESSAGE] Deleted Subscriber " + key);
                IMachineMessage message = new MachineMessage("[ICANPASSMESSAGE] Deleted Subscriber " + key);
                hub.Publish(message);
            }

            if (purgeKeys.Count > 0)
            {
                utility.SaveState(configFilename, subscribers);
            }
        }
        // POST MachineImageMessageApi
        public string Post()
        {
            var stream = new MemoryStream();

            ControllerContext.Request.Content.CopyToAsync(stream).Wait();

            stream.Seek(0, SeekOrigin.Begin);

            HttpMultipartParser.MultipartFormDataParser parser = new HttpMultipartParser.MultipartFormDataParser(stream);
            var filesList = parser.Files;

            if (filesList.Count > 0)
            {
                string uploadsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, uploadRelativeDirectory);
                if (!Directory.Exists(uploadsDirectory))
                {
                    Directory.CreateDirectory(uploadsDirectory);
                }

                Guid   guid = Guid.NewGuid();
                string resourceIdForImage = filesList.First().FileName;
                resourceIdForImage = Path.GetFileNameWithoutExtension(resourceIdForImage) + "_" + guid.ToString() + Path.GetExtension(resourceIdForImage);
                Stream data = filesList.First().Data;

                bool ImageParsedSuccessfully = false;
                try
                {
                    data.Seek(0, SeekOrigin.Begin);
                    Image image = Image.FromStream(data);
                    image.Save(Path.Combine(uploadsDirectory, resourceIdForImage));
                    ImageParsedSuccessfully = true;

                    // Publish IMachineImageMessage here only
                    string host = System.Configuration.ConfigurationSettings.AppSettings["ChatInterfaceHost"];
                    string port = System.Configuration.ConfigurationSettings.AppSettings["ChatInterfacePort"];
                    string networkLocalHttpUri       = "http://" + host + ":" + port + "/MachineImageMessageApi/" + resourceIdForImage;
                    string deviceLocalFilePath       = Path.Combine(uploadsDirectory, resourceIdForImage);
                    MachineImageMessage imageMessage = new MachineImageMessage(resourceIdForImage, image, networkLocalHttpUri, deviceLocalFilePath);
                    hub?.Publish <IMachineImageMessage>(imageMessage);

                    // Posting the information as IMachineMessage
                    IMachineMessage message = new MachineMessage(string.Format("MachineImageMessage published: {0}, Image parsing done: {1}, PossibleAccessLink: {2}", resourceIdForImage, ImageParsedSuccessfully, networkLocalHttpUri));
                    hub?.Publish <IMachineMessage>(message);

                    Console.WriteLine("[INFO] /MachineImageMessageApi: MachineImageMessage published: {0}, Image parsing done: {1}, PossibleAccessLink: {2}", resourceIdForImage, ImageParsedSuccessfully, networkLocalHttpUri);
                }
                catch
                {
                    data.Seek(0, SeekOrigin.Begin);
                    FileStream fileStream = new FileStream(Path.Combine(uploadsDirectory, resourceIdForImage), FileMode.Create);
                    data.CopyTo(fileStream);
                    fileStream.Close();
                }

                if (hub == null)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("MachineImageMessageApiController error, hub is not available. IMachineMessage not pushed with message");
                    Console.ResetColor();
                    return("Server Api Error");
                }

                return(resourceIdForImage);
            }

            return("Acknowledged");
        }
        public void PushMachineMessage(string message)
        {
            IMachineMessage machineMessage = new MachineMessage(message);

            hub.Publish <IMachineMessage>(machineMessage);
        }