[35] | 1 | #include <iostream> |
---|
[63] | 2 | #include <sys/types.h> |
---|
| 3 | #include <sys/wait.h> |
---|
[96] | 4 | #include <sys/socket.h> |
---|
| 5 | #include <netinet/in.h> |
---|
| 6 | #include <netdb.h> |
---|
| 7 | #include <arpa/inet.h> |
---|
[97] | 8 | #include <sys/mman.h> |
---|
| 9 | #include <fcntl.h> |
---|
| 10 | #include <sys/ioctl.h> |
---|
| 11 | |
---|
[96] | 12 | //#include "socket/ServerSocket.h" |
---|
| 13 | //#include "socket/SocketException.h" |
---|
[35] | 14 | |
---|
[131] | 15 | #include "vtcross/containers.h" |
---|
| 16 | #include "tinyxml.h" |
---|
| 17 | #include "tinystr.h" |
---|
| 18 | |
---|
[35] | 19 | using namespace std; |
---|
| 20 | |
---|
[97] | 21 | #define CE_SERVER_PORT 30001 |
---|
| 22 | #define POLICY_SERVER_PORT 30003 |
---|
[35] | 23 | |
---|
[97] | 24 | void DieWithError(char *errorMessage) |
---|
| 25 | { |
---|
| 26 | perror(errorMessage); |
---|
| 27 | exit(1); |
---|
| 28 | } |
---|
| 29 | |
---|
[65] | 30 | void print_current_config(Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) { |
---|
[35] | 31 | int i = 0; |
---|
| 32 | int j = 0; |
---|
| 33 | |
---|
| 34 | for(i = 0; i < ce_info->numUtilities ; i++) { |
---|
| 35 | cout << "Utility: " << uList[i]->name << endl; |
---|
| 36 | cout << " Units: " << uList[i]->units << endl; |
---|
| 37 | cout << " Goal: " << uList[i]->goal << endl; |
---|
| 38 | cout << " Target: " << uList[i]->target << endl; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | for(i = 0; i < ce_info->numParameters; i++) { |
---|
| 42 | cout << "Parameter: " << pList[i]->name << endl; |
---|
[97] | 43 | printf("Cognitive Radio:: Radio Operation Profile has been sucessfully sent.\n"); |
---|
[35] | 44 | cout << " Units: " << pList[i]->units << endl; |
---|
| 45 | cout << " Min: " << pList[i]->min << endl; |
---|
| 46 | cout << " Max: " << pList[i]->max << endl; |
---|
| 47 | cout << " Step: " << pList[i]->step << endl; |
---|
| 48 | for(j = 0; j < pList[i]->numAffects; j++) { |
---|
| 49 | cout << " Affect: " << pList[i]->affection_list[j].u->name << " -> " << pList[i]->affection_list[j].relation << endl; |
---|
| 50 | } |
---|
| 51 | } |
---|
[80] | 52 | |
---|
| 53 | for(i = 0; i < ce_info->numObservables; i++) { |
---|
[35] | 54 | cout << "Observable: " << oList[i]->name << endl; |
---|
| 55 | for(j = 0; j < oList[i]->numAffects; j++) { |
---|
| 56 | cout << " Affect: " << oList[i]->affection_list[j].u->name << " -> " << oList[i]->affection_list[j].relation << endl; |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | int parse_ce_config( TiXmlDocument * doc , Utility * u[], Parameter * p[], Observable * o[], CE_Info * ce_info) { |
---|
| 63 | |
---|
| 64 | TiXmlElement* pElem; //!current element |
---|
| 65 | TiXmlElement* pChild; //!current child of pElem |
---|
| 66 | TiXmlElement* pChild1; //!current child of pElem |
---|
| 67 | TiXmlElement* pSecondChild; //!current child of pElem |
---|
| 68 | TiXmlHandle hDoc(doc); //!handle to xml document |
---|
| 69 | TiXmlHandle hRoot(0); //! handle to root element |
---|
| 70 | |
---|
| 71 | int count = 0; |
---|
| 72 | int i = 0; |
---|
| 73 | int j = 0; |
---|
| 74 | int k = 0; |
---|
[86] | 75 | int match_found = 0; |
---|
[35] | 76 | |
---|
| 77 | pElem = hDoc.FirstChildElement().Element(); |
---|
| 78 | if(!pElem) { cout << "no valid root! quit-ing function!" << endl; return 0; } |
---|
| 79 | hRoot = TiXmlHandle(pElem); |
---|
| 80 | |
---|
| 81 | // Pull utility information from XML file. |
---|
| 82 | |
---|
| 83 | pElem = hRoot.FirstChild("utilities").Element(); |
---|
| 84 | pChild1 = hRoot.Child("utilities",count).Element(); |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | for(pChild = pChild1->FirstChildElement("utility"); pChild; pChild = pChild->NextSiblingElement()) |
---|
| 88 | { |
---|
| 89 | u[i] = new Utility; |
---|
| 90 | const char *uName = pChild->Attribute("name"); |
---|
| 91 | if(uName) u[i]->name = uName; |
---|
| 92 | const char *uUnits = pChild->Attribute("units"); |
---|
| 93 | if(uUnits) u[i]->units = uUnits; |
---|
| 94 | const char *uGoal = pChild->Attribute("goal"); |
---|
| 95 | if(uGoal) u[i]->goal = uGoal; |
---|
| 96 | if(pChild->QueryFloatAttribute("target",&u[i]->target) != TIXML_SUCCESS) u[i]->target = -1; |
---|
| 97 | i++; |
---|
| 98 | } |
---|
| 99 | ce_info->numUtilities = i; |
---|
[95] | 100 | cout << "Initialize:: Parsed " << ce_info->numUtilities << " utilities." << endl; |
---|
[35] | 101 | |
---|
| 102 | // Pull observable information from XML file. |
---|
| 103 | i = 0; |
---|
| 104 | pElem = hRoot.FirstChild("observables").Element(); |
---|
| 105 | pChild1 = hRoot.Child("observables",count).Element(); |
---|
| 106 | |
---|
| 107 | for(pChild = pChild1->FirstChildElement("observable"); pChild; pChild = pChild->NextSiblingElement()) |
---|
| 108 | { |
---|
| 109 | |
---|
| 110 | const char *oName = pChild->Attribute("name"); |
---|
| 111 | o[i] = new Observable; |
---|
| 112 | |
---|
| 113 | if(oName) o[i]->name = oName; |
---|
| 114 | |
---|
| 115 | j = 0; |
---|
| 116 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; pSecondChild = pSecondChild->NextSiblingElement()) |
---|
| 117 | { |
---|
| 118 | const char *oUtilName = pSecondChild->Attribute("utility"); |
---|
| 119 | |
---|
| 120 | // If a utility affects this parameter find the utility object and assign it |
---|
| 121 | if(oUtilName) { |
---|
| 122 | // Search for correct utility |
---|
[86] | 123 | for( k=0 ; u[k]!=NULL ; k++ ){ |
---|
[35] | 124 | if(u[k]->name == oUtilName) { |
---|
| 125 | o[i]->affection_list[j].u = u[k]; |
---|
[86] | 126 | // Set relationship |
---|
| 127 | const char *oRelate = pSecondChild->Attribute("relationship"); |
---|
| 128 | if(oRelate) o[i]->affection_list[j].relation = oRelate; |
---|
| 129 | j++; |
---|
| 130 | match_found = 1; |
---|
[35] | 131 | break; |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | } |
---|
[86] | 135 | if(!match_found) cout << "Error: " << o[i]->name << ": " << oUtilName << " not a valid utility: Affect not added." << endl; |
---|
| 136 | match_found = 0; |
---|
[35] | 137 | } |
---|
| 138 | o[i]->numAffects = j; |
---|
| 139 | i++; |
---|
| 140 | } |
---|
| 141 | ce_info->numObservables = i; |
---|
[95] | 142 | cout << "Initialize:: Parsed " << ce_info->numObservables << " observables." << endl; |
---|
[35] | 143 | |
---|
| 144 | |
---|
| 145 | // Pull parameter information from XML file. |
---|
| 146 | pElem = hRoot.FirstChild("parameters").Element(); |
---|
| 147 | pChild1 = hRoot.Child("parameters",count).Element(); |
---|
| 148 | |
---|
| 149 | i = 0; |
---|
| 150 | for(pChild = pChild1->FirstChildElement("parameter"); pChild; pChild = pChild->NextSiblingElement()) |
---|
| 151 | { |
---|
| 152 | p[i] = new Parameter; |
---|
| 153 | |
---|
| 154 | const char *pName = pChild->Attribute("name"); |
---|
| 155 | if(pName) p[i]->name = pName; |
---|
| 156 | const char *pUnits = pChild->Attribute("units"); |
---|
| 157 | if(pUnits) p[i]->units = pUnits; |
---|
| 158 | |
---|
| 159 | if(pChild->QueryFloatAttribute("min",&p[i]->min) != TIXML_SUCCESS) p[i]->min = -1; |
---|
| 160 | if(pChild->QueryFloatAttribute("max",&p[i]->max) != TIXML_SUCCESS) p[i]->max = -1; |
---|
| 161 | if(pChild->QueryFloatAttribute("step",&p[i]->step) != TIXML_SUCCESS) p[i]->step = -1; |
---|
| 162 | |
---|
| 163 | j = 0; |
---|
| 164 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; pSecondChild = pSecondChild->NextSiblingElement()) |
---|
| 165 | { |
---|
| 166 | const char *pUtilName = pSecondChild->Attribute("utility"); |
---|
| 167 | |
---|
| 168 | // If a utility affects this parameter find the utility object and assign it |
---|
| 169 | if(pUtilName) { |
---|
| 170 | // Search for correct utility |
---|
| 171 | for( k=0 ; u[k]!=NULL ; k++ ){ |
---|
| 172 | if(u[k]->name == pUtilName) { |
---|
[86] | 173 | // If match found, assign it to this index |
---|
[35] | 174 | p[i]->affection_list[j].u = u[k]; |
---|
[86] | 175 | const char *pRelate = pSecondChild->Attribute("relationship"); |
---|
| 176 | if(pRelate) { |
---|
| 177 | p[i]->affection_list[j].relation = pRelate; |
---|
| 178 | } else { |
---|
| 179 | cout << "Error: No relation found." << endl; |
---|
| 180 | } |
---|
| 181 | match_found = 1; |
---|
| 182 | j++; |
---|
[35] | 183 | break; |
---|
| 184 | } |
---|
| 185 | } |
---|
| 186 | } |
---|
[86] | 187 | if(!match_found) cout << "Error: " << p[i]->name << ": " << pUtilName << " not a valid utility: Affect not added." << endl; |
---|
| 188 | match_found = 0; |
---|
[35] | 189 | } |
---|
| 190 | p[i]->numAffects = j; |
---|
| 191 | i++; |
---|
| 192 | |
---|
| 193 | } |
---|
| 194 | ce_info->numParameters = i; |
---|
[95] | 195 | cout << "Initialize:: Parsed " << ce_info->numParameters << " parameters." << endl; |
---|
[35] | 196 | return 1; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | |
---|
[63] | 200 | void error(char *msg) |
---|
| 201 | { |
---|
| 202 | perror(msg); |
---|
| 203 | exit(1); |
---|
| 204 | } |
---|
[35] | 205 | |
---|
| 206 | |
---|
[81] | 207 | int ReceiveMessage(int socket,char * buffer) |
---|
| 208 | { |
---|
| 209 | int i,n; |
---|
[97] | 210 | |
---|
| 211 | n = recv(socket,buffer,256,MSG_PEEK); |
---|
[65] | 212 | |
---|
[81] | 213 | for(i=0;i<256;i++){ |
---|
| 214 | if(strcmp(&buffer[i],"\0") == 0) break; |
---|
| 215 | } |
---|
| 216 | n = recv(socket,buffer,i+1,0); |
---|
| 217 | if (n < 0) |
---|
| 218 | error("ERROR reading from socket"); |
---|
| 219 | // printf("ReadMessage:%s %d\n",buffer,n); |
---|
| 220 | |
---|
| 221 | return n; |
---|
[65] | 222 | } |
---|
| 223 | |
---|
[81] | 224 | |
---|
[65] | 225 | int SendMessage(int socketfd, string message) { |
---|
| 226 | int n; |
---|
| 227 | |
---|
[85] | 228 | message.append("\0"); |
---|
[65] | 229 | // Write message back to client |
---|
[97] | 230 | n = send(socketfd,message.c_str(),(message.size()+1),0); |
---|
[90] | 231 | if (n<0) |
---|
| 232 | error("Error sending to client\n"); |
---|
| 233 | if(n == 0) |
---|
| 234 | printf("Client closed the socket.\n"); |
---|
[85] | 235 | |
---|
[90] | 236 | //printf("SendMessage:%s %d\n",message.c_str(),n); |
---|
| 237 | return n; |
---|
[65] | 238 | } |
---|
| 239 | |
---|
[72] | 240 | void GetEnvironment() { |
---|
| 241 | |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | void Policy_ValidateSettings() { |
---|
| 245 | |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | |
---|
| 249 | void LoadCEConfiguration(int socketfd,Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info){ |
---|
[65] | 250 | int n,i,j; |
---|
| 251 | char counter[55]; |
---|
| 252 | char var[50]; |
---|
[82] | 253 | //int total_bytes; |
---|
[72] | 254 | |
---|
[97] | 255 | printf("Cognitive Radio:: Sending Radio Operating Profile to Cognitive Engine.\n\n"); |
---|
[72] | 256 | |
---|
[80] | 257 | // utilities |
---|
[72] | 258 | // Send number of utilities |
---|
| 259 | sprintf(counter,"%d",ce_info->numUtilities); |
---|
| 260 | SendMessage(socketfd,counter); |
---|
[80] | 261 | // send utility |
---|
| 262 | for(i = 0; i < ce_info->numUtilities; i++) { |
---|
[72] | 263 | SendMessage(socketfd,uList[i]->name); |
---|
| 264 | SendMessage(socketfd,uList[i]->units); |
---|
| 265 | SendMessage(socketfd,uList[i]->goal); |
---|
| 266 | sprintf(var,"%f",uList[i]->target); |
---|
| 267 | SendMessage(socketfd,var); |
---|
| 268 | } |
---|
[66] | 269 | |
---|
[72] | 270 | // parameters |
---|
[80] | 271 | sprintf(counter,"%i",ce_info->numParameters); |
---|
[72] | 272 | SendMessage(socketfd,counter); |
---|
| 273 | for(i = 0; i < ce_info->numParameters; i++) { |
---|
| 274 | SendMessage(socketfd,pList[i]->name); |
---|
| 275 | SendMessage(socketfd,pList[i]->units); |
---|
| 276 | sprintf(var,"%f",pList[i]->min); |
---|
| 277 | SendMessage(socketfd,var); |
---|
| 278 | sprintf(var,"%f",pList[i]->max); |
---|
| 279 | SendMessage(socketfd,var); |
---|
| 280 | sprintf(var,"%f",pList[i]->step); |
---|
| 281 | SendMessage(socketfd,var); |
---|
| 282 | |
---|
| 283 | sprintf(counter,"%i",pList[i]->numAffects); |
---|
[65] | 284 | SendMessage(socketfd,counter); |
---|
[72] | 285 | for(j = 0; j < pList[i]->numAffects; j++) { |
---|
| 286 | SendMessage(socketfd,pList[i]->affection_list[j].u->name); |
---|
| 287 | SendMessage(socketfd,pList[i]->affection_list[j].relation); |
---|
[65] | 288 | } |
---|
[72] | 289 | } |
---|
[66] | 290 | |
---|
[80] | 291 | // observables |
---|
[72] | 292 | sprintf(counter,"%i",ce_info->numObservables); |
---|
| 293 | SendMessage(socketfd,counter); |
---|
| 294 | for(i = 0; i < ce_info->numObservables; i++) { |
---|
| 295 | SendMessage(socketfd,oList[i]->name); |
---|
| 296 | |
---|
| 297 | sprintf(counter,"%i",oList[i]->numAffects); |
---|
[65] | 298 | SendMessage(socketfd,counter); |
---|
[72] | 299 | for(j = 0; j < oList[i]->numAffects; j++) { |
---|
| 300 | SendMessage(socketfd,oList[i]->affection_list[j].u->name); |
---|
| 301 | SendMessage(socketfd,oList[i]->affection_list[j].relation); |
---|
[71] | 302 | } |
---|
[72] | 303 | } |
---|
[66] | 304 | |
---|
[72] | 305 | // Receive ACK for utils |
---|
[81] | 306 | char buffer[256]; |
---|
[66] | 307 | string message; |
---|
[81] | 308 | n = ReceiveMessage(socketfd, buffer); |
---|
[95] | 309 | //printf("%s\n", buffer); |
---|
[81] | 310 | //cout << message << endl; |
---|
[95] | 311 | //printf("ACK received.\n"); |
---|
[66] | 312 | |
---|
[72] | 313 | } |
---|
[66] | 314 | |
---|
[72] | 315 | void UpdateCEConfiguration() { |
---|
| 316 | |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | void ResetCEConfiguration(){ |
---|
| 320 | |
---|
| 321 | } |
---|
| 322 | |
---|
[87] | 323 | void UpdateCEExperience(int socketfd, int num_rows, int num_cols, |
---|
| 324 | float * past_exp[]) |
---|
| 325 | { |
---|
| 326 | int i, j; |
---|
| 327 | char counter[55]; |
---|
| 328 | char var[50]; |
---|
[72] | 329 | |
---|
[87] | 330 | for (i = 0; i < num_rows; i++){ |
---|
| 331 | for (j = 0; j< num_cols; j++){ |
---|
| 332 | sprintf(var,"%f",past_exp[i][j]); |
---|
[90] | 333 | //printf("%f, \n", past_exp[i][j]); |
---|
| 334 | //printf("%s, \n", var); |
---|
[87] | 335 | } |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | // send the number of rows to the ce first |
---|
| 339 | sprintf(counter,"%d",num_rows); |
---|
| 340 | SendMessage(socketfd,counter); |
---|
| 341 | // send the number of columns to the ce |
---|
| 342 | sprintf(counter,"%d",num_cols); |
---|
| 343 | SendMessage(socketfd,counter); |
---|
| 344 | // update ce with experience |
---|
| 345 | for (i = 0; i < num_rows; i++){ |
---|
| 346 | for (j = 0; j< num_cols; j++){ |
---|
| 347 | sprintf(var,"%f",past_exp[i][j]); |
---|
| 348 | SendMessage(socketfd,var); |
---|
| 349 | } |
---|
| 350 | } |
---|
[90] | 351 | |
---|
[72] | 352 | } |
---|
| 353 | |
---|
| 354 | void ResetCEExperience() { |
---|
| 355 | |
---|
| 356 | } |
---|
| 357 | |
---|
[82] | 358 | // Update operating settings |
---|
| 359 | // This function will interact with the hardware "drivers" |
---|
[87] | 360 | void UpdateRadioSettings() |
---|
| 361 | { |
---|
[72] | 362 | } |
---|
| 363 | |
---|
[97] | 364 | |
---|
| 365 | int RequestPolicyValidation(Parameter * pList[], CE_Info *ce_info) |
---|
| 366 | { |
---|
| 367 | char counter[55]; |
---|
| 368 | char var[50]; |
---|
| 369 | int i; |
---|
| 370 | string control_msg; |
---|
| 371 | |
---|
| 372 | int socketfd = ce_info->policy_socket; |
---|
| 373 | |
---|
| 374 | // Control message that validation request is coming |
---|
| 375 | control_msg = "val"; |
---|
| 376 | SendMessage(socketfd,control_msg); |
---|
| 377 | |
---|
| 378 | printf("Cognitive Radio:: Here. %i\n\n", socketfd); |
---|
| 379 | |
---|
| 380 | // Send parameter information |
---|
| 381 | sprintf(counter,"%i",ce_info->numParameters); |
---|
| 382 | SendMessage(socketfd,counter); |
---|
| 383 | for(i = 0; i < ce_info->numParameters; i++) { |
---|
| 384 | SendMessage(socketfd,pList[i]->name); |
---|
| 385 | SendMessage(socketfd,pList[i]->units); |
---|
| 386 | sprintf(var,"%f",pList[i]->min); |
---|
| 387 | SendMessage(socketfd,var); |
---|
| 388 | sprintf(var,"%f",pList[i]->max); |
---|
| 389 | SendMessage(socketfd,var); |
---|
| 390 | sprintf(var,"%f",pList[i]->step); |
---|
| 391 | SendMessage(socketfd,var); |
---|
| 392 | sprintf(var,"%f",pList[i]->value); |
---|
| 393 | SendMessage(socketfd,var); |
---|
| 394 | |
---|
| 395 | } |
---|
| 396 | return 1; |
---|
| 397 | |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | |
---|
[82] | 401 | int RequestCEOptimization(int sockfd, Utility *uList[], |
---|
[80] | 402 | Parameter *pList[], Observable *oList[], |
---|
| 403 | CE_Info *ce_info) |
---|
| 404 | { |
---|
[81] | 405 | char buffer[256]; |
---|
[80] | 406 | int i; |
---|
| 407 | float var; |
---|
[72] | 408 | |
---|
[95] | 409 | // Send request optimization message followed by the current environment parameters. |
---|
| 410 | /* |
---|
| 411 | SendMessage(sockfd,"request"); |
---|
| 412 | for (i = 0; i < ce_info->numObservables; i++){ |
---|
| 413 | SendMessage(sockfd,..); |
---|
| 414 | } |
---|
| 415 | */ |
---|
| 416 | |
---|
| 417 | // Receive optimized values from the Cognitive Engine |
---|
[80] | 418 | for (i = 0; i < ce_info->numParameters; i++){ |
---|
[81] | 419 | bzero(buffer,256); |
---|
| 420 | ReceiveMessage(sockfd,buffer); |
---|
| 421 | var = atof(buffer); |
---|
[80] | 422 | pList[i]->value = var; |
---|
| 423 | } |
---|
| 424 | |
---|
[82] | 425 | |
---|
[97] | 426 | // If policy engine is connect, validate new values |
---|
| 427 | if(ce_info->policy_engine == 1) { |
---|
| 428 | |
---|
| 429 | printf("Cognitive Radio:: Found Policy Engine!\n"); |
---|
| 430 | printf("Cognitive Radio:: Validating parameters with Policy Engine\n\n"); |
---|
| 431 | RequestPolicyValidation(pList,ce_info); |
---|
| 432 | printf("Cognitive Radio:: Done\n\n"); |
---|
| 433 | |
---|
| 434 | } |
---|
| 435 | |
---|
| 436 | |
---|
[81] | 437 | return 1; |
---|
[80] | 438 | } |
---|
| 439 | |
---|
| 440 | void RunSimulator(int socketfd, Utility * uList[], |
---|
| 441 | Parameter * pList[], Observable * oList[], |
---|
| 442 | CE_Info * ce_info) { |
---|
[85] | 443 | |
---|
| 444 | float **past_exp; |
---|
[87] | 445 | int num_rows, num_cols; |
---|
[80] | 446 | |
---|
[78] | 447 | // Set fake current environment params = current environment |
---|
[82] | 448 | RequestCEOptimization(socketfd, uList, pList, oList, ce_info); |
---|
[72] | 449 | |
---|
| 450 | // Act like we are updating the hardware tranmission settings |
---|
[82] | 451 | UpdateRadioSettings(); |
---|
[72] | 452 | |
---|
| 453 | // Send back fake utility values |
---|
[87] | 454 | // need to initialize |
---|
| 455 | //UpdateCEExperience(socketfd, num_rows, num_cols, past_exp); |
---|
[63] | 456 | } |
---|
[35] | 457 | |
---|
[97] | 458 | void InitializePE(int socket, CE_Info * ce_info) |
---|
[87] | 459 | { |
---|
[97] | 460 | // Policy Engine is connected |
---|
| 461 | // Set global policy engine value to 1 |
---|
| 462 | ce_info->policy_engine = 1; |
---|
| 463 | ce_info->policy_socket = socket; |
---|
| 464 | |
---|
| 465 | return; |
---|
| 466 | } |
---|
| 467 | |
---|
| 468 | void InitializeCE(int socketfd, Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) |
---|
| 469 | { |
---|
[80] | 470 | LoadCEConfiguration(socketfd, uList, pList, oList, ce_info); |
---|
[85] | 471 | |
---|
[87] | 472 | // cr experience |
---|
| 473 | float **past_exp; |
---|
| 474 | int num_cols; |
---|
| 475 | // get number of columns |
---|
| 476 | num_cols = ce_info->numUtilities + ce_info->numParameters; |
---|
| 477 | num_cols = num_cols + ce_info->numObservables; |
---|
| 478 | num_cols = num_cols + 1; // overall utility |
---|
[90] | 479 | int num_rows = 2; |
---|
[87] | 480 | past_exp = (float **)malloc(sizeof(float)*num_rows); |
---|
| 481 | int i; |
---|
| 482 | for (i=0; i<num_rows; i++){ |
---|
| 483 | past_exp[i] = (float*)malloc(sizeof(float)*num_cols); |
---|
| 484 | } |
---|
| 485 | // sample experience #1 |
---|
| 486 | past_exp[0][0] = 1e3f; // throughput |
---|
| 487 | past_exp[0][1] = 1; // spectral_efficiency |
---|
| 488 | past_exp[0][2] = -3.5; // log10_ber |
---|
| 489 | past_exp[0][3] = 1; // mod_scheme |
---|
[90] | 490 | past_exp[0][4] = -10; // tx_power |
---|
[87] | 491 | past_exp[0][5] = 10.0; // SNR |
---|
[90] | 492 | past_exp[0][6] = 0.762; // overall utility*/ |
---|
| 493 | // sample experience #2 |
---|
[87] | 494 | past_exp[1][0] = 1e2f; // throughput |
---|
| 495 | past_exp[1][1] = 1; // spectral_efficiency |
---|
| 496 | past_exp[1][2] = -3.5; // log10_ber |
---|
| 497 | past_exp[1][3] = 1; // mod_scheme |
---|
[90] | 498 | past_exp[1][4] = -14; // tx_power |
---|
| 499 | past_exp[1][5] = 3.0; // SNR |
---|
| 500 | past_exp[1][6] = 0.462; // overall utility |
---|
[72] | 501 | |
---|
[87] | 502 | // update ce with experience |
---|
[97] | 503 | printf("Cognitive Radio:: Sending Previous Experience to New Cognitive Engine.\n\n"); |
---|
[87] | 504 | UpdateCEExperience(socketfd, num_rows, num_cols, past_exp); |
---|
| 505 | |
---|
[80] | 506 | RunSimulator(socketfd, uList, pList, oList, ce_info); |
---|
[72] | 507 | } |
---|
| 508 | |
---|
[97] | 509 | int AcceptTCPConnection(int servSock) |
---|
| 510 | { |
---|
| 511 | int clntSock; /* Socket descriptor for client */ |
---|
| 512 | struct sockaddr_in echoClntAddr; |
---|
| 513 | unsigned int clntLen; |
---|
[35] | 514 | |
---|
[97] | 515 | /* Set the size of the in-out parameter */ |
---|
| 516 | clntLen = sizeof(echoClntAddr); |
---|
[35] | 517 | |
---|
[97] | 518 | /* Wait for a client to connect */ |
---|
| 519 | //if ((clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr, &clntLen)) < 0) { |
---|
| 520 | if ((clntSock = accept(servSock, NULL, NULL)) < 0) { |
---|
| 521 | return -1; |
---|
| 522 | } |
---|
| 523 | |
---|
| 524 | /* clntSock is connected to a client! */ |
---|
| 525 | |
---|
| 526 | printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr)); |
---|
[63] | 527 | |
---|
[97] | 528 | return clntSock; |
---|
| 529 | } |
---|
[95] | 530 | |
---|
[97] | 531 | int CreateTCPServerSocket(unsigned short port) |
---|
| 532 | { |
---|
| 533 | int sock; /* socket to create */ |
---|
| 534 | struct sockaddr_in echoServAddr; /* Local address */ |
---|
| 535 | |
---|
| 536 | /* Create socket for incoming connections */ |
---|
| 537 | if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) |
---|
| 538 | DieWithError("socket() failed"); |
---|
| 539 | |
---|
| 540 | /* Construct local address structure */ |
---|
| 541 | memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */ |
---|
| 542 | echoServAddr.sin_family = AF_INET; /* Internet address family */ |
---|
| 543 | echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */ |
---|
| 544 | echoServAddr.sin_port = htons(port); /* Local port */ |
---|
| 545 | |
---|
| 546 | /* Bind to the local address */ |
---|
| 547 | if (bind(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) |
---|
| 548 | DieWithError("bind() failed"); |
---|
| 549 | |
---|
| 550 | /* Mark the socket so it will listen for incoming connections */ |
---|
| 551 | if (listen(sock, 5) < 0) { |
---|
| 552 | printf("listen() failed\n"); |
---|
| 553 | return 0; |
---|
| 554 | } |
---|
| 555 | |
---|
| 556 | return sock; |
---|
| 557 | } |
---|
| 558 | |
---|
| 559 | void HandleTCPClient(int socketfd, Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) |
---|
| 560 | { |
---|
| 561 | char buffer[256]; /* Buffer for echo string */ |
---|
| 562 | |
---|
| 563 | /* Receive message from client */ |
---|
| 564 | bzero(buffer,256); |
---|
| 565 | ReceiveMessage(socketfd,buffer); |
---|
| 566 | |
---|
| 567 | printf("Cognitive Radio:: Message Received - %s.\n\n", buffer); |
---|
| 568 | |
---|
| 569 | if(strcmp(buffer,"c_register") == 0) |
---|
| 570 | InitializeCE(socketfd, uList, pList, oList, ce_info); |
---|
| 571 | |
---|
| 572 | if(strcmp(buffer,"p_register") == 0) |
---|
| 573 | InitializePE(socketfd, ce_info); |
---|
| 574 | |
---|
| 575 | if(strcmp(buffer,"optimize") == 0) |
---|
| 576 | RunSimulator(socketfd, uList, pList, oList, ce_info); |
---|
| 577 | |
---|
| 578 | //close(socketfd); /* Close client socket */ |
---|
| 579 | } |
---|
| 580 | |
---|
| 581 | int StartServers(Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) { |
---|
| 582 | int * servSock; |
---|
| 583 | int running = 1; |
---|
| 584 | struct timeval selTimeout; |
---|
| 585 | int timeout = 10; |
---|
| 586 | int cognitive_engine = 0; |
---|
| 587 | int policy_engine = 1; |
---|
| 588 | int port, rc, on = 1; |
---|
| 589 | int new_sd; |
---|
| 590 | int desc_ready = 0; |
---|
| 591 | fd_set sockSet; |
---|
| 592 | |
---|
| 593 | servSock = (int *) malloc(2 * sizeof(int)); |
---|
| 594 | |
---|
| 595 | servSock[cognitive_engine] = CreateTCPServerSocket(CE_SERVER_PORT); |
---|
| 596 | servSock[policy_engine] = CreateTCPServerSocket(POLICY_SERVER_PORT); |
---|
| 597 | |
---|
| 598 | |
---|
| 599 | int maxDescriptor = servSock[cognitive_engine]; |
---|
| 600 | |
---|
| 601 | if(servSock[cognitive_engine] < servSock[policy_engine]) |
---|
| 602 | maxDescriptor = servSock[policy_engine]; |
---|
| 603 | |
---|
| 604 | rc = setsockopt(servSock[cognitive_engine], SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on)); |
---|
| 605 | if(rc < 0) |
---|
| 606 | { |
---|
| 607 | perror("setsockopt() failed"); |
---|
| 608 | close(servSock[cognitive_engine]); |
---|
| 609 | exit(-1); |
---|
| 610 | } |
---|
| 611 | |
---|
| 612 | rc = setsockopt(servSock[policy_engine], SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on)); |
---|
| 613 | if(rc < 0) |
---|
| 614 | { |
---|
| 615 | perror("setsockopt() failed"); |
---|
| 616 | close(servSock[policy_engine]); |
---|
| 617 | exit(-1); |
---|
| 618 | } |
---|
| 619 | |
---|
| 620 | rc = ioctl(servSock[cognitive_engine], FIONBIO, (char*)&on); |
---|
| 621 | if(rc < 0) |
---|
| 622 | { |
---|
| 623 | perror("ioctl() failed"); |
---|
| 624 | close(servSock[cognitive_engine]); |
---|
| 625 | exit(-1); |
---|
| 626 | } |
---|
| 627 | |
---|
| 628 | rc = ioctl(servSock[policy_engine], FIONBIO, (char*)&on); |
---|
| 629 | if(rc < 0) |
---|
| 630 | { |
---|
| 631 | perror("ioctl() failed"); |
---|
| 632 | close(servSock[policy_engine]); |
---|
| 633 | exit(-1); |
---|
| 634 | } |
---|
| 635 | |
---|
| 636 | printf("Starting server: Hit return to shutdown\n"); |
---|
| 637 | while (running) |
---|
| 638 | { |
---|
| 639 | /* Zero socket descriptor vector and set for server sockets */ |
---|
| 640 | /* This must be reset every time select() is called */ |
---|
| 641 | FD_ZERO(&sockSet); |
---|
| 642 | /* Add keyboard to descriptor vector */ |
---|
| 643 | FD_SET(STDIN_FILENO, &sockSet); |
---|
| 644 | FD_SET(servSock[cognitive_engine], &sockSet); |
---|
| 645 | FD_SET(servSock[policy_engine], &sockSet); |
---|
| 646 | |
---|
| 647 | /* Timeout specification */ |
---|
| 648 | /* This must be reset every time select() is called */ |
---|
| 649 | selTimeout.tv_sec = timeout; /* timeout (secs.) */ |
---|
| 650 | selTimeout.tv_usec = 0; /* 0 microseconds */ |
---|
| 651 | |
---|
| 652 | /* Suspend program until descriptor is ready or timeout */ |
---|
| 653 | rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout); |
---|
| 654 | if (rc == 0) |
---|
| 655 | printf("No echo requests for %i secs...Server still alive\n", timeout); |
---|
| 656 | else |
---|
| 657 | { |
---|
| 658 | if (FD_ISSET(0, &sockSet)) /* Check keyboard */ |
---|
| 659 | { |
---|
| 660 | printf("Shutting down server\n"); |
---|
| 661 | getchar(); |
---|
| 662 | running = 0; |
---|
| 663 | } |
---|
| 664 | |
---|
| 665 | desc_ready = rc; |
---|
| 666 | |
---|
| 667 | for (port = 0; port <= maxDescriptor && desc_ready > 0; port++) { |
---|
| 668 | if (FD_ISSET(port, &sockSet)) |
---|
| 669 | { |
---|
| 670 | printf("Request on port %d: ", port); |
---|
| 671 | desc_ready -= 1; |
---|
| 672 | |
---|
| 673 | |
---|
| 674 | if( (port == servSock[cognitive_engine]) || (port == servSock[policy_engine])) { |
---|
| 675 | |
---|
| 676 | do |
---|
| 677 | { |
---|
| 678 | new_sd = AcceptTCPConnection(port); |
---|
| 679 | if(new_sd < 0) |
---|
| 680 | { |
---|
| 681 | break; |
---|
| 682 | } |
---|
| 683 | |
---|
| 684 | HandleTCPClient(new_sd, uList, pList, oList, ce_info); |
---|
| 685 | FD_SET(new_sd,&sockSet); |
---|
| 686 | if(new_sd > maxDescriptor) |
---|
| 687 | maxDescriptor = new_sd; |
---|
| 688 | printf("New incoming connection - %i\n\n",new_sd); |
---|
| 689 | } while(new_sd != -1); |
---|
| 690 | } else { |
---|
| 691 | |
---|
| 692 | printf("Request on already open descriptor.\n\n"); |
---|
| 693 | HandleTCPClient(port, uList, pList, oList, ce_info); |
---|
| 694 | |
---|
| 695 | } |
---|
| 696 | |
---|
| 697 | } |
---|
| 698 | } |
---|
| 699 | } |
---|
| 700 | } |
---|
| 701 | |
---|
| 702 | /* Close sockets */ |
---|
| 703 | for (port = 0; port < 2; port++) |
---|
| 704 | close(servSock[port]); |
---|
| 705 | |
---|
| 706 | /* Free list of sockets */ |
---|
| 707 | free(servSock); |
---|
| 708 | |
---|
[63] | 709 | return 0; |
---|
[35] | 710 | } |
---|
| 711 | |
---|
| 712 | int main(int argc, char* argv[]) { |
---|
| 713 | |
---|
| 714 | |
---|
| 715 | // CognitiveEngine CE; |
---|
| 716 | // CognitiveEngineShell Shell; |
---|
| 717 | string pFilename; |
---|
[97] | 718 | int fd; |
---|
[35] | 719 | |
---|
| 720 | Utility * uList[10]; |
---|
| 721 | Parameter * pList[10]; |
---|
| 722 | Observable * oList[10]; |
---|
[97] | 723 | struct CE_Info *ce_info; |
---|
[35] | 724 | |
---|
[97] | 725 | if((fd = open("/dev/zero", O_RDWR)) == -1) |
---|
| 726 | return 1; |
---|
| 727 | |
---|
| 728 | ce_info = (struct CE_Info *)mmap(0,sizeof(CE_Info),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0); |
---|
| 729 | |
---|
| 730 | close(fd); |
---|
| 731 | |
---|
[35] | 732 | if(argc < 2) { |
---|
| 733 | cout << "Warning no XML file specific using default: example.xml" << endl; |
---|
| 734 | pFilename = "example.xml"; |
---|
| 735 | } else { |
---|
| 736 | pFilename = argv[1]; |
---|
| 737 | } |
---|
| 738 | |
---|
| 739 | TiXmlDocument doc( pFilename.c_str() ); |
---|
| 740 | bool loadOkay = doc.LoadFile(); |
---|
| 741 | if (!loadOkay) |
---|
| 742 | { |
---|
| 743 | cout << "Loading " << pFilename << " failed." << endl; |
---|
| 744 | return 0; |
---|
| 745 | } |
---|
| 746 | |
---|
[95] | 747 | cout << "\n\nInitialize:: Attemping to parse " << pFilename << "." << endl; |
---|
[97] | 748 | parse_ce_config( &doc , uList, pList, oList, ce_info); |
---|
[95] | 749 | cout << "Initialize:: Configuration file parsing completed.\n" << endl; |
---|
[35] | 750 | |
---|
[72] | 751 | //print_current_config(uList, pList, oList, &ce_info); |
---|
[69] | 752 | |
---|
[97] | 753 | StartServers(uList, pList, oList, ce_info); |
---|
[35] | 754 | return 1; |
---|
[71] | 755 | } |
---|