- Timestamp:
- 06/05/12 17:34:59 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/DimState.h
r14032 r14086 2 2 #define FACT_DimState 3 3 4 #include <set> 5 #include <string> 6 #include <functional> 7 4 8 #include "State.h" 9 #include "EventImp.h" 10 #include "WindowLog.h" 11 #include "Description.h" 12 #include "StateMachineImp.h" 5 13 6 14 class DimState … … 14 22 15 23 protected: 16 typedef function<void(const EventImp &)> callback;24 typedef std::function<void(const EventImp &)> callback; 17 25 18 26 callback fCallback; … … 23 31 24 32 last = cur; 25 cur = make_pair(evt.GetTime(), disconnected ? kOffline : evt.GetQoS());33 cur = std::make_pair(evt.GetTime(), disconnected ? kOffline : evt.GetQoS()); 26 34 27 35 msg = disconnected ? "" : evt.GetString(); … … 41 49 42 50 public: 43 DimState(const string &n, const string s="STATE") : server(n), 51 DimState() { std::cout << "DefCon" << std::endl; } 52 DimState(const std::string &n, const std::string s="STATE") : server(n), 44 53 service(n+"/"+s), 45 last( make_pair(Time(), kOffline)), cur(make_pair(Time(), kOffline))54 last(std::make_pair(Time(), kOffline)), cur(std::make_pair(Time(), kOffline)) 46 55 { 47 56 } … … 50 59 } 51 60 52 conststring server;53 conststring service;54 55 pair<Time, int32_t> last;56 pair<Time, int32_t> cur;57 st ring msg;61 /*const*/ std::string server; 62 /*const*/ std::string service; 63 64 std::pair<Time, int32_t> last; 65 std::pair<Time, int32_t> cur; 66 std::string msg; 58 67 59 68 virtual void Subscribe(StateMachineImp &imp) 60 69 { 61 imp.Subscribe(service .c_str())62 (imp.Wrap( bind(&DimState::Handler, this,placeholders::_1)));70 imp.Subscribe(service) 71 (imp.Wrap(std::bind(&DimState::Handler, this, std::placeholders::_1))); 63 72 } 64 73 … … 76 85 }; 77 86 78 ostream &operator<<(ostream& out, const DimState &s)87 inline std::ostream &operator<<(std::ostream& out, const DimState &s) 79 88 { 80 89 const State rc = s.description(); … … 105 114 class DimDescribedState : public DimState 106 115 { 107 vector<State> states; 108 109 public: 110 DimDescribedState(const string &n) : DimState(n) 111 { 112 } 116 typedef std::function<void()> callback_desc; 117 118 callback_desc fCallbackStates; 119 120 virtual void CallbackStates() 121 { 122 if (fCallbackStates) 123 fCallbackStates(); 124 } 125 126 127 public: 128 DimDescribedState(const std::string &n) : DimState(n) 129 { 130 } 131 132 std::vector<State> states; 113 133 114 134 virtual void Subscribe(StateMachineImp &imp) 115 135 { 116 imp.Subscribe( (server+"/STATE_LIST").c_str())117 (imp.Wrap( bind(&DimDescribedState::HandleDesc, this,placeholders::_1)));136 imp.Subscribe(server+"/STATE_LIST") 137 (imp.Wrap(std::bind(&DimDescribedState::HandleDesc, this, std::placeholders::_1))); 118 138 119 139 DimState::Subscribe(imp); 140 } 141 142 void SetCallbackStates(const callback_desc &cb) 143 { 144 fCallbackStates = cb; 120 145 } 121 146 … … 126 151 states = State::SplitStates(evt.GetString()); 127 152 states.push_back(State(kOffline, "Offline")); 153 154 CallbackStates(); 128 155 } 129 156 } … … 139 166 }; 140 167 168 class DimDescriptions : public DimDescribedState 169 { 170 typedef std::function<void()> callback_desc; 171 172 callback_desc fCallbackDescriptions; 173 174 virtual void CallbackDescriptions() 175 { 176 if (fCallbackDescriptions) 177 fCallbackDescriptions(); 178 } 179 180 public: 181 DimDescriptions(const std::string &n) : DimDescribedState(n) 182 { 183 } 184 185 std::vector<State> states; 186 std::vector<std::vector<Description>> descriptions; 187 188 virtual void Subscribe(StateMachineImp &imp) 189 { 190 imp.Subscribe(server+"/SERVICE_DESC") 191 (imp.Wrap(std::bind(&DimDescriptions::HandleServiceDesc, this, std::placeholders::_1))); 192 193 DimState::Subscribe(imp); 194 } 195 196 void SetCallbackDescriptions(const callback_desc &cb) 197 { 198 fCallbackDescriptions = cb; 199 } 200 201 202 void HandleServiceDesc(const EventImp &evt) 203 { 204 descriptions.clear(); 205 if (evt.GetSize()>0) 206 { 207 std::string buf; 208 std::stringstream stream(evt.GetString()); 209 while (getline(stream, buf, '\n')) 210 descriptions.push_back(Description::SplitDescription(buf)); 211 } 212 213 CallbackDescriptions(); 214 } 215 }; 216 141 217 class DimVersion : public DimState 142 218 { … … 155 231 DimVersion() : DimState("DIS_DNS", "VERSION_NUMBER") { } 156 232 157 st ring version() const233 std::string version() const 158 234 { 159 235 if (!online()) 160 236 return "Offline"; 161 237 162 ostringstream out;238 std::ostringstream out; 163 239 out << "V" << state()/100 << 'r' << state()%100; 164 240 return out.str(); … … 173 249 class DimControl : public DimState 174 250 { 175 map<string, callback> fCallbacks;251 std::map<std::string, callback> fCallbacks; 176 252 177 253 void Handler(const EventImp &evt) … … 185 261 // Evaluate msg 186 262 const size_t p0 = msg.find_first_of(':'); 187 if (p0==st ring::npos)263 if (p0==std::string::npos) 188 264 return; 189 265 190 266 // Evaluate scriptdepth 191 267 const size_t ps = msg.find_first_of('-'); 192 if (ps!=st ring::npos)268 if (ps!=std::string::npos) 193 269 scriptdepth = atoi(msg.c_str()+ps+1); 194 270 195 271 // Find filename 196 272 const size_t p1 = msg.find_last_of('['); 197 if (p1==st ring::npos)273 if (p1==std::string::npos) 198 274 return; 199 275 200 276 const size_t p2 = msg.find_first_of(':', p0+1); 201 277 202 const size_t p3 = p2==st ring::npos || p2>p1 ? p1-1 : p2;278 const size_t p3 = p2==std::string::npos || p2>p1 ? p1-1 : p2; 203 279 204 280 file = msg.substr(p0+2, p3-p0-2); … … 220 296 DimControl() : DimState("DIM_CONTROL") { } 221 297 222 st ring file;223 st ring shortmsg;298 std::string file; 299 std::string shortmsg; 224 300 int scriptdepth; 225 301 226 void AddCallback(const st ring &script, const callback &cb)302 void AddCallback(const std::string &script, const callback &cb) 227 303 { 228 304 fCallbacks[script] = cb; … … 235 311 }; 236 312 313 class DimDnsServerList 314 { 315 protected: 316 typedef std::function<void(const std::string &)> callback_srv; 317 typedef std::function<void(const EventImp &)> callback_evt; 318 319 callback_srv fCallbackServerAdd; 320 callback_srv fCallbackServerRemove; 321 callback_evt fCallbackServerEvent; 322 323 std::set<std::string> fServerList; 324 325 void HandlerServerImp(const EventImp &evt); 326 327 virtual void CallbackServerAdd(const std::string &str) 328 { 329 if (fCallbackServerAdd) 330 fCallbackServerAdd(str); 331 } 332 virtual void CallbackServerRemove(const std::string &str) 333 { 334 if (fCallbackServerRemove) 335 fCallbackServerRemove(str); 336 } 337 virtual void CallbackServerEvent(const EventImp &evt) 338 { 339 if (fCallbackServerEvent) 340 fCallbackServerEvent(evt); 341 } 342 virtual void HandlerServer(const EventImp &evt) 343 { 344 HandlerServerImp(evt); 345 CallbackServerEvent(evt); 346 } 347 348 public: 349 DimDnsServerList() 350 { 351 } 352 virtual ~DimDnsServerList() 353 { 354 } 355 356 Time time; 357 std::string msg; 358 359 virtual void Subscribe(StateMachineImp &imp) 360 { 361 imp.Subscribe("DIS_DNS/SERVER_LIST") 362 (imp.Wrap(std::bind(&DimDnsServerList::HandlerServer, this, std::placeholders::_1))); 363 } 364 365 void SetCallbackServerAdd(const callback_srv &cb) 366 { 367 fCallbackServerAdd = cb; 368 } 369 370 void SetCallbackServerRemove(const callback_srv &cb) 371 { 372 fCallbackServerRemove = cb; 373 } 374 375 void SetCallbackServerEvent(const callback_evt &cb) 376 { 377 fCallbackServerEvent = cb; 378 } 379 380 //const Time &time() const { return time; } 381 //const std::string &msg() const { return msg; } 382 }; 383 384 struct Service 385 { 386 std::string name; 387 std::string server; 388 std::string service; 389 std::string format; 390 bool iscmd; 391 }; 392 393 inline bool operator<(const Service& left, const Service& right) 394 { 395 return left.name < right.name; 396 } 397 398 class DimDnsServiceList : public DimDnsServerList 399 { 400 StateMachineImp *fStateMachine; 401 402 typedef std::function<void(const Service &)> callback_svc; 403 404 callback_svc fCallbackServiceAdd; 405 //callback_evt fCallbackServiceEvt; 406 407 std::vector<std::string> fServiceList; 408 409 void CallbackServerAdd(const std::string &server) 410 { 411 fStateMachine->Subscribe(server+"/SERVICE_LIST") 412 (fStateMachine->Wrap(std::bind(&DimDnsServiceList::HandlerServiceList, this, std::placeholders::_1))); 413 414 DimDnsServerList::CallbackServerAdd(server); 415 } 416 417 void HandlerServiceListImp(const EventImp &evt); 418 419 /* 420 virtual void CallbackServiceEvt(const EventImp &evt) 421 { 422 if (fCallbackServiceEvt) 423 fCallbackServiceEvt(evt); 424 } 425 */ 426 virtual void HandlerServiceList(const EventImp &evt) 427 { 428 HandlerServiceListImp(evt); 429 //CallbackServiceEvent(evt); 430 } 431 432 433 virtual void CallbackServiceAdd(const Service &service) 434 { 435 if (fCallbackServiceAdd) 436 fCallbackServiceAdd(service); 437 } 438 439 public: 440 DimDnsServiceList() : fStateMachine(0) 441 { 442 } 443 444 void Subscribe(StateMachineImp &imp) 445 { 446 fStateMachine = &imp; 447 DimDnsServerList::Subscribe(imp); 448 } 449 450 void SetCallbackServiceAdd(const callback_svc &cb) 451 { 452 fCallbackServiceAdd = cb; 453 } 454 /* 455 void SetCallbackServiceEvt(const callback_svc &cb) 456 { 457 fCallbackServiceEvt = cb; 458 } 459 */ 460 }; 461 237 462 #endif
Note:
See TracChangeset
for help on using the changeset viewer.