Changeset 14086 for trunk/FACT++


Ignore:
Timestamp:
06/05/12 17:34:59 (12 years ago)
Author:
tbretz
Message:
Added std:: and some new classes for the server list and the service list.
File:
1 edited

Legend:

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

    r14032 r14086  
    22#define FACT_DimState
    33
     4#include <set>
     5#include <string>
     6#include <functional>
     7
    48#include "State.h"
     9#include "EventImp.h"
     10#include "WindowLog.h"
     11#include "Description.h"
     12#include "StateMachineImp.h"
    513
    614class DimState
     
    1422
    1523protected:
    16     typedef function<void(const EventImp &)> callback;
     24    typedef std::function<void(const EventImp &)> callback;
    1725
    1826    callback fCallback;
     
    2331
    2432        last = cur;
    25         cur  = make_pair(evt.GetTime(), disconnected ? kOffline : evt.GetQoS());
     33        cur  = std::make_pair(evt.GetTime(), disconnected ? kOffline : evt.GetQoS());
    2634
    2735        msg = disconnected ? "" : evt.GetString();
     
    4149
    4250public:
    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),
    4453        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))
    4655    {
    4756    }
     
    5059    }
    5160
    52     const string server;
    53     const string service;
    54 
    55     pair<Time, int32_t> last;
    56     pair<Time, int32_t> cur;
    57     string 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;
    5867
    5968    virtual void Subscribe(StateMachineImp &imp)
    6069    {
    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)));
    6372    }
    6473
     
    7685};
    7786
    78 ostream &operator<<(ostream& out, const DimState &s)
     87inline std::ostream &operator<<(std::ostream& out, const DimState &s)
    7988{
    8089    const State rc = s.description();
     
    105114class DimDescribedState : public DimState
    106115{
    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
     127public:
     128    DimDescribedState(const std::string &n) : DimState(n)
     129    {
     130    }
     131
     132    std::vector<State> states;
    113133
    114134    virtual void Subscribe(StateMachineImp &imp)
    115135    {
    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)));
    118138
    119139        DimState::Subscribe(imp);
     140    }
     141
     142    void SetCallbackStates(const callback_desc &cb)
     143    {
     144        fCallbackStates = cb;
    120145    }
    121146
     
    126151            states = State::SplitStates(evt.GetString());
    127152            states.push_back(State(kOffline, "Offline"));
     153
     154            CallbackStates();
    128155        }
    129156    }
     
    139166};
    140167
     168class 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
     180public:
     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
    141217class DimVersion : public DimState
    142218{
     
    155231    DimVersion() : DimState("DIS_DNS", "VERSION_NUMBER") { }
    156232
    157     string version() const
     233    std::string version() const
    158234    {
    159235        if (!online())
    160236            return "Offline";
    161237
    162         ostringstream out;
     238        std::ostringstream out;
    163239        out << "V" << state()/100 << 'r' << state()%100;
    164240        return out.str();
     
    173249class DimControl : public DimState
    174250{
    175     map<string, callback> fCallbacks;
     251    std::map<std::string, callback> fCallbacks;
    176252
    177253    void Handler(const EventImp &evt)
     
    185261        // Evaluate msg
    186262        const size_t p0 = msg.find_first_of(':');
    187         if (p0==string::npos)
     263        if (p0==std::string::npos)
    188264            return;
    189265
    190266        // Evaluate scriptdepth
    191267        const size_t ps = msg.find_first_of('-');
    192         if (ps!=string::npos)
     268        if (ps!=std::string::npos)
    193269            scriptdepth = atoi(msg.c_str()+ps+1);
    194270
    195271        // Find filename
    196272        const size_t p1 = msg.find_last_of('[');
    197         if (p1==string::npos)
     273        if (p1==std::string::npos)
    198274            return;
    199275
    200276        const size_t p2 = msg.find_first_of(':', p0+1);
    201277
    202         const size_t p3 = p2==string::npos || p2>p1 ? p1-1 : p2;
     278        const size_t p3 = p2==std::string::npos || p2>p1 ? p1-1 : p2;
    203279
    204280        file = msg.substr(p0+2, p3-p0-2);
     
    220296    DimControl() : DimState("DIM_CONTROL") { }
    221297
    222     string file;
    223     string shortmsg;
     298    std::string file;
     299    std::string shortmsg;
    224300    int scriptdepth;
    225301
    226     void AddCallback(const string &script, const callback &cb)
     302    void AddCallback(const std::string &script, const callback &cb)
    227303    {
    228304        fCallbacks[script] = cb;
     
    235311};
    236312
     313class DimDnsServerList
     314{
     315protected:
     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
     348public:
     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
     384struct Service
     385{
     386    std::string name;
     387    std::string server;
     388    std::string service;
     389    std::string format;
     390    bool   iscmd;
     391};
     392
     393inline bool operator<(const Service& left, const Service& right)
     394{
     395    return left.name < right.name;
     396}
     397
     398class 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
     439public:
     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
    237462#endif
Note: See TracChangeset for help on using the changeset viewer.