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

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