Index: /vtcross/branches/bhilburn/src/include/vtcross/socket_comm.h
===================================================================
--- /vtcross/branches/bhilburn/src/include/vtcross/socket_comm.h	(revision 142)
+++ /vtcross/branches/bhilburn/src/include/vtcross/socket_comm.h	(revision 147)
@@ -2,18 +2,18 @@
  * Virginia Tech, 2009
  *
- * LICENSE INFORMATION GOES HERE
+ * TODO LICENSE INFORMATION GOES HERE
  */
 
-/* DESCRIPTION OF FILE.
+/* TODO DESCRIPTION OF FILE.
  */
 
+
 #include <cstdlib>
-#include <cstdio>
+#include <cstring>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <stdint.h>
 #include <sys/types.h>
 #include <sys/socket.h>
-#include <netinet/in.h>
-#include <netdb.h>
-#include <string>
-#include <string.h>
 
 #include "vtcross/common.h"
@@ -21,132 +21,118 @@
 #include "vtcross/error.h"
 
-void ReadMessage(int socket,char * buffer) {
-    int i,n;
 
-    n = recv(socket,buffer,256,MSG_PEEK);
-    for(i=0;i<256;i++){
-	    if(strcmp(&buffer[i],"\0") == 0) break;
+void
+ReadMessage(int32_t socketFD, char* msgBuffer)
+{
+    ssize_t msgLength;
+    
+    msgLength = recv(socketFD, msgBuffer, 256, MSG_PEEK);
+    for(size_t i = 0; i < 256; i++) {
+	    if(strcmp(&msgBuffer[i], "\0") == 0)
+            break;
     }
-    n = recv(socket,buffer,i+1,0);
-    if (n < 0) 
-         error("ERROR reading from socket");
-    //printf("ReadMessage:%s %d\n",buffer,n);
+
+    msgLength = recv(socketFD, msgBuffer, i + 1, 0);
+    if (msgLength < 0)
+        ERROR(1, "Error reading from socket");
 }
 
-// setup client socket connection
-int ClientSocket(int argc, char** argv)
+
+int32_t 
+ClientSocket(char* serverName, char* portNumber)
 {
-    int sockfd;
-    int portno;
+    int32_t socketFD;
+    int32_t portNo;
+
     struct sockaddr_in serv_addr;
     struct hostent *server;
     
-    // server name
-    server = gethostbyname(argv[1]);
-    if (server == NULL) {
-        fprintf(stderr,"ERROR, no such host\n");
-        exit(0);
-    }
-    // port no.
-    portno = atoi(argv[2]);
-    // socket file descriptor
-    sockfd = socket(AF_INET, SOCK_STREAM, 0);
-    if (sockfd < 0) 
-        error("ERROR opening socket");
-    // initiate struct socketaddr_in
-    bzero((char *) &serv_addr, sizeof(serv_addr));
+    server = gethostbyname(serverName);
+    if(server == NULL)
+        ERROR(1, "No server found by that hostname.");
+
+    portNo = atoi(portNumber);
+
+    socketFD = socket(AF_INET, SOCK_STREAM, 0);
+    if(socketFD < 0) 
+        ERROR(1, "Error opening socket");
+
+    memset((void *) &serv_addr, 0, sizeof(serv_addr));
     serv_addr.sin_family = AF_INET;
-    serv_addr.sin_port = htons(portno);
-    bcopy((char *)server->h_addr, 
-         (char *)&serv_addr.sin_addr.s_addr,
-         server->h_length);
-    // cast sockaddr_in to sockaddr
-    if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) 
-        error("ERROR connecting");
+    serv_addr.sin_port = htons(portNo);
+    memcpy((char *) &serv_addr.sin_addr.s_addr, (char *) server->h_addr, \
+            server->h_length);
 
-    return sockfd;
+    if(connect(socketFD, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) 
+        ERROR(1, "Error connecting to remote socket.");
+
+    return socketFD;
 }
 
     
-int SendMessage(int socketfd, char *message) 
+int32_t
+SendMessage(int32_t socketFD, char* message) 
 {
-    int n;
      
+    // TODO explain this. What, exactly, does the below line do and how does it
+    // affect the rest of the function?
     strcat(message, "\0000");
-    n = send(socketfd,message,(strlen(message)+1),0);
-    if (n<0)
-        error("Error sending to server\n");
-    else if(n == 0)
-        printf("Server closed the socket.\n");
+
+    ssize_t numSentBytes = send(socketFD, message, (strlen(message) + 1), 0);
+    if(numSentBytes < 0)
+        ERROR("Error sending to server.");
+    else if(numSentBytes == 0)
+        LOG("socket_comm::SendMessage - Server closed the socket.\n");
    
-    return n;
-}
-
-int GetParameter(int sockfd, struct Parameter pList[], struct CE_Info *ce_info)
-{
-    char buffer[256];
-    int i;
-
-    // numParameters
-    bzero(buffer,256);
-    ReadMessage(sockfd,buffer);
-    ce_info->numParameters = atoi(buffer);
-    printf("number of parameters: %d\n", ce_info->numParameters);
-    
-    for (i = 0; i < ce_info->numParameters; i++){
-        bzero(buffer,256);
-        ReadMessage(sockfd,buffer);
-        printf("Name: %s\n", buffer);
-        pList[i].name = std::string(buffer);
-//        strcpy(pList[i].name.c_str(), buffer);
-    
-        bzero(buffer,256);
-        ReadMessage(sockfd,buffer);
-        printf("Units: %s\n", buffer);
-        pList[i].units = std::string(buffer);
- //       strcpy(pList[i].units.c_str(), buffer);
-
-        bzero(buffer,256);
-        ReadMessage(sockfd,buffer);
-        printf("Min: %s\n", buffer);
-        pList[i].min = atof(buffer);
-    
-        bzero(buffer,256);
-        ReadMessage(sockfd,buffer);
-        printf("Max: %s\n", buffer);
-        pList[i].max = atof(buffer);
-    
-        bzero(buffer,256);
-        ReadMessage(sockfd,buffer);
-        printf("Step: %s\n", buffer);
-        pList[i].step = atof(buffer);
-    
-        bzero(buffer,256);
-        ReadMessage(sockfd,buffer);
-        printf("Value: %s\n", buffer);
-        pList[i].value = atof(buffer);
-    
-    }
-    return 1;
+    return numSentBytes;
 }
 
 
-int GetRequest(int sockfd, struct Parameter pList[], struct CE_Info *ce_info)
+// TODO This function is currently returning 1... always... is this necessary?
+// If we want a fail/success return type, then why aren't we ever returning a
+// failure?
+int32_t 
+GetParameter(int32_t socketFD, struct Parameter pList[], \
+        struct CE_Info *ce_info)
 {
     char buffer[256];
-   
-    // Get control identifier
-    // Possibly could implement some sort of authentication here
-    bzero(buffer,256);
-    ReadMessage(sockfd,buffer);
+    memset(buffer, 0, 256);
 
-    if(strcmp(buffer,"val") != 0) {
-            printf("Policy Engine:: Unexpected control data received.\n\n");
-        return 0;
+    ReadMessage(socketFD, buffer);
+    ce_info->numParameters = atoi(buffer);
+    LOG("socket_comm::GetParameter - Number of parameters: %d\n", \
+            ce_info->numParameters);
+    
+    for(size_t i = 0; i < ce_info->numParameters; i++) {
+        memset(buffer, 0, 256);
+        ReadMessage(socketFD, buffer);
+        LOG("socket_comm::GetParameter - Name: %s\n", buffer);
+        pList[i].name = std::string(buffer);
+    
+        memset(buffer, 0, 256);
+        ReadMessage(socketFD, buffer);
+        LOG("socket_comm::GetParameter - Units: %s\n", buffer);
+        pList[i].units = std::string(buffer);
+
+        memset(buffer, 0, 256);
+        ReadMessage(socketFD, buffer);
+        LOG("socket_comm::GetParameter - Min: %s\n", buffer);
+        pList[i].min = atof(buffer);
+    
+        memset(buffer, 0, 256);
+        ReadMessage(socketFD, buffer);
+        LOG("socket_comm::GetParameter - Max: %s\n", buffer);
+        pList[i].max = atof(buffer);
+    
+        memset(buffer, 0, 256);
+        ReadMessage(socketFD, buffer);
+        LOG("socket_comm::GetParameter - Step: %s\n", buffer);
+        pList[i].step = atof(buffer);
+    
+        memset(buffer, 0, 256);
+        ReadMessage(socketFD, buffer);
+        LOG("socket_comm::GetParameter - Value: %s\n", buffer);
+        pList[i].value = atof(buffer);
     }
-
-    printf("Policy Engine:: Getting parameters.\n\n");
-    // Get potenial transmission parameter values
-    GetParameter(sockfd, pList, ce_info);
 
     return 1;
@@ -154,2 +140,24 @@
 
 
+// TODO if we are just returing fail/success here, then why not return a bool
+// instead of an entire 32 bit integer?  Seems wasteful.
+int32_t 
+GetRequest(int32_t socketFD, struct Parameter pList[], struct CE_Info *ce_info)
+{
+    char buffer[256];
+    memset(buffer, 0, 256);
+   
+    ReadMessage(socketFD, buffer);
+
+    if(strcmp(buffer, "val") != 0) {
+        LOG("socket_comm::GetRequest - Unexpected control data received.\n\n");
+        return 0;
+    }
+
+    LOG("socket_comm::GetRequest - Getting parameters.\n\n");
+    GetParameter(socketFD, pList, ce_info);
+
+    return 1;
+}
+
+
