Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[aacde3]: inc / sipwitch / service.h Maximize Restore History

Download this file

service.h    271 lines (211 with data), 7.3 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
// Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
//
// This program 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.
//
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
/**
* Service configuration and component callbacks.
* The service class offers tree based configuration subsystem that is
* parsed from XML configuration files. The service class also offers a
* callback bus for attaching server components and for controlling
* component startup and shutdown. This service bus can be extended through
* plugins as well as local objects in your server, all of which derive from
* the callback member class of service. Other features include support of
* snapshot dumps and checking functions to determine state of running
* servers.
* @file sipwitch/service.h
*/
#ifndef _SIPWITCH_SERVICE_H_
#define _SIPWITCH_SERVICE_H_
#ifndef _UCOMMON_LINKED_H_
#include <ucommon/linked.h>
#endif
#ifndef _UCOMMON_THREAD_H_
#include <ucommon/thread.h>
#endif
#ifndef _UCOMMON_STRING_H_
#include <ucommon/string.h>
#endif
#ifndef _UCOMMON_FSYS_H_
#include <ucommon/fsys.h>
#endif
#ifndef _SIPWITCH_NAMESPACE_H_
#include <sipwitch/namespace.h>
#endif
#ifndef _SIPWITCH_MAPPED_H_
#include <sipwitch/mapped.h>
#endif
#ifndef _SIPWITCH_PROCESS_H_
#include <sipwitch/process.h>
#endif
#ifndef _SIPWITCH_CDR_H_
#include <sipwitch/cdr.h>
#endif
#define CONFIG_KEY_SIZE 177
NAMESPACE_SIPWITCH
using namespace UCOMMON_NAMESPACE;
class __EXPORT service : public memalloc
{
public:
typedef treemap<char *>keynode;
typedef enum {EXT_DIALING, USER_DIALING, ALL_DIALING} dialmode_t;
typedef struct {
const char *key;
const char *value;
} define;
class __EXPORT keyclone : public treemap<char *>
{
public:
void splice(keyclone *trunk);
inline void reset(const char *tag)
{id = (char *)tag;};
};
class __EXPORT usernode
{
public:
service::keynode *keys;
service *heap;
usernode();
};
class __EXPORT pointer
{
private:
keynode *node;
public:
pointer();
pointer(const char *path);
pointer(pointer const&);
~pointer();
inline operator bool() const
{return node != NULL;};
inline bool operator!() const
{return node == NULL;};
void operator=(keynode *node);
inline keynode *operator*() const
{return node;};
inline keynode *operator->() const
{return node;};
};
class __EXPORT instance
{
private:
int state;
public:
instance();
~instance();
inline const service *operator->() const
{return service::cfg;};
};
class __EXPORT callback : public OrderedObject
{
protected:
friend class service;
friend class modules;
unsigned runlevel;
bool active_flag;
static LinkedObject *runlevels[4];
static unsigned count;
static unsigned short sip_port;
static int sip_protocol;
static int sip_family;
static int sip_tlsmode;
static const char *sip_domain;
static const char *sip_realm;
static unsigned sip_prefix, sip_range;
static char session_uuid[40];
callback(int level = 0);
virtual ~callback();
inline static void *alloc(service *cfgp, size_t size)
{return cfgp->alloc(size);};
inline static char *dup(service *cfgp, const char *s)
{return cfgp->dup(s);};
inline static bool isConfigured(void)
{return service::cfg != NULL;};
inline bool isActive(void) const
{return active_flag;};
virtual void cdrlog(cdr *call);
virtual void errlog(shell::loglevel_t level, const char *text);
virtual bool check(void);
virtual void snapshot(FILE *fp);
virtual void start(service *cfg);
virtual void stop(service *cfg);
virtual void reload(service *cfg);
virtual void publish(service *cfg);
};
service(const char *name, size_t s = 0);
virtual ~service();
static volatile dialmode_t dialmode;
bool load(FILE *fp, keynode *node = NULL);
keynode *getPath(const char *path);
keynode *getNode(keynode *base, const char *id, const char *value);
keynode *addNode(keynode *base, define *defs);
keynode *addNode(keynode *base, const char *id, const char *value);
keynode *getNode(keynode *base, const char *grp, const char *attr, const char *value);
keynode *getList(const char *path);
inline static LinkedObject *getModules(void)
{return service::callback::runlevels[3];};
inline static bool isLinked(keynode *node)
{return node->isLeaf();};
inline static bool isValue(keynode *node)
{return (node->getPointer() != NULL);};
inline static bool isUndefined(keynode *node)
{return !isLinked(node) && !isValue(node);};
inline static bool isNode(keynode *node)
{return isLinked(node) && isValue(node);};
static void publish(const char *addr);
static void published(struct sockaddr_storage *peer);
static const char *getValue(keynode *base, const char *id);
static void dump(FILE *fp, keynode *node, unsigned level);
static void snapshot(void);
static void dumpfile(void);
static bool period(long slice);
static void result(const char *value);
static void startup(void);
static void shutdown(void);
static long uptime(void);
static bool match(const char *digits, const char *pattern, bool partial);
static keynode *get(void);
static keynode *getProtected(const char *path);
static keynode *getUser(const char *uid);
static keynode *path(const char *p);
static keynode *list(const char *p);
inline static keynode *getEnviron(void)
{return getProtected("environ");};
inline keynode *getRoot(void)
{return &root;};
virtual void dump(FILE *fp);
virtual void confirm(void);
void commit(void);
static bool check(void);
static void release(keynode *node);
protected:
friend class instance;
class __LOCAL keymap : public LinkedObject
{
public:
service::keynode *node;
const char *id;
};
keynode root;
stringbuf<1024> buffer;
LinkedObject *keys[CONFIG_KEY_SIZE];
static service *cfg;
static condlock_t locking;
void setHeader(const char *header);
void clearId(void);
void addAttributes(keynode *node, char *astr);
};
#define RUNLEVELS (sizeof(callback::runlevels) / sizeof(LinkedObject *))
#define MODULE_RUNLEVEL (RUNLEVELS - 1)
#define GENERIC_RUNLEVEL (RUNLEVELS - 2)
END_NAMESPACE
#endif