mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2025-12-19 04:17:44 +00:00
more rest-dial testing
This commit is contained in:
@@ -4,7 +4,7 @@ const sysError = require('./error');
|
||||
const makeTask = require('../../tasks/make_task');
|
||||
const RestCallSession = require('../../session/rest-call-session');
|
||||
const CallInfo = require('../../session/call-info');
|
||||
const {CallDirection} = require('../../utils/constants');
|
||||
const {CallDirection, CallStatus} = require('../../utils/constants');
|
||||
const parseUrl = require('parse-url');
|
||||
const SipError = require('drachtio-srf').SipError;
|
||||
const Srf = require('drachtio-srf');
|
||||
@@ -19,7 +19,8 @@ const srfs = drachtio.map((d) => {
|
||||
srf.connect(d);
|
||||
srf
|
||||
.on('connect', (err, hp) => {
|
||||
console.log(err, `Connected to drachtio at ${hp}`);
|
||||
if (!err) console.log(`Connected to drachtio at ${hp} for REST outdials`);
|
||||
else console.log(`Error connecting to drachtio for outdials: ${err}`);
|
||||
srf.locals.mrf = new Mrf(srf);
|
||||
})
|
||||
.on('error', (err) => console.log(err));
|
||||
@@ -53,7 +54,7 @@ router.post('/', async(req, res) => {
|
||||
const logger = req.app.locals.logger;
|
||||
logger.debug({body: req.body}, 'got createCall request');
|
||||
try {
|
||||
let uri;
|
||||
let uri, cs, to;
|
||||
const restDial = await validate(logger, req.body);
|
||||
const sbcAddress = sbcs[idxSbc++ % sbcs.length];
|
||||
const srf = srfs[idxDrachtio++ % srfs.length];
|
||||
@@ -65,12 +66,15 @@ router.post('/', async(req, res) => {
|
||||
switch (target.type) {
|
||||
case 'phone':
|
||||
uri = `sip:${target.number}@${sbcAddress}`;
|
||||
to = target.number;
|
||||
break;
|
||||
case 'user':
|
||||
uri = `sip:${target.name}`;
|
||||
to = target.name;
|
||||
break;
|
||||
case 'sip':
|
||||
uri = target.sipUri;
|
||||
to = uri;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -112,12 +116,12 @@ router.post('/', async(req, res) => {
|
||||
const callInfo = new CallInfo({
|
||||
direction: CallDirection.Outbound,
|
||||
req: inviteReq,
|
||||
to: req.body.to,
|
||||
to,
|
||||
tag: req.body.tag,
|
||||
accountSid: req.body.account_sid,
|
||||
applicationSid: req.body.application_sid
|
||||
});
|
||||
const cs = new RestCallSession({logger, application, srf, req: inviteReq, ep, tasks, callInfo});
|
||||
cs = new RestCallSession({logger, application, srf, req: inviteReq, ep, tasks, callInfo});
|
||||
cs.exec(req);
|
||||
|
||||
res.status(201).json({sid: cs.callSid});
|
||||
@@ -129,19 +133,27 @@ router.post('/', async(req, res) => {
|
||||
sipLogger.info(`outbound REST call attempt to ${JSON.stringify(target)} has been sent`);
|
||||
},
|
||||
cbProvisional: (prov) => {
|
||||
const callStatus = prov.body ? CallStatus.EarlyMedia : CallStatus.Ringing;
|
||||
if ([180, 183].includes(prov.status) && prov.body) connectStream(prov.body);
|
||||
restDial.emit('callStatus', prov.status, !!prov.body);
|
||||
cs.emit('callStatusChange', {callStatus, sipStatus: prov.status});
|
||||
}
|
||||
});
|
||||
connectStream(dlg.remote.sdp);
|
||||
cs.emit('callStatusChange', {callStatus: CallStatus.InProgress, sipStatus: 200});
|
||||
restDial.emit('callStatus', 200);
|
||||
restDial.emit('connect', dlg);
|
||||
}
|
||||
catch (err) {
|
||||
let callStatus = CallStatus.Failed;
|
||||
if (err instanceof SipError) {
|
||||
if ([486, 603].includes(err.status)) callStatus = CallStatus.Busy;
|
||||
else if (487 === err.status) callStatus = CallStatus.NoAnswer;
|
||||
sipLogger.info(`REST outdial failed with ${err.status}`);
|
||||
cs.emit('callStatusChange', {callStatus, sipStatus: err.status});
|
||||
}
|
||||
else {
|
||||
cs.emit('callStatusChange', {callStatus, sipStatus: 500});
|
||||
sipLogger.error({err}, 'REST outdial failed');
|
||||
}
|
||||
ep.destroy();
|
||||
|
||||
@@ -27,8 +27,8 @@ class CallInfo {
|
||||
this.accountSid = parentCallInfo.accountSid;
|
||||
this.applicationSid = parentCallInfo.applicationSid;
|
||||
this.from = req.callingNumber;
|
||||
this.to = to || req.calledNumber;
|
||||
this.callerName = this.from.name || req.callingNumber;
|
||||
this.to = to;
|
||||
this.callerId = this.from.name || req.callingNumber;
|
||||
this.callId = req.get('Call-ID');
|
||||
this.callStatus = CallStatus.Trying,
|
||||
this.sipStatus = 100;
|
||||
@@ -70,7 +70,7 @@ class CallInfo {
|
||||
callId: this.callId,
|
||||
sipStatus: this.sipStatus,
|
||||
callStatus: this.callStatus,
|
||||
callerId: this.callId,
|
||||
callerId: this.callerId,
|
||||
accountSid: this.accountSid,
|
||||
applicationSid: this.applicationSid
|
||||
};
|
||||
|
||||
@@ -223,7 +223,7 @@ class CallSession extends Emitter {
|
||||
this.callInfo.updateCallStatus(callStatus, sipStatus);
|
||||
if (typeof duration === 'number') this.callInfo.duration = duration;
|
||||
try {
|
||||
this.notifyHook(call_status_hook);
|
||||
if (call_status_hook) this.notifyHook(call_status_hook);
|
||||
} catch (err) {
|
||||
this.logger.info(err, `CallSession:_notifyCallStatusChange error sending ${callStatus} ${sipStatus}`);
|
||||
}
|
||||
|
||||
@@ -14,6 +14,9 @@ class RestCallSession extends CallSession {
|
||||
});
|
||||
this.req = req;
|
||||
this.ep = ep;
|
||||
|
||||
this.on('callStatusChange', this._notifyCallStatusChange.bind(this));
|
||||
this._notifyCallStatusChange({callStatus: CallStatus.Trying, sipStatus: 100});
|
||||
}
|
||||
|
||||
setDialog(dlg) {
|
||||
|
||||
Reference in New Issue
Block a user