]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/smux.cpp
Tables update implemented
[stg.git] / projects / stargazer / plugins / other / smux / smux.cpp
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <arpa/inet.h>
4
5 #include <cstring>
6 #include <cerrno>
7 #include <ctime>
8 #include <csignal>
9
10 #include <vector>
11 #include <algorithm>
12 #include <iterator>
13 #include <stdexcept>
14 #include <utility>
15
16 #include "stg/common.h"
17 #include "stg/plugin_creator.h"
18
19 #include "smux.h"
20 #include "utils.h"
21
22 PLUGIN_CREATOR<SMUX> smc;
23
24 PLUGIN * GetPlugin()
25 {
26 return smc.GetPlugin();
27 }
28
29 bool SPrefixLess(const Sensors::value_type & a,
30                  const Sensors::value_type & b)
31 {
32 return a.first.PrefixLess(b.first);
33 }
34
35 SMUX_SETTINGS::SMUX_SETTINGS()
36     : ip(0),
37       port(0)
38 {}
39
40 int SMUX_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
41 {
42 PARAM_VALUE pv;
43 std::vector<PARAM_VALUE>::const_iterator pvi;
44 int p;
45
46 pv.param = "Port";
47 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
48 if (pvi == s.moduleParams.end())
49     {
50     errorStr = "Parameter \'Port\' not found.";
51     printfd(__FILE__, "Parameter 'Port' not found\n");
52     return -1;
53     }
54 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
55     {
56     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
57     printfd(__FILE__, "Cannot parse parameter 'Port'\n");
58     return -1;
59     }
60 port = p;
61
62 pv.param = "Password";
63 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
64 if (pvi == s.moduleParams.end())
65     {
66     errorStr = "Parameter \'Password\' not found.";
67     printfd(__FILE__, "Parameter 'Password' not found\n");
68     password = "";
69     }
70 else
71     {
72     password = pvi->value[0];
73     }
74
75 pv.param = "Server";
76 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
77 if (pvi == s.moduleParams.end())
78     {
79     errorStr = "Parameter \'Server\' not found.";
80     printfd(__FILE__, "Parameter 'Server' not found\n");
81     return -1;
82     }
83 ip = inet_strington(pvi->value[0]);
84
85 return 0;
86 }
87
88 SMUX::SMUX()
89     : PLUGIN(),
90       users(NULL),
91       tariffs(NULL),
92       running(false),
93       stopped(true),
94       sock(-1)
95 {
96 pthread_mutex_init(&mutex, NULL);
97
98 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
99 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
100 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
101 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
102
103 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
104 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
105 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
106 }
107
108 SMUX::~SMUX()
109 {
110     {
111     Sensors::iterator it;
112     for (it = sensors.begin(); it != sensors.end(); ++it)
113         delete it->second;
114     }
115     {
116     Tables::iterator it;
117     for (it = tables.begin(); it != tables.end(); ++it)
118         delete it->second;
119     }
120 printfd(__FILE__, "SMUX::~SMUX()\n");
121 pthread_mutex_destroy(&mutex);
122 }
123
124 int SMUX::ParseSettings()
125 {
126 return smuxSettings.ParseSettings(settings);
127 }
128
129 int SMUX::Start()
130 {
131 if (PrepareNet())
132     return -1;
133
134 // Users
135 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
136 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
137 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
138 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
139 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
140 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledDetailStatsUsersSensor(*users);
141 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new DisabledUsersSensor(*users);
142 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new PassiveUsersSensor(*users);
143 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new CreditUsersSensor(*users);
144 sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new FreeMbUsersSensor(*users);
145 sensors[OID(".1.3.6.1.4.1.38313.1.1.12")] = new TariffChangeUsersSensor(*users);
146 // Tariffs
147 sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs);
148
149 // Table data
150 tables[".1.3.6.1.4.1.38313.1.1.6"] = new TariffUsersTable(".1.3.6.1.4.1.38313.1.1.6", *users);
151
152 UpdateTables();
153
154 #ifdef DEBUG
155 Sensors::const_iterator it(sensors.begin());
156 while (it != sensors.end())
157     {
158     printfd(__FILE__, "%s = %s\n",
159             it->first.ToString().c_str(),
160             it->second->ToString().c_str());
161     ++it;
162     }
163 #endif
164
165 if (!running)
166     {
167     if (pthread_create(&thread, NULL, Runner, this))
168         {
169         errorStr = "Cannot create thread.";
170         printfd(__FILE__, "Cannot create thread\n");
171         return -1;
172         }
173     }
174
175 return 0;
176 }
177
178 int SMUX::Stop()
179 {
180 printfd(__FILE__, "SMUX::Stop() - Before\n");
181 running = false;
182
183 if (!stopped)
184     {
185     //5 seconds to thread stops itself
186     for (int i = 0; i < 25 && !stopped; i++)
187         {
188         struct timespec ts = {0, 200000000};
189         nanosleep(&ts, NULL);
190         }
191
192     //after 5 seconds waiting thread still running. now killing it
193     if (!stopped)
194         {
195         printfd(__FILE__, "SMUX::Stop() - failed to stop thread, killing it\n");
196         if (pthread_kill(thread, SIGINT))
197             {
198             errorStr = "Cannot kill thread.";
199             printfd(__FILE__, "SMUX::Stop() - Cannot kill thread\n");
200             return -1;
201             }
202         printfd(__FILE__, "SMUX::Stop() -  killed Run\n");
203         }
204     }
205
206 pthread_join(thread, NULL);
207
208 close(sock);
209
210 printfd(__FILE__, "SMUX::Stop() - After\n");
211 return 0;
212 }
213
214 void * SMUX::Runner(void * d)
215 {
216 SMUX * smux = static_cast<SMUX *>(d);
217
218 smux->Run();
219
220 return NULL;
221 }
222
223 void SMUX::Run()
224 {
225 SendOpenPDU(sock);
226 SendRReqPDU(sock);
227 running = true;
228 stopped = false;
229 while(running)
230     {
231     if (WaitPackets(sock))
232         {
233         SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
234         if (pdus)
235             DispatchPDUs(pdus);
236         }
237     if (!running)
238         break;
239     }
240 SendClosePDU(sock);
241 stopped = true;
242 }
243
244 bool SMUX::PrepareNet()
245 {
246 sock = socket(AF_INET, SOCK_STREAM, 0);
247
248 if (sock < 0)
249     {
250     errorStr = "Cannot create socket.";
251     printfd(__FILE__, "Cannot create socket\n");
252     return true;
253     }
254
255 struct sockaddr_in addr;
256
257 addr.sin_family = AF_INET;
258 addr.sin_port = htons(smuxSettings.GetPort());
259 addr.sin_addr.s_addr = smuxSettings.GetIP();
260
261 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
262     {
263     errorStr = "Cannot connect.";
264     printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
265     return true;
266     }
267
268 return false;
269 }
270
271 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
272 {
273 SMUXHandlers::iterator it;
274 it = smuxHandlers.find(pdus->present);
275 if (it != smuxHandlers.end())
276     {
277     return (this->*(it->second))(pdus);
278     }
279 else
280     {
281     switch (pdus->present)
282         {
283         case SMUX_PDUs_PR_NOTHING:
284             printfd(__FILE__, "PDUs: nothing\n");
285             break;
286         case SMUX_PDUs_PR_open:
287             printfd(__FILE__, "PDUs: open\n");
288             break;
289         case SMUX_PDUs_PR_registerRequest:
290             printfd(__FILE__, "PDUs: registerRequest\n");
291             break;
292         default:
293             printfd(__FILE__, "PDUs: undefined\n");
294         }
295     asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
296     }
297 return false;
298 }
299
300 bool SMUX::UpdateTables()
301 {
302 Sensors newSensors;
303 bool done = true;
304 Tables::iterator it(tables.begin());
305 while (it != tables.end())
306     {
307     try
308         {
309         it->second->UpdateSensors(newSensors);
310         }
311     catch (const std::runtime_error & ex)
312         {
313         printfd(__FILE__,
314                 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
315                 it->first.c_str(), ex.what());
316         done = false;
317         break;
318         }
319     ++it;
320     }
321 if (!done)
322     {
323     Sensors::iterator it(newSensors.begin());
324     while (it != newSensors.end())
325         {
326         delete it->second;
327         ++it;
328         }
329     return false;
330     }
331
332 it = tables.begin();
333 while (it != tables.end())
334     {
335     std::pair<Sensors::iterator, Sensors::iterator> res;
336     res = std::equal_range(sensors.begin(),
337                            sensors.end(),
338                            std::pair<OID, Sensor *>(OID(it->first), NULL),
339                            SPrefixLess);
340     Sensors::iterator sit(res.first);
341     while (sit != res.second)
342         {
343         delete sit->second;
344         ++sit;
345         }
346     sensors.erase(res.first, res.second);
347     ++it;
348     }
349
350 sensors.insert(newSensors.begin(), newSensors.end());
351
352 return true;
353 }