Changeset 150 for vtcross/branches
- Timestamp:
- 03/13/09 10:42:48 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
vtcross/branches/bhilburn/src/policy_engines/default_policy_engine.cpp
r136 r150 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 * This file is a temporary demo of a policy engine using some of our socket 10 * communication functions. This is *not* an actual implementation of our 11 * defined PolicyEngine class. 12 */ 13 14 1 15 #include <cstdlib> 2 #include <cstdio> 16 #include <cstring> 17 #include <netdb.h> 18 #include <netinet/in.h> 19 #include <stdint.h> 3 20 #include <sys/types.h> 4 21 #include <sys/socket.h> 5 #include <netinet/in.h>6 #include <netdb.h>7 #include <string>8 #include <string.h>9 22 10 #include "vtcross/c br.h"23 #include "vtcross/common.h" 11 24 #include "vtcross/containers.h" 12 #include "vtcross/ common.h"25 #include "vtcross/socket_comm.h" 13 26 14 27 15 28 #define CR_PORT 30002 16 29 17 // error handling 18 void error(char *msg) 30 31 int32_t 32 ValidateTransmissionParameters(struct Parameter pList[], \ 33 struct CE_Info *ce_info, int decision_array[]) 19 34 { 20 perror(msg); 21 exit(0); 22 } 23 24 void ReadMessage(int socket,char * buffer) { 25 int i,n; 26 27 n = recv(socket,buffer,256,MSG_PEEK); 28 for(i=0;i<256;i++){ 29 if(strcmp(&buffer[i],"\0") == 0) break; 30 } 31 n = recv(socket,buffer,i+1,0); 32 if (n < 0) 33 error("ERROR reading from socket"); 34 //printf("ReadMessage:%s %d\n",buffer,n); 35 } 36 37 // setup client socket connection 38 int ClientSocket(int argc, char** argv) 39 { 40 int sockfd; 41 int portno; 42 struct sockaddr_in serv_addr; 43 struct hostent *server; 44 45 // server name 46 server = gethostbyname(argv[1]); 47 if (server == NULL) { 48 fprintf(stderr,"ERROR, no such host\n"); 49 exit(0); 50 } 51 // port no. 52 portno = atoi(argv[2]); 53 // socket file descriptor 54 sockfd = socket(AF_INET, SOCK_STREAM, 0); 55 if (sockfd < 0) 56 error("ERROR opening socket"); 57 // initiate struct socketaddr_in 58 bzero((char *) &serv_addr, sizeof(serv_addr)); 59 serv_addr.sin_family = AF_INET; 60 serv_addr.sin_port = htons(portno); 61 bcopy((char *)server->h_addr, 62 (char *)&serv_addr.sin_addr.s_addr, 63 server->h_length); 64 // cast sockaddr_in to sockaddr 65 if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) 66 error("ERROR connecting"); 67 68 return sockfd; 69 } 70 71 72 int SendMessage(int socketfd, char *message) 73 { 74 int n; 75 76 strcat(message, "\0000"); 77 n = send(socketfd,message,(strlen(message)+1),0); 78 if (n<0) 79 error("Error sending to server\n"); 80 else if(n == 0) 81 printf("Server closed the socket.\n"); 82 83 return n; 84 } 85 86 int GetParameter(int sockfd, struct Parameter pList[], struct CE_Info *ce_info) 87 { 88 char buffer[256]; 89 int i; 90 91 // numParameters 92 bzero(buffer,256); 93 ReadMessage(sockfd,buffer); 94 ce_info->numParameters = atoi(buffer); 95 printf("number of parameters: %d\n", ce_info->numParameters); 96 97 for (i = 0; i < ce_info->numParameters; i++){ 98 bzero(buffer,256); 99 ReadMessage(sockfd,buffer); 100 printf("Name: %s\n", buffer); 101 pList[i].name = std::string(buffer); 102 // strcpy(pList[i].name.c_str(), buffer); 103 104 bzero(buffer,256); 105 ReadMessage(sockfd,buffer); 106 printf("Units: %s\n", buffer); 107 pList[i].units = std::string(buffer); 108 // strcpy(pList[i].units.c_str(), buffer); 109 110 bzero(buffer,256); 111 ReadMessage(sockfd,buffer); 112 printf("Min: %s\n", buffer); 113 pList[i].min = atof(buffer); 114 115 bzero(buffer,256); 116 ReadMessage(sockfd,buffer); 117 printf("Max: %s\n", buffer); 118 pList[i].max = atof(buffer); 119 120 bzero(buffer,256); 121 ReadMessage(sockfd,buffer); 122 printf("Step: %s\n", buffer); 123 pList[i].step = atof(buffer); 124 125 bzero(buffer,256); 126 ReadMessage(sockfd,buffer); 127 printf("Value: %s\n", buffer); 128 pList[i].value = atof(buffer); 129 130 } 131 return 1; 132 } 133 134 135 int GetRequest(int sockfd, struct Parameter pList[], struct CE_Info *ce_info) 136 { 137 char buffer[256]; 138 139 // Get control identifier 140 // Possibly could implement some sort of authentication here 141 bzero(buffer,256); 142 ReadMessage(sockfd,buffer); 143 144 if(strcmp(buffer,"val") != 0) { 145 printf("Policy Engine:: Unexpected control data received.\n\n"); 146 return 0; 147 } 148 149 printf("Policy Engine:: Getting parameters.\n\n"); 150 // Get potenial transmission parameter values 151 GetParameter(sockfd, pList, ce_info); 152 153 return 1; 154 } 155 156 int ValidateTransmissionParameters(struct Parameter pList[], struct CE_Info *ce_info, int decision_array[]) 157 { 158 int i = 0; 159 // Decision array holds parameter decision 160 // 1 = OK 161 // 0 = FAIL 162 163 printf("Policy Engine:: Policies Validated.\n\n"); 164 for (i = 0; i < ce_info->numParameters; i++){ 35 LOG("Policy Engine:: Policies Validated.\n"); 36 for (size_t i = 0; i < ce_info->numParameters; i++) 165 37 decision_array[i] = 1; 166 }167 38 168 39 return 1; … … 170 41 171 42 172 int RegisterPE(int sockfd) 43 int32_t 44 RegisterPE(int32_t socketFD) 173 45 { 174 // Send register message to cognitive radio 175 SendMessage(sockfd, "p_register"); 176 printf("Policy Engine:: Registration message sent.\n\n"); 46 SendMessage(socketFD, "p_register"); 47 LOG("Policy Engine:: Registration message sent.\n"); 177 48 return 1; 178 49 } 179 50 180 int SendCEDecision(int sockfd, struct Parameter pList[], struct CE_Info *ce_info, int decision_array[]) 51 52 int32_t 53 SendCEDecision(int32_t socketFD, struct Parameter pList[], \ 54 struct CE_Info *ce_info, int32_t decision_array[]) 181 55 { 182 // send results back to server183 56 char var[50]; 184 int i;185 57 186 // parameter 187 for (i = 0; i < ce_info->numParameters; i++){ 58 for (size_t i = 0; i < ce_info->numParameters; i++) { 188 59 sprintf(var, "%i", decision_array[i]); 189 SendMessage(sock fd, var);60 SendMessage(socketFD, var); 190 61 } 191 62 … … 193 64 } 194 65 195 int ParsePolicies() {196 66 197 printf("Policy Engine:: Policies parsed.\n\n"); 67 int32_t 68 ParsePolicies() 69 { 198 70 71 LOG("Policy Engine:: Policies parsed.\n"); 199 72 return 1; 200 73 } 201 74 202 // main client socket 203 int main(int argc, char *argv[]) 75 76 int32_t 77 main(int32_t argc, char *argv[]) 204 78 { 205 int decision_array[10]; 79 if(argc < 3) 80 ERROR(1, "Usage: %s hostname port\n", argv[0]); 81 82 int32_t decision_array[10]; 206 83 struct Parameter pList[10]; 207 84 struct CE_Info ce_info; 208 int sockfd; 209 int ret = 0; 85 int32_t ret = 0; 210 86 211 // setup client socket connection 212 if (argc < 3) { 213 fprintf(stderr,"usage: %s hostname port\n", argv[0]); 214 exit(0); 215 } 216 217 // setup client socket connection with server 218 sockfd = ClientSocket(argc, argv); 87 int32_t socketFD = ClientSocket(argv[1], argv[2]); 219 88 220 // Parse initial Policies 221 printf("Policy Engine:: Parsing local policies.\n\n"); 222 ret = ParsePolicies(); 89 LOG("Policy Engine:: Parsing local policies.\n"); 90 ParsePolicies(); 223 91 224 ret = RegisterPE(sockfd);92 RegisterPE(socketFD); 225 93 226 94 while(1) { 227 228 printf("Policy Engine:: Waiting for Policy Check Request.\n\n"); 229 ret = GetRequest(sockfd, pList, &ce_info); 95 LOG("Policy Engine:: Waiting for Policy Check Request.\n"); 96 ret = GetRequest(socketFD, pList, &ce_info); 230 97 231 98 if(ret == 1) { 232 printf("Policy Engine:: Validating Transmission Parameters.\n\n");99 LOG("Policy Engine:: Validating Transmission Parameters.\n"); 233 100 ValidateTransmissionParameters(pList, &ce_info, decision_array); 234 101 235 printf("Policy Engine:: Sending Policy decision to Server.\n\n");236 SendCEDecision(sock fd, pList, &ce_info, decision_array);102 LOG("Policy Engine:: Sending Policy decision to Server.\n"); 103 SendCEDecision(socketFD, pList, &ce_info, decision_array); 237 104 } 238 105 }