[35] | 1 | #include <iostream> |
---|
[63] | 2 | #include <sys/types.h> |
---|
| 3 | #include <sys/wait.h> |
---|
[35] | 4 | #include "tinyxml.h" |
---|
| 5 | #include "tinystr.h" |
---|
| 6 | #include "socket/ServerSocket.h" |
---|
| 7 | #include "socket/SocketException.h" |
---|
| 8 | |
---|
| 9 | using namespace std; |
---|
| 10 | |
---|
| 11 | #define SERVER_PORT 30000 |
---|
| 12 | |
---|
| 13 | struct CE_Info { |
---|
| 14 | int numUtilities; |
---|
| 15 | int numParameters; |
---|
| 16 | int numObservables; |
---|
| 17 | }; |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | struct Utility { |
---|
| 22 | string name; |
---|
| 23 | string units; |
---|
| 24 | string goal; |
---|
| 25 | float target; |
---|
| 26 | }; |
---|
| 27 | struct Affect { |
---|
| 28 | Utility * u; |
---|
| 29 | string relation; |
---|
| 30 | }; |
---|
| 31 | struct Parameter { |
---|
| 32 | string name; |
---|
| 33 | string units; |
---|
| 34 | float min; |
---|
| 35 | int numAffects; |
---|
| 36 | Affect affection_list[10]; |
---|
| 37 | float max; |
---|
| 38 | float step; |
---|
| 39 | }; |
---|
| 40 | |
---|
| 41 | struct Observable { |
---|
| 42 | string name; |
---|
| 43 | Affect affection_list[10]; |
---|
| 44 | int numAffects; |
---|
| 45 | }; |
---|
| 46 | |
---|
| 47 | void print_current_config(Utility * uList[], Parameter * pList[10], Observable * oList[], CE_Info * ce_info) { |
---|
| 48 | int i = 0; |
---|
| 49 | int j = 0; |
---|
| 50 | |
---|
| 51 | for(i = 0; i < ce_info->numUtilities ; i++) { |
---|
| 52 | cout << "Utility: " << uList[i]->name << endl; |
---|
| 53 | cout << " Units: " << uList[i]->units << endl; |
---|
| 54 | cout << " Goal: " << uList[i]->goal << endl; |
---|
| 55 | cout << " Target: " << uList[i]->target << endl; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | for(i = 0; i < ce_info->numParameters; i++) { |
---|
| 60 | cout << "Parameter: " << pList[i]->name << endl; |
---|
| 61 | cout << " Units: " << pList[i]->units << endl; |
---|
| 62 | cout << " Min: " << pList[i]->min << endl; |
---|
| 63 | cout << " Max: " << pList[i]->max << endl; |
---|
| 64 | cout << " Step: " << pList[i]->step << endl; |
---|
| 65 | for(j = 0; j < pList[i]->numAffects; j++) { |
---|
| 66 | cout << " Affect: " << pList[i]->affection_list[j].u->name << " -> " << pList[i]->affection_list[j].relation << endl; |
---|
| 67 | } |
---|
| 68 | } |
---|
| 69 | for(i = 0; i < ce_info->numObservables; i++) { |
---|
| 70 | cout << "Observable: " << oList[i]->name << endl; |
---|
| 71 | for(j = 0; j < oList[i]->numAffects; j++) { |
---|
| 72 | cout << " Affect: " << oList[i]->affection_list[j].u->name << " -> " << oList[i]->affection_list[j].relation << endl; |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | int parse_ce_config( TiXmlDocument * doc , Utility * u[], Parameter * p[], Observable * o[], CE_Info * ce_info) { |
---|
| 79 | |
---|
| 80 | TiXmlElement* pElem; //!current element |
---|
| 81 | TiXmlElement* pChild; //!current child of pElem |
---|
| 82 | TiXmlElement* pChild1; //!current child of pElem |
---|
| 83 | TiXmlElement* pSecondChild; //!current child of pElem |
---|
| 84 | TiXmlHandle hDoc(doc); //!handle to xml document |
---|
| 85 | TiXmlHandle hRoot(0); //! handle to root element |
---|
| 86 | |
---|
| 87 | int count = 0; |
---|
| 88 | int i = 0; |
---|
| 89 | int j = 0; |
---|
| 90 | int k = 0; |
---|
| 91 | |
---|
| 92 | pElem = hDoc.FirstChildElement().Element(); |
---|
| 93 | if(!pElem) { cout << "no valid root! quit-ing function!" << endl; return 0; } |
---|
| 94 | hRoot = TiXmlHandle(pElem); |
---|
| 95 | |
---|
| 96 | // Pull utility information from XML file. |
---|
| 97 | |
---|
| 98 | pElem = hRoot.FirstChild("utilities").Element(); |
---|
| 99 | pChild1 = hRoot.Child("utilities",count).Element(); |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | for(pChild = pChild1->FirstChildElement("utility"); pChild; pChild = pChild->NextSiblingElement()) |
---|
| 103 | { |
---|
| 104 | u[i] = new Utility; |
---|
| 105 | const char *uName = pChild->Attribute("name"); |
---|
| 106 | if(uName) u[i]->name = uName; |
---|
| 107 | const char *uUnits = pChild->Attribute("units"); |
---|
| 108 | if(uUnits) u[i]->units = uUnits; |
---|
| 109 | const char *uGoal = pChild->Attribute("goal"); |
---|
| 110 | if(uGoal) u[i]->goal = uGoal; |
---|
| 111 | if(pChild->QueryFloatAttribute("target",&u[i]->target) != TIXML_SUCCESS) u[i]->target = -1; |
---|
| 112 | i++; |
---|
| 113 | } |
---|
| 114 | ce_info->numUtilities = i; |
---|
| 115 | cout << "Parsed " << ce_info->numUtilities << " utilities." << endl; |
---|
| 116 | |
---|
| 117 | // Pull observable information from XML file. |
---|
| 118 | i = 0; |
---|
| 119 | pElem = hRoot.FirstChild("observables").Element(); |
---|
| 120 | pChild1 = hRoot.Child("observables",count).Element(); |
---|
| 121 | |
---|
| 122 | for(pChild = pChild1->FirstChildElement("observable"); pChild; pChild = pChild->NextSiblingElement()) |
---|
| 123 | { |
---|
| 124 | |
---|
| 125 | const char *oName = pChild->Attribute("name"); |
---|
| 126 | o[i] = new Observable; |
---|
| 127 | |
---|
| 128 | if(oName) o[i]->name = oName; |
---|
| 129 | |
---|
| 130 | j = 0; |
---|
| 131 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; pSecondChild = pSecondChild->NextSiblingElement()) |
---|
| 132 | { |
---|
| 133 | const char *oUtilName = pSecondChild->Attribute("utility"); |
---|
| 134 | |
---|
| 135 | // If a utility affects this parameter find the utility object and assign it |
---|
| 136 | if(oUtilName) { |
---|
| 137 | // Search for correct utility |
---|
| 138 | for(k=0;k<10;k++){ |
---|
| 139 | if(u[k]->name == oUtilName) { |
---|
| 140 | o[i]->affection_list[j].u = u[k]; |
---|
| 141 | break; |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | // Set relationship |
---|
| 147 | const char *oRelate = pSecondChild->Attribute("relationship"); |
---|
| 148 | if(oRelate) o[i]->affection_list[j].relation = oRelate; |
---|
| 149 | j++; |
---|
| 150 | } |
---|
| 151 | o[i]->numAffects = j; |
---|
| 152 | i++; |
---|
| 153 | } |
---|
| 154 | ce_info->numObservables = i; |
---|
| 155 | cout << "Parsed " << ce_info->numObservables << " observables." << endl; |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | // Pull parameter information from XML file. |
---|
| 159 | pElem = hRoot.FirstChild("parameters").Element(); |
---|
| 160 | pChild1 = hRoot.Child("parameters",count).Element(); |
---|
| 161 | |
---|
| 162 | i = 0; |
---|
| 163 | for(pChild = pChild1->FirstChildElement("parameter"); pChild; pChild = pChild->NextSiblingElement()) |
---|
| 164 | { |
---|
| 165 | p[i] = new Parameter; |
---|
| 166 | |
---|
| 167 | const char *pName = pChild->Attribute("name"); |
---|
| 168 | if(pName) p[i]->name = pName; |
---|
| 169 | const char *pUnits = pChild->Attribute("units"); |
---|
| 170 | if(pUnits) p[i]->units = pUnits; |
---|
| 171 | |
---|
| 172 | if(pChild->QueryFloatAttribute("min",&p[i]->min) != TIXML_SUCCESS) p[i]->min = -1; |
---|
| 173 | if(pChild->QueryFloatAttribute("max",&p[i]->max) != TIXML_SUCCESS) p[i]->max = -1; |
---|
| 174 | if(pChild->QueryFloatAttribute("step",&p[i]->step) != TIXML_SUCCESS) p[i]->step = -1; |
---|
| 175 | |
---|
| 176 | j = 0; |
---|
| 177 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; pSecondChild = pSecondChild->NextSiblingElement()) |
---|
| 178 | { |
---|
| 179 | const char *pUtilName = pSecondChild->Attribute("utility"); |
---|
| 180 | |
---|
| 181 | // If a utility affects this parameter find the utility object and assign it |
---|
| 182 | if(pUtilName) { |
---|
| 183 | // Search for correct utility |
---|
| 184 | for( k=0 ; u[k]!=NULL ; k++ ){ |
---|
| 185 | if(u[k]->name == pUtilName) { |
---|
| 186 | p[i]->affection_list[j].u = u[k]; |
---|
| 187 | break; |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | // Set relationship |
---|
| 193 | const char *pRelate = pSecondChild->Attribute("relationship"); |
---|
| 194 | if(pRelate) { |
---|
| 195 | p[i]->affection_list[j].relation = pRelate; |
---|
| 196 | } else { |
---|
| 197 | cout << "Error: No relation found." << endl; |
---|
| 198 | } |
---|
| 199 | j++; |
---|
| 200 | |
---|
| 201 | } |
---|
| 202 | p[i]->numAffects = j; |
---|
| 203 | i++; |
---|
| 204 | |
---|
| 205 | } |
---|
| 206 | ce_info->numParameters = i; |
---|
| 207 | cout << "Parsed " << ce_info->numParameters << " parameters." << endl; |
---|
| 208 | return 1; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | |
---|
[63] | 212 | void error(char *msg) |
---|
| 213 | { |
---|
| 214 | perror(msg); |
---|
| 215 | exit(1); |
---|
| 216 | } |
---|
[35] | 217 | |
---|
[63] | 218 | void StartMessageParser( int socketfd) { |
---|
| 219 | char buffer[256]; |
---|
| 220 | int n; |
---|
[35] | 221 | |
---|
[63] | 222 | while(1) { |
---|
| 223 | // Clear incoming data buffer |
---|
| 224 | bzero(buffer,256); |
---|
| 225 | |
---|
| 226 | // Wait for incoming message |
---|
| 227 | n = recv(socketfd,buffer,255,0); |
---|
| 228 | if (n < 0) error("ERROR reading from socket"); |
---|
| 229 | if (n == 0) error("Client has closed the connection."); |
---|
| 230 | |
---|
| 231 | // Print message |
---|
| 232 | printf("Here is the message: %s\n",buffer); |
---|
[35] | 233 | |
---|
[63] | 234 | // Write message back to client |
---|
| 235 | n = write(socketfd,"I got your message",18); |
---|
| 236 | if (n < 0) error("ERROR writing to socket"); |
---|
| 237 | } |
---|
| 238 | } |
---|
[35] | 239 | |
---|
[63] | 240 | int StartShell(int port) { |
---|
| 241 | // Start socket server |
---|
| 242 | int sockfd, newsockfd, clilen; |
---|
| 243 | struct sockaddr_in serv_addr, cli_addr; |
---|
[35] | 244 | |
---|
[63] | 245 | // Setup socket connection |
---|
| 246 | |
---|
| 247 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
---|
| 248 | if (sockfd < 0) |
---|
| 249 | error("ERROR opening socket"); |
---|
| 250 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
---|
| 251 | serv_addr.sin_family = AF_INET; |
---|
| 252 | serv_addr.sin_addr.s_addr = INADDR_ANY; |
---|
| 253 | serv_addr.sin_port = htons(port); |
---|
| 254 | if (bind(sockfd, (struct sockaddr *) &serv_addr, |
---|
| 255 | sizeof(serv_addr)) < 0) |
---|
| 256 | error("ERROR on binding"); |
---|
| 257 | listen(sockfd,5); |
---|
| 258 | clilen = sizeof(cli_addr); |
---|
| 259 | newsockfd = accept(sockfd, |
---|
| 260 | (struct sockaddr *) &cli_addr, |
---|
| 261 | (socklen_t*)&clilen); |
---|
| 262 | if (newsockfd < 0) |
---|
| 263 | error("ERROR on accept"); |
---|
[35] | 264 | |
---|
[63] | 265 | // Begin parsing the messages |
---|
| 266 | StartMessageParser(newsockfd); |
---|
| 267 | |
---|
| 268 | return 0; |
---|
[35] | 269 | } |
---|
| 270 | |
---|
| 271 | int main(int argc, char* argv[]) { |
---|
| 272 | |
---|
| 273 | |
---|
| 274 | // CognitiveEngine CE; |
---|
| 275 | // CognitiveEngineShell Shell; |
---|
| 276 | string pFilename; |
---|
| 277 | |
---|
| 278 | Utility * uList[10]; |
---|
| 279 | Parameter * pList[10]; |
---|
| 280 | Observable * oList[10]; |
---|
| 281 | CE_Info ce_info; |
---|
| 282 | |
---|
| 283 | if(argc < 2) { |
---|
| 284 | cout << "Warning no XML file specific using default: example.xml" << endl; |
---|
| 285 | pFilename = "example.xml"; |
---|
| 286 | } else { |
---|
| 287 | pFilename = argv[1]; |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | TiXmlDocument doc( pFilename.c_str() ); |
---|
| 291 | bool loadOkay = doc.LoadFile(); |
---|
| 292 | if (!loadOkay) |
---|
| 293 | { |
---|
| 294 | cout << "Loading " << pFilename << " failed." << endl; |
---|
| 295 | return 0; |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | cout << "Attemping to parse " << pFilename << "." << endl; |
---|
| 299 | parse_ce_config( &doc , uList, pList, oList, &ce_info); |
---|
| 300 | cout << "Configuration file parsing completed." << endl; |
---|
| 301 | |
---|
[63] | 302 | // Open cognitive engine port |
---|
| 303 | StartShell(30000); |
---|
[35] | 304 | |
---|
[63] | 305 | // Open policy engine port |
---|
| 306 | // StartShell(30001); |
---|
| 307 | |
---|
[35] | 308 | return 1; |
---|
| 309 | } |
---|