Index: server/server.cpp
===================================================================
--- server/server.cpp	(revision c4c2a3c296e22864e1e5346d67b1ece27ba6d918)
+++ server/server.cpp	(revision c044a36f579106497263bfffc4f0c15ddef7a1d7)
@@ -5,4 +5,5 @@
 #include <iostream>
 #include <sstream>
+#include <fstream>
 #include <cstring>
 #include <cmath>
@@ -10,4 +11,6 @@
 #include <vector>
 #include <map>
+
+#include <csignal>
 
 #include <sys/time.h>
@@ -37,7 +40,9 @@
 using namespace std;
 
+bool done;
+
 // from used to be const. Removed that so I could take a reference
 // and use it to send messages
-bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed);
+bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed, ofstream& outputLog);
 
 void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
@@ -45,5 +50,5 @@
 void damagePlayer(Player *p, int damage);
 
-void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player>& mapPlayers, MessageProcessor &msgProcessor, int sock);
+void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player>& mapPlayers, MessageProcessor &msgProcessor, int sock, ofstream& outputLog);
 
 // this should probably go somewhere in the common folder
@@ -79,4 +84,8 @@
 
    return NULL;
+}
+
+void quit(int sig) {
+   done = true;
 }
 
@@ -92,7 +101,12 @@
    unsigned int unusedPlayerId = 1, unusedProjectileId = 1;
    int scoreBlue, scoreRed;
+   ofstream outputLog;
+
+   done = false;
 
    scoreBlue = 0;
    scoreRed = 0;
+
+   signal(SIGINT, quit);
 
    //SSL_load_error_strings();
@@ -104,4 +118,7 @@
       exit(1);
    }
+
+   outputLog.open("server.log", ios::app);
+   outputLog << "Started server on " << getCurrentDateTimeString() << endl;
 
    WorldMap* gameMap = WorldMap::loadMapFromFile("../data/map.txt");
@@ -138,5 +155,5 @@
    timespec ts;
    int timeLastUpdated = 0, curTime = 0, timeLastBroadcast = 0;
-   while (true) {
+   while (!done) {
 
       usleep(5000);
@@ -150,6 +167,6 @@
          timeLastUpdated = curTime;
 
-         msgProcessor.cleanAckedMessages();
-         msgProcessor.resendUnackedMessages(sock);
+         msgProcessor.cleanAckedMessages(&outputLog);
+         msgProcessor.resendUnackedMessages(sock, &outputLog);
 
          map<unsigned int, Player>::iterator it;
@@ -191,5 +208,5 @@
                   for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
                   {
-                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
+                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
                         error("sendMessage");
                   }
@@ -206,5 +223,5 @@
                for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
                {
-                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
+                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
                      error("sendMessage");
                }
@@ -319,5 +336,5 @@
                   for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
                   {
-                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
+                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
                         error("sendMessage");
                   }
@@ -329,5 +346,5 @@
                   for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
                   {
-                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
+                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
                         error("sendMessage");
                   }
@@ -370,5 +387,5 @@
                   for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
                   {
-                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
+                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
                         error("sendMessage");
                   }
@@ -383,5 +400,5 @@
                   for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
                   {
-                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
+                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
                         error("sendMessage");
                   }
@@ -404,5 +421,5 @@
                for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
                {
-                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
+                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
                      error("sendMessage");
                }
@@ -422,5 +439,5 @@
 
                      if (flagType != WorldMap::OBJECT_NONE) {
-                        addObjectToMap(flagType, target->pos.x, target->pos.y, gameMap, mapPlayers, msgProcessor, sock);
+                        addObjectToMap(flagType, target->pos.x, target->pos.y, gameMap, mapPlayers, msgProcessor, sock, outputLog);
                      }
                   }
@@ -452,5 +469,5 @@
                for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
                {
-                  if (msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
+                  if (msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
                      error("sendMessage");
                }
@@ -474,5 +491,5 @@
                for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
                {
-                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
+                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
                      error("sendMessage");
                }
@@ -492,5 +509,5 @@
 
                   if (flagType != WorldMap::OBJECT_NONE) {
-                     addObjectToMap(flagType, target->pos.x, target->pos.y, gameMap, mapPlayers, msgProcessor, sock);
+                     addObjectToMap(flagType, target->pos.x, target->pos.y, gameMap, mapPlayers, msgProcessor, sock, outputLog);
                   }
                }
@@ -502,5 +519,5 @@
                for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
                {
-                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
+                  if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr), &outputLog) < 0 )
                      error("sendMessage");
                }
@@ -510,8 +527,8 @@
       }
 
-      n = msgProcessor.receiveMessage(&clientMsg, sock, &from);
+      n = msgProcessor.receiveMessage(&clientMsg, sock, &from, &outputLog);
 
       if (n >= 0) {
-         broadcastResponse = processMessage(clientMsg, from, msgProcessor, mapPlayers, gameMap, unusedPlayerId, serverMsg, sock, scoreBlue, scoreRed);
+         broadcastResponse = processMessage(clientMsg, from, msgProcessor, mapPlayers, gameMap, unusedPlayerId, serverMsg, sock, scoreBlue, scoreRed, outputLog);
 
          if (broadcastResponse)
@@ -523,5 +540,5 @@
             {
                cout << "Sent message back to " << it->second.name << endl;
-               if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
+               if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr), &outputLog) < 0 )
                   error("sendMessage");
             }
@@ -531,14 +548,17 @@
             cout << "Should be sending back the message" << endl;
 
-            if ( msgProcessor.sendMessage(&serverMsg, sock, &from) < 0 )
+            if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
                error("sendMessage");
          }
       }
    }
+
+   outputLog << "Stopped server on " << getCurrentDateTimeString() << endl;
+   outputLog.close();
 
    return 0;
 }
 
-bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed)
+bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed, ofstream& outputLog)
 {
    DataAccess da;
@@ -630,5 +650,5 @@
                cout << "sending info about " << it->second.name  << endl;
                cout << "sending id " << it->second.id  << endl;
-               if ( msgProcessor.sendMessage(&serverMsg, sock, &from) < 0 )
+               if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
                   error("sendMessage");
             }
@@ -643,5 +663,5 @@
                itObjects->serialize(serverMsg.buffer);
                cout << "sending item id " << itObjects->id  << endl;
-               if ( msgProcessor.sendMessage(&serverMsg, sock, &from) < 0 )
+               if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
                   error("sendMessage");
             }
@@ -651,5 +671,5 @@
             memcpy(serverMsg.buffer, &scoreBlue, 4);
             memcpy(serverMsg.buffer+4, &scoreRed, 4);
-            if ( msgProcessor.sendMessage(&serverMsg, sock, &from) < 0 )
+            if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
                error("sendMessage");
 
@@ -661,5 +681,5 @@
             {
                cout << "Sent message back to " << it->second.name << endl;
-               if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
+               if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr), &outputLog) < 0 )
                   error("sendMessage");
             }
@@ -701,5 +721,5 @@
 
                if (flagType != WorldMap::OBJECT_NONE) {
-                  addObjectToMap(flagType, p->pos.x, p->pos.y, gameMap, mapPlayers, msgProcessor, sock);
+                  addObjectToMap(flagType, p->pos.x, p->pos.y, gameMap, mapPlayers, msgProcessor, sock, outputLog);
                }
             }
@@ -826,5 +846,5 @@
                   for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
                   {
-                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
+                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr), &outputLog) < 0 )
                         error("sendMessage");
                   }
@@ -862,5 +882,5 @@
             flagType = WorldMap::OBJECT_RED_FLAG;
 
-         addObjectToMap(flagType, mapPlayers[id].pos.x, mapPlayers[id].pos.y, gameMap, mapPlayers, msgProcessor, sock);
+         addObjectToMap(flagType, mapPlayers[id].pos.x, mapPlayers[id].pos.y, gameMap, mapPlayers, msgProcessor, sock, outputLog);
 
          mapPlayers[id].hasBlueFlag = false;
@@ -942,5 +962,5 @@
 }
 
-void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player>& mapPlayers, MessageProcessor &msgProcessor, int sock) {
+void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player>& mapPlayers, MessageProcessor &msgProcessor, int sock, ofstream& outputLog) {
    NETWORK_MSG serverMsg;
 
@@ -954,5 +974,5 @@
    for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    {
-      if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
+      if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr), &outputLog) < 0 )
          error("sendMessage");
    }
