more rest-dial testing

This commit is contained in:
Dave Horton
2020-02-02 15:58:33 -05:00
parent 3df8b2d940
commit a697840de7
4 changed files with 24 additions and 9 deletions

View File

@@ -4,7 +4,7 @@ const sysError = require('./error');
const makeTask = require('../../tasks/make_task'); const makeTask = require('../../tasks/make_task');
const RestCallSession = require('../../session/rest-call-session'); const RestCallSession = require('../../session/rest-call-session');
const CallInfo = require('../../session/call-info'); const CallInfo = require('../../session/call-info');
const {CallDirection} = require('../../utils/constants'); const {CallDirection, CallStatus} = require('../../utils/constants');
const parseUrl = require('parse-url'); const parseUrl = require('parse-url');
const SipError = require('drachtio-srf').SipError; const SipError = require('drachtio-srf').SipError;
const Srf = require('drachtio-srf'); const Srf = require('drachtio-srf');
@@ -19,7 +19,8 @@ const srfs = drachtio.map((d) => {
srf.connect(d); srf.connect(d);
srf srf
.on('connect', (err, hp) => { .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); srf.locals.mrf = new Mrf(srf);
}) })
.on('error', (err) => console.log(err)); .on('error', (err) => console.log(err));
@@ -53,7 +54,7 @@ router.post('/', async(req, res) => {
const logger = req.app.locals.logger; const logger = req.app.locals.logger;
logger.debug({body: req.body}, 'got createCall request'); logger.debug({body: req.body}, 'got createCall request');
try { try {
let uri; let uri, cs, to;
const restDial = await validate(logger, req.body); const restDial = await validate(logger, req.body);
const sbcAddress = sbcs[idxSbc++ % sbcs.length]; const sbcAddress = sbcs[idxSbc++ % sbcs.length];
const srf = srfs[idxDrachtio++ % srfs.length]; const srf = srfs[idxDrachtio++ % srfs.length];
@@ -65,12 +66,15 @@ router.post('/', async(req, res) => {
switch (target.type) { switch (target.type) {
case 'phone': case 'phone':
uri = `sip:${target.number}@${sbcAddress}`; uri = `sip:${target.number}@${sbcAddress}`;
to = target.number;
break; break;
case 'user': case 'user':
uri = `sip:${target.name}`; uri = `sip:${target.name}`;
to = target.name;
break; break;
case 'sip': case 'sip':
uri = target.sipUri; uri = target.sipUri;
to = uri;
break; break;
} }
@@ -112,12 +116,12 @@ router.post('/', async(req, res) => {
const callInfo = new CallInfo({ const callInfo = new CallInfo({
direction: CallDirection.Outbound, direction: CallDirection.Outbound,
req: inviteReq, req: inviteReq,
to: req.body.to, to,
tag: req.body.tag, tag: req.body.tag,
accountSid: req.body.account_sid, accountSid: req.body.account_sid,
applicationSid: req.body.application_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); cs.exec(req);
res.status(201).json({sid: cs.callSid}); 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`); sipLogger.info(`outbound REST call attempt to ${JSON.stringify(target)} has been sent`);
}, },
cbProvisional: (prov) => { cbProvisional: (prov) => {
const callStatus = prov.body ? CallStatus.EarlyMedia : CallStatus.Ringing;
if ([180, 183].includes(prov.status) && prov.body) connectStream(prov.body); if ([180, 183].includes(prov.status) && prov.body) connectStream(prov.body);
restDial.emit('callStatus', prov.status, !!prov.body); restDial.emit('callStatus', prov.status, !!prov.body);
cs.emit('callStatusChange', {callStatus, sipStatus: prov.status});
} }
}); });
connectStream(dlg.remote.sdp); connectStream(dlg.remote.sdp);
cs.emit('callStatusChange', {callStatus: CallStatus.InProgress, sipStatus: 200});
restDial.emit('callStatus', 200); restDial.emit('callStatus', 200);
restDial.emit('connect', dlg); restDial.emit('connect', dlg);
} }
catch (err) { catch (err) {
let callStatus = CallStatus.Failed;
if (err instanceof SipError) { 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}`); sipLogger.info(`REST outdial failed with ${err.status}`);
cs.emit('callStatusChange', {callStatus, sipStatus: err.status});
} }
else { else {
cs.emit('callStatusChange', {callStatus, sipStatus: 500});
sipLogger.error({err}, 'REST outdial failed'); sipLogger.error({err}, 'REST outdial failed');
} }
ep.destroy(); ep.destroy();

View File

@@ -27,8 +27,8 @@ class CallInfo {
this.accountSid = parentCallInfo.accountSid; this.accountSid = parentCallInfo.accountSid;
this.applicationSid = parentCallInfo.applicationSid; this.applicationSid = parentCallInfo.applicationSid;
this.from = req.callingNumber; this.from = req.callingNumber;
this.to = to || req.calledNumber; this.to = to;
this.callerName = this.from.name || req.callingNumber; this.callerId = this.from.name || req.callingNumber;
this.callId = req.get('Call-ID'); this.callId = req.get('Call-ID');
this.callStatus = CallStatus.Trying, this.callStatus = CallStatus.Trying,
this.sipStatus = 100; this.sipStatus = 100;
@@ -70,7 +70,7 @@ class CallInfo {
callId: this.callId, callId: this.callId,
sipStatus: this.sipStatus, sipStatus: this.sipStatus,
callStatus: this.callStatus, callStatus: this.callStatus,
callerId: this.callId, callerId: this.callerId,
accountSid: this.accountSid, accountSid: this.accountSid,
applicationSid: this.applicationSid applicationSid: this.applicationSid
}; };

View File

@@ -223,7 +223,7 @@ class CallSession extends Emitter {
this.callInfo.updateCallStatus(callStatus, sipStatus); this.callInfo.updateCallStatus(callStatus, sipStatus);
if (typeof duration === 'number') this.callInfo.duration = duration; if (typeof duration === 'number') this.callInfo.duration = duration;
try { try {
this.notifyHook(call_status_hook); if (call_status_hook) this.notifyHook(call_status_hook);
} catch (err) { } catch (err) {
this.logger.info(err, `CallSession:_notifyCallStatusChange error sending ${callStatus} ${sipStatus}`); this.logger.info(err, `CallSession:_notifyCallStatusChange error sending ${callStatus} ${sipStatus}`);
} }

View File

@@ -14,6 +14,9 @@ class RestCallSession extends CallSession {
}); });
this.req = req; this.req = req;
this.ep = ep; this.ep = ep;
this.on('callStatusChange', this._notifyCallStatusChange.bind(this));
this._notifyCallStatusChange({callStatus: CallStatus.Trying, sipStatus: 100});
} }
setDialog(dlg) { setDialog(dlg) {