SIP Witch 1.9.15
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cdr.cpp
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #include <sipwitch-config.h>
18 #include <ucommon/ucommon.h>
19 #include <ucommon/export.h>
20 #include <sipwitch/cdr.h>
21 #include <sipwitch/control.h>
22 #include <sipwitch/service.h>
23 #include <sipwitch/modules.h>
24 #include <sipwitch/events.h>
25 
26 namespace sipwitch {
27 
28 class __LOCAL cdrthread : public DetachedThread, public Conditional
29 {
30 public:
31  cdrthread();
32 
33  inline void lock(void)
34  {Conditional::lock();};
35 
36  inline void unlock(void)
37  {Conditional::unlock();};
38 
39  inline void signal(void)
40  {Conditional::signal();};
41 
42 private:
43  void exit(void);
44  void run(void);
45 };
46 
47 static LinkedObject *freelist = NULL;
48 static LinkedObject *runlist = NULL;
49 static Mutex private_lock;
50 static memalloc private_heap;
51 static cdrthread run;
52 static bool running = false;
53 static bool down = false;
54 static bool logging = false;
55 
57 {
58 }
59 
60 void cdrthread::exit(void)
61 {
62 }
63 
64 void cdrthread::run(void)
65 {
66  running = true;
67  linked_pointer<cdr> cp;
68  LinkedObject *next;
69  FILE *fp;
70 
71  shell::log(DEBUG1, "starting cdr thread");
72 
73  for(;;) {
74  Conditional::lock();
75  if(!running) {
76  Conditional::unlock();
77  shell::log(DEBUG1, "stopping cdr thread");
78  down = true;
79  return;
80  }
81  Conditional::wait();
82  cp = runlist;
83  fp = NULL;
84  if(runlist && logging)
85  fp = fopen(control::env("calls"), "a");
86  runlist = NULL;
87  logging = false;
88  Conditional::unlock();
89  while(is(cp)) {
90  next = cp->getNext();
91  modules::cdrlog(fp, *cp);
92  private_lock.acquire();
93  cp->enlist(&freelist);
94  private_lock.release();
95  cp = next;
96  }
97  if(fp)
98  fclose(fp);
99  }
100 }
101 
102 void cdr::post(cdr *rec)
103 {
104  switch(rec->type) {
105  case STOP:
106  events::drop(rec);
107  break;
108  case START:
109  events::connect(rec);
110  default:
111  break;
112  }
113 
114  run.lock();
115  rec->enlist(&runlist);
116  if(rec->type == STOP)
117  logging = true;
118  run.signal();
119  run.unlock();
120 }
121 
122 cdr *cdr::get(void) {
123  cdr *rec;
124 
125  private_lock.acquire();
126  if(freelist) {
127  rec = (cdr *)freelist;
128  freelist = rec->getNext();
129  private_lock.release();
130  rec->uuid[0] = 0;
131  rec->ident[0] = 0;
132  rec->dialed[0] = 0;
133  rec->joined[0] = 0;
134  rec->display[0] = 0;
135  rec->network[0] = 0;
136  rec->reason[0] = 0;
137  rec->cid = rec->sequence = 0;
138  rec->starting = 0;
139  rec->duration = 0;
140  return rec;
141  }
142  private_lock.release();
143  return (cdr *)(private_heap.zalloc(sizeof(cdr)));
144 }
145 
146 void cdr::start(void)
147 {
148  run.start();
149 }
150 
151 void cdr::stop(void)
152 {
153  run.lock();
154  running = false;
155  run.signal();
156  run.unlock();
157 
158  while(!down)
159  Thread::sleep(20);
160 }
161 
162 } // end namespace
Used for definitions of plugin modules.
void lock(void)
Definition: cdr.cpp:33
void exit(void)
Definition: cdr.cpp:60
static cdr * get(void)
Get a free cdr node to fill from the cdr memory pool.
Definition: cdr.cpp:122
static void start(void)
Start cdr subsystem and que dispatch thread.
Definition: cdr.cpp:146
void run(void)
Definition: cdr.cpp:64
static void cdrlog(FILE *file, cdr *call)
Post cdr record to a file.
Definition: modules.cpp:91
unsigned cid
Internal call sequence identifiers.
Definition: cdr.h:102
enum sipwitch::cdr::@0 type
Start or end of call?
time_t starting
Time the call was received.
Definition: cdr.h:107
static void post(cdr *cdr)
Post cdr record to callbacks through the cdr thread queue.
Definition: cdr.cpp:102
char reason[16]
Reason the call was terminated.
Definition: cdr.h:97
Basic server call detail record.
Stream events to local clients.
char joined[MAX_IDENT_SIZE]
Call destination eventually joined to.
Definition: cdr.h:82
char dialed[MAX_IDENT_SIZE]
Destination requested on our switch.
Definition: cdr.h:77
static void stop(void)
Stop cdr subsystem.
Definition: cdr.cpp:151
char display[MAX_DISPLAY_SIZE]
Display name of calling party.
Definition: cdr.h:87
Service configuration and component callbacks.
char ident[MAX_IDENT_SIZE]
Ident of calling parting.
Definition: cdr.h:72
unsigned sequence
Definition: cdr.h:102
void unlock(void)
Definition: cdr.cpp:36
Manage control interface.
Interface class for call detail records.
Definition: cdr.h:56
static void drop(cdr *rec)
Send call disconnected event to clients from cdr stop record.
Definition: events.cpp:273
unsigned long duration
Total duration of the call in seconds.
Definition: cdr.h:112
char uuid[48]
A unique identifier for each and every call.
Definition: cdr.h:67
void signal(void)
Definition: cdr.cpp:39
static const char * env(const char *id)
Return the value of a server environment variable.
Definition: control.h:131
#define DEBUG1
Definition: control.h:49
static void connect(cdr *rec)
Send call connection to clients from cdr start record.
Definition: events.cpp:260
char network[MAX_NETWORK_SIZE *2]
Subnet interface the caller appeared on.
Definition: cdr.h:92