-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.js
85 lines (71 loc) · 2.26 KB
/
app.js
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
const Srf = require('drachtio-srf');
const SipRequest = Srf.SipRequest;
const SipResponse = Srf.SipResponse;
const assert = require('assert');
const noop = () => {};
const noopLogger = {debug: noop, info: noop, error: console.error};
const CallManager = require('./lib/call-manager');
const ReferHandler = require ('./lib/refer-handler');
const forwardInDialogRequests = require('./lib/dialog-request-forwarder');
function simring(...args) {
if (args.length === 1) {
const logger = args[0];
assert.ok(typeof logger.debug === 'function' &&
typeof logger.info === 'function' &&
typeof logger.error === 'function', 'invalid logger object: must provide [debug|info|error] functions');
return function(req, res, uriList, opts, notifiers) {
assert(req instanceof SipRequest);
assert(res instanceof SipResponse);
assert(Array.isArray(uriList));
const manager = new CallManager({req, res, uriList, opts, notifiers, logger});
return manager.simring();
};
}
assert.ok(args.length >= 3 &&
args[0] instanceof SipRequest &&
args[1] instanceof SipResponse &&
Array.isArray(args[2]), 'invalid simring arguments: usage: simring(req, res, uriArray..)');
const opts = {
req: args[0],
res: args[1],
uriList: args[2],
opts: args[3] || {},
notifiers: args[4] || {},
logger: noopLogger
};
const manager = new CallManager(opts);
return manager.simring();
}
function transfer(opts) {
const referHandler = new ReferHandler(opts);
return referHandler.transfer();
}
class Simring {
constructor(req, res, uriList, opts, notifiers) {
const callOpts = {
req,
res,
uriList: typeof uriList == 'string' ? [uriList] : (uriList || []),
opts: opts || {},
notifiers: notifiers || {},
logger: noopLogger
} ;
this.manager = new CallManager(callOpts);
}
set logger(logger) {
this.manager.logger = logger;
}
get finished() {
return this.manager.finished;
}
get started() {
return this.manager.started;
}
start(anotherUriList, callOpts) {
return this.manager.simring(anotherUriList, callOpts);
}
addUri(uri, callOpts) {
return this.manager.addUri(uri, callOpts);
}
}
module.exports = {simring, Simring, transfer, forwardInDialogRequests};