Added image output to manager, added plots to results pages
[progcomp2012.git] / judge / manager / network_controller.cpp
index 6d56554..fcca84e 100644 (file)
 #include "network_controller.h"
 
-NetworkController::NetworkController(const Piece::Colour & newColour, const char * newName) : Controller(newColour, newName), sfd(-1)
+using namespace std;
+
+MovementResult NetworkSender::QuerySetup(const char * opponentName, string setup[])
 {
-       struct protoent * tcp = getprotobyname("tcp");
-       sfd = socket(PF_INET, SOCK_STREAM, tcp->p_proto);
-       if (sfd < 0)
-       {
-       //      fprintf(stderr, "NetworkController::NetworkController - couldn't create a TCP socket!");
-               perror("NetworkController::NetworkController - creating TCP socket... ");
-               return;
-       }
+       //fprintf(stderr,"      NetworkSender::QuerySetup... ");
+       MovementResult result = controller->QuerySetup(opponentName, setup);
+       
+       for (int ii=0; ii < 4; ++ii)
+               assert(network->SendMessage("%s",setup[ii].c_str())); //TODO: Proper error check
+
+       //fprintf(stderr,"Done!\n");
+       return result;
 }
 
-NetworkController::~NetworkController()
+MovementResult NetworkReceiver::QuerySetup(const char * opponentName, string setup[])
 {
-       if (Valid())
+       //fprintf(stderr,"      NetworkReceiver::QuerySetup... ");
+       for (int ii=0; ii < 4; ++ii)
        {
-               if (shutdown(sfd, SHUT_RDWR) == -1)
-               {
-               //      fprintf(stderr, "NetworkController::~NetworkController - Can't shutdown socket %d!", sfd);
-                       perror("NetworkController::~NetworkController - shutting down socket... ");
-                       close(sfd);
-                       sfd = -1;
-               }
+               assert(network->GetMessage(setup[ii], 20));
        }
-       close(sfd);
+       //fprintf(stderr,"Done!\n");
+       return MovementResult::OK;
 }
 
-Server::Server(const Piece::Colour & newColour, const char * newName) : NetworkController(newColour, newName)
+MovementResult NetworkSender::QueryMove(string & buffer)
 {
-       struct sockaddr_in ipa;
-       ipa.sin_family = AF_INET;
-       ipa.sin_port = htons(NetworkController::port);  
-       ipa.sin_addr.s_addr = INADDR_ANY;
-       memset(&ipa,0, sizeof ipa);
-
-       if (bind(sfd, (struct sockaddr*)&ipa, sizeof (ipa)) == -1) //dERP DERP DERP
-       {
-       //      fprintf(stderr, "Server::Server - Couldn't bind to socket! Abort\n");
-               perror("Server::Server - binding to socket... ");
-               close(sfd);
-               sfd = -1;
-               return;
-       }
-
-       //Listen for at most 1 connection
-       if (listen(sfd, 1) == -1)
-       {
-               fprintf(stderr, "Server::Server - listening failed.\n");
-               close(sfd);
-               sfd = -1;
-               return;
-       }       
-
-       //Accept the connection
-       sfd = accept(sfd, NULL, NULL);
-       if (sfd < 0)
-       {
-               fprintf(stderr, "Server::Server - couldn't accept connection.\n");
-               close(sfd);
-               sfd = -1;
-               return;
-       }
-
-       //We are now ready to play!
+       MovementResult result = controller->QueryMove(buffer);
+       network->SendMessage("%s", buffer.c_str());
+       return result;
 }
 
-
-Client::Client(const Piece::Colour & newColour, const char * newName, const char * server) : NetworkController(newColour, newName)
+MovementResult NetworkReceiver::QueryMove(string & buffer)
 {
-       struct sockaddr_in ipa;
-       ipa.sin_family = AF_INET;
-       ipa.sin_port = htons(NetworkController::port);  
-       int Res = inet_pton(AF_INET, server, &ipa.sin_addr);
-
-       if (Res < 0)
-       {
-               fprintf(stderr, "Client::Client - First parameter is not a valid address family!\n");
-               close(sfd);
-               sfd = -1;
-               return;
-       }
-       else if (Res == 0)
-       {
-               fprintf(stderr, "Client::Client - Second parameter does not contain a valid IP Address!\n");
-               close(sfd);
-               sfd = -1;
-               return;
-       }
-       
-       if (connect(sfd, (struct sockaddr*)&sfd, sizeof sfd) == -1)
-       {
-               fprintf(stderr, "Client::Client - Connection to server at \"%s\" failed.\n", server);
-               close(sfd);
-               sfd = -1;
-               return;
-       }
-
-       //We are now ready to play!
+       assert(network->GetMessage(buffer, 20));
+       return MovementResult::OK;
 }
-//EOF
-
-

UCC git Repository :: git.ucc.asn.au