public void run() { bool result; AgentConnection agentConnection; InstantiateRequest request; //simulationState.stdout.Send("CH: entering main loop\n"); tcpListener.Start(); while (!quit) { Thread.Sleep(0); tcpClient = tcpListener.AcceptTcpClient(); agentConnection = new AgentConnection(simulationState, tcpClient); //simulationState.stdout.Send(String.Format("CH: accepted client.\n")); if (agentConnection.init()) { agentConnections.Add(agentConnection); request = new InstantiateRequest(agentConnection); simulationState.instantiateRequests.Send(request); // Block until we get the signal that the agent has been instantiated in the simulation. // Then, we can start running it's thread and it's perceive-act loop. if (instantiationResults.Recv(out result)) { if (result) { agentConnection.start(); } else { Debug.Log("CH Error: instantiation unsuccesful."); } } else { Debug.LogError("CH Error: instantiation empty."); } } } //simulationState.stdout.Send("CH: exit main loop.\n"); }
public void run() { int agentID = agentState.agentID; MailBox <PerceptRequest> perceptRequests = simulationState.perceptRequests; //To send percept requests to unity MailBox <Percept> percepts = agentState.percepts; //To receive percepts from unity MailBox <Action> actions = agentState.actions; //To send action request to unity MailBox <ActionResult> results = agentState.results; //To receive action result from unit Percept percept; Action action; ActionResult result; while (!quit) { Thread.Sleep(0); try { //Receive request: for perception or action. receiveRequest(out action); if (action == null) //Percept request received { perceptRequests.Send(new PerceptRequest(agentID, percepts)); // send a percept request to unity //simulationState.stdout.Send(String.Format("AC {0}: sending percept request.\n", name)); percepts.Recv(out percept); // block until I receive percept from unity //simulationState.stdout.Send(String.Format("AC {0}: percept ready, sending...\n", name)); sendPercept(percept); // send percept to agent } else //Action request received //simulationState.stdout.Send(String.Format("AC {0}: action received.\n", name)); { if (action.type == ActionType.goodbye) // if the action is say goodbye, close the connection { sendResult(ActionResult.success); quit = true; } else { //simulationState.stdout.Send(String.Format("AC {0}: sending action to handler...\n", name)); actions.Send(action); // send action to handler if (results.Recv(out result)) // get action result from handler //simulationState.stdout.Send(String.Format("AC {0}: action result received.\n", name)); //Thread.Sleep(action.duration); // sleep for the duration of the action. //simulationState.stdout.Send(String.Format("AC {0}: sending action result to agent.\n", name)); { sendResult(result); // send action result to agent } } //sendResult(ActionResult.success); //PETOR //simulationState.stdout.Send(String.Format("AC {0}: perceive-act loop iteration complete.\n", name)); } } catch (System.ObjectDisposedException) { quit = true; } catch (System.IO.IOException) { quit = true; } } //simulationState.stdout.Send(String.Format("AC {0}: quitting...\n", name)); try { tcpClient.Close(); //simulationState.stdout.Send(String.Format("AC {0}: Connection closed.\n", name)); } catch (System.ObjectDisposedException) { //simulationState.stdout.Send(String.Format("AC {0}: Error while closing connection.\n", name)); } }