]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/smux.cpp
Code deduplication (plugin creation via template PLUGIN_CREATOR)
[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
13 #include "stg/common.h"
14 #include "stg/plugin_creator.h"
15
16 #include "smux.h"
17 #include "utils.h"
18
19 PLUGIN_CREATOR<SMUX> sac;
20
21 PLUGIN * GetPlugin()
22 {
23 return sac.GetPlugin();
24 }
25
26 SMUX_SETTINGS::SMUX_SETTINGS()
27     : ip(0),
28       port(0)
29 {}
30
31 int SMUX_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
32 {
33 PARAM_VALUE pv;
34 std::vector<PARAM_VALUE>::const_iterator pvi;
35 int p;
36 ///////////////////////////
37 pv.param = "Port";
38 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
39 if (pvi == s.moduleParams.end())
40     {
41     errorStr = "Parameter \'Port\' not found.";
42     printfd(__FILE__, "Parameter 'Port' not found\n");
43     return -1;
44     }
45 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
46     {
47     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
48     printfd(__FILE__, "Cannot parse parameter 'Port'\n");
49     return -1;
50     }
51 port = p;
52
53 pv.param = "Password";
54 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
55 if (pvi == s.moduleParams.end())
56     {
57     errorStr = "Parameter \'Password\' not found.";
58     printfd(__FILE__, "Parameter 'Password' not found\n");
59     password = "";
60     }
61 else
62     {
63     password = pvi->value[0];
64     }
65
66 pv.param = "Server";
67 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
68 if (pvi == s.moduleParams.end())
69     {
70     errorStr = "Parameter \'Server\' not found.";
71     printfd(__FILE__, "Parameter 'Server' not found\n");
72     return -1;
73     }
74 ip = inet_strington(pvi->value[0]);
75
76 return 0;
77 }
78
79 SMUX::SMUX()
80     : PLUGIN(),
81       users(NULL),
82       tariffs(NULL),
83       running(false),
84       stopped(true),
85       sock(-1)
86 {
87 pthread_mutex_init(&mutex, NULL);
88
89 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
90 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
91 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
92 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
93
94 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
95 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
96 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
97 }
98
99 SMUX::~SMUX()
100 {
101 Sensors::iterator it;
102 for (it = sensors.begin(); it != sensors.end(); ++it)
103     delete it->second;
104 printfd(__FILE__, "SMUX::~SMUX()\n");
105 pthread_mutex_destroy(&mutex);
106 }
107
108 int SMUX::ParseSettings()
109 {
110 return smuxSettings.ParseSettings(settings);
111 }
112
113 int SMUX::Start()
114 {
115 if (PrepareNet())
116     return -1;
117
118 // Users
119 sensors[".1.3.6.1.4.1.38313.1.1.1"] = new TotalUsersSensor(*users);
120 sensors[".1.3.6.1.4.1.38313.1.1.2"] = new ConnectedUsersSensor(*users);
121 sensors[".1.3.6.1.4.1.38313.1.1.3"] = new AuthorizedUsersSensor(*users);
122 sensors[".1.3.6.1.4.1.38313.1.1.4"] = new AlwaysOnlineUsersSensor(*users);
123 sensors[".1.3.6.1.4.1.38313.1.1.5"] = new NoCashUsersSensor(*users);
124 sensors[".1.3.6.1.4.1.38313.1.1.7"] = new DisabledDetailStatsUsersSensor(*users);
125 sensors[".1.3.6.1.4.1.38313.1.1.8"] = new DisabledUsersSensor(*users);
126 sensors[".1.3.6.1.4.1.38313.1.1.9"] = new PassiveUsersSensor(*users);
127 sensors[".1.3.6.1.4.1.38313.1.1.10"] = new CreditUsersSensor(*users);
128 sensors[".1.3.6.1.4.1.38313.1.1.11"] = new FreeMbUsersSensor(*users);
129 sensors[".1.3.6.1.4.1.38313.1.1.12"] = new TariffChangeUsersSensor(*users);
130 // Tariffs
131 sensors[".1.3.6.1.4.1.38313.1.2.1"] = new TotalTariffsSensor(*tariffs);
132
133 if (!running)
134     {
135     if (pthread_create(&thread, NULL, Runner, this))
136         {
137         errorStr = "Cannot create thread.";
138         printfd(__FILE__, "Cannot create thread\n");
139         return -1;
140         }
141     }
142
143 return 0;
144 }
145
146 int SMUX::Stop()
147 {
148 printfd(__FILE__, "SMUX::Stop() - Before\n");
149 running = false;
150
151 if (!stopped)
152     {
153     //5 seconds to thread stops itself
154     for (int i = 0; i < 25 && !stopped; i++)
155         {
156         struct timespec ts = {0, 200000000};
157         nanosleep(&ts, NULL);
158         }
159
160     //after 5 seconds waiting thread still running. now killing it
161     if (!stopped)
162         {
163         printfd(__FILE__, "SMUX::Stop() - failed to stop thread, killing it\n");
164         if (pthread_kill(thread, SIGINT))
165             {
166             errorStr = "Cannot kill thread.";
167             printfd(__FILE__, "SMUX::Stop() - Cannot kill thread\n");
168             return -1;
169             }
170         printfd(__FILE__, "SMUX::Stop() -  killed Run\n");
171         }
172     }
173
174 pthread_join(thread, NULL);
175
176 close(sock);
177
178 printfd(__FILE__, "SMUX::Stop() - After\n");
179 return 0;
180 }
181
182 void * SMUX::Runner(void * d)
183 {
184 SMUX * smux = static_cast<SMUX *>(d);
185
186 smux->Run();
187
188 return NULL;
189 }
190
191 void SMUX::Run()
192 {
193 SendOpenPDU(sock);
194 SendRReqPDU(sock);
195 running = true;
196 stopped = false;
197 while(running)
198     {
199     if (WaitPackets(sock))
200         {
201         SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
202         if (pdus)
203             DispatchPDUs(pdus);
204         }
205     if (!running)
206         break;
207     }
208 SendClosePDU(sock);
209 stopped = true;
210 }
211
212 bool SMUX::PrepareNet()
213 {
214 sock = socket(AF_INET, SOCK_STREAM, 0);
215
216 if (sock < 0)
217     {
218     errorStr = "Cannot create socket.";
219     printfd(__FILE__, "Cannot create socket\n");
220     return true;
221     }
222
223 struct sockaddr_in addr;
224
225 addr.sin_family = AF_INET;
226 addr.sin_port = htons(smuxSettings.GetPort());
227 addr.sin_addr.s_addr = smuxSettings.GetIP();
228
229 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
230     {
231     errorStr = "Cannot connect.";
232     printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
233     return true;
234     }
235
236 return false;
237 }
238
239 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
240 {
241 SMUXHandlers::iterator it;
242 it = smuxHandlers.find(pdus->present);
243 if (it != smuxHandlers.end())
244     {
245     return (this->*(it->second))(pdus);
246     }
247 else
248     {
249     switch (pdus->present)
250         {
251         case SMUX_PDUs_PR_NOTHING:
252             printfd(__FILE__, "PDUs: nothing\n");
253             break;
254         case SMUX_PDUs_PR_open:
255             printfd(__FILE__, "PDUs: open\n");
256             break;
257         case SMUX_PDUs_PR_registerRequest:
258             printfd(__FILE__, "PDUs: registerRequest\n");
259             break;
260         default:
261             printfd(__FILE__, "PDUs: undefined\n");
262         }
263     asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
264     }
265 return false;
266 }