Fix up all logging output

Remove debug logging
Use cerr or clog in the correct places
refactor a few more functions
This commit is contained in:
mdiluzio 2015-01-04 11:59:58 +00:00
parent b141314434
commit 3ed25cd37f
7 changed files with 121 additions and 125 deletions

View file

@ -16,7 +16,7 @@ if [[ $? != 0 ]]; then
exit
fi
echo "Performing install"
echo "TTRTS: Performing install"
sudo make install
if [[ $? != 0 ]]; then
echo "TTRTS: Install failed, check output"

View file

@ -50,14 +50,12 @@ int runClient(int argc, char* argv[])
if (sockfd < 0)
fatal_perror("ERROR opening socket");
std::cout<<"Opened socket on "<<sockfd<<std::endl;
// Get the hostent information for the host by name
server = gethostbyname(argv[1]);
if (server == NULL)
fatal_error("ERROR, no such host");
std::cout<<"Connecting to "<<argv[1]<<std::endl;
std::cout<<"TTRTS: Connecting to "<<argv[1]<<std::endl;
// Empty the server address struct
memset(&serv_addr,0, sizeof(serv_addr));
@ -75,23 +73,21 @@ int runClient(int argc, char* argv[])
if (connect(sockfd, (const sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
fatal_perror("ERROR connecting");
std::cout<<"Waiting for handshake"<<std::endl;
unsigned int player;
std::string gameNameString;
PerformClientHandshake(sockfd, player, gameNameString);
myPlayer = (player_t)player;
std::cout<<"I am player "<<std::to_string((int)myPlayer)<<std::endl;
std::cout<<"Game is "<<gameNameString<<std::endl;
std::cout<<"TTRTS: I am player "<<std::to_string((int)myPlayer)<<std::endl;
std::cout<<"TTRTS: Game is "<<gameNameString<<std::endl;
// Clean out the games dir
CreateAndCleanGameDir(gameNameString);
while ( n >= 0 )
{
std::cout<<"Waiting for gamestate"<<std::endl;
std::cout<<"TTRTS: Waiting for gamestate"<<std::endl;
std::string gamestate;
while( gamestate.find("END") == std::string::npos )
@ -111,14 +107,12 @@ int runClient(int argc, char* argv[])
// Get the order file for this turn
std::string orders = GetOrdersFromPlayerFile(thisGame,myPlayer);
std::cout<<"Sending orders"<<std::endl;
std::cout<<"TTRTS: Sending orders"<<std::endl;
std::cout<<orders<<std::endl;
// Write to the socket with the buffer
n = write(sockfd,orders.c_str(),orders.length());
if (0 < n)
fatal_perror("ERROR writing to socket");
std::cout<<"Order Sent"<<std::endl;
}
return 0;

View file

@ -1,4 +1,5 @@
#include "filesystem.h"
#include "net.h"
#include <iostream>
@ -95,10 +96,7 @@ CTTRTSGame GetGameFromFile( const std::string& filename )
// If still not good
if( access( gamefile.c_str(), F_OK ) == -1 )
{
std::cerr<<"Error: "<< gamefile <<" file not found"<<std::endl;
return CTTRTSGame(0,0);
}
fatal_perror("Could not open game file");
std::ifstream file(gamefile);
@ -113,10 +111,7 @@ CTTRTSGame GetGameFromFile( const std::string& filename )
gameDescriptor.assign((std::istreambuf_iterator<char>(file)),std::istreambuf_iterator<char>());
if( gameDescriptor.size() == 0 )
{
std::cerr<<"Error: failed to read in any information from "<<gamefile<<std::endl;
return CTTRTSGame(0,0);
}
fatal_error("failed to read in any information from gamefile");
// Create the game
return GetGameFromString(gameDescriptor);
@ -130,7 +125,7 @@ std::string GetOrdersFromPlayerFile(const CTTRTSGame &game, player_t &player)
snprintf(playerOrderFileName, 128, "%s%s/Player_%i_Turn_%i.txt", gameDir.c_str(),game.GetName().c_str(),(int) player, game.GetTurn());
// Wait for the player order file to be created
std::cout<<"Waiting for "<< playerOrderFileName << std::endl;
std::clog<<"TTRTS: Waiting for "<< playerOrderFileName << std::endl;
bool hasOrderFile = false;
while(!hasOrderFile)
{
@ -186,39 +181,29 @@ int CreateAndCleanGameDir(const std::string& gameName)
int ret = stat( gameDir.c_str(), &info );
if( ret == 0 && info.st_mode & S_IFDIR )
{
std::cout<< gameDir << " game directory already exists"<<std::endl;
std::cout<<"Confirm to delete contents [y/N] ";
std::cout<<"TTRTS: " << gameDir << " game directory already exists"<<std::endl;
std::cout<<"TTRTS: Confirm to delete contents [y/N] ";
std::string input;
std::cin>>input;
if( !input.size() || std::tolower(input[0]) != 'y' )
{
std::cerr<<"Aborting..."<<std::endl;
return -1;
}
}
else if ( ret == 0 )
{
std::cerr<< gameDir << " exists but is not directory \nAborting..."<<std::endl;
return -1;
fatal_error("TTRTS_GAMES exists but is not directory \nAborting...");
}
// Create the game directory
char cmd2[128];
snprintf(cmd2,128, "test -d %s || mkdir %s",gameDir.c_str(),gameDir.c_str());
if( system(cmd2) == -1)
{
std::cerr<<"Error: Failed to create the game directory"<<std::endl;
return -1;
}
fatal_error("Error: Failed to create the game directory");
// Clean out the game directory
char cmd1[128];
snprintf(cmd1,128, "rm -rf %s/*",gameDir.c_str());
if ( system(cmd1) == -1 )
{
std::cerr<<"Error: Failed to clean the game directory"<<std::endl;
return -1;
}
fatal_error("Error: Failed to clean the game directory");
return 0;
}
@ -228,7 +213,7 @@ int runFromFilesystem(int argc, char* argv[])
{
std::string gamefile = argv[1];
std::cout<<"Launching TTRTS with "<<gamefile<<std::endl;
std::cout<<"TTRTS: Launching with "<<gamefile<<std::endl;
CTTRTSGame game = GetGameFromFile(gamefile);
// Grab the players involved
@ -244,14 +229,11 @@ int runFromFilesystem(int argc, char* argv[])
// While the game isn't finished
while ( ! game.GameOver() )
{
std::cout<<"Starting turn "<<game.GetTurn()<<std::endl;
std::cout<<"TTRTS: Starting turn "<<game.GetTurn()<<std::endl;
// Create a turn file
if( !OutputGameStateFile(game))
{
std::cerr<<"Error: Failed to output new turn file" << std::endl;
return 1;
}
fatal_error("Error: Failed to output new turn file");
// Wait for order files
for( player_t player : players)
@ -265,29 +247,28 @@ int runFromFilesystem(int argc, char* argv[])
}
// Simulate turn
std::cout<<"Simulating this turn!"<<std::endl;
std::cout<<"TTRTS: Simulating this turn!"<<std::endl;
if ( game.SimulateToNextTurn() )
{
std::cerr << "Error: Failed to simulate for turn "<<game.GetTurn()<<std::endl;
return -1;
}
fatal_error("Failed to simulate game turn");
}
// Output final gamestate
OutputGameStateFile(game);
std::cout<<"TTRTS: Game Over!"<<std::endl;
// Get the winning player
player_t winningPlayer = game.GetWinningPlayer();
// Print the winner!
if ( winningPlayer != player_t::NUM_INVALID )
{
std::cout<<"Game over! Winner:"<<(int) winningPlayer <<std::endl;
std::cout<<"TTRTS: Winner:"<<(int) winningPlayer <<std::endl;
}
else
{
std::cout<<"Game over! It was a draw!"<<std::endl;
std::cout<<"TTRTS: It was a draw!"<<std::endl;
}
return (int)winningPlayer;

View file

@ -31,3 +31,4 @@ int main(int argc, char* argv[])
return runFromFilesystem(argc,argv);
};

View file

@ -12,7 +12,7 @@ void WaitForOrdersFromClient(const ClientInfo info, CTTRTSGame &game, std::mute
{
char buffer[1028]; // buffer for orders
std::cout<<"Waiting for "<<inet_ntoa(info.cli_addr.sin_addr)<<std::endl;
std::clog<<"TTRTS: Waiting for player "<<(int)info.player<<" at "<<inet_ntoa(info.cli_addr.sin_addr)<<std::endl;
std::string orders;
@ -30,7 +30,7 @@ void WaitForOrdersFromClient(const ClientInfo info, CTTRTSGame &game, std::mute
orders+=buffer;
}
std::cout<<"Recieved orders from "<<inet_ntoa(info.cli_addr.sin_addr)<<std::endl;
std::clog<<"Recieved orders from "<<inet_ntoa(info.cli_addr.sin_addr)<<std::endl;
mut.lock();
game.IssueOrders(info.player , orders);
@ -78,7 +78,6 @@ void PerformClientHandshake(int sockfd, unsigned int &player, std::string &gameN
fatal_perror("ERROR recieving handshake from server");
std::string handshake(handshakeBuffer);
std::cout<<"Handshake:"<<handshake<< std::endl;
if ( write( sockfd, handshake.c_str(), handshake.length()+1 ) < 0 )
fatal_perror("ERROR sending handshake to server");
@ -90,13 +89,10 @@ void PerformClientHandshake(int sockfd, unsigned int &player, std::string &gameN
gameNameString = gameName;
}
void PerformServerHandshake(const ClientInfo &client, const CTTRTSGame &game)
void PerformServerHandshake(const ClientInfo &client, const std::string &game)
{
char handshake[64];
snprintf(handshake, sizeof(handshake), TTRTS_HANDSHAKE_FORMAT,(unsigned int)client.player,game.GetName().c_str());
// Output the handshake
std::cout<<"Handshaking:"<<handshake<< std::endl;
snprintf(handshake, sizeof(handshake), TTRTS_HANDSHAKE_FORMAT,(unsigned int)client.player,game.c_str());
// Send handshake
if ( write( client.clientsockfd,handshake,sizeof(handshake) ) < 0 )
@ -107,18 +103,16 @@ void PerformServerHandshake(const ClientInfo &client, const CTTRTSGame &game)
if ( read(client.clientsockfd,buffer,sizeof(buffer)-1) < 0 )
fatal_perror("ERROR reading from client");
std::cout<<"Received:"<<buffer<< std::endl;
// Verify handshake
if ( std::string(buffer) != std::string(handshake) )
fatal_error("Error in client handshake");
std::cout<<"Success on handshake with "<<handshake<< std::endl;
std::clog<<"TTRTS: Success on handshake with player "<<(int)client.player<< std::endl;
}
void TryBindSocket(int sockfd, sockaddr_in &serv_addr)
void TryBindSocket(int sockfd, const sockaddr_in &serv_addr)
{
std::cout<<"Binding socket"<< std::endl;
std::clog<<"TTRTS: Binding to socket"<< std::endl;
int retry = 1;
while (1)
{
@ -129,8 +123,67 @@ void TryBindSocket(int sockfd, sockaddr_in &serv_addr)
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) >= 0)
break;
std::cout<<"Binding failed on try "<<retry<< std::endl;
std::cerr<<"Warning: Binding failed on try "<<retry<< std::endl;
sleep(retry);
retry++;
}
}
ClientInfo &WaitForClientConnection(int sockfd, const std::string &game, ClientInfo &clientInfo)
{
socklen_t clilen = sizeof(sockaddr_in);
// accept waits for a connection from a client
// it returns a new socket file descriptor for this connection
// client information will be stored in cli_addr
clientInfo.clientsockfd = accept(sockfd, (sockaddr *) &clientInfo.cli_addr, &clilen);
if (clientInfo.clientsockfd < 0)
fatal_perror("ERROR on accept");
std::clog<<"TTRTS: Client connected from "<<inet_ntoa(clientInfo.cli_addr.sin_addr)<<" socket "<<clientInfo.clientsockfd<< std::endl;
// Handshake with client
PerformServerHandshake(clientInfo, game);
return clientInfo;
}
int SetUpServerListeningSocket(const sockaddr_in &serv_addr)
{
int sockfd;
std::clog<<"TTRTS: Opening socket"<< std::endl;
// Create a new socket
// AF_INET is general internet socket domain
// SOCK_STREAM as messages will be read in on this socket, SOCK_DGRAM would be for packets
// 0 is for default protocol
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
fatal_perror("ERROR opening socket");
// bind our socket to this server address
TryBindSocket(sockfd, serv_addr);
// Listen on the socket for messages
// Second param is length of backlog queue, the maximum number of connections
// that can be waiting while the process is handling a single connection
// max is usually set to 5
listen(sockfd,5);
return sockfd;
}
sockaddr_in GetLocalServerAddress()
{
sockaddr_in serv_addr; // Server address
// empty the server address
memset(&serv_addr,0, sizeof(serv_addr));
// Set the server address family to AF_INET
serv_addr.sin_family = AF_INET;
// htons swaps from host byte order to network byte order
serv_addr.sin_port = htons(TTRTS_PORT);
// The host for this address is this current machine's IP, INADDR_ANY fetches this
serv_addr.sin_addr.s_addr = INADDR_ANY;
return serv_addr;
}

View file

@ -26,6 +26,16 @@ struct ClientInfo
player_t player;
};
// Get the address of a local server
sockaddr_in GetLocalServerAddress();
// Set up a new listening socket for the server
int SetUpServerListeningSocket(const sockaddr_in &serv_addr);
// Wait for client connection on listening socket sockfd
// Will fill clientInfo with client information
ClientInfo &WaitForClientConnection(int sockfd, const std::string &game, ClientInfo &clientInfo);
// Wait for orders from a client, will not return until client has send valid orders
// Will automatically add orders to the game
void WaitForOrdersFromClient(const ClientInfo info, CTTRTSGame &game, std::mutex &mut);
@ -37,10 +47,10 @@ void WaitForOrdersFromClients(std::vector<ClientInfo> &myClients, CTTRTSGame &ga
void SendGamestateToClients(std::vector<ClientInfo> &myClients, const CTTRTSGame &game, std::mutex &gameMutex);
// Tries to bind to a socket, will attempt 10 times with longer waits between
void TryBindSocket(int sockfd, sockaddr_in &serv_addr);
void TryBindSocket(int sockfd, const sockaddr_in &serv_addr);
// Perform the server side handshake with a client
void PerformServerHandshake(const ClientInfo &client, const CTTRTSGame &game);
void PerformServerHandshake(const ClientInfo &client, const std::string &game);
// Perform the client side handshake with the server
void PerformClientHandshake(int sockfd, unsigned int &player, std::string &gameNameString);

View file

@ -16,39 +16,13 @@
void RunServerForGame(CTTRTSGame &game)
{
std::cout<<"Setting up server on port "<<TTRTS_PORT<< std::endl;
std::cout<<"TTRTS: Setting up server"<<std::endl;
// Server side information
int sockfd; // socket File descriptor
sockaddr_in serv_addr; // Server address
int portno = TTRTS_PORT;
sockaddr_in serv_addr = GetLocalServerAddress();
// empty the server address
memset(&serv_addr,0, sizeof(serv_addr));
// Set the server address family to AF_INET
serv_addr.sin_family = AF_INET;
// htons swaps from host byte order to network byte order
serv_addr.sin_port = htons(portno);
// The host for this address is this current machine's IP, INADDR_ANY fetches this
serv_addr.sin_addr.s_addr = INADDR_ANY;
std::cout<<"Opening socket"<< std::endl;
// Create a new socket
// AF_INET is general internet socket domain
// SOCK_STREAM as messages will be read in on this socket, SOCK_DGRAM would be for packets
// 0 is for default protocol
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
fatal_perror("ERROR opening socket");
// bind our socket to this server address
TryBindSocket(sockfd, serv_addr);
// Listen on the socket for messages
// Second param is length of backlog queue, the maximum number of connections
// that can be waiting while the process is handling a single connection
// max is usually set to 5
listen(sockfd,5);
// socket File descriptor
int sockfd = SetUpServerListeningSocket(serv_addr);
// Get information about the game
std::vector<player_t> players = game.GetPlayers();
@ -61,59 +35,40 @@ void RunServerForGame(CTTRTSGame &game)
// Set of clients
std::vector<ClientInfo> myClients;
std::cout<<"Waiting for clients"<< std::endl;
std::cout<<"TTRTS: Waiting for "<<numClients<<" players"<<std::endl;
// Loop while we're connecting the clients
while ( myClients.size() < numClients )
{
// information for each client
sockaddr_in cli_addr; // Client address
int clientsockfd; // new socket File descriptor
socklen_t clilen = sizeof(sockaddr_in);
// accept waits for a connection from a client
// it returns a new socket file descriptor for this connection
// client information will be stored in cli_addr
clientsockfd = accept(sockfd, (sockaddr *) &cli_addr, &clilen);
if (clientsockfd < 0)
fatal_perror("ERROR on accept");
std::cout<<"Client connected from "<<inet_ntoa(cli_addr.sin_addr)<<" socket "<<clientsockfd<< std::endl;
// Fill out client info struct
ClientInfo clientInfo;
clientInfo.cli_addr = cli_addr;
clientInfo.clientsockfd = clientsockfd;
clientInfo.player = *player_iterator;
clientInfo.player = *player_iterator;
player_iterator++;
// Handshake with client
PerformServerHandshake(clientInfo, game);
clientInfo = WaitForClientConnection(sockfd, game.GetName(), clientInfo);
// Add out client info to our list
myClients.push_back(clientInfo);
// Iterate to next player
player_iterator++;
}
std::cout<<"All clients connected"<< std::endl;
std::cout<<"TTRTS: All players connected"<< std::endl;
std::cout<<"Hit enter to begin...";
std::cout<<"TTRTS: Hit enter to begin...";
std::cin.ignore();
// Loop for each turn
while ( !game.GameOver() )
{
// Send data to clients
std::cout<<"Sending clients gamedata"<< std::endl;
std::cout<<"TTRTS: Broadcasting Gamestate"<< std::endl;
SendGamestateToClients(myClients, game, gameMutex);
// Wait for orders from clients
std::cout<<"Waiting for client orders"<< std::endl;
std::cout<<"TTRTS: Waiting for orders from players"<< std::endl;
WaitForOrdersFromClients(myClients, game, gameMutex);
std::cout<<"Orders recieved, simulating turn"<< std::endl;
std::cout<<"TTRTS: All orders recieved, simulating turn"<< std::endl;
// Step to the next turn
gameMutex.lock();
@ -134,14 +89,16 @@ int runServer(int argc, char* argv[])
RunServerForGame(game);
std::cout<<"TTRTS: Game over!"<<std::endl;
// Get the winning player
player_t winningPlayer = game.GetWinningPlayer();
// Print the winner!
if ( winningPlayer != player_t::NUM_INVALID )
std::cout<<"Game over! Winner:"<<(int) winningPlayer <<std::endl;
std::cout<<"TTRTS: Winner is player "<<(int) winningPlayer <<std::endl;
else
std::cout<<"Game over! It was a draw!"<<std::endl;
std::cout<<"TTRTS: It was a draw!"<<std::endl;
// Return
return (int)winningPlayer;