Changeset 14546 for trunk/FACT++
- Timestamp:
- 10/31/12 14:28:52 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/RemoteControl.h
r14540 r14546 175 175 } 176 176 177 /* 177 178 void JsSleep(uint32_t ms) 178 179 { … … 185 186 186 187 T::Unlock(); 187 } 188 }*/ 188 189 189 190 int JsWait(const string &server, int32_t state, uint32_t ms) … … 237 238 } 238 239 240 struct EventInfo 241 { 242 EventImp *ptr; 243 uint64_t counter; 244 Event data; 245 EventInfo(EventImp *p) : ptr(p), counter(0) { } 246 }; 247 239 248 // Keep a copy of the data for access by V8 240 map<string, EventImp *> fEvents; // For unsibscription 241 map<string, pair<uint64_t, Event>> fData; 242 249 map<string, EventInfo> fInfo; 243 250 std::mutex fMutex; 244 251 245 252 pair<uint64_t, EventImp *> JsGetEvent(const std::string &service) 246 253 { 254 // This function is called from JavaScript 247 255 const lock_guard<mutex> lock(fMutex); 248 256 249 const auto it = fData.find(service); 250 251 return it==fData.end() ? make_pair(uint64_t(0), (Event*)0) : make_pair(it->second.first, &it->second.second); 257 const auto it = fInfo.find(service); 258 if (it==fInfo.end()) 259 return make_pair(0, static_cast<EventImp*>(NULL)); 260 261 EventInfo &info = it->second; 262 return make_pair(info.counter, (EventImp*)&info.data); 252 263 } 253 264 254 265 int Handle(const EventImp &evt, const string &service) 255 266 { 256 const lock_guard<mutex> lock(fMutex); 257 258 const auto it = fData.find(service); 259 if (it==fData.end()) 260 fData[service] = make_pair(0, static_cast<Event>(evt)); 261 else 262 { 263 it->second.first++; 264 it->second.second = static_cast<Event>(evt); 265 } 267 // This function is called from the StateMachine 268 fMutex.lock(); 269 270 const auto it = fInfo.find(service); 271 272 // This should never happen... but just in case. 273 if (it==fInfo.end()) 274 { 275 fMutex.unlock(); 276 return StateMachineImp::kSM_KeepState; 277 } 278 279 EventInfo &info = it->second; 280 281 const uint64_t cnt = info.counter++; 282 info.data = static_cast<Event>(evt); 283 284 fMutex.unlock(); 285 286 JsHandleEvent(evt, cnt, service); 266 287 267 288 return StateMachineImp::kSM_KeepState; … … 273 294 return 0; 274 295 296 // This function is called from JavaScript 297 const lock_guard<mutex> lock(fMutex); 298 275 299 // Do not subscribe twice 276 if (f Events.find(service)!=fEvents.end())300 if (fInfo.find(service)!=fInfo.end()) 277 301 return 0; 278 302 279 return fEvents[service] = &fImp->Subscribe(service)(fImp->Wrap(bind(&RemoteControl<T>::Handle, this, placeholders::_1, service))); 303 EventImp *ptr = &fImp->Subscribe(service)(fImp->Wrap(bind(&RemoteControl<T>::Handle, this, placeholders::_1, service))); 304 fInfo.insert(make_pair(service, EventInfo(ptr))); 305 return ptr; 280 306 } 281 307 … … 285 311 return false; 286 312 287 const auto it = fEvents.find(service); 288 if (it==fEvents.end()) 313 // This function is called from JavaScript 314 const lock_guard<mutex> lock(fMutex); 315 316 const auto it = fInfo.find(service); 317 if (it==fInfo.end()) 289 318 return false; 290 319 291 fImp->Unsubscribe(it->second );292 f Events.erase(it);320 fImp->Unsubscribe(it->second.ptr); 321 fInfo.erase(it); 293 322 294 323 return true; … … 297 326 void UnsubscribeAll() 298 327 { 299 for (auto it=fEvents.begin(); it!=fEvents.end(); it++) 300 fImp->Unsubscribe(it->second); 301 fEvents.clear(); 328 // This function is called from JavaScript 329 const lock_guard<mutex> lock(fMutex); 330 331 for (auto it=fInfo.begin(); it!=fInfo.end(); it++) 332 fImp->Unsubscribe(it->second.ptr); 333 334 fInfo.clear(); 302 335 } 303 336
Note:
See TracChangeset
for help on using the changeset viewer.