public void startListening() { //UPD broadcast client m_discoveryClient = new UdpClient(CJobDispatcher.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, CJobDispatcher.m_comPortHerd); m_listener.Start(); HerdAgentTcpState tcpState = new HerdAgentTcpState(); tcpState.ip = shepherd; //tcpState.herdAgent = this; m_listener.BeginAcceptTcpClient(CommunicationCallback, tcpState); }
public async void CommunicationCallback(IAsyncResult ar) { if (getState() != AgentState.BUSY) { acceptJobQuery(ar); try { setState(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 == CJobDispatcher.m_cleanCacheMessage) { //not yet implemented in the herd client, just in case... logMessage("Cleaning cache directory"); cleanCacheDir(); } else if (xmlItemContent == CJobDispatcher.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("Running job"); returnCode = await runJobAsync(m_cancelTokenSource.Token); if (returnCode == m_noErrorCode || returnCode == m_jobInternalErrorCode) { logMessage("Job finished"); await writeMessageAsync(CJobDispatcher.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(CJobDispatcher.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 { waitAsyncWriteOpsToFinish(); m_tcpClient.Close(); setState(AgentState.AVAILABLE); //try to recover //start listening again HerdAgentTcpState tcpState = new HerdAgentTcpState(); tcpState.ip = new IPEndPoint(0, 0); //m_listener.Start(); m_listener.BeginAcceptTcpClient(CommunicationCallback, tcpState); } } }
public async void CommunicationCallback(IAsyncResult ar) { if (getState() != AgentState.BUSY) { acceptJobQuery(ar); try { setState(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 == CJobDispatcher.m_cleanCacheMessage) { //not yet implemented in the herd client, just in case... logMessage("Cleaning cache directory"); cleanCacheDir(); } else if (xmlItemContent == CJobDispatcher.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("Running job"); returnCode = await runJobAsync(m_cancelTokenSource.Token); if (returnCode == m_noErrorCode || returnCode == m_jobInternalErrorCode) { logMessage("Job finished"); await writeMessageAsync(CJobDispatcher.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(CJobDispatcher.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 { waitAsyncWriteOpsToFinish(); m_tcpClient.Close(); setState(AgentState.AVAILABLE); //try to recover //start listening again HerdAgentTcpState tcpState = new HerdAgentTcpState(); tcpState.ip = new IPEndPoint(0, 0); //m_listener.Start(); m_listener.BeginAcceptTcpClient(CommunicationCallback, tcpState); } } }