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

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