source: trunk/FACT++/src/DimDescriptionService.cc@ 16329

Last change on this file since 16329 was 15168, checked in by tbretz, 12 years ago
Store a global set of all described services.
File size: 5.7 KB
Line 
1// **************************************************************************
2/** @class DimDescriptionService
3
4@brief A DimService which broadcasts descriptions for services and commands
5
6The DimDescriptionService creates a service with the name of the server like
7SERVER/SERVICE_DESC. This is meant in addition to the SERVICE_LIST service
8of each node to contain a description of the service and its arguments.
9
10Assume you have created a service (or command) with the format I:2;F:1
11a valid description string would look like
12
13 Description|int[addr]:Address range (from - to)|val[byte]:Value to be set
14
15Description is a general description of the command or service itself,
16int and val are the names of the arguments (e.g. names of FITS columns),
17addr and byte have the meaning of a unit (e.g. unit of FITS column)
18and the text after the colon is a description of the arguments
19(e.g. comment of a FITS column). The description must not contain a
20line-break character \n.
21
22You can omit either the name, the unit or the comment or any combination of them.
23The descriptions of the individual format strings are separated by a vertical line.
24
25The description should contain as many descriptions as format chunks, e.g.
26
27 - I:1 should contain one description chunks
28 - I:1;F:1 should contain two description chunks
29 - I:2;F:1 should contain two description chunks
30 - I:2;I:1;F:1 should contain three description chunks
31
32*/
33// **************************************************************************
34#include "DimDescriptionService.h"
35
36#include <stdexcept>
37
38#include "dis.hxx"
39#include "Time.h"
40
41using namespace std;
42
43DimService *DimDescriptionService::fService = 0;
44int DimDescriptionService::fCount = 0;
45std::string DimDescriptionService::fData = "";
46
47set<string> DimDescribedService::fServices;
48
49// --------------------------------------------------------------------------
50//
51//! When the constructor is first called, a service with the name
52//! SERVER/SERVICE_DESC is created. The server name SERVER is retrieved
53//! from DimServer::itsName. If DimServer::itsName is empty, the
54//! server name is extracted from the given name as the part before the
55//! first '/'. A string "name=format\n" is added to fData and stored
56//! in fDescription.
57//!
58//! A counter fCount for the number of instantiations is increased.
59//!
60//! @param name
61//! The name of the service or command to be described, e.g. SERVER/COMMAND
62//!
63//! @param desc
64//! A description string. For details see class reference
65//!
66//! @throws
67//! If a server name couldn't be reliably determined a logic_error
68//! exception is thrown; if the given description contains a '\n'
69//! also a logic_error is thrown.
70//
71DimDescriptionService::DimDescriptionService(const std::string &name, const std::string &desc)
72{
73 string server = DimServer::itsName ? DimServer::itsName : "";
74 if (server.empty())
75 {
76 const size_t p = name.find_first_of('/');
77 if (p==string::npos)
78 throw logic_error("Could not determine server name");
79
80 server = name.substr(0, p);
81 }
82
83 if (desc.find_first_of('\n')!=string::npos)
84 throw logic_error("Description for "+name+" contains '\\n'");
85
86 if (!fService)
87 {
88 fService = new DimService((server+"/SERVICE_DESC").c_str(), const_cast<char*>(""));
89 fData =
90 server + "/SERVICE_DESC"
91 "=Descriptions of services or commands and there arguments"
92 "|Description[string]:For a detailed "
93 "explanation of the descriptive string see the class reference "
94 "of DimDescriptionService.\n" +
95 server + "/CLIENT_LIST"
96 "=Native Dim service: A list of all connected clients\n" +
97 server + "/VERSION_NUMBER"
98 "=Native Dim service: Version number of Dim in use"
99 "|DimVer[int]:Version*100+Release (e.g. V19r17 = 1917)\n" +
100 server + "/EXIT"
101 "=This is a native Dim command: Exit program"
102 "remotely. FACT++ programs use the given number as return code."
103 "|Rc[int]:Return code, under normal circumstances this should be 0 or 1 (42 will call exit() directly, 0x42 will call abort() directly.\n" +
104 server + "/SERVICE_LIST"
105 "=Native Dim service: List of services, commands and formats"
106 "|ServiceList[string]:For details see the Dim manual.\n";
107 }
108
109
110 fCount++;
111
112 fDescription = name + '=' + desc;
113
114 if (fData.find(fDescription+'\n')!=std::string::npos)
115 return;
116
117 fData += fDescription + '\n';
118
119 const Time t;
120 fService->setTimestamp(t.Time_t(), t.ms());
121 fService->setData(const_cast<char*>(fData.c_str()));
122 fService->updateService();
123}
124
125
126// --------------------------------------------------------------------------
127//
128//! If fDescription is found in fData it is removed from fData.
129//! The counter fCount is decreased and fService deleted if the counter
130//! reached 0.
131//
132DimDescriptionService::~DimDescriptionService()
133{
134 const size_t pos = fData.find(fDescription+'\n');
135 if (pos!=std::string::npos)
136 fData.replace(pos, fDescription.size()+1, "");
137
138 if (--fCount>0)
139 return;
140
141 delete fService;
142 fService=0;
143}
144
145void DimDescribedService::setTime(const Time &t)
146{
147 setTimestamp(t.Time_t(), t.ms());
148}
149
150void DimDescribedService::setTime()
151{
152 setTime(Time());
153}
154
155int DimDescribedService::Update(const Time &t)
156{
157 setTime(t);
158 return updateService();
159}
160
161int DimDescribedService::Update()
162{
163 return Update(Time());
164}
165
166int DimDescribedService::Update(const string &data)
167{
168 return Update(data.data());
169}
170
171int DimDescribedService::Update(const char *data)
172{
173 DimService::setData(const_cast<char*>(data));
174 return Update();
175}
Note: See TracBrowser for help on using the repository browser.