Commit 63820e45 authored by Matthew Hausknecht's avatar Matthew Hausknecht

Refactored code for launching agents.

parent b9548574
...@@ -62,7 +62,7 @@ class Communicator(object): ...@@ -62,7 +62,7 @@ class Communicator(object):
raise TimeoutError raise TimeoutError
else: else:
retryCount -= 1 retryCount -= 1
print 'error receiving message, trying again' print '[Trainer] waiting for message'
time.sleep(0.3) time.sleep(0.3)
#raise ValueError('Error while receiving message') #raise ValueError('Error while receiving message')
(msg,sep,rest) = msg.partition('\0') (msg,sep,rest) = msg.partition('\0')
......
This diff is collapsed.
...@@ -86,6 +86,7 @@ ...@@ -86,6 +86,7 @@
#include <rcsc/player/say_message_builder.h> #include <rcsc/player/say_message_builder.h>
#include <rcsc/player/audio_sensor.h> #include <rcsc/player/audio_sensor.h>
#include <rcsc/player/freeform_parser.h> #include <rcsc/player/freeform_parser.h>
#include <rcsc/player/free_message.h>
#include <rcsc/common/basic_client.h> #include <rcsc/common/basic_client.h>
#include <rcsc/common/logger.h> #include <rcsc/common/logger.h>
...@@ -108,6 +109,7 @@ ...@@ -108,6 +109,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <poll.h>
using namespace rcsc; using namespace rcsc;
using namespace hfo; using namespace hfo;
...@@ -119,7 +121,7 @@ Agent::Agent() ...@@ -119,7 +121,7 @@ Agent::Agent()
M_action_generator(createActionGenerator()), M_action_generator(createActionGenerator()),
lastTrainerMessageTime(-1), lastTrainerMessageTime(-1),
server_port(6008), server_port(6008),
server_running(false), client_connected(false),
num_teammates(-1), num_teammates(-1),
num_opponents(-1), num_opponents(-1),
playing_offense(false) playing_offense(false)
...@@ -230,12 +232,14 @@ bool Agent::initImpl(CmdLineParser & cmd_parser) { ...@@ -230,12 +232,14 @@ bool Agent::initImpl(CmdLineParser & cmd_parser) {
assert(num_teammates >= 0); assert(num_teammates >= 0);
assert(num_opponents >= 0); assert(num_opponents >= 0);
startServer(server_port);
return true; return true;
} }
void Agent::startServer(int server_port) { void Agent::startServer(int server_port) {
std::cout << "[Agent Server] Starting Server on Port " << server_port << std::endl; std::cout << "[Agent Server] Starting Server on Port " << server_port << std::endl;
struct sockaddr_in serv_addr, cli_addr; struct sockaddr_in serv_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0); sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) { if (sockfd < 0) {
perror("[Agent Server] ERROR opening socket"); perror("[Agent Server] ERROR opening socket");
...@@ -250,16 +254,32 @@ void Agent::startServer(int server_port) { ...@@ -250,16 +254,32 @@ void Agent::startServer(int server_port) {
exit(1); exit(1);
} }
listen(sockfd, 5); listen(sockfd, 5);
socklen_t clilen = sizeof(cli_addr); }
std::cout << "[Agent Server] Waiting for client to connect... " << std::endl;
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); void Agent::listenForConnection() {
if (newsockfd < 0) { int rv;
perror("[Agent Server] ERROR on accept"); struct pollfd ufd;
close(sockfd); ufd.fd = sockfd;
exit(1); ufd.events = POLLIN;
rv = poll(&ufd, 1, 1000);
if (rv == -1) {
perror("poll"); // error occurred in poll()
} else if (rv == 0) {
std::cout << "[Agent Server] Waiting for client to connect... " << std::endl;
} else {
if (ufd.revents & POLLIN) {
struct sockaddr_in cli_addr;
socklen_t clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) {
perror("[Agent Server] ERROR on accept");
close(sockfd);
exit(1);
}
std::cout << "[Agent Server] Connected" << std::endl;
clientHandshake();
}
} }
std::cout << "[Agent Server] Connected" << std::endl;
server_running = true;
} }
void Agent::clientHandshake() { void Agent::clientHandshake() {
...@@ -316,6 +336,9 @@ void Agent::clientHandshake() { ...@@ -316,6 +336,9 @@ void Agent::clientHandshake() {
exit(1); exit(1);
} }
std::cout << "[Agent Server] Handshake complete" << std::endl; std::cout << "[Agent Server] Handshake complete" << std::endl;
client_connected = true;
rcsc::FreeMessage<5> *free_msg = new FreeMessage<5>("ready");
addSayMessage(free_msg);
} }
FeatureExtractor* Agent::getFeatureExtractor(feature_set_t feature_set_indx, FeatureExtractor* Agent::getFeatureExtractor(feature_set_t feature_set_indx,
...@@ -367,9 +390,13 @@ status_t Agent::getGameStatus(const rcsc::AudioSensor& audio_sensor, ...@@ -367,9 +390,13 @@ status_t Agent::getGameStatus(const rcsc::AudioSensor& audio_sensor,
virtual method in super class virtual method in super class
*/ */
void Agent::actionImpl() { void Agent::actionImpl() {
if (!server_running) { // For now let's not worry about turning the neck or setting the vision.
startServer(server_port); this->setViewAction(new View_Tactical());
clientHandshake(); this->setNeckAction(new Neck_TurnToBallOrScan());
if (!client_connected) {
listenForConnection();
return;
} }
// Update and send the game status // Update and send the game status
...@@ -451,10 +478,6 @@ void Agent::actionImpl() { ...@@ -451,10 +478,6 @@ void Agent::actionImpl() {
close(sockfd); close(sockfd);
exit(1); exit(1);
} }
// For now let's not worry about turning the neck or setting the vision.
this->setViewAction(new View_Tactical());
this->setNeckAction(new Neck_TurnToBallOrScan());
} }
/*-------------------------------------------------------------------*/ /*-------------------------------------------------------------------*/
......
...@@ -72,7 +72,7 @@ protected: ...@@ -72,7 +72,7 @@ protected:
// Start the server and listen for a connection. // Start the server and listen for a connection.
void startServer(int server_port=6008); void startServer(int server_port=6008);
void listenForConnection();
// Transmit information to the client and ensure it can recieve. // Transmit information to the client and ensure it can recieve.
void clientHandshake(); void clientHandshake();
...@@ -80,7 +80,7 @@ protected: ...@@ -80,7 +80,7 @@ protected:
FeatureExtractor* feature_extractor; FeatureExtractor* feature_extractor;
long lastTrainerMessageTime; // Last time the trainer sent a message long lastTrainerMessageTime; // Last time the trainer sent a message
int server_port; // Port to start the server on int server_port; // Port to start the server on
bool server_running; // Is the server running? bool client_connected; // Has the client connected and handshake?
int sockfd, newsockfd; // Server sockets int sockfd, newsockfd; // Server sockets
int num_teammates, num_opponents; int num_teammates, num_opponents;
bool playing_offense; bool playing_offense;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment