Changeset 14546 for trunk/FACT++


Ignore:
Timestamp:
10/31/12 14:28:52 (12 years ago)
Author:
tbretz
Message:
Some simplification to the JavaScript interfacing, mainly unifying the different maps, added a callback in case when an event is received.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/RemoteControl.h

    r14540 r14546  
    175175    }
    176176
     177    /*
    177178    void JsSleep(uint32_t ms)
    178179    {
     
    185186
    186187        T::Unlock();
    187     }
     188    }*/
    188189
    189190    int JsWait(const string &server, int32_t state, uint32_t ms)
     
    237238    }
    238239
     240    struct EventInfo
     241    {
     242        EventImp *ptr;
     243        uint64_t counter;
     244        Event data;
     245        EventInfo(EventImp *p) : ptr(p), counter(0) { }
     246    };
     247
    239248    // 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;
    243250    std::mutex fMutex;
    244251
    245252    pair<uint64_t, EventImp *> JsGetEvent(const std::string &service)
    246253    {
     254        // This function is called from JavaScript
    247255        const lock_guard<mutex> lock(fMutex);
    248256
    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);
    252263    }
    253264
    254265    int Handle(const EventImp &evt, const string &service)
    255266    {
    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);
    266287
    267288        return StateMachineImp::kSM_KeepState;
     
    273294            return 0;
    274295
     296        // This function is called from JavaScript
     297        const lock_guard<mutex> lock(fMutex);
     298
    275299        // Do not subscribe twice
    276         if (fEvents.find(service)!=fEvents.end())
     300        if (fInfo.find(service)!=fInfo.end())
    277301            return 0;
    278302
    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;
    280306    }
    281307
     
    285311            return false;
    286312
    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())
    289318            return false;
    290319
    291         fImp->Unsubscribe(it->second);
    292         fEvents.erase(it);
     320        fImp->Unsubscribe(it->second.ptr);
     321        fInfo.erase(it);
    293322
    294323        return true;
     
    297326    void UnsubscribeAll()
    298327    {
    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();
    302335    }
    303336
Note: See TracChangeset for help on using the changeset viewer.