public JobTransmitter() { m_job = new Job(); m_xmlStream = new XMLStream(); m_nextFileSize = 0; m_logMessageHandler = null; }
public void CheckCancellationRequests() { try { if (!m_netStream.DataAvailable) { return; } XMLStream inputXMLStream = new XMLStream(); var bytes = m_netStream.Read(inputXMLStream.getBuffer(), inputXMLStream.getBufferOffset(), inputXMLStream.getBufferSize() - inputXMLStream.getBufferOffset()); inputXMLStream.addBytesRead(bytes); //we let the xmlstream object know that some bytes have been read in its buffer string xmlItem = inputXMLStream.peekNextXMLItem(); if (xmlItem != "") { string xmlItemContent = inputXMLStream.getLastXMLItemContent(); if (xmlItemContent == JobTransmitter.m_quitMessage) { inputXMLStream.addProcessedBytes(bytes); inputXMLStream.discardProcessedData(); LogMessage("Stopping job execution"); m_cancelTokenSource.Cancel(); } } } catch (IOException) { LogMessage("IOException in CheckCancellationRequests()"); } catch (OperationCanceledException) { LogMessage("Thread finished gracefully"); } catch (ObjectDisposedException) { LogMessage("Network stream closed: async read finished"); } catch (InvalidOperationException ex) { LogMessage("InvalidOperationException in CheckCancellationRequests"); LogMessage(ex.ToString()); } catch (Exception ex) { LogMessage("Unhandled exception in CheckCancellationRequests"); LogMessage(ex.ToString()); } }
public async Task <int> RunTaskAsync(HerdTask task, CancellationToken cancelToken) { int returnCode = m_noErrorCode; NamedPipeServerStream pipeServer = null; Process myProcess = new Process(); string actualPipeName = task.Pipe; if (task.Pipe != "") { if (CPUArchitecture == PropValues.Linux32 || CPUArchitecture == PropValues.Linux64) { //we need to prepend the name of the pipe with "/tmp/" to be accesible to the client process actualPipeName = "/tmp/" + task.Pipe; } pipeServer = new NamedPipeServerStream(actualPipeName); } XMLStream xmlStream = new XMLStream(); try { //not to read 23.232 as 23232 Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; myProcess.StartInfo.FileName = getCachedFilename(task.Exe); myProcess.StartInfo.Arguments = task.Arguments; myProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName(myProcess.StartInfo.FileName); myProcess.StartInfo.RedirectStandardOutput = true; myProcess.StartInfo.UseShellExecute = false; if (myProcess.Start()) { LogToFile("Running command: " + myProcess.StartInfo.FileName + " " + myProcess.StartInfo.Arguments); } else { LogToFile("Error running command: " + myProcess.StartInfo.FileName + " " + myProcess.StartInfo.Arguments); return(m_jobInternalErrorCode); } string xmlItem; if (pipeServer != null) { bool clientEnded = false; pipeServer.WaitForConnection(); while (pipeServer.IsConnected && !clientEnded) { //check if the herd agent sent us a quit message //in case it did, the function will cancel the cancellation token CheckCancellationRequests(); //check if we have been asked to cancel cancelToken.ThrowIfCancellationRequested(); int numBytes = await xmlStream.readFromNamedPipeStreamAsync(pipeServer, cancelToken); xmlItem = xmlStream.processNextXMLItem(); while (xmlItem != "" && !clientEnded) { if (xmlItem != "<End></End>") { await xmlStream.WriteMessageAsync(m_tcpClient.GetStream(), "<" + task.Pipe + ">" + xmlItem + "</" + task.Pipe + ">", cancelToken); xmlItem = xmlStream.processNextXMLItem(); } else { clientEnded = true; } } } } LogMessage("Task ended: " + task.Name + ". Waiting for process to end"); if (!myProcess.HasExited) { await WaitForExitAsync(myProcess, cancelToken); } int exitCode = myProcess.ExitCode; LogMessage("Process exited in task " + task.Name + ". Return code=" + exitCode); if (exitCode < 0) { await xmlStream.WriteMessageAsync(m_tcpClient.GetStream(), "<" + task.Pipe + "><End>Error</End></" + task.Pipe + ">", cancelToken); returnCode = m_jobInternalErrorCode; } else { await xmlStream.WriteMessageAsync(m_tcpClient.GetStream(), "<" + task.Pipe + "><End>Ok</End></" + task.Pipe + ">", cancelToken); } LogMessage("Exit code: " + myProcess.ExitCode); } catch (OperationCanceledException) { LogMessage("Thread finished gracefully"); if (myProcess != null) { myProcess.Kill(); } returnCode = m_remotelyCancelledErrorCode; } catch (AuthenticationException ex) { LogMessage(ex.ToString()); } catch (Exception ex) { LogMessage("unhandled exception in runTaskAsync()"); LogMessage(ex.ToString()); if (myProcess != null) { myProcess.Kill(); } returnCode = m_jobInternalErrorCode; } finally { LogMessage("Task " + task.Name + " finished"); if (pipeServer != null) { pipeServer.Dispose(); } } return(returnCode); }
void DiscoveryCallback(IAsyncResult ar) { UdpClient udpClient = (UdpClient)((ShepherdUdpState)(ar.AsyncState)).Client; IPEndPoint ip = (IPEndPoint)((ShepherdUdpState)(ar.AsyncState)).Ip; XMLStream inputXMLStream = new XMLStream(); XElement xmlDescription; string herdAgentXMLDescription; try { Byte[] receiveBytes = udpClient.EndReceive(ar, ref ip); { herdAgentXMLDescription = Encoding.ASCII.GetString(receiveBytes); if (herdAgentXMLDescription.IndexOf('<') == 0) { xmlDescription = XElement.Parse(herdAgentXMLDescription); HerdAgentInfo herdAgentInfo = new HerdAgentInfo(); herdAgentInfo.Parse(xmlDescription); //we copy the ip address into the properties herdAgentInfo.ipAddress = ip; //we update the ack time DateTime now = DateTime.Now; herdAgentInfo.lastACK = now; bool agentAddedToList = false; lock (m_listLock) { if (!m_herdAgentList.ContainsKey(herdAgentInfo.ProcessorId)) { m_herdAgentList[herdAgentInfo.IpAddressString] = herdAgentInfo; //We have to use the ip address until ProcessorId is a GUID on all deployed agents agentAddedToList = true; } } if (agentAddedToList) { //check how much time ago the agent list was updated double lastUpdateElapsedTime = (now - m_lastHerdAgentListUpdate).TotalSeconds; //notify, if we have to, that the agent list has probably changed if (lastUpdateElapsedTime > m_herdAgentListUpdateTime) { m_lastHerdAgentListUpdate = now; } m_notifyAgentListChanged?.Invoke(herdAgentInfo); } } } udpClient.BeginReceive(new AsyncCallback(DiscoveryCallback), ar.AsyncState); } catch (TaskCanceledException ex) { LogMessage("Task canceled exception in Shepherd"); LogMessage(ex.ToString()); } catch (Exception ex) { LogMessage("Exception in discovery callback function"); LogMessage(ex.StackTrace); } }