Exemplo n.º 1
0
 public CJobDispatcher()
 {
     m_job               = new CJob();
     m_xmlStream         = new XMLStream();
     m_nextFileSize      = 0;
     m_tempDir           = "";
     m_logMessageHandler = null;
 }
Exemplo n.º 2
0
 public CJobDispatcher()
 {
     m_job = new CJob();
     m_xmlStream = new XMLStream();
     m_nextFileSize = 0;
     m_tempDir = "";
     m_logMessageHandler = null;
 }
Exemplo n.º 3
0
        public void DiscoveryCallback(IAsyncResult ar)
        {
            UdpClient u = (UdpClient)((ShepherdUdpState)(ar.AsyncState)).client;
            IPEndPoint ip = (IPEndPoint)((ShepherdUdpState)(ar.AsyncState)).ip;

            XMLStream inputXMLStream = new XMLStream();
            //IPEndPoint ip= new IPEndPoint();
            XElement xmlDescription;
            string herdAgentXMLDescription;
            try
            {
                Byte[] receiveBytes = u.EndReceive(ar, ref ip);
                //if (!IsLocalIpAddress(ip.Address.ToString()))
                {
                    herdAgentXMLDescription = Encoding.ASCII.GetString(receiveBytes);
                    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;

                    lock (m_listLock)
                    {
                        if (!m_herdAgentList.ContainsKey(ip))
                            m_herdAgentList.Add(ip, herdAgentInfo);
                        else
                            m_herdAgentList[ip] = herdAgentInfo;
                    }
                    //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;

                        if (m_notifyAgentListChanged != null)
                            m_notifyAgentListChanged();
                    }
                }

                u.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);
            }
        }
Exemplo n.º 4
0
        public void checkCancellationRequests()
        {
            int bytes = 0;

            try
            {
                if (!m_netStream.DataAvailable)
                {
                    return;
                }

                XMLStream inputXMLStream = new XMLStream();
                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 == CJobDispatcher.m_quitMessage)
                    {
                        inputXMLStream.addProcessedBytes(bytes);
                        inputXMLStream.discardProcessedData();
                        logMessage("Stopping job execution");
                        m_cancelTokenSource.Cancel();
                    }
                }
            }
            catch (IOException)
            {
                logMessage("IOException in readFromShepherd()");
            }
            catch (OperationCanceledException)
            {
                logMessage("Thread finished gracefully");
            }
            catch (ObjectDisposedException)
            {
                logMessage("Network stream closed: async read finished");
            }
            catch (Exception ex)
            {
                logMessage("Unhandled exception in readFromShepherd");
                logMessage(ex.ToString());
            }
        }
Exemplo n.º 5
0
        public void DiscoveryCallback(IAsyncResult ar)
        {
            UdpClient  u  = (UdpClient)((ShepherdUdpState)(ar.AsyncState)).client;
            IPEndPoint ip = (IPEndPoint)((ShepherdUdpState)(ar.AsyncState)).ip;

            XMLStream inputXMLStream = new XMLStream();
            //IPEndPoint ip= new IPEndPoint();
            XElement xmlDescription;
            string   herdAgentXMLDescription;

            try
            {
                Byte[] receiveBytes = u.EndReceive(ar, ref ip);
                //if (!IsLocalIpAddress(ip.Address.ToString()))
                {
                    herdAgentXMLDescription = Encoding.ASCII.GetString(receiveBytes);
                    xmlDescription          = XElement.Parse(herdAgentXMLDescription);
                    HerdAgentViewModel HerdAgentViewModel = new HerdAgentViewModel();
                    HerdAgentViewModel.parse(xmlDescription);
                    //we copy the ip address into the properties
                    HerdAgentViewModel.ipAddress = ip;
                    //we update the ack time
                    DateTime now = DateTime.Now;
                    HerdAgentViewModel.lastACK = now;

                    lock (m_listLock)
                    {
                        if (!m_herdAgentList.ContainsKey(ip))
                        {
                            m_herdAgentList.Add(ip, HerdAgentViewModel);
                        }
                        else
                        {
                            m_herdAgentList[ip] = HerdAgentViewModel;
                        }
                    }
                    //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;

                        if (m_notifyAgentListChanged != null)
                        {
                            m_notifyAgentListChanged();
                        }
                    }
                }

                u.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);
            }
        }
Exemplo n.º 6
0
        public async Task <int> runTaskAsync(CTask task, CancellationToken cancelToken)
        {
            int returnCode = m_noErrorCode;
            NamedPipeServerStream pipeServer = null;
            Process myProcess = new Process();

            if (task.pipe != "")
            {
                pipeServer = new NamedPipeServerStream(task.pipe);
            }

            try
            {
                myProcess.StartInfo.FileName         = getCachedFilename(task.exe);
                myProcess.StartInfo.Arguments        = task.arguments;
                myProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName(myProcess.StartInfo.FileName);
                logMessage("Running command: " + myProcess.StartInfo.FileName + " " + myProcess.StartInfo.Arguments);

                //not to read 23.232 as 23232
                Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;

                myProcess.Start();
                //addSpawnedProcessToList(myProcess);

                XMLStream xmlStream = new XMLStream();

                string xmlItem;

                if (pipeServer != null)
                {
                    pipeServer.WaitForConnection();

                    while (pipeServer.IsConnected)
                    {
                        //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 != "")
                        {
                            await xmlStream.writeMessageAsync(m_tcpClient.GetStream(), "<" + task.pipe + ">" + xmlItem + "</" + task.pipe + ">"
                                                              , cancelToken, false);

                            xmlItem = xmlStream.processNextXMLItem();
                        }
                    }
                }
                logMessage("Named pipe has been closed for task " + task.name);
                await waitForExitAsync(myProcess, cancelToken);

                int exitCode = myProcess.ExitCode;
                logMessage("Process exited in task " + task.name + ". Return code=" + exitCode);
                //myProcess.WaitForExit();

                if (exitCode < 0)
                {
                    await xmlStream.writeMessageAsync(m_tcpClient.GetStream(), "<" + task.pipe + "><End>Error</End></" + task.pipe + ">"
                                                      , cancelToken, false);

                    returnCode = m_jobInternalErrorCode;
                }
                else
                {
                    await xmlStream.writeMessageAsync(m_tcpClient.GetStream(), "<" + task.pipe + "><End>Ok</End></" + task.pipe + ">"
                                                      , cancelToken, false);
                }
                logMessage("Exit code: " + myProcess.ExitCode);
            }
            catch (OperationCanceledException)
            {
                logMessage("Thread finished gracefully");
                if (myProcess != null)
                {
                    myProcess.Kill();
                }
                returnCode = m_remotelyCancelledErrorCode;
            }
            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");
                //removeSpawnedProcessFromList(myProcess);
                if (pipeServer != null)
                {
                    pipeServer.Close();
                }
            }
            return(returnCode);
        }
Exemplo n.º 7
0
        public void checkCancellationRequests()
        {
            int bytes = 0;

            try
            {
                if (!m_netStream.DataAvailable) return;

                XMLStream inputXMLStream = new XMLStream();
                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 == CJobDispatcher.m_quitMessage)
                    {
                        inputXMLStream.addProcessedBytes(bytes);
                        inputXMLStream.discardProcessedData();
                        logMessage("Stopping job execution");
                        m_cancelTokenSource.Cancel();
                    }
                }
            }
            catch (IOException)
            {
                logMessage("IOException in readFromShepherd()");
            }
            catch (OperationCanceledException)
            {
                logMessage("Thread finished gracefully");
            }
            catch (ObjectDisposedException)
            {
                logMessage("Network stream closed: async read finished");
            }
            catch (Exception ex)
            {
                logMessage("Unhandled exception in readFromShepherd");
                logMessage(ex.ToString());
            }
        }
Exemplo n.º 8
0
        public async Task<int> runTaskAsync(CTask task, CancellationToken cancelToken)
        {
            int returnCode= m_noErrorCode;
            NamedPipeServerStream pipeServer = null;
            Process myProcess = new Process();
            if (task.pipe != "")
                pipeServer = new NamedPipeServerStream(task.pipe);

            try 
            {
                myProcess.StartInfo.FileName = getCachedFilename(task.exe);
                myProcess.StartInfo.Arguments = task.arguments;
                myProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName(myProcess.StartInfo.FileName);
                logMessage("Running command: " + myProcess.StartInfo.FileName + " " + myProcess.StartInfo.Arguments);

                //not to read 23.232 as 23232
                Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;

                myProcess.Start();
                //addSpawnedProcessToList(myProcess);

                XMLStream xmlStream = new XMLStream();

                string xmlItem;

                if (pipeServer != null)
                {
                    pipeServer.WaitForConnection();

                    while (pipeServer.IsConnected)
                    {
                        //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 != "")
                        {
                            await xmlStream.writeMessageAsync(m_tcpClient.GetStream(), "<" + task.pipe + ">" + xmlItem + "</" + task.pipe + ">"
                                , cancelToken, false);
                            
                            xmlItem = xmlStream.processNextXMLItem();
                        }
                    }
                }
                logMessage("Named pipe has been closed for task " + task.name);
                await waitForExitAsync(myProcess,cancelToken);
                
                int exitCode = myProcess.ExitCode;
                logMessage("Process exited in task " + task.name + ". Return code=" + exitCode);
                //myProcess.WaitForExit();

                if (exitCode < 0)
                {
                    await xmlStream.writeMessageAsync(m_tcpClient.GetStream(), "<" + task.pipe + "><End>Error</End></" + task.pipe + ">"
                        , cancelToken,false);
                    returnCode = m_jobInternalErrorCode;
                }
                else
                    await xmlStream.writeMessageAsync(m_tcpClient.GetStream(), "<" + task.pipe + "><End>Ok</End></" + task.pipe + ">"
                        , cancelToken,false);
                logMessage("Exit code: " + myProcess.ExitCode);
            }
            catch (OperationCanceledException)
            {
                logMessage("Thread finished gracefully");
                if (myProcess!=null) myProcess.Kill();
                returnCode = m_remotelyCancelledErrorCode;
            }
            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");
                //removeSpawnedProcessFromList(myProcess);
                if (pipeServer!=null) pipeServer.Close();
            }
            return returnCode;
        }