1 | // **************************************************************************
|
---|
2 | /** @class DimDescriptionService
|
---|
3 |
|
---|
4 | @brief A DimService which broadcasts descriptions for services and commands
|
---|
5 |
|
---|
6 | The DimDescriptionService creates a service with the name of the server like
|
---|
7 | SERVER/SERVICE_DESC. This is meant in addition to the SERVICE_LIST service
|
---|
8 | of each node to contain a description of the service and its arguments.
|
---|
9 |
|
---|
10 | Assume you have created a service (or command) with the format I:2;F:1
|
---|
11 | a valid description string would look like
|
---|
12 |
|
---|
13 | Description|int[addr]:Address range (from - to)|val[byte]:Value to be set
|
---|
14 |
|
---|
15 | Description is a general description of the command or service itself,
|
---|
16 | int and val are the names of the arguments (e.g. names of FITS columns),
|
---|
17 | addr and byte have the meaning of a unit (e.g. unit of FITS column)
|
---|
18 | and 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
|
---|
20 | line-break character \n.
|
---|
21 |
|
---|
22 | You can omit either the name, the unit or the comment or any combination of them.
|
---|
23 | The descriptions of the individual format strings are separated by a vertical line.
|
---|
24 |
|
---|
25 | The 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 |
|
---|
40 | using namespace std;
|
---|
41 |
|
---|
42 | DimService *DimDescriptionService::fService = 0;
|
---|
43 | int DimDescriptionService::fCount = 0;
|
---|
44 | std::string DimDescriptionService::fData = "";
|
---|
45 |
|
---|
46 | // --------------------------------------------------------------------------
|
---|
47 | //
|
---|
48 | //! When the constructor is first called, a service with the name
|
---|
49 | //! SERVER/SERVICE_DESC is created. The server name SERVER is retrieved
|
---|
50 | //! from DimServer::itsName. If DimServer::itsName is empty, the
|
---|
51 | //! server name is extracted from the given name as the part before the
|
---|
52 | //! first '/'. A string "name=format\n" is added to fData and stored
|
---|
53 | //! in fDescription.
|
---|
54 | //!
|
---|
55 | //! A counter fCount for the number of instantiations is increased.
|
---|
56 | //!
|
---|
57 | //! @param name
|
---|
58 | //! The name of the service or command to be described, e.g. SERVER/COMMAND
|
---|
59 | //!
|
---|
60 | //! @param desc
|
---|
61 | //! A description string. For details see class reference
|
---|
62 | //!
|
---|
63 | //! @throws
|
---|
64 | //! If a server name couldn't be reliably determined a logic_error
|
---|
65 | //! exception is thrown; if the given description contains a '\n'
|
---|
66 | //! also a logic_error is thrown.
|
---|
67 | //
|
---|
68 | DimDescriptionService::DimDescriptionService(const std::string &name, const std::string &desc)
|
---|
69 | {
|
---|
70 | string server = DimServer::itsName ? DimServer::itsName : "";
|
---|
71 | if (server.empty())
|
---|
72 | {
|
---|
73 | const size_t p = name.find_first_of('/');
|
---|
74 | if (p==string::npos)
|
---|
75 | throw logic_error("Could not determine server name");
|
---|
76 |
|
---|
77 | server = name.substr(0, p);
|
---|
78 | }
|
---|
79 |
|
---|
80 | if (desc.find_first_of('\n')!=string::npos)
|
---|
81 | throw logic_error("Description for "+name+" contains '\\n'");
|
---|
82 |
|
---|
83 | if (!fService)
|
---|
84 | {
|
---|
85 | fService = new DimService((server+"/SERVICE_DESC").c_str(), const_cast<char*>(""));
|
---|
86 | fData =
|
---|
87 | server + "/SERVICE_DESC"
|
---|
88 | "=This service holds a descriptions for services or command "
|
---|
89 | "and there arguments|Description[string]:For a detailed "
|
---|
90 | "explanation of the descriptive string see the class reference "
|
---|
91 | "of DimDescriptionService.\n" +
|
---|
92 | server + "/CLIENT_LIST"
|
---|
93 | "=This is a native Dim service. It contains a list "
|
---|
94 | "of all clients currently connected.\n" +
|
---|
95 | server + "/VERSION_NUMBER"
|
---|
96 | "=This is a native Dim service. It contains the version number "
|
---|
97 | "of Dim in use and can be used to check if a client is connected."
|
---|
98 | "|DimVer[int]:Version*100+Release (e.g. V19r17 = 1917)\n" +
|
---|
99 | server + "/EXIT"
|
---|
100 | "=This is a native Dim service. It allows to exit the program "
|
---|
101 | "remotely. FACT++ programs use the given number as return code."
|
---|
102 | "|Rc[int]:Return code (42 will call exit() directly, 0x4242 will call abort() directly.\n" +
|
---|
103 | server + "/SERVICE_LIST"
|
---|
104 | "=This is a native Dim service. It provides a list of all "
|
---|
105 | "commands and services together with their formats currently "
|
---|
106 | "provided by the server."
|
---|
107 | "|ServiceList[string]:For details see the Dim manual.\n";
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | fCount++;
|
---|
112 |
|
---|
113 | fDescription = name + '=' + desc;
|
---|
114 |
|
---|
115 | if (fData.find(fDescription+'\n')!=std::string::npos)
|
---|
116 | return;
|
---|
117 |
|
---|
118 | fData += fDescription + '\n';
|
---|
119 |
|
---|
120 | fService->setData(const_cast<char*>(fData.c_str()));
|
---|
121 | fService->updateService();
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | // --------------------------------------------------------------------------
|
---|
126 | //
|
---|
127 | //! If fDescription is found in fData it is removed from fData.
|
---|
128 | //! The counter fCount is decreased and fService deleted if the counter
|
---|
129 | //! reached 0.
|
---|
130 | //
|
---|
131 | DimDescriptionService::~DimDescriptionService()
|
---|
132 | {
|
---|
133 | const size_t pos = fData.find(fDescription+'\n');
|
---|
134 | if (pos!=std::string::npos)
|
---|
135 | fData.replace(pos, fDescription.size()+1, "");
|
---|
136 |
|
---|
137 | if (--fCount>0)
|
---|
138 | return;
|
---|
139 |
|
---|
140 | delete fService;
|
---|
141 | fService=0;
|
---|
142 | }
|
---|