[156] | 1 | /* Virginia Tech Cognitive Radio Open Source Systems |
---|
| 2 | * Virginia Tech, 2009 |
---|
| 3 | * |
---|
| 4 | * TODO LICENSE INFORMATION GOES HERE |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | /* TODO DESCRIPTION OF FILE. |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | #include <cstdlib> |
---|
| 11 | #include <cstring> |
---|
| 12 | #include <netdb.h> |
---|
| 13 | #include <netinet/in.h> |
---|
| 14 | #include <stdint.h> |
---|
| 15 | #include <sys/types.h> |
---|
| 16 | #include <sys/socket.h> |
---|
| 17 | |
---|
| 18 | #include "vtcross/common.h" |
---|
| 19 | #include "vtcross/containers.h" |
---|
| 20 | #include "vtcross/debug.h" |
---|
| 21 | #include "vtcross/error.h" |
---|
| 22 | #include "vtcross/socketcomm.h" |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | void |
---|
| 26 | ReadMessage(int32_t socketFD, char* msgBuffer) |
---|
| 27 | { |
---|
| 28 | ssize_t msgLength = recv(socketFD, msgBuffer, 256, MSG_PEEK); |
---|
| 29 | |
---|
| 30 | size_t i; |
---|
| 31 | for(i = 0; i < 256; i++) { |
---|
| 32 | if(strcmp(&msgBuffer[i], "\0") == 0) |
---|
| 33 | break; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | msgLength = recv(socketFD, msgBuffer, i + 1, 0); |
---|
| 37 | if (msgLength < 0) |
---|
| 38 | ERROR(1, "Error reading from socket"); |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | int32_t |
---|
| 43 | ClientSocket(char* serverName, char* portNumber) |
---|
| 44 | { |
---|
| 45 | int32_t socketFD; |
---|
| 46 | int32_t portNo; |
---|
| 47 | |
---|
| 48 | struct sockaddr_in serv_addr; |
---|
| 49 | struct hostent *server; |
---|
| 50 | |
---|
| 51 | server = gethostbyname(serverName); |
---|
| 52 | if(server == NULL) |
---|
| 53 | ERROR(1, "No server found by that hostname."); |
---|
| 54 | |
---|
| 55 | portNo = atoi(portNumber); |
---|
| 56 | |
---|
| 57 | socketFD = socket(AF_INET, SOCK_STREAM, 0); |
---|
| 58 | if(socketFD < 0) |
---|
| 59 | ERROR(1, "Error opening socket"); |
---|
| 60 | |
---|
| 61 | memset((void *) &serv_addr, 0, sizeof(serv_addr)); |
---|
| 62 | serv_addr.sin_family = AF_INET; |
---|
| 63 | serv_addr.sin_port = htons(portNo); |
---|
| 64 | memcpy((char *) &serv_addr.sin_addr.s_addr, (char *) server->h_addr, \ |
---|
| 65 | server->h_length); |
---|
| 66 | |
---|
| 67 | if(connect(socketFD, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) |
---|
| 68 | ERROR(1, "Error connecting to remote socket."); |
---|
| 69 | |
---|
| 70 | return socketFD; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | int32_t |
---|
| 75 | SendMessage(int32_t socketFD, char* message) |
---|
| 76 | { |
---|
| 77 | |
---|
| 78 | // TODO explain this. What, exactly, does the below line do and how does it |
---|
| 79 | // affect the rest of the function? |
---|
| 80 | strcat(message, "\0000"); |
---|
| 81 | |
---|
| 82 | ssize_t numSentBytes = send(socketFD, message, (strlen(message) + 1), 0); |
---|
| 83 | if(numSentBytes < 0) { |
---|
| 84 | ERROR(1, "Error sending to server."); |
---|
| 85 | } |
---|
| 86 | else if(numSentBytes == 0) { |
---|
| 87 | LOG("socket_comm::SendMessage - Server closed the socket.\n"); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | return numSentBytes; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | // TODO This function is currently returning 1... always... is this necessary? |
---|
| 95 | // If we want a fail/success return type, then why aren't we ever returning a |
---|
| 96 | // failure? |
---|
| 97 | int32_t |
---|
| 98 | GetParameter(int32_t socketFD, struct Parameter pList[], \ |
---|
| 99 | struct CE_Info *ce_info) |
---|
| 100 | { |
---|
| 101 | char buffer[256]; |
---|
| 102 | memset(buffer, 0, 256); |
---|
| 103 | |
---|
| 104 | ReadMessage(socketFD, buffer); |
---|
| 105 | ce_info->numParameters = atoi(buffer); |
---|
| 106 | LOG("socket_comm::GetParameter - Number of parameters: %d\n", \ |
---|
| 107 | ce_info->numParameters); |
---|
| 108 | |
---|
| 109 | for(size_t i = 0; i < ce_info->numParameters; i++) { |
---|
| 110 | memset(buffer, 0, 256); |
---|
| 111 | ReadMessage(socketFD, buffer); |
---|
| 112 | LOG("socket_comm::GetParameter - Name: %s\n", buffer); |
---|
| 113 | pList[i].name = std::string(buffer); |
---|
| 114 | |
---|
| 115 | memset(buffer, 0, 256); |
---|
| 116 | ReadMessage(socketFD, buffer); |
---|
| 117 | LOG("socket_comm::GetParameter - Units: %s\n", buffer); |
---|
| 118 | pList[i].units = std::string(buffer); |
---|
| 119 | |
---|
| 120 | memset(buffer, 0, 256); |
---|
| 121 | ReadMessage(socketFD, buffer); |
---|
| 122 | LOG("socket_comm::GetParameter - Min: %s\n", buffer); |
---|
| 123 | pList[i].min = atof(buffer); |
---|
| 124 | |
---|
| 125 | memset(buffer, 0, 256); |
---|
| 126 | ReadMessage(socketFD, buffer); |
---|
| 127 | LOG("socket_comm::GetParameter - Max: %s\n", buffer); |
---|
| 128 | pList[i].max = atof(buffer); |
---|
| 129 | |
---|
| 130 | memset(buffer, 0, 256); |
---|
| 131 | ReadMessage(socketFD, buffer); |
---|
| 132 | LOG("socket_comm::GetParameter - Step: %s\n", buffer); |
---|
| 133 | pList[i].step = atof(buffer); |
---|
| 134 | |
---|
| 135 | memset(buffer, 0, 256); |
---|
| 136 | ReadMessage(socketFD, buffer); |
---|
| 137 | LOG("socket_comm::GetParameter - Value: %s\n", buffer); |
---|
| 138 | pList[i].value = atof(buffer); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | return 1; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | // TODO if we are just returing fail/success here, then why not return a bool |
---|
| 146 | // instead of an entire 32 bit integer? Seems wasteful. |
---|
| 147 | int32_t |
---|
| 148 | GetRequest(int32_t socketFD, struct Parameter pList[], struct CE_Info *ce_info) |
---|
| 149 | { |
---|
| 150 | char buffer[256]; |
---|
| 151 | memset(buffer, 0, 256); |
---|
| 152 | |
---|
| 153 | ReadMessage(socketFD, buffer); |
---|
| 154 | |
---|
| 155 | if(strcmp(buffer, "val") != 0) { |
---|
| 156 | LOG("socket_comm::GetRequest - Unexpected control data received.\n\n"); |
---|
| 157 | return 0; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | LOG("socket_comm::GetRequest - Getting parameters.\n\n"); |
---|
| 161 | GetParameter(socketFD, pList, ce_info); |
---|
| 162 | |
---|
| 163 | return 1; |
---|
| 164 | } |
---|
| 165 | |
---|