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