support OPTIONS ping to SBCs

This commit is contained in:
Dave Horton
2020-02-17 21:41:35 -05:00
parent 3c89b7fd76
commit 162dfff5a3
9 changed files with 109 additions and 77 deletions

View File

@@ -4,56 +4,18 @@ const RestCallSession = require('../../session/rest-call-session');
const CallInfo = require('../../session/call-info');
const {CallDirection, CallStatus} = require('../../utils/constants');
const SipError = require('drachtio-srf').SipError;
const Srf = require('drachtio-srf');
const sysError = require('./error');
const Mrf = require('drachtio-fsmrf');
const installSrfLocals = require('../../utils/install-srf-locals');
const Requestor = require('../../utils/requestor');
let idxDrachtio = 0;
let idxSbc = 0;
let srfs = [];
let initializedSrfs = false;
/**
* Connect to a single drachtio server, returning a Promise when connected.
* Upon connect, add ourselves to the list of active servers, removing if we lose the connection
*/
function connectSrf(logger, d) {
return new Promise((resolve, reject) => {
const srf = new Srf();
srf.connect(d);
srf
.on('connect', (err, hp) => {
if (!err) logger.info(`connectSrf: Connected to drachtio at ${hp} for REST outdials`);
else logger.error(`connectSrf: Error connecting to drachtio for outdials: ${err}`);
srf.locals.mrf = new Mrf(srf);
installSrfLocals(srf, logger);
srfs.push(srf);
resolve(srf);
})
.on('error', (err) => {
logger.error(err, 'connectSrf error');
srfs = srfs.filter((s) => s !== srf);
reject(err);
});
});
}
/**
* Retrieve a connection to a drachtio server, lazily creating when first called
*/
function getSrfForOutdial(logger) {
return new Promise((resolve, reject) => {
if (srfs.length === 0 && initializedSrfs) return reject('no available drachtio servers for outdial');
else if (srfs.length > 0) return resolve(srfs[idxDrachtio++ % srfs.length]);
else {
const {srf} = require('../../../');
const drachtio = srf.locals.drachtio;
logger.debug(drachtio, 'getSrfForOutdial - attempting to connect');
initializedSrfs = true;
resolve(Promise.race(drachtio.map((d) => connectSrf(logger, d))));
}
});
const {srf} = require('../../../');
const {getSrf} = srf.locals;
const srfForOutdial = getSrf();
if (!srfForOutdial) throw new Error('no available feature servers for outbound call creation');
return srfForOutdial;
}
router.post('/', async(req, res) => {
@@ -62,8 +24,10 @@ router.post('/', async(req, res) => {
try {
let uri, cs, to;
const restDial = makeTask(logger, {'rest:dial': req.body});
const srf = await getSrfForOutdial(logger);
const sbcAddress = srf.locals.sbcs[idxSbc++ % srf.locals.sbcs.length];
const srf = getSrfForOutdial(logger);
const {getSBC, getFreeswitch} = srf.locals;
const sbcAddress = getSBC();
if (!sbcAddress) throw new Error('no available SBCs for outbound call creation');
const target = restDial.to;
const opts = { callingNumber: restDial.from };
@@ -84,8 +48,9 @@ router.post('/', async(req, res) => {
/* create endpoint for outdial */
const mrf = srf.locals.mrf;
const ms = await mrf.connect(srf.locals.freeswitch);
const fsOpts = getFreeswitch();
if (!fsOpts) throw new Error('no available Freeswitch for outbound call creation');
const ms = await mrf.connect(fsOpts);
logger.debug('createCall: successfully connected to media server');
const ep = await ms.createEndpoint();
logger.debug(`createCall: successfully allocated endpoint, sending INVITE to ${sbcAddress}`);