/// <summary> /// Выполнение команды /// </summary> /// <param name="command"></param> /// <returns></returns> static NSRCresponse executeCommand(NSRCcommand command) { return(new NSRCresponse(command.Id) { Success = true, Response = runBash(command.CommandText), }); //else return new NSRCresponse(command.Id) { Success = false, Response = "Command not supported" }; }
internal void AddResponse(NSRCresponse response) { NSRCcommand cmd = SentCommands.FirstOrDefault(i => i.Id == response.Id); if (cmd != null) { cmd.SetResponse(response); } }
/// <summary> /// Взять следующую команду из очереди /// </summary> /// <returns></returns> public NSRCcommand GetCommand() { if (Commands.Count == 0) { return(null); } NSRCcommand cmd = Commands.Dequeue(); SentCommands.Add(cmd); cmd.State = CommandState.Sent; return(cmd); }
/// <summary> /// Ждем ответа от команды /// </summary> /// <param name="id">Идентификатор команды</param> public void waitCommand(Guid id) { Boolean result = false; Int32 countSent = 10; //количество попыток ожидания ответа от команды Int32 sent = 0; while (!result && sent < countSent) { Thread.Sleep(500); NSRCcommand command = server.SentCommands.Where(t => t.Id == id).FirstOrDefault(); if (command != null) { if (command.State == CommandState.Sent) { sent++; } result = command.State == CommandState.Success || command.State == CommandState.Error; } } }
public async Task <JsonResult> OnPost() { ServerName = Request.Form["ServerName"]; NSRCcommand command = new NSRCcommand() { CommandText = Request.Form["CommandText"] }; server.AddCommand(command); Guid commandId = command.Id; await Task.Run(() => waitCommand(commandId)); ViewData["ServerName"] = ServerName; var sentCommand = server.SentCommands.Where(t => t.Id == commandId).FirstOrDefault(); return(new JsonResult(JsonConvert.SerializeObject(new { CommandText = sentCommand.CommandText, CreateDate = sentCommand.CreateDate.ToString("dd.MM.yyyy HH:mm:ss"), ResponseText = sentCommand.ResponseText }))); }
/// <summary> /// Запрос на сервер /// </summary> /// <returns></returns> static Boolean callConnect() { BasicHttpBinding basicHttpBinding = null; EndpointAddress endpointAddress = null; ChannelFactory <INSRCservice> factory = null; INSRCservice serviceProxy = null; Boolean rv = false; try { basicHttpBinding = new BasicHttpBinding(); basicHttpBinding.Security.Mode = BasicHttpSecurityMode.None; endpointAddress = new EndpointAddress(new Uri($"http://{host}:{port}/nsrc_service.asmx")); factory = new ChannelFactory <INSRCservice>(basicHttpBinding, endpointAddress); serviceProxy = factory.CreateChannel(); NSRCcommand command = serviceProxy.Connect(serverName); while (command != null) { rv = true; NSRCresponse response = executeCommand(command); command = serviceProxy.SendResponse(serverName, response); timeout = small_timeout; // переключаем в режим частой проверки } //WriteConsole("command null"); factory.Close(); ((ICommunicationObject)serviceProxy).Close(); } catch (Exception ex) { WriteConsole(ex.Message, true); } return(rv); }
/// <summary> /// Добавить команду /// </summary> /// <param name="command"></param> internal void AddCommand(NSRCcommand command) { Commands.Enqueue(command); }