FACT++  1.0
DimServiceInfoList.cc
Go to the documentation of this file.
1 // **************************************************************************
45 // **************************************************************************
46 #include "DimServiceInfoList.h"
47 
48 #include <sstream>
49 
50 #include "WindowLog.h"
51 #include "Converter.h"
52 
53 #include "tools.h"
54 #include "Time.h"
55 
56 using namespace std;
57 
58 // --------------------------------------------------------------------------
59 //
71 DimInfo *DimServiceInfoList::CreateDimInfo(const string &str, const string &svc) const
72 {
73  return new DimInfo((str+'/'+svc).c_str(),
74  const_cast<char*>(""),
75  const_cast<DimServiceInfoList*>(this));
76 }
77 
78 // --------------------------------------------------------------------------
79 //
89 //
90 void DimServiceInfoList::AddServer(const string &s)
91 {
92  // Check if this server is already in the list.
93  // This should never happen if Dim works reliable
94  const ServiceInfoList::iterator v = fServiceInfoList.find(s);
95  if (v!=fServiceInfoList.end())
96  {
97  stringstream err;
98  err << "DimServiceInfoList: Server '" << s << "' in list not as it ought to be.";
99  throw runtime_error(err.str());
100  }
101 
102  fServiceInfoList[s].push_back(CreateSL(s));
103  fServiceInfoList[s].push_back(CreateFMT(s));
104  fServiceInfoList[s].push_back(CreateDS(s));
105 }
106 
107 // --------------------------------------------------------------------------
108 //
120 //
122 {
123  fList->RemoveAllServices(s);
124 
125  const ServiceInfoList::iterator v = fServiceInfoList.find(s);
126  if (v==fServiceInfoList.end())
127  return;
128  /*
129  {
130  stringstream err;
131  err << "DimServiceInfoList: Server '" << s << "' not in list as it ought to be.";
132  throw runtime_error(err.str());
133  }*/
134 
135  // Remove the server from the server list
136  delete v->second[0];
137  delete v->second[1];
138  delete v->second[2];
139 
140  fServiceInfoList.erase(v);
141  fServiceList.erase(s);
142 }
143 
144 // --------------------------------------------------------------------------
145 //
152 {
153  while (!fServiceInfoList.empty())
154  RemoveServer(fServiceInfoList.begin()->first);
155 }
156 
157 
158 // --------------------------------------------------------------------------
159 //
169 //
171 {
172  // Get the name of the service
173  const string svc = getInfo()->getName();
174 
175  // Get the server name from the service name
176  const string server = svc.substr(0, svc.find_first_of('/'));
177  const string service = svc.substr(svc.find_first_of('/')+1);
178 
179  if (service=="SERVICE_LIST")
180  {
181  // For easy and fast access get the corresponding reference
182  TypeList &list = fServiceList[server].first;
183 
184  const string str = getInfo()->getString();
185 
186  // WHAT's THIS???
187  if (str.length()==0)
188  return;
189 
190  // Initialize the entry with an empty list
191  if (str[0]!='+' && str[0]!='-')
192  {
193  fList->RemoveAllServices(server);
194  list.clear();
195  }
196 
197  string buffer;
198 
199  // Tokenize the stream into lines
200  stringstream stream(str);
201  while (getline(stream, buffer, '\n'))
202  {
203  if (buffer.empty())
204  continue;
205 
206  // Get the type and compare it with fType
207  const string type = buffer.substr(buffer.find_last_of('|')+1);
208  if (type=="RPC")
209  continue;
210 
211  /*
212  const bool iscmd = type=="CMD";
213  if (type!=fType && fType!="*")
214  continue;
215  */
216 
217  // Get format, name and command name
218  const string fmt = buffer.substr(buffer.find_first_of('|')+1, buffer.find_last_of('|')-buffer.find_first_of('|')-1);
219  const string name = buffer.substr(buffer.find_first_of('/')+1, buffer.find_first_of('|')-buffer.find_first_of('/')-1);
220  //const string cmd = buffer.substr(0, buffer.find_first_of('|'));
221 
222  const bool iscmd = type=="CMD";
223 
224  // FIXME: Do we need to check that the buffer starts with SERVER ?
225 
226  if (buffer[0]=='-')
227  {
228  // Check if this server is not found in the list.
229  // This should never happen if Dim works reliable
230  const TypeList::iterator v = list.find(name);
231  /*
232  if (v==list.end())
233  {
234  stringstream err;
235  err << "DimServiceInfoList: Service '" << server << "/" << name << "' not in list as it ought to be.";
236  // Seems to happen why more than one client is subscribed
237  // and e.g. the datalogger is immediately quit
238  throw runtime_error(err.str());
239  }*/
240 
241  fList->RemoveService(server, name, iscmd);
242  if (v!=list.end())
243  list.erase(v);
244 
245  continue;
246  }
247 
248  if (buffer[0]=='+')
249  {
250  // Check if this server is not found in the list.
251  // This should never happen if Dim works reliable
252  const TypeList::iterator v = list.find(name);
253  if (v!=list.end())
254  {
255  stringstream err;
256  err << "DimServiceInfoList: Service '" << server << "/" << name << "' already in list not as it ought to be.";
257  throw runtime_error(err.str());
258  }
259 
260  list[name] = make_pair(fmt, iscmd);
261  fList->AddService(server, name, fmt, iscmd);
262 
263  continue;
264  }
265 
266  // Add name the the list
267  list[name] = make_pair(fmt, iscmd);
268  fList->AddService(server, name, fmt, iscmd);
269  }
270 
271  return;
272  }
273 
274  if (service=="SERVICE_DESC")
275  {
276  // For easy and fast access get the corresponding reference
277  DescriptionList &list = fServiceList[server].second;
278 
279  list.clear();
280 
281  string buffer;
282 
283  stringstream stream(getInfo()->getString());
284  while (getline(stream, buffer, '\n'))
285  {
286  if (buffer.empty())
287  continue;
288 
289  const vector<Description> v = Description::SplitDescription(buffer);
290 
291  const string name = v[0].name.substr(v[0].name.find_first_of('/')+1);
292  const string comment = v[0].comment;
293 
294  list[name] = make_pair(comment, vector<Description>(v.begin()+1, v.end()));
295 
296  fList->AddDescription(server, name, v);
297  }
298 
299  return;
300  }
301 
302  if (service=="STATE_LIST")
303  {
304  vector<State> &vec = fServiceList[server].third;
305  vec = State::SplitStates(getInfo()->getString());
306  fList->AddStates(server, vec);
307 
308  return;
309  }
310 }
311 
312 // --------------------------------------------------------------------------
313 //
327 //
328 vector<string> DimServiceInfoList::GetServiceList(const std::string &server, bool iscmd) const
329 {
330  const ServiceList::const_iterator m = fServiceList.find(server);
331  if (m==fServiceList.end())
332  return vector<string>();
333 
334  const TypeList &list = m->second.first;
335 
336  vector<string> vec;
337  for (TypeList::const_iterator i=list.begin(); i!=list.end(); i++)
338  if (i->second.second==iscmd)
339  vec.push_back(server+'/'+i->first);
340 
341  return vec;
342 }
343 
344 // --------------------------------------------------------------------------
345 //
355 //
356 vector<string> DimServiceInfoList::GetServiceList(bool iscmd) const
357 {
358  vector<string> vec;
359  for (ServiceList::const_iterator m=fServiceList.begin(); m!=fServiceList.end(); m++)
360  {
361  const TypeList &list = m->second.first;
362 
363  for (TypeList::const_iterator i=list.begin(); i!=list.end(); i++)
364  if (i->second.second==iscmd)
365  vec.push_back(m->first+'/'+i->first);
366  }
367 
368  return vec;
369 }
370 
371 // --------------------------------------------------------------------------
372 //
387 //
388 std::vector<Description> DimServiceInfoList::GetDescription(const std::string &server, const std::string &service) const
389 {
390  const ServiceList::const_iterator s = fServiceList.find(server);
391  if (s==fServiceList.end())
392  return vector<Description>();
393 
394  const DescriptionList &descs = s->second.second;
395 
396  const DescriptionList::const_iterator d = descs.find(service);
397  if (d==descs.end())
398  return vector<Description>();
399 
400  vector<Description> vec;
401  vec.push_back(Description(service, d->second.first));
402  vec.insert(vec.end(), d->second.second.begin(), d->second.second.end());
403 
404  return vec;
405 }
406 
407 // --------------------------------------------------------------------------
408 //
418 //
419 vector<State> DimServiceInfoList::GetStates(const std::string &server) const
420 {
421  const ServiceList::const_iterator s = fServiceList.find(server);
422  if (s==fServiceList.end())
423  return vector<State>();
424 
425  return s->second.third;
426 }
427 
428 // --------------------------------------------------------------------------
429 //
443 //
444 State DimServiceInfoList::GetState(const std::string &server, int state) const
445 {
446  const ServiceList::const_iterator s = fServiceList.find(server);
447  if (s==fServiceList.end())
448  {
449  stringstream str;
450  str << "DimServiceInfoList::GetState: Searching for state #" << state << " server " << server << " not found.";
451  return State(-3, "Server not found", str.str());
452  }
453 
454  const std::vector<State> &v = s->second.third;
455 
456  for (vector<State>::const_iterator i=v.begin(); i!=v.end(); i++)
457  if (i->index==state)
458  return *i;
459 
460  stringstream str;
461  str << "DimServiceInfoList::GetState: State #" << state << " not found on server " << server << ".";
462  return State(-2, "State not found", str.str());
463 }
464 
465 // --------------------------------------------------------------------------
466 //
479 //
480 int DimServiceInfoList::IsCommand(const std::string &server, const std::string &service) const
481 {
482  const ServiceList::const_iterator s = fServiceList.find(server);
483  if (s==fServiceList.end())
484  return -2;
485 
486  const TypeList &list = s->second.first;
487 
488  const TypeList::const_iterator t = list.find(service);
489  if (t==list.end())
490  return -1;
491 
492  return t->second.second;
493 }
494 
495 
496 // --------------------------------------------------------------------------
497 //
515 //
516 int DimServiceInfoList::PrintDescription(std::ostream &out, bool iscmd, const string &serv, const string &service) const
517 {
518  int rc = 0;
519  for (ServiceList::const_iterator i=fServiceList.begin(); i!=fServiceList.end(); i++)
520  {
521  const string &server = i->first;
522 
523  if (!serv.empty() && server!=serv)
524  continue;
525 
526  out << kRed << "----- " << server << " -----" << endl;
527 
528  const TypeList &types = i->second.first;
529  const DescriptionList &descs = i->second.second;
530 
531  for (TypeList::const_iterator t=types.begin(); t!=types.end(); t++)
532  {
533  if (!service.empty() && t->first!=service)
534  continue;
535 
536  if (t->second.second!=iscmd)
537  continue;
538 
539  rc++;
540 
541  out << " " << t->first;
542 
543  // Check t->second->first for command or service
544  const string fmt = t->second.first;
545  if (!fmt.empty())
546  out << '[' << fmt << ']';
547 
548  const DescriptionList::const_iterator d = descs.find(t->first);
549  if (d==descs.end())
550  {
551  out << endl;
552  continue;
553  }
554 
555  const string comment = d->second.first;
556  const vector<Description> &v = d->second.second;
557 
558  for (vector<Description>::const_iterator j=v.begin(); j!=v.end(); j++)
559  out << " <" << j->name << ">";
560  out << endl;
561 
562  if (!comment.empty())
563  out << " " << comment << endl;
564 
565  for (vector<Description>::const_iterator j=v.begin(); j!=v.end(); j++)
566  {
567  out << " " << kGreen << j->name;
568  if (!j->comment.empty())
569  out << kReset << ": " << kBlue << j->comment;
570  if (!j->unit.empty())
571  out << kYellow << " [" << j->unit << "]";
572  out << endl;
573  }
574  }
575  out << endl;
576  }
577 
578  return rc;
579 }
580 
581 // --------------------------------------------------------------------------
582 //
593 //
594 int DimServiceInfoList::PrintStates(std::ostream &out, const string &serv) const
595 {
596  int rc = 0;
597  for (ServiceList::const_iterator i=fServiceList.begin(); i!=fServiceList.end(); i++)
598  {
599  const string &server = i->first;
600 
601  if (!serv.empty() && server!=serv)
602  continue;
603 
604  out << kRed << "----- " << server << " -----" << endl;
605 
606  const vector<State> &v = i->second.third;
607 
608  if (v.empty())
609  out << " <no states>" << endl;
610  else
611  rc++;
612 
613  for (vector<State>::const_iterator s=v.begin(); s!=v.end(); s++)
614  {
615  out << kBold << setw(5) << s->index << kReset << ": ";
616  out << kYellow << s->name;
617  out << kBlue << " (" << s->comment << ")" << endl;
618  }
619  out << endl;
620  }
621 
622  return rc;
623 }
624 
625 
626 // --------------------------------------------------------------------------
627 //
645 void DimServiceInfoList::SendDimCommand(const string &server, string str, ostream &lout) const
646 {
647  str = Tools::Trim(str);
648 
649  // Find the delimiter between the command name and the data
650  size_t p0 = str.find_first_of(' ');
651  if (p0==string::npos)
652  p0 = str.length();
653 
654  // Get just the command name separated from the data
655  const string name = str.substr(0, p0);
656 
657  // Compile the command which will be sent to the state-machine
658  const string cmd = server + '/' + name;
659 
660  const ServiceList::const_iterator m = fServiceList.find(server);
661  if (m==fServiceList.end())
662  throw runtime_error("Unkown server '"+server+"'");
663 
664  const TypeList &services = m->second.first;
665 
666  const TypeList::const_iterator t = services.find(name);
667  if (t==services.end())
668  throw runtime_error("Command '"+name+"' not known on server '"+server+"'");
669 
670  if (!t->second.second)
671  throw runtime_error("'"+server+"/"+name+" not a command.");
672 
673  // Get the format of the event data
674  const string fmt = t->second.first;
675 
676  // Avoid compiler warning of unused parameter
677  lout << flush;
678 
679  // Convert the user entered data according to the format string
680  // into a data block which will be attached to the event
681 #ifndef DEBUG
682  ostringstream sout;
683  const Converter conv(sout, fmt, false);
684 #else
685  const Converter conv(lout, fmt, false);
686 #endif
687  if (!conv)
688  throw runtime_error("Couldn't properly parse the format... ignored.");
689 
690 #ifdef DEBUG
691  lout << kBlue << cmd;
692 #endif
693  const vector<char> v = conv.GetVector(str.substr(p0));
694 #ifdef DEBUG
695  lout << kBlue << " [" << v.size() << "]" << endl;
696 #endif
697 
698  DimClient::sendCommandNB(cmd.c_str(), (void*)v.data(), v.size());
699 }
700 
701 // --------------------------------------------------------------------------
702 //
719 bool DimServiceInfoList::SendDimCommand(ostream &lout, const string &server, const string &str) const
720 {
721  try
722  {
723  SendDimCommand(server, str, lout);
724  //lout << kGreen << "Command emitted successfully to " << server << "." << endl;
725  return true;
726  }
727  catch (const runtime_error &e)
728  {
729  lout << kRed << e.what() << endl;
730  return false;
731  }
732 }
733 
734 // --------------------------------------------------------------------------
735 //
747 //
748 void DimServiceInfoList::SendDimCommand(const std::string &server, const std::string &str) const
749 {
750  ostringstream dummy;
751  SendDimCommand(server, str, dummy);
752 }
753 
void RemoveServer(std::string)
std::map< const std::string, ServiceType > TypeList
int i
Definition: db_dim_client.c:21
Set color Green.
Definition: WindowLog.h:18
void AddServer(const std::string &s)
int PrintDescription(std::ostream &out, bool iscmd, const std::string &serv="", const std::string &service="") const
char str[80]
Definition: test_client.c:7
Set color Yellow.
Definition: WindowLog.h:19
Set color Red.
Definition: WindowLog.h:17
std::vector< State > GetStates(const std::string &server) const
STL namespace.
Definition: dic.hxx:31
std::map< const std::string, std::pair< std::string, std::vector< Description > > > DescriptionList
bool SendDimCommand(std::ostream &lout, const std::string &server, const std::string &str) const
A struct which stores a name, a unit and a comment.
Definition: Description.h:7
int IsCommand(const std::string &server, const std::string &service) const
DimServiceInfoList(DimServiceInfoListImp *list)
Definition: dis.c:69
int type
static void sendCommandNB(const char *name, int data)
Definition: diccpp.cxx:1140
Set color Blue.
Definition: WindowLog.h:20
std::vector< std::string > GetServiceList(bool iscmd=false) const
static std::vector< Description > SplitDescription(const std::string &buffer)
Definition: Description.cc:82
int buffer[BUFFSIZE]
Definition: db_dim_client.c:14
DimInfo * CreateDimInfo(const std::string &str, const std::string &svc) const
A mal containing all the available informations.
TT t
Definition: test_client.c:26
State GetState(const std::string &server, int state) const
if(extraDns) new Dns
std::string Trim(const std::string &str)
Definition: tools.cc:68
int PrintStates(std::ostream &out, const std::string &serv="") const
A compiler for the DIM data format string.
Definition: Converter.h:16
Reset all attributes.
Definition: WindowLog.h:29
static std::vector< State > SplitStates(const std::string &buffer)
Time of state change.
Definition: State.cc:58
Maintains a list of all servers based on DIS_DNS/SERVER_LIST.
Definition: DimServerList.h:11
Set attribute Bold.
Definition: WindowLog.h:36
std::vector< char > GetVector(const void *d, size_t size) const
Definition: Converter.cc:724
std::vector< Description > GetDescription(const std::string &server, const std::string &service) const