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