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

Last change on this file since 11570 was 11275, checked in by tbretz, 13 years ago
Fixed fServiceList.erase in RemoveServer; the entry might not exist.
File size: 23.1 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 << "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
116//!
117//! @throws
118//! a runtime_error is the server to be removed is not in the list
119//
120void DimServiceInfoList::RemoveServer(const string &s)
121{
122 const ServiceInfoList::iterator v = fServiceInfoList.find(s);
123 if (v==fServiceInfoList.end())
124 {
125 stringstream err;
126 err << "Server '" << s << "' not in list as it ought to be.";
127 throw runtime_error(err.str());
128 }
129
130 RemoveAllServices(s);
131
132 // Remove the server from the server list
133 delete v->second[0];
134 delete v->second[1];
135 delete v->second[2];
136
137 fServiceInfoList.erase(v);
138 fServiceList.erase(s);
139}
140
141// --------------------------------------------------------------------------
142//
143//! Removes the service subscription for SERVICE_LIST, SERVICE_DESC and
144//! STATE_LIST for all servers, as well as all stored informations.
145//! Don't forget to call this function if it is overwritten in a derived
146//! class.
147//!
148void DimServiceInfoList::RemoveAllServers()
149{
150 for (ServiceInfoList::iterator i=fServiceInfoList.begin();
151 i!=fServiceInfoList.end(); i++)
152 RemoveServer(i->first);
153}
154
155
156// --------------------------------------------------------------------------
157//
158//! This function processes the update of the SERVICE_LIST, SERVICE_DESC,
159//! and STATE_LIST updates.
160//!
161//! Whenever a service is added or removed or all services of a server are
162//! removed (the list is newly sent completely) the virtual functions
163//! AddService(), RemoveService() and RemoveAllServices() aee called.
164//!
165//! If a new description or a new state is added, the virtual functions
166//! AddDescription() and AddStates() respectively are called.
167//
168void DimServiceInfoList::infoHandler()
169{
170 // Get the name of the service
171 const string svc = getInfo()->getName();
172
173 // Get the server name from the service name
174 const string server = svc.substr(0, svc.find_first_of('/'));
175 const string service = svc.substr(svc.find_first_of('/')+1);
176
177 if (service=="SERVICE_LIST")
178 {
179 // For easy and fast access get the corresponding reference
180 TypeList &list = fServiceList[server].first;
181
182 const string str = getInfo()->getString();
183
184 // WHAT's THIS???
185 if (str.length()==0)
186 return;
187
188 // Initialize the entry with an empty list
189 if (str[0]!='+' && str[0]!='-')
190 {
191 RemoveAllServices(server);
192 list.clear();
193 }
194
195 string buffer;
196
197 // Tokenize the stream into lines
198 stringstream stream(str);
199 while (getline(stream, buffer, '\n'))
200 {
201 if (buffer.empty())
202 continue;
203
204 // Get the type and compare it with fType
205 const string type = buffer.substr(buffer.find_last_of('|')+1);
206 if (type=="RPC")
207 continue;
208
209 /*
210 const bool iscmd = type=="CMD";
211 if (type!=fType && fType!="*")
212 continue;
213 */
214
215 // Get format, name and command name
216 const string fmt = buffer.substr(buffer.find_first_of('|')+1, buffer.find_last_of('|')-buffer.find_first_of('|')-1);
217 const string name = buffer.substr(buffer.find_first_of('/')+1, buffer.find_first_of('|')-buffer.find_first_of('/')-1);
218 //const string cmd = buffer.substr(0, buffer.find_first_of('|'));
219
220 const bool iscmd = type=="CMD";
221
222 // FIXME: Do we need to check that the buffer starts with SERVER ?
223
224 if (buffer[0]=='-')
225 {
226 // Check if this server is not found in the list.
227 // This should never happen if Dim works reliable
228 const TypeList::iterator v = list.find(name);
229 if (v==list.end())
230 {
231 stringstream err;
232 err << "Service '" << server << "/" << name << "' not in list as it ought to be.";
233 throw runtime_error(err.str());
234 }
235
236 RemoveService(server, name, iscmd);
237 list.erase(v);
238
239 continue;
240 }
241
242 if (buffer[0]=='+')
243 {
244 // Check if this server is not found in the list.
245 // This should never happen if Dim works reliable
246 const TypeList::iterator v = list.find(name);
247 if (v!=list.end())
248 {
249 stringstream err;
250 err << "Service '" << server << "/" << name << "' already in list not as it ought to be.";
251 throw runtime_error(err.str());
252 }
253
254 list[name] = make_pair(fmt, iscmd);
255 AddService(server, name, fmt, iscmd);
256
257 continue;
258 }
259
260 // Add name the the list
261 list[name] = make_pair(fmt, iscmd);
262 AddService(server, name, fmt, iscmd);
263 }
264
265 return;
266 }
267
268 if (service=="SERVICE_DESC")
269 {
270 // For easy and fast access get the corresponding reference
271 DescriptionList &list = fServiceList[server].second;
272
273 list.clear();
274
275 string buffer;
276
277 stringstream stream(getInfo()->getString());
278 while (getline(stream, buffer, '\n'))
279 {
280 if (buffer.empty())
281 continue;
282
283 const vector<Description> v = Description::SplitDescription(buffer);
284
285 const string name = v[0].name.substr(v[0].name.find_first_of('/')+1);
286 const string comment = v[0].comment;
287
288 list[name] = make_pair(comment, vector<Description>(v.begin()+1, v.end()));
289
290 AddDescription(server, name, v);
291 }
292
293 return;
294 }
295
296 if (service=="STATE_LIST")
297 {
298 vector<State> &vec = fServiceList[server].third;
299 vec = State::SplitStates(getInfo()->getString());
300 AddStates(server, vec);
301
302 return;
303 }
304
305 DimServerList::infoHandler();
306}
307
308// --------------------------------------------------------------------------
309//
310//! Returns a list of all services available for the given server.
311//! Depending on iscmd either only services or only commands are returned.
312//!
313//! @param server
314//! server for which the list should be returned
315//!
316//! @param iscmd
317//! true if only commands should be returned, false for services
318//!
319//! @returns
320//! a vector<string> which contains all the service or command names for
321//! the given server. The names returned are always SERVER/SERVICE
322//! If the server was not fund an empty vector is returned.
323//
324vector<string> DimServiceInfoList::GetServiceList(const std::string &server, bool iscmd) const
325{
326 const ServiceList::const_iterator m = fServiceList.find(server);
327 if (m==fServiceList.end())
328 return vector<string>();
329
330 const TypeList &list = m->second.first;
331
332 vector<string> vec;
333 for (TypeList::const_iterator i=list.begin(); i!=list.end(); i++)
334 if (i->second.second==iscmd)
335 vec.push_back(server+'/'+i->first);
336
337 return vec;
338}
339
340// --------------------------------------------------------------------------
341//
342//! Returns a list of all services available in the network.
343//! Depending on iscmd either only services or only commands are returned.
344//!
345//! @param iscmd
346//! true if only commands should be returned, false for services
347//!
348//! @returns
349//! a vector<string> which contains all the service or command names in
350//! the network. The names returned are always SERVER/SERVICE
351//
352vector<string> DimServiceInfoList::GetServiceList(bool iscmd) const
353{
354 vector<string> vec;
355 for (ServiceList::const_iterator m=fServiceList.begin(); m!=fServiceList.end(); m++)
356 {
357 const TypeList &list = m->second.first;
358
359 for (TypeList::const_iterator i=list.begin(); i!=list.end(); i++)
360 if (i->second.second==iscmd)
361 vec.push_back(m->first+'/'+i->first);
362 }
363
364 return vec;
365}
366
367// --------------------------------------------------------------------------
368//
369//! Returns a list of all descriptions for the given service on the
370//! given server. Service in this context can also be a command.
371//!
372//! @param server
373//! Server name to look for
374//!
375//! @param service
376//! Service/command name to look for
377//!
378//! @returns
379//! a vector<Description> which contains all argument descriptions for
380//! the given service or command. The first entry contains the name
381//! and the general description for the given service. If the server
382//! or service was not found an empty vector is returned.
383//
384std::vector<Description> DimServiceInfoList::GetDescription(const std::string &server, const std::string &service) const
385{
386 const ServiceList::const_iterator s = fServiceList.find(server);
387 if (s==fServiceList.end())
388 return vector<Description>();
389
390 const DescriptionList &descs = s->second.second;
391
392 const DescriptionList::const_iterator d = descs.find(service);
393 if (d==descs.end())
394 return vector<Description>();
395
396 vector<Description> vec;
397 vec.push_back(Description(service, d->second.first));
398 vec.insert(vec.end(), d->second.second.begin(), d->second.second.end());
399
400 return vec;
401}
402
403// --------------------------------------------------------------------------
404//
405//! Returns a list of all states associated with the given server.
406//!
407//! @param server
408//! Server name to look for
409//!
410//! @returns
411//! a vector<State> which contains all state descriptions for
412//! the given server. If the server or service was not found an
413//! empty vector is returned.
414//
415vector<State> DimServiceInfoList::GetStates(const std::string &server) const
416{
417 const ServiceList::const_iterator s = fServiceList.find(server);
418 if (s==fServiceList.end())
419 return vector<State>();
420
421 return s->second.third;
422}
423
424// --------------------------------------------------------------------------
425//
426//! Returns the Description of the state as defined by the arguments.
427//! given server. Service in this context can also be a command.
428//!
429//! @param server
430//! Server name to look for
431//!
432//! @param state
433//! The state index to look for (e.g. 1)
434//!
435//! @returns
436//! The State object containing the description. If the server was
437//! not found the State object will contain the index -3, if the
438//! state was not found -2.
439//
440State DimServiceInfoList::GetState(const std::string &server, int state) const
441{
442 const ServiceList::const_iterator s = fServiceList.find(server);
443 if (s==fServiceList.end())
444 {
445 stringstream str;
446 str << "DimServiceInfoList::GetState: Searching for state #" << state << " server " << server << " not found.";
447 return State(-3, "Server not found", str.str());
448 }
449
450 const std::vector<State> &v = s->second.third;
451
452 for (vector<State>::const_iterator i=v.begin(); i!=v.end(); i++)
453 if (i->index==state)
454 return *i;
455
456 stringstream str;
457 str << "DimServiceInfoList::GetState: State #" << state << " not found on server " << server << ".";
458 return State(-2, "State not found", str.str());
459}
460
461// --------------------------------------------------------------------------
462//
463//! Returns whether the given service on the given server is a command
464//! or not.
465//!
466//! @param server
467//! Server name to look for
468//!
469//! @param service
470//! The service name to look for
471//!
472//! @returns
473//! 1 if it is a command, 0 if it is a service, -1 if the service
474//! was not found on the server, -2 if the server was not found.
475//
476int DimServiceInfoList::IsCommand(const std::string &server, const std::string &service) const
477{
478 const ServiceList::const_iterator s = fServiceList.find(server);
479 if (s==fServiceList.end())
480 return -2;
481
482 const TypeList &list = s->second.first;
483
484 const TypeList::const_iterator t = list.find(service);
485 if (t==list.end())
486 return -1;
487
488 return t->second.second;
489}
490
491
492// --------------------------------------------------------------------------
493//
494//! Print the full available documentation (description) of all available
495//! services or comments to the the given stream.
496//!
497//! @param out
498//! ostream to which the output is send.
499//!
500//! @param iscmd
501//! true if all commands should be printed, false for services.
502//!
503//! @param serv
504//! if a server is given, only the information for this server is printed
505//!
506//! @param service
507//! if a service is given, only information for this service is printed
508//!
509//! @returns
510//! the number of descriptions found
511//
512int DimServiceInfoList::PrintDescription(std::ostream &out, bool iscmd, const string &serv, const string &service) const
513{
514 int rc = 0;
515 for (ServiceList::const_iterator i=fServiceList.begin(); i!=fServiceList.end(); i++)
516 {
517 const string &server = i->first;
518
519 if (!serv.empty() && server!=serv)
520 continue;
521
522 out << kRed << "----- " << server << " -----" << endl;
523
524 const TypeList &types = i->second.first;
525 const DescriptionList &descs = i->second.second;
526
527 for (TypeList::const_iterator t=types.begin(); t!=types.end(); t++)
528 {
529 if (!service.empty() && t->first!=service)
530 continue;
531
532 if (t->second.second!=iscmd)
533 continue;
534
535 rc++;
536
537 out << " " << t->first;
538
539 // Check t->second->first for command or service
540 const string fmt = t->second.first;
541 if (!fmt.empty())
542 out << '[' << fmt << ']';
543
544 const DescriptionList::const_iterator d = descs.find(t->first);
545 if (d==descs.end())
546 {
547 out << endl;
548 continue;
549 }
550
551 const string comment = d->second.first;
552 const vector<Description> &v = d->second.second;
553
554 for (vector<Description>::const_iterator j=v.begin(); j!=v.end(); j++)
555 out << " <" << j->name << ">";
556 out << endl;
557
558 if (!comment.empty())
559 out << " " << comment << endl;
560
561 for (vector<Description>::const_iterator j=v.begin(); j!=v.end(); j++)
562 {
563 out << " " << kGreen << j->name;
564 if (!j->comment.empty())
565 out << kReset << ": " << kBlue << j->comment;
566 if (!j->unit.empty())
567 out << kYellow << " [" << j->unit << "]";
568 out << endl;
569 }
570 }
571 out << endl;
572 }
573
574 return rc;
575}
576
577// --------------------------------------------------------------------------
578//
579//! Print the full list of stated for the given server.
580//!
581//! @param out
582//! ostream to which the output is send.
583//!
584//! @param serv
585//! if a server is given, only the information for this server is printed
586//!
587//! @returns
588//! the number of states found
589//
590int DimServiceInfoList::PrintStates(std::ostream &out, const string &serv) const
591{
592 int rc = 0;
593 for (ServiceList::const_iterator i=fServiceList.begin(); i!=fServiceList.end(); i++)
594 {
595 const string &server = i->first;
596
597 if (!serv.empty() && server!=serv)
598 continue;
599
600 out << kRed << "----- " << server << " -----" << endl;
601
602 const vector<State> &v = i->second.third;
603
604 if (v.size()==0)
605 out << " <no states>" << endl;
606 else
607 rc++;
608
609 for (vector<State>::const_iterator s=v.begin(); s!=v.end(); s++)
610 {
611 out << kBold << setw(5) << s->index << kReset << ": ";
612 out << kYellow << s->name;
613 out << kBlue << " (" << s->comment << ")" << endl;
614 }
615 out << endl;
616 }
617
618 return rc;
619}
620
621
622// --------------------------------------------------------------------------
623//
624//! Tries to send a dim command according to the arguments.
625//! The command given is evaluated according to the available format string.
626//!
627//! @param server
628//! The name of the server to which the command should be send, e.g. DRIVE
629//!
630//! @param str
631//! Command and data, eg "TRACK 12.5 13.8"
632//!
633//! @param lout
634//! the ostream to which errors and debug output is redirected
635//!
636//! @throws
637//! runtime_error if the server or command was not found, or if the
638//! format associated with the command could not be properly parsed,
639//! or if the command could not successfully be emitted.
640//!
641void DimServiceInfoList::SendDimCommand(const string &server, string str, ostream &lout) const
642{
643 str = Tools::Trim(str);
644
645 // Find the delimiter between the command name and the data
646 size_t p0 = str.find_first_of(' ');
647 if (p0==string::npos)
648 p0 = str.length();
649
650 // Get just the command name separated from the data
651 const string name = str.substr(0, p0);
652
653 // Compile the command which will be sent to the state-machine
654 const string cmd = server + '/' + name;
655
656 const ServiceList::const_iterator m = fServiceList.find(server);
657 if (m==fServiceList.end())
658 throw runtime_error("Unkown server '"+server+"'");
659
660 const TypeList &services = m->second.first;
661
662 const TypeList::const_iterator t = services.find(name);
663 if (t==services.end())
664 throw runtime_error("Command '"+name+"' not known on server '"+server+"'");
665
666 if (!t->second.second)
667 throw runtime_error("'"+server+"/"+name+" not a command.");
668
669 // Get the format of the event data
670 const string fmt = t->second.first;
671
672 // Convert the user entered data according to the format string
673 // into a data block which will be attached to the event
674 const Converter conv(lout, fmt, false);
675 if (!conv)
676 throw runtime_error("Couldn't properly parse the format... ignored.");
677
678 lout << kBlue << cmd;
679 const vector<char> v = conv.GetVector(str.substr(p0));
680 lout << endl;
681
682 const int rc = DimClient::sendCommand(cmd.c_str(), (void*)v.data(), v.size());
683 if (!rc)
684 throw runtime_error("ERROR - Sending command "+cmd+" failed.");
685}
686
687// --------------------------------------------------------------------------
688//
689//! Catches the runtime_erros thrown by
690//! SendDimCommand(const string &, string, ostream &)
691//! and redirects the error message to the output stream.
692//!
693//! @param lout
694//! the ostream to which errors and debug output is redirected
695//!
696//! @param server
697//! The name of the server to which the command should be send, e.g. DRIVE
698//!
699//! @param str
700//! Command and data, eg "TRACK 12.5 13.8"
701//!
702//! @returns
703//! true if SendDimComment didn't throw an exception, false otherwise
704//!
705bool DimServiceInfoList::SendDimCommand(ostream &lout, const string &server, const string &str) const
706{
707 try
708 {
709 SendDimCommand(server, str, lout);
710 lout << kGreen << "Command emitted successfully to " << server << "." << endl;
711 return true;
712 }
713 catch (const runtime_error &e)
714 {
715 lout << kRed << e.what() << endl;
716 return false;
717 }
718}
719
720// --------------------------------------------------------------------------
721//
722//! Calls SendDimCommand(const string &, string, ostream &) and dumps
723//! the output.
724//!
725//! @param server
726//! The name of the server to which the command should be send, e.g. DRIVE
727//!
728//! @param str
729//! Command and data, eg "TRACK 12.5 13.8"
730//!
731//! @throws
732//! see SendDimCommand(const string &, string, ostream &)
733//
734void DimServiceInfoList::SendDimCommand(const std::string &server, const std::string &str) const
735{
736 ostringstream dummy;
737 SendDimCommand(server, str, dummy);
738}
Note: See TracBrowser for help on using the repository browser.