Changeset 10390


Ignore:
Timestamp:
Apr 18, 2011, 2:32:50 PM (9 years ago)
Author:
tbretz
Message:
Moved the handling of the server and service lists to DimNetwork; simplified the remaining code accordingly; adapted to the use of DimServiceInfoList instead of ServiceList.
Location:
trunk/FACT++/src
Files:
2 edited

Legend:

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

    r10341 r10390  
    11#include "RemoteControl.h"
    22
    3 StateClient::StateClient(const std::string &name, MessageImp &imp) :
    4     MessageDimRX(name, imp), fState(-2),
    5     fInfoState((name + "/STATE").c_str(), const_cast<char*>(""), this)
    6 {
    7 }
    8 
    9 void StateClient::infoHandler()
    10 {
    11     DimInfo *curr = getInfo(); // get current DimInfo address
    12     if (!curr)
    13         return;
    14 
    15     if (curr==&fInfoState)
    16     {
    17         const char *txt = fInfoState.getString();
    18 
    19         fState     = strlen(txt)==0 ? -2 : fInfoState.getQuality();
    20         fStateTime = Time(fInfoState.getTimestamp(),
    21                           fInfoState.getTimestampMillisecs());
    22         fStateMsg  = txt;
    23 
    24         if (fState!=-2)
    25         {
    26             stringstream msg;
    27             msg << fInfoState.getName() << ": Changed state to " << fState << " '" << txt << "' received.";
    28             fMsg.Write(fStateTime, msg.str().c_str(), MessageImp::kInfo);
    29         }
    30         return;
    31     }
    32 
    33     MessageDimRX::infoHandler();
    34 }
    35 
    363// ==========================================================================
    37 
    38 void RemoteControlImp::infoHandler()
    39 {
    40     const vector<string> list = fServiceList.GetServerList();
    41 
    42     // Remover StateClients which are not available anymore
    43     for (ClientList::iterator l=fClientList.begin(); l!=fClientList.end(); l++)
    44     {
    45         const string &server = l->first;
    46 
    47         // HasServer doesn't work here, I don't understand why.
    48         if (find(list.begin(), list.end(), server)!=list.end())
    49             continue;
    50 
    51         delete l->second;
    52         fClientList.erase(l);
    53     }
    54 
    55 
    56     // Add StateClients which are new
    57     for (vector<string>::const_iterator l=list.begin(); l!=list.end(); l++)
    58     {
    59         if (*l=="DIS_DNS")
    60             continue;
    61 
    62         const ClientList::const_iterator p = fClientList.find(*l);
    63         if (p!=fClientList.end())
    64             continue;
    65 
    66         fClientList[*l] = new StateClient(*l, *this);
    67     }
    68 }
    694
    705bool RemoteControlImp::ProcessCommand(const std::string &str)
     
    8217        {
    8318            const string c = str.substr(p2+1);
    84             return !fCommandList.SendDimCommand(lout, s, c);
     19            return !SendDimCommand(lout, s, c);
    8520        }
    8621
    87         if(fServiceList.HasServer(s))
     22        if (HasServer(s))
    8823        {
    8924            fCurrentServer = s;
     
    10035    }
    10136
    102     return !fCommandList.SendDimCommand(lout, fCurrentServer, str);
     37    return !SendDimCommand(lout, fCurrentServer, str);
    10338}
    10439
    10540// ==========================================================================
    106 #include <boost/regex.hpp>
    10741
    10842#include "tools.h"
     
    12357        return beg + "> ";//Form("\n[%d] \033[34m\033[1m%s\033[0m> ", GetLine(), fCurrentServer.c_str());
    12458
    125     // The server is valid, check for the state from the last status message
    126     const string msg = l->second->GetMsg();
    127 
    128     static const boost::regex expr("(.+)\\[[0-9]+\\].*");
     59    const State state = GetState(fCurrentServer, l->second->GetState());
    12960
    13061    // The server
    13162    const string serv = Form("\033[34m\033[1m%s\033[0m", fCurrentServer.c_str());
    13263
    133     boost::smatch what;
    134     const int rc = boost::regex_match(msg, what, expr, boost::match_extra);
    135 
    13664    // If no match found or something wrong found just output the server
    137     if (!rc || what.size()!=2)
    138         return beg + " " + serv + "> ";//Form("[%d-%s] ", GetLine(), msg.c_str());
     65    if (state.index<-1)
     66        return beg + " " + serv + "> ";
    13967
    14068    // If everything found add the state to the server
    141     const string state = what[1];
    142     return beg + " " + serv + Form(":\033[32m\033[1m%s\033[0m> ", state.c_str());
     69    return beg + " " + serv + ":\033[32m\033[1m" + state.name + "\033[0m> ";
    14370}
    14471
     
    15885        return beg + "> ";//Form("\n[%d] \033[34m\033[1m%s\033[0m> ", GetLine(), fCurrentServer.c_str());
    15986
    160     // The server is valid, check for the state from the last status message
    161     const string msg = l->second->GetMsg();
    162 
    163     static const boost::regex expr("(.+)\\[[0-9]+\\].*");
    164 
    165     boost::smatch what;
    166     const int rc = boost::regex_match(msg, what, expr, boost::match_extra);
     87    const State state = GetState(fCurrentServer, l->second->GetState());
    16788
    16889    // If no match found or something wrong found just output the server
    169     if (!rc || what.size()!=2)
    170         return beg + " " + fCurrentServer + "> ";//Form("[%d-%s] ", GetLine(), msg.c_str());
     90    if (state.index<-1)
     91        return beg + " " + fCurrentServer + "> ";
    17192
    17293    // If everything found add the state to the server
    173     const string state = what[1];
    174     return beg + " " + fCurrentServer + ":" + state + "> ";
     94    return beg + " " + fCurrentServer + ":" + state.name + "> ";
    17595}
  • trunk/FACT++/src/RemoteControl.h

    r10341 r10390  
    11#ifndef FACT_RemoteControl
    22#define FACT_RemoteControl
    3 
    4 // **************************************************************************
    5 /** @class StateClient
    6 
    7 @brief A simple Dim client diriving from MessageDimRX subscribing to the STATE service
    8 
    9 This is a simple dim client which subscribes to the MESSAGE and STATE
    10 service of a server. It stores the last state and its time as well as
    11 the last message sent.
    12 
    13 **/
    14 // **************************************************************************
    15 #include "MessageDim.h"
    16 #include "Time.h"
    17 
    18 class StateClient : public MessageDimRX
    19 {
    20 private:
    21     Time fStateTime;           /// Combine into one MTime (together with value)
    22     int  fState;               /// -2 not initialized, -1 not connected, 0>= state of client
    23 
    24     std::string fStateMsg;     /// The message received with the last state change
    25 
    26     DimStampedInfo fInfoState; /// The dim service subscription
    27 
    28 protected:
    29     void infoHandler();
    30 
    31 public:
    32     StateClient(const std::string &name, MessageImp &imp);
    33 
    34     bool        IsConnected() const { return fState>=0; }
    35     int         GetState() const    { return fState;    }
    36     std::string GetMsg() const      { return fStateMsg; }
    37 };
    383
    394// **************************************************************************
     
    5116**/
    5217// **************************************************************************
    53 #include "ServiceList.h"
    54 #include "DimErrorRedirecter.h"
    55 
    56 using namespace std;
    57 
    58 class RemoteControlImp : public MessageImp, public DimErrorRedirecter, public DimInfoHandler
    59 {
    60 private:
     18#include "DimNetwork.h"
     19
     20class RemoteControlImp : public DimNetwork
     21{
     22protected:
    6123    std::ostream &lout;          /// Output stream for local synchrounous output
    6224
    63 protected:
    64     typedef std::map<const std::string, StateClient*> ClientList;
    65 
    66     ServiceList fCommandList;    /// An up-to-date list of all available commands
    67     ServiceList fServiceList;    /// An up-to-date list of all available services
    68     ClientList  fClientList;     /// A list with all MESSAGE services to which we subscribed
    69 
    7025    std::string fCurrentServer;  /// The server to which we currently cd'ed
    71 
    72     void infoHandler();
    7326
    7427protected:
    7528    // Redirect asynchronous output to the output window
    7629    RemoteControlImp(std::ostream &out, std::ostream &in) :
    77         MessageImp(out),
    78         DimErrorRedirecter(static_cast<MessageImp&>(*this)),
    79         lout(in), fCommandList("CMD", out), fServiceList("", out)
    80     {
    81         fCommandList.SetHandler(this);
    82         fServiceList.SetHandler(this);
     30        DimNetwork(out), lout(in)
     31    {
    8332    }
    8433    bool ProcessCommand(const std::string &str);
    8534};
     35
     36
    8637
    8738// **************************************************************************
     
    11061{
    11162private:
    112     std::ostream &lout; /// Output stream for local synchrounous output
    113 
    11463    static void append(std::string &str)
    11564    {
     
    13483
    13584                if (p1==string::npos && p2!=string::npos)
    136                     return T::Complete(fCommandList.GetServiceList(), text);
    137 
    138                 std::vector<std::string> v = fCommandList.GetServerList();
     85                    return T::Complete(GetCommandList(), text);
     86
     87                std::vector<std::string> v = GetServerList();
    13988                for_each(v.begin(), v.end(), RemoteControl::append);
    14089                return T::Complete(v, text);
    14190            }
    14291            else
    143                 return T::Complete(fCommandList.GetServiceList(fCurrentServer), text);
    144         }
    145         return T::Complete(fCommandList.GetServiceList(l), text);
     92                return T::Complete(GetCommandList(fCurrentServer), text);
     93        }
     94        return T::Complete(GetCommandList(l), text);
    14695    }
    14796
     
    14998    {
    15099        RemoteControlImp::infoHandler();
    151         if (!fCurrentServer.empty() && !fCommandList.HasServer(fCurrentServer))
     100        if (!fCurrentServer.empty() && !HasServer(fCurrentServer))
    152101        {
    153102            fCurrentServer = "";
     
    159108    // Redirect asynchronous output to the output window
    160109    RemoteControl(const char *name) : T(name),
    161         RemoteControlImp(T::GetStreamOut(), T::GetStreamIn()),
    162         lout(T::GetStreamIn())
    163     {
    164     }
    165 
     110        RemoteControlImp(T::GetStreamOut(), T::GetStreamIn())
     111    {
     112    }
    166113
    167114    bool PrintGeneralHelp()
     
    170117        lout << " " << kUnderline << "Specific commands:" << endl;
    171118        lout << kBold << "   h,help <arg> " << kReset << "List help text for given server or command." << endl;
    172         lout << kBold << "   s,servers    " << kReset << "List all servers which are connected." << endl;
     119//        lout << kBold << "   s,servers    " << kReset << "List all servers which are connected." << endl;
    173120        lout << kBold << "   svc,services " << kReset << "List all services in the network." << endl;
     121        lout << kBold << "   st,states    " << kReset << "List all states in the network." << endl;
    174122        lout << endl;
    175123        return true;
     
    179127    {
    180128        lout << endl << kBold << "List of commands:" << endl;
    181         fCommandList.PrintDescription(lout);
     129        PrintDescription(lout, true);
    182130        return true;
    183131    }
     
    198146            if (!fCurrentServer.empty())
    199147            {
    200                 if (fCommandList.PrintDescription(lout, fCurrentServer, svc)==0)
     148                if (PrintDescription(lout, true, fCurrentServer, svc)==0)
    201149                    lout << "   " << svc << ": <not found>" << endl;
    202150            }
    203151            else
    204152            {
    205                 if (fCommandList.PrintDescription(lout, s, c)==0)
     153                if (PrintDescription(lout, true, s, c)==0)
    206154                    lout << "   <no matches found>" <<endl;
    207155            }
     
    216164            return true;
    217165
    218         if (str=="servers" || str=="s")
    219         {
    220             fCommandList.PrintServerList(lout);
    221             return true;
    222         }
    223 
    224166        if (str=="services" || str=="svc")
    225167        {
    226             fServiceList.PrintDescription(lout);
     168            PrintDescription(lout, false);
     169            return true;
     170        }
     171
     172        if (str=="states" || str=="st")
     173        {
     174            PrintStates(lout);
    227175            return true;
    228176        }
Note: See TracChangeset for help on using the changeset viewer.