[292045]: switchview / events.cpp  Maximize  Restore  History

Download this file

340 lines (285 with data), 8.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Copyright (C) 2011 David Sugar, Haakon Eriksen, GNU Free Call Foundation
//
// This file is part of SwitchView.
//
// SwitchView is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SwitchView is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SwitchView. If not, see <http://www.gnu.org/licenses/>.
#include "switchview.h"
#if !defined(_MSWINDOWS_)
#include <sys/un.h>
#endif
using namespace UCOMMON_NAMESPACE;
using namespace SIPWITCH_NAMESPACE;
#ifdef _MSWINDWS_
#define CONTROL_PATH "\\\\.\\mailslot\\sipwitch_ctrl"
#else
#define CONTROL_PATH "run/sipwitch.ctrl"
#endif
Events *dispatcher = NULL;
callmap_t *mapped_calls = NULL;
statmap_t *mapped_stats = NULL;
usermap_t *mapped_users = NULL;
static timedevent_t notify;
static bool stopped = false;
static bool started = false;
static string_t callmap = CALL_MAP;
static string_t usermap = REGISTRY_MAP;
static string_t statmap = STAT_MAP;
static string_t contact = "-";
static string_t publish = "-";
static socket_t ipc = INVALID_SOCKET;
#ifdef _MSWINDOWS_
static struct sockaddr_in ipcaddr;
#else
static struct sockaddr_un ipcaddr;
#endif
static events *msgdup(events *msg)
{
events *buf = new events;
memcpy(buf, msg, sizeof(events));
return buf;
}
static bool remap(void)
{
Thread::yield();
if(mapped_calls)
delete mapped_calls;
if(mapped_users)
delete mapped_users;
if(mapped_stats)
delete mapped_stats;
mapped_calls = new callmap_t(*callmap);
mapped_users = new usermap_t(*usermap);
mapped_stats = new statmap_t(*statmap);
if(mapped_calls && mapped_users && mapped_stats)
return true;
return false;
}
static bool server_control(const char *cmd)
{
fsys_t fs;
string_t path = shell::path(shell::SYSTEM_VAR, CONTROL_PATH);
fs.open(*path, fsys::WRONLY);
#if !defined(_MSWINDOWS_)
if(!is(fs)) {
string_t path = str("/tmp/sipwitch-") + shell::userid();
fs.open(path, fsys::WRONLY);
}
#endif
if(!is(fs))
return false;
fs.write(cmd, strlen(cmd));
fs.close();
return true;
}
Events::Events() :
QObject(), JoinableThread()
{
Q_ASSERT(dispatcher == NULL);
dispatcher = this;
connect(this, SIGNAL(serverOnlineSignal()),
switchview, SLOT(online()), Qt::QueuedConnection);
connect(this, SIGNAL(serverOfflineSignal()),
switchview, SLOT(offline()), Qt::QueuedConnection);
}
Events::~Events()
{
}
void Events::start(void)
{
new Events();
dispatcher->JoinableThread::start();
}
void Events::stop(void)
{
stopped = true;
Socket::release(ipc);
ipc = INVALID_SOCKET;
notify.signal();
delete dispatcher;
dispatcher = NULL;
}
bool Events::dispatch(events *msg)
{
struct tm *dt;
char buf[128];
switch(msg->type) {
case events::WELCOME:
started = true;
dt = localtime(&msg->server.started);
strftime(buf, sizeof(buf), "%c", dt);
emit serverOnlineSignal();
emit stateSignal(strdup(msg->server.state), strdup(buf));
emit realmSignal(strdup(msg->server.realm));
return true;
case events::CONTACT:
// only send for contact changes...
if(!eq(contact, msg->contact)) {
contact ^= msg->contact;
emit contactSignal(contact.c_mem());
Thread::yield();
}
return true;
case events::PUBLISH:
// only send for contact changes...
if(!eq(publish, msg->contact)) {
publish ^= msg->contact;
emit publishSignal(publish.c_mem());
Thread::yield();
}
return true;
case events::STATE:
emit stateSignal(strdup(msg->server.state), NULL);
return true;
case events::REALM:
emit realmSignal(strdup(msg->server.realm));
return true;
case events::NOTICE:
case events::WARNING:
case events::FAILURE:
emit notifySignal(msgdup(msg));
return true;
case events::TERMINATE:
emit notifySignal(msgdup(msg));
emit stateSignal(strdup("down"), strdup("offline"));
return false;
case events::CALL:
emit callSignal(msgdup(msg));
return true;
case events::DROP:
emit dropSignal(msgdup(msg));
return true;
case events::ACTIVATE:
emit activateSignal(msgdup(msg));
return true;
case events::RELEASE:
emit releaseSignal(msgdup(msg));
return true;
default:
return true;
}
}
void Events::run(void)
{
timeout_t reconnect_timeout = reconnect();
notify.reset();
Thread::yield();
for(;;) {
event_t event;
string_t path = shell::path(shell::SYSTEM_VAR, "run/sipwitch/events");
if(ipc == INVALID_SOCKET && !stopped) {
callmap = CALL_MAP;
usermap = REGISTRY_MAP;
statmap = STAT_MAP;
memset(&ipcaddr, 0, sizeof(ipcaddr));
#ifdef _MSWINDOWS_
DWORD port = 0;
DWORD index = 0;
TCHAR keyname[128];
TCHAR keyvalue[128];
DWORD size = sizeof(keyname), vsize = sizeof(keyvalue), vtype;
DWORD *dp;
HKEY keys = HKEY_LOCAL_MACHINE, subkey;
if(RegOpenKeyEx(keys, "SOFTWARE\\sipwitch", 0, KEY_READ, &subkey) != ERROR_SUCCESS)
goto drop;
while((RegEnumValue(subkey, index++, keyname, &size, NULL, &vtype, (BYTE *)keyvalue, &vsize) == ERROR_SUCCESS) && (vtype == REG_DWORD) && (keyname[0] != 0)) {
dp = (DWORD *)&keyvalue;
if(eq("port", keyname))
port = *dp;
vsize = sizeof(keyvalue);
size = sizeof(keyname);
}
RegCloseKey(subkey);
if(!port)
goto drop;
ipc = ::socket(AF_INET, SOCK_STREAM, 0);
ipcaddr.sin_family = AF_INET;
ipcaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
ipcaddr.sin_port = htons((unsigned short)port);
if(::connect(ipc, (struct sockaddr *)&ipcaddr, sizeof(ipcaddr)) < 0)
goto drop;
#else
ipc = ::socket(AF_UNIX, SOCK_STREAM, 0);
memset(&ipcaddr, 0, sizeof(ipcaddr));
ipcaddr.sun_family = AF_UNIX;
String::set(ipcaddr.sun_path, sizeof(ipcaddr.sun_path), *path);
if(::connect(ipc, (struct sockaddr *)&ipcaddr, SUN_LEN(&ipcaddr)) < 0) {
memset(&ipcaddr, 0, sizeof(ipcaddr));
ipcaddr.sun_family = AF_UNIX;
callmap = str(CALL_MAP "-") + shell::userid();
statmap = str(STAT_MAP "-") + shell::userid();
usermap = str(REGISTRY_MAP "-") + shell::userid();
string_t path = str("/tmp/sipwitch-") + shell::userid() + str("/events");
String::set(ipcaddr.sun_path, sizeof(ipcaddr.sun_path), *path);
if(::connect(ipc, (struct sockaddr *)&ipcaddr, SUN_LEN(&ipcaddr)) < 0)
goto drop;
}
#endif
}
if(ipc == INVALID_SOCKET)
goto drop;
if(!remap())
goto drop;
while(::recv(ipc, (char *)&event, sizeof(event), 0) == sizeof(event)) {
if(!dispatch(&event))
break;
}
drop:
if(ipc != INVALID_SOCKET)
Socket::release(ipc);
ipc = INVALID_SOCKET;
if(stopped)
return;
if(started) {
emit stateSignal(strdup("down"), strdup("offline"));
emit serverOfflineSignal();
}
timeout_t timeout = reconnect_timeout;
if(!timeout)
timeout = 60000;
while((notify.wait(timeout) == false) && !reconnect_timeout)
;
}
}
bool Events::reconnect(void)
{
if(!dispatcher)
return false;
if(ipc == INVALID_SOCKET)
return false;
notify.signal();
return true;
}
void Events::reload(void)
{
server_control("reload\n");
}
void Events::changeRealm(void)
{
char buf[256];
snprintf(buf, sizeof(buf), "realm %s\n", mapped->realm());
server_control(buf);
}
void Events::changeState(void)
{
char buf[256];
snprintf(buf, sizeof(buf), "state %s\n", mapped->realm());
server_control(buf);
}
bool Events::isAdmin(void)
{
string_t path = shell::path(shell::SYSTEM_VAR, CONTROL_PATH);
if(fsys::is_writable(*path))
return true;
return false;
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks