Exemple #1
0
        public void StartListening()
        {
            //UPD broadcast client
            m_discoveryClient = new UdpClient(JobTransmitter.m_discoveryPortHerd);
            HerdAgentUdpState state    = new HerdAgentUdpState();
            IPEndPoint        shepherd = new IPEndPoint(0, 0);

            state.ip = shepherd;
            // state.herdAgent = this;
            m_discoveryClient.BeginReceive(DiscoveryCallback, state);


            //TCP communication socket
            m_listener = new TcpListener(IPAddress.Any, JobTransmitter.m_comPortHerd);
            m_listener.Start();
            HerdAgentTcpState tcpState = new HerdAgentTcpState();

            tcpState.ip = shepherd;

            m_listener.BeginAcceptTcpClient(CommunicationCallback, tcpState);
        }
Exemple #2
0
        public async void CommunicationCallback(IAsyncResult ar)
        {
            if (State != AgentState.Busy)
            {
                LogMessage("Receiving job");
                AcceptJobQuery(ar);

                try
                {
                    State = AgentState.Busy;

                    int ret = await ReadAsync(m_cancelTokenSource.Token);

                    string xmlItem = m_xmlStream.processNextXMLItem();
                    string xmlItemContent;
                    int    returnCode;

                    if (xmlItem != "")
                    {
                        xmlItemContent = m_xmlStream.getLastXMLItemContent();
                        if (xmlItemContent == JobTransmitter.m_acquireMessage)
                        {
                            LogMessage("Receiving job data from "
                                       + m_tcpClient.Client.RemoteEndPoint.ToString());
                            bool bret = await ReceiveJobQuery(m_cancelTokenSource.Token);

                            if (bret)
                            {
                                //run the job
                                LogMessage("Job received");
                                LogMessage(m_job.ToString());
                                LogMessage("Running job");
                                returnCode = await RunJobAsync(m_cancelTokenSource.Token);

                                if (returnCode == m_noErrorCode || returnCode == m_jobInternalErrorCode)
                                {
                                    LogMessage("Job finished. Code=" + returnCode);
                                    await WriteMessageAsync(JobTransmitter.m_endMessage, m_cancelTokenSource.Token, true);

                                    LogMessage("Sending job results");
                                    //we will have to enqueue async write operations to wait for them to finish before closing the tcpClient
                                    StartEnqueueingAsyncWriteOps();
                                    SendJobResult(m_cancelTokenSource.Token);

                                    LogMessage("Job results sent");
                                }
                                else if (returnCode == m_remotelyCancelledErrorCode)
                                {
                                    LogMessage("The job was remotely cancelled");
                                    WriteMessage(JobTransmitter.m_errorMessage, false);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogMessage("Unhandled exception in the herd agent's communication callback function");
                    LogMessage(ex.ToString() + ex.InnerException + ex.StackTrace);
                }
                finally
                {
                    LogMessage("Waiting for queued async write operations to finish");
                    waitAsyncWriteOpsToFinish();
                    LogMessage("Closing the TCP connection");
                    m_tcpClient.Close();
                    State = AgentState.Available;

                    //try to recover
                    //start listening again
                    HerdAgentTcpState tcpState = new HerdAgentTcpState();
                    tcpState.ip = new IPEndPoint(0, 0);

                    m_listener.BeginAcceptTcpClient(CommunicationCallback, tcpState);
                }
            }
        }