source: trunk/FACT++/src/DimServiceInfoList.cc@ 13707

Last change on this file since 13707 was 13220, checked in by tbretz, 13 years ago
Improved debugging output.
File size: 23.7 KB
Line 
1// **************************************************************************
2/** @class DimServiceInfoList
3
4@brief Maintains a list of all services available in the Dim network
5
6The idea of this class is to maintain a list of services available
7in the Dim network as well as state descriptions if available.
8
9Therefore, it subscribes to the SERVICE_LIST, SERVICE_DESC and STATE_LIST
10services of all servers.
11
12To maintain the list it derives from DimServerList which maintains
13a list of all servers.
14
15To maintain the subscriptions it overwrites:
16
17- void DimServerList::AddServer(const std::string &s)
18- void DimServerList::RemoveServer(const std::string &s)
19- void DimServerList::RemoveAllServers()
20
21If a derived class also overwrites these functions it must be ensured that
22the member functions of DimServiceInfoList are still called properly.
23
24Whenever a service is added or removed, or all services of one server
25is removed the following virtual functions are called:
26
27- virtual void AddService(const std::string &server, const std::string &service, const std::string &fmt, bool iscmd)
28- virtual void RemoveService(const std::string &server, const std::string &service, bool iscmd)
29- virtual void RemoveAllServices(const std::string &server)
30
31Note, that these functions are not called from the RemoveServer() and
32RemoveAllServer() functions. It might be a difference whether all services
33were removed but the server is still online or the server went offline.
34
35If a description or a state was added, this is signaled though:
36
37- virtual void AddDescription(const std::string &server, const std::string &service, const std::vector<Description> &vec)
38- virtual void AddStates(const std::string &server, const std::vector<State> &vec)
39
40Note, that Descriptions and States are never removed except a service or
41server goes offline. It is expected that if a service comes online also
42the list of descritions is sent again.
43
44*/
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
56using namespace std;
57
58// --------------------------------------------------------------------------
59//
60//! A helper to shorten the call to create a DimInfo.
61//!
62//! @param str
63//! name of the server to which we want to subscribe
64//!
65//! @param svc
66//! name of the servic on the server to which we want to subscribe
67//!
68//! @returns
69//! a pointer to the newly created DimInfo
70//!
71DimInfo *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//
80//! Adds the service subscription for SERVICE_LIST, SERVICE_DESC and
81//! STATE_LIST for the given server. Don't forget to call this function
82//! if it is overwritten in a derived class.
83//!
84//! @param s
85//! server which should be added
86//!
87//! @throws
88//! a runtime_error is the server is already in the list
89//
90void 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//
109//! Removes the service subscription for SERVICE_LIST, SERVICE_DESC and
110//! STATE_LIST for the given server, as well as the stored informations.
111//! Don't forget to call this function if it is overwritten in a derived
112//! class.
113//!
114//! @param s
115//! server which should be removed. We need to make a copy, otherwise
116//! RemoveServer will destroy the staring the reference is pointing to
117//!
118//! @throws
119//! a runtime_error is the server to be removed is not in the list
120//
121void DimServiceInfoList::RemoveServer(const string s)
122{
123 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//
146//! Removes the service subscription for SERVICE_LIST, SERVICE_DESC and
147//! STATE_LIST for all servers, as well as all stored informations.
148//! Don't forget to call this function if it is overwritten in a derived
149//! class.
150//!
151void DimServiceInfoList::RemoveAllServers()
152{
153 while (fServiceInfoList.size()>0)
154 RemoveServer(fServiceInfoList.begin()->first);
155}
156
157
158// --------------------------------------------------------------------------
159//
160//! This function processes the update of the SERVICE_LIST, SERVICE_DESC,
161//! and STATE_LIST updates.
162//!
163//! Whenever a service is added or removed or all services of a server are
164//! removed (the list is newly sent completely) the virtual functions
165//! AddService(), RemoveService() and RemoveAllServices() aee called.
166//!
167//! If a new description or a new state is added, the virtual functions
168//! AddDescription() and AddStates() respectively are called.
169//
170void DimServiceInfoList::infoHandler()
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 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 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 AddService(server, name, fmt, iscmd);
262
263 continue;
264 }
265
266 // Add name the the list
267 list[name] = make_pair(fmt, iscmd);
268 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 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 AddStates(server, vec);
307
308 return;
309 }
310
311 DimServerList::infoHandler();
312}
313
314// --------------------------------------------------------------------------
315//
316//! Returns a list of all services available for the given server.
317//! Depending on iscmd either only services or only commands are returned.
318//!
319//! @param server
320//! server for which the list should be returned
321//!
322//! @param iscmd
323//! true if only commands should be returned, false for services
324//!
325//! @returns
326//! a vector<string> which contains all the service or command names for
327//! the given server. The names returned are always SERVER/SERVICE
328//! If the server was not fund an empty vector is returned.
329//
330vector<string> DimServiceInfoList::GetServiceList(const std::string &server, bool iscmd) const
331{
332 const ServiceList::const_iterator m = fServiceList.find(server);
333 if (m==fServiceList.end())
334 return vector<string>();
335
336 const TypeList &list = m->second.first;
337
338 vector<string> vec;
339 for (TypeList::const_iterator i=list.begin(); i!=list.end(); i++)
340 if (i->second.second==iscmd)
341 vec.push_back(server+'/'+i->first);
342
343 return vec;
344}
345
346// --------------------------------------------------------------------------
347//
348//! Returns a list of all services available in the network.
349//! Depending on iscmd either only services or only commands are returned.
350//!
351//! @param iscmd
352//! true if only commands should be returned, false for services
353//!
354//! @returns
355//! a vector<string> which contains all the service or command names in
356//! the network. The names returned are always SERVER/SERVICE
357//
358vector<string> DimServiceInfoList::GetServiceList(bool iscmd) const
359{
360 vector<string> vec;
361 for (ServiceList::const_iterator m=fServiceList.begin(); m!=fServiceList.end(); m++)
362 {
363 const TypeList &list = m->second.first;
364
365 for (TypeList::const_iterator i=list.begin(); i!=list.end(); i++)
366 if (i->second.second==iscmd)
367 vec.push_back(m->first+'/'+i->first);
368 }
369
370 return vec;
371}
372
373// --------------------------------------------------------------------------
374//
375//! Returns a list of all descriptions for the given service on the
376//! given server. Service in this context can also be a command.
377//!
378//! @param server
379//! Server name to look for
380//!
381//! @param service
382//! Service/command name to look for
383//!
384//! @returns
385//! a vector<Description> which contains all argument descriptions for
386//! the given service or command. The first entry contains the name
387//! and the general description for the given service. If the server
388//! or service was not found an empty vector is returned.
389//
390std::vector<Description> DimServiceInfoList::GetDescription(const std::string &server, const std::string &service) const
391{
392 const ServiceList::const_iterator s = fServiceList.find(server);
393 if (s==fServiceList.end())
394 return vector<Description>();
395
396 const DescriptionList &descs = s->second.second;
397
398 const DescriptionList::const_iterator d = descs.find(service);
399 if (d==descs.end())
400 return vector<Description>();
401
402 vector<Description> vec;
403 vec.push_back(Description(service, d->second.first));
404 vec.insert(vec.end(), d->second.second.begin(), d->second.second.end());
405
406 return vec;
407}
408
409// --------------------------------------------------------------------------
410//
411//! Returns a list of all states associated with the given server.
412//!
413//! @param server
414//! Server name to look for
415//!
416//! @returns
417//! a vector<State> which contains all state descriptions for
418//! the given server. If the server or service was not found an
419//! empty vector is returned.
420//
421vector<State> DimServiceInfoList::GetStates(const std::string &server) const
422{
423 const ServiceList::const_iterator s = fServiceList.find(server);
424 if (s==fServiceList.end())
425 return vector<State>();
426
427 return s->second.third;
428}
429
430// --------------------------------------------------------------------------
431//
432//! Returns the Description of the state as defined by the arguments.
433//! given server. Service in this context can also be a command.
434//!
435//! @param server
436//! Server name to look for
437//!
438//! @param state
439//! The state index to look for (e.g. 1)
440//!
441//! @returns
442//! The State object containing the description. If the server was
443//! not found the State object will contain the index -3, if the
444//! state was not found -2.
445//
446State DimServiceInfoList::GetState(const std::string &server, int state) const
447{
448 const ServiceList::const_iterator s = fServiceList.find(server);
449 if (s==fServiceList.end())
450 {
451 stringstream str;
452 str << "DimServiceInfoList::GetState: Searching for state #" << state << " server " << server << " not found.";
453 return State(-3, "Server not found", str.str());
454 }
455
456 const std::vector<State> &v = s->second.third;
457
458 for (vector<State>::const_iterator i=v.begin(); i!=v.end(); i++)
459 if (i->index==state)
460 return *i;
461
462 stringstream str;
463 str << "DimServiceInfoList::GetState: State #" << state << " not found on server " << server << ".";
464 return State(-2, "State not found", str.str());
465}
466
467// --------------------------------------------------------------------------
468//
469//! Returns whether the given service on the given server is a command
470//! or not.
471//!
472//! @param server
473//! Server name to look for
474//!
475//! @param service
476//! The service name to look for
477//!
478//! @returns
479//! 1 if it is a command, 0 if it is a service, -1 if the service
480//! was not found on the server, -2 if the server was not found.
481//
482int DimServiceInfoList::IsCommand(const std::string &server, const std::string &service) const
483{
484 const ServiceList::const_iterator s = fServiceList.find(server);
485 if (s==fServiceList.end())
486 return -2;
487
488 const TypeList &list = s->second.first;
489
490 const TypeList::const_iterator t = list.find(service);
491 if (t==list.end())
492 return -1;
493
494 return t->second.second;
495}
496
497
498// --------------------------------------------------------------------------
499//
500//! Print the full available documentation (description) of all available
501//! services or comments to the the given stream.
502//!
503//! @param out
504//! ostream to which the output is send.
505//!
506//! @param iscmd
507//! true if all commands should be printed, false for services.
508//!
509//! @param serv
510//! if a server is given, only the information for this server is printed
511//!
512//! @param service
513//! if a service is given, only information for this service is printed
514//!
515//! @returns
516//! the number of descriptions found
517//
518int DimServiceInfoList::PrintDescription(std::ostream &out, bool iscmd, const string &serv, const string &service) const
519{
520 int rc = 0;
521 for (ServiceList::const_iterator i=fServiceList.begin(); i!=fServiceList.end(); i++)
522 {
523 const string &server = i->first;
524
525 if (!serv.empty() && server!=serv)
526 continue;
527
528 out << kRed << "----- " << server << " -----" << endl;
529
530 const TypeList &types = i->second.first;
531 const DescriptionList &descs = i->second.second;
532
533 for (TypeList::const_iterator t=types.begin(); t!=types.end(); t++)
534 {
535 if (!service.empty() && t->first!=service)
536 continue;
537
538 if (t->second.second!=iscmd)
539 continue;
540
541 rc++;
542
543 out << " " << t->first;
544
545 // Check t->second->first for command or service
546 const string fmt = t->second.first;
547 if (!fmt.empty())
548 out << '[' << fmt << ']';
549
550 const DescriptionList::const_iterator d = descs.find(t->first);
551 if (d==descs.end())
552 {
553 out << endl;
554 continue;
555 }
556
557 const string comment = d->second.first;
558 const vector<Description> &v = d->second.second;
559
560 for (vector<Description>::const_iterator j=v.begin(); j!=v.end(); j++)
561 out << " <" << j->name << ">";
562 out << endl;
563
564 if (!comment.empty())
565 out << " " << comment << endl;
566
567 for (vector<Description>::const_iterator j=v.begin(); j!=v.end(); j++)
568 {
569 out << " " << kGreen << j->name;
570 if (!j->comment.empty())
571 out << kReset << ": " << kBlue << j->comment;
572 if (!j->unit.empty())
573 out << kYellow << " [" << j->unit << "]";
574 out << endl;
575 }
576 }
577 out << endl;
578 }
579
580 return rc;
581}
582
583// --------------------------------------------------------------------------
584//
585//! Print the full list of stated for the given server.
586//!
587//! @param out
588//! ostream to which the output is send.
589//!
590//! @param serv
591//! if a server is given, only the information for this server is printed
592//!
593//! @returns
594//! the number of states found
595//
596int DimServiceInfoList::PrintStates(std::ostream &out, const string &serv) const
597{
598 int rc = 0;
599 for (ServiceList::const_iterator i=fServiceList.begin(); i!=fServiceList.end(); i++)
600 {
601 const string &server = i->first;
602
603 if (!serv.empty() && server!=serv)
604 continue;
605
606 out << kRed << "----- " << server << " -----" << endl;
607
608 const vector<State> &v = i->second.third;
609
610 if (v.size()==0)
611 out << " <no states>" << endl;
612 else
613 rc++;
614
615 for (vector<State>::const_iterator s=v.begin(); s!=v.end(); s++)
616 {
617 out << kBold << setw(5) << s->index << kReset << ": ";
618 out << kYellow << s->name;
619 out << kBlue << " (" << s->comment << ")" << endl;
620 }
621 out << endl;
622 }
623
624 return rc;
625}
626
627
628// --------------------------------------------------------------------------
629//
630//! Tries to send a dim command according to the arguments.
631//! The command given is evaluated according to the available format string.
632//!
633//! @param server
634//! The name of the server to which the command should be send, e.g. DRIVE
635//!
636//! @param str
637//! Command and data, eg "TRACK 12.5 13.8"
638//!
639//! @param lout
640//! the ostream to which errors and debug output is redirected
641//!
642//! @throws
643//! runtime_error if the server or command was not found, or if the
644//! format associated with the command could not be properly parsed,
645//! or if the command could not successfully be emitted.
646//!
647void DimServiceInfoList::SendDimCommand(const string &server, string str, ostream &lout) const
648{
649 str = Tools::Trim(str);
650
651 // Find the delimiter between the command name and the data
652 size_t p0 = str.find_first_of(' ');
653 if (p0==string::npos)
654 p0 = str.length();
655
656 // Get just the command name separated from the data
657 const string name = str.substr(0, p0);
658
659 // Compile the command which will be sent to the state-machine
660 const string cmd = server + '/' + name;
661
662 const ServiceList::const_iterator m = fServiceList.find(server);
663 if (m==fServiceList.end())
664 throw runtime_error("Unkown server '"+server+"'");
665
666 const TypeList &services = m->second.first;
667
668 const TypeList::const_iterator t = services.find(name);
669 if (t==services.end())
670 throw runtime_error("Command '"+name+"' not known on server '"+server+"'");
671
672 if (!t->second.second)
673 throw runtime_error("'"+server+"/"+name+" not a command.");
674
675 // Get the format of the event data
676 const string fmt = t->second.first;
677
678 // Avoid compiler warning of unused parameter
679 lout << flush;
680
681 // Convert the user entered data according to the format string
682 // into a data block which will be attached to the event
683#ifndef DEBUG
684 ostringstream sout;
685 const Converter conv(sout, fmt, false);
686#else
687 const Converter conv(lout, fmt, false);
688#endif
689 if (!conv)
690 throw runtime_error("Couldn't properly parse the format... ignored.");
691
692#ifdef DEBUG
693 lout << kBlue << cmd;
694#endif
695 const vector<char> v = conv.GetVector(str.substr(p0));
696#ifdef DEBUG
697 lout << kBlue << " [" << v.size() << "]" << endl;
698#endif
699
700 const int rc = DimClient::sendCommand(cmd.c_str(), (void*)v.data(), v.size());
701 if (!rc)
702 throw runtime_error("ERROR - Sending command "+cmd+" failed.");
703}
704
705// --------------------------------------------------------------------------
706//
707//! Catches the runtime_erros thrown by
708//! SendDimCommand(const string &, string, ostream &)
709//! and redirects the error message to the output stream.
710//!
711//! @param lout
712//! the ostream to which errors and debug output is redirected
713//!
714//! @param server
715//! The name of the server to which the command should be send, e.g. DRIVE
716//!
717//! @param str
718//! Command and data, eg "TRACK 12.5 13.8"
719//!
720//! @returns
721//! true if SendDimComment didn't throw an exception, false otherwise
722//!
723bool DimServiceInfoList::SendDimCommand(ostream &lout, const string &server, const string &str) const
724{
725 try
726 {
727 SendDimCommand(server, str, lout);
728 lout << kGreen << "Command emitted successfully to " << server << "." << endl;
729 return true;
730 }
731 catch (const runtime_error &e)
732 {
733 lout << kRed << e.what() << endl;
734 return false;
735 }
736}
737
738// --------------------------------------------------------------------------
739//
740//! Calls SendDimCommand(const string &, string, ostream &) and dumps
741//! the output.
742//!
743//! @param server
744//! The name of the server to which the command should be send, e.g. DRIVE
745//!
746//! @param str
747//! Command and data, eg "TRACK 12.5 13.8"
748//!
749//! @throws
750//! see SendDimCommand(const string &, string, ostream &)
751//
752void DimServiceInfoList::SendDimCommand(const std::string &server, const std::string &str) const
753{
754 ostringstream dummy;
755 SendDimCommand(server, str, dummy);
756}
Note: See TracBrowser for help on using the repository browser.