mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2025-12-20 08:40:38 +00:00
add support for live call control
This commit is contained in:
@@ -11,21 +11,51 @@ const Srf = require('drachtio-srf');
|
||||
const drachtio = config.get('outdials.drachtio');
|
||||
const sbcs = config.get('outdials.sbc');
|
||||
const Mrf = require('drachtio-fsmrf');
|
||||
const installSrfLocals = require('../../utils/install-srf-locals');
|
||||
let idxDrachtio = 0;
|
||||
let idxSbc = 0;
|
||||
let srfs = [];
|
||||
let initializedSrfs = false;
|
||||
|
||||
const srfs = drachtio.map((d) => {
|
||||
const srf = new Srf();
|
||||
srf.connect(d);
|
||||
srf
|
||||
.on('connect', (err, 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));
|
||||
return srf;
|
||||
});
|
||||
/**
|
||||
* 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 {
|
||||
logger.debug(drachtio, 'getSrfForOutdial - attempting to connect');
|
||||
initializedSrfs = true;
|
||||
resolve(Promise.race(drachtio.map((d) => connectSrf(logger, d))));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function validate(logger, payload) {
|
||||
const data = Object.assign({}, {
|
||||
@@ -57,7 +87,7 @@ router.post('/', async(req, res) => {
|
||||
let uri, cs, to;
|
||||
const restDial = await validate(logger, req.body);
|
||||
const sbcAddress = sbcs[idxSbc++ % sbcs.length];
|
||||
const srf = srfs[idxDrachtio++ % srfs.length];
|
||||
const srf = await getSrfForOutdial(logger);
|
||||
const target = restDial.to;
|
||||
const opts = {
|
||||
'callingNumber': restDial.from
|
||||
|
||||
Reference in New Issue
Block a user