1 | /********************************************************************\ |
---|
2 | |
---|
3 | General code to start a server of the Evidence Control System |
---|
4 | |
---|
5 | - The server is started with the given name. |
---|
6 | - DIM exit and error handlers are implemented. |
---|
7 | - The Status service is published (special format, see below). |
---|
8 | It can be updated with the State() method. The text will also be logged. |
---|
9 | - If the severity of a State() call is FATAL, exit() will be called (with |
---|
10 | this severity, the call to State() is guranteed not to return). |
---|
11 | - Configuration data can be requested by GetConfig(). |
---|
12 | - Signal handlers to ignore common signals are installed. |
---|
13 | These signals will then cause pause() to return which can be used |
---|
14 | by the application to terminate gracefully. |
---|
15 | - The static method ToString() converts the contents of a |
---|
16 | DIMInfo service into text |
---|
17 | |
---|
18 | All memory allocated by the non-static methods will be freed by the |
---|
19 | class destructor. |
---|
20 | |
---|
21 | Oliver Grimm, December 2009 |
---|
22 | |
---|
23 | \********************************************************************/ |
---|
24 | |
---|
25 | #include "Evidence.h" |
---|
26 | |
---|
27 | EvidenceServer *ThisServer; |
---|
28 | |
---|
29 | // Constructor starts server with given name |
---|
30 | EvidenceServer::EvidenceServer(const char *Name) { |
---|
31 | |
---|
32 | // Initialize |
---|
33 | Status = NULL; |
---|
34 | ExitRequest = false; |
---|
35 | ThisServer = this; |
---|
36 | |
---|
37 | // Catch some signals |
---|
38 | signal(SIGQUIT, &SignalHandler); // CTRL-Backspace |
---|
39 | signal(SIGTERM, &SignalHandler); // Termination signal |
---|
40 | signal(SIGINT, &SignalHandler); // CTRL-C |
---|
41 | signal(SIGHUP, &SignalHandler); // Terminal closed |
---|
42 | |
---|
43 | // Catch C++ unhandled exceptions |
---|
44 | set_terminate(Terminate); |
---|
45 | |
---|
46 | // Start server |
---|
47 | static char Init[] = "Server started"; |
---|
48 | Status = new DimService((string(Name) + "/Status").c_str(), (char *) "C", Init, sizeof(Init)); |
---|
49 | |
---|
50 | start(Name); |
---|
51 | addExitHandler(this); |
---|
52 | |
---|
53 | } |
---|
54 | |
---|
55 | // Destructor: Free memory |
---|
56 | EvidenceServer::~EvidenceServer() { |
---|
57 | |
---|
58 | for (unsigned int i=0; i<ConfigList.size(); i++) { |
---|
59 | delete[] ConfigList[i].Name; |
---|
60 | delete[] ConfigList[i].Value; |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | // DIM exit handler |
---|
65 | void EvidenceServer::exitHandler(int Code) { |
---|
66 | State(INFO, "Server stopped (DIM exit code %d)", Code); |
---|
67 | exit(EXIT_SUCCESS); |
---|
68 | } |
---|
69 | |
---|
70 | // DIM error handler |
---|
71 | void EvidenceServer::errorHandler(int Severity, int Code, char *Message) { |
---|
72 | State(ERROR, "%s (DIM error code %d, severity %d)\n", Message, Code, Severity); |
---|
73 | } |
---|
74 | |
---|
75 | // Set status of server |
---|
76 | // |
---|
77 | // The message format is special: after the string-terminating '\0' the Severity |
---|
78 | // is given, terminated by another '\0' The buffer for the DIM service must have |
---|
79 | // the same lifetime as the DIM service. If Severity is FATAL, exit() will be invoked. |
---|
80 | void EvidenceServer::State(StateType Severity, const char *Format, ...) { |
---|
81 | |
---|
82 | static const char* StateString[] = {"Info", "Warn", "Error", "Fatal"}; |
---|
83 | static char ErrorString[] = "vasprintf() failed in State()"; |
---|
84 | static char SBuf[STATUS_SIZE]; |
---|
85 | char TBuf[STATUS_SIZE]; |
---|
86 | char *Tmp; |
---|
87 | |
---|
88 | // Assemble message from application |
---|
89 | va_list ArgumentPointer; |
---|
90 | va_start(ArgumentPointer, Format); |
---|
91 | if (vasprintf(&Tmp, Format, ArgumentPointer) == -1) Tmp = ErrorString; |
---|
92 | va_end(ArgumentPointer); |
---|
93 | |
---|
94 | // Create normal string |
---|
95 | snprintf(TBuf, sizeof(TBuf), "%s (%s): %s", Status->getName(), StateString[Severity], Tmp); |
---|
96 | |
---|
97 | // Create string with severity encoding |
---|
98 | snprintf(SBuf, sizeof(SBuf), "%s**", Tmp); |
---|
99 | SBuf[strlen(SBuf)-2] = '\0'; |
---|
100 | SBuf[strlen(SBuf)+1] = Severity; |
---|
101 | |
---|
102 | if (Tmp != ErrorString) free(Tmp); |
---|
103 | |
---|
104 | // Send message to console and log file |
---|
105 | printf("%s\n", TBuf); |
---|
106 | if (Severity != INFO) DimClient::sendCommand("DColl/Log", TBuf); |
---|
107 | |
---|
108 | // Update DIM status service (including severity encoding) |
---|
109 | if (Status != NULL) Status->updateService(SBuf, strlen(SBuf)+2); |
---|
110 | |
---|
111 | // Terminate if message type is fatal |
---|
112 | if (Severity == FATAL) exit(EXIT_FAILURE); |
---|
113 | } |
---|
114 | |
---|
115 | // Get configuration data (program terminates if data is missing) |
---|
116 | // |
---|
117 | // The memory allocated by all calls to this function will be freed by |
---|
118 | // the destructor. |
---|
119 | char* EvidenceServer::GetConfig(const char *Item, const char *Default) { |
---|
120 | |
---|
121 | // Determine configuration file update time |
---|
122 | DimCurrentInfo ModifyTime("Config/ModifyTime", 0); |
---|
123 | int Time = ModifyTime.getInt(), ItemNo = -1; |
---|
124 | |
---|
125 | // Check if configuration request already in list |
---|
126 | for (unsigned int i=0; i<ConfigList.size(); i++) { |
---|
127 | if (strcmp(ConfigList[i].Name, Item) == 0) { |
---|
128 | // Return original value if still up to date |
---|
129 | if (ConfigList[i].Time >= Time) return ConfigList[i].Value; |
---|
130 | |
---|
131 | // Otherwise, free memory of old value |
---|
132 | delete[] ConfigList[i].Name; |
---|
133 | delete[] ConfigList[i].Value; |
---|
134 | ItemNo = i; |
---|
135 | break; |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | // Make configuration request |
---|
140 | DimRpcInfo Config((char *) "ConfigRequest", (char *) ""); |
---|
141 | Config.setData((char *) Item); |
---|
142 | char *Result = Config.getString(); |
---|
143 | |
---|
144 | // Terminate if not successful |
---|
145 | if (strlen(Result) == 0) { |
---|
146 | if (Default == NULL) State(FATAL, "Missing configuration data '%s'", Item); |
---|
147 | Result = (char *) Default; |
---|
148 | } |
---|
149 | |
---|
150 | // Enlarge list if necessary |
---|
151 | if (ItemNo == -1) { |
---|
152 | struct ConfigItem New; |
---|
153 | ConfigList.push_back(New); |
---|
154 | ItemNo = ConfigList.size()-1; |
---|
155 | } |
---|
156 | |
---|
157 | // Create new entry in item list, allocate memory and copy data to this memory |
---|
158 | ConfigList[ItemNo].Value = new char [strlen(Result)+1]; |
---|
159 | ConfigList[ItemNo].Name = new char [strlen(Item)+1]; |
---|
160 | strcpy(ConfigList[ItemNo].Name, Item); |
---|
161 | strcpy(ConfigList[ItemNo].Value, Result); |
---|
162 | ConfigList[ItemNo].Time = Time; |
---|
163 | |
---|
164 | // Return address to configuration value |
---|
165 | return ConfigList[ItemNo].Value; |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | // ====== Static methods ====== |
---|
170 | |
---|
171 | // Signal handler (causes pause() and other syscalls to return) |
---|
172 | void EvidenceServer::SignalHandler(int) { |
---|
173 | |
---|
174 | ThisServer->ExitRequest = true; |
---|
175 | } |
---|
176 | |
---|
177 | // C++ exception handler |
---|
178 | void EvidenceServer::Terminate() { |
---|
179 | |
---|
180 | string Msg = string(ThisServer->Status->getName()) + ": Caught unhandled exception"; |
---|
181 | |
---|
182 | printf("%s\n", Msg.c_str()); |
---|
183 | DimClient::sendCommand("DColl/Log", Msg.c_str()); |
---|
184 | |
---|
185 | abort(); |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | // Translates DIMInfo to string (memory has to be freed by caller) |
---|
190 | // No DIM structures are supported (only a single number or string is converted) |
---|
191 | // For string conversion, a terminating \0 is enforced. |
---|
192 | char *EvidenceServer::ToString(DimInfo *Item) { |
---|
193 | |
---|
194 | char *Text; |
---|
195 | int R; |
---|
196 | |
---|
197 | if (strlen(Item->getFormat()) != 1) return NULL; |
---|
198 | |
---|
199 | switch (*(Item->getFormat())) { |
---|
200 | case 'I': R = asprintf(&Text, "%d", Item->getInt()); break; |
---|
201 | case 'S': R = asprintf(&Text, "%hd", Item->getShort()); break; |
---|
202 | case 'F': R = asprintf(&Text, "%.5f", Item->getFloat()); break; |
---|
203 | case 'D': R = asprintf(&Text, "%.5f", Item->getDouble()); break; |
---|
204 | case 'X': R = asprintf(&Text, "%lld", Item->getLonglong()); break; |
---|
205 | case 'C': *(Item->getString() + Item->getSize()) = '\0'; |
---|
206 | R = asprintf(&Text, "%s", Item->getString()); |
---|
207 | break; |
---|
208 | default: return NULL; |
---|
209 | } |
---|
210 | |
---|
211 | return (R == -1) ? NULL : Text; |
---|
212 | } |
---|