mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2025-12-20 16:50:39 +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
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const api = require('express').Router();
|
||||
|
||||
api.use('/createCall', require('./create-call'));
|
||||
api.use('/updateCall', require('./update-call'));
|
||||
|
||||
module.exports = api;
|
||||
|
||||
22
lib/http-routes/api/update-call.js
Normal file
22
lib/http-routes/api/update-call.js
Normal file
@@ -0,0 +1,22 @@
|
||||
const router = require('express').Router();
|
||||
const sysError = require('./error');
|
||||
const sessionTracker = require('../../session/session-tracker');
|
||||
|
||||
router.post('/:callSid', async(req, res) => {
|
||||
const logger = req.app.locals.logger;
|
||||
const callSid = req.params.callSid;
|
||||
logger.debug({body: req.body}, 'got upateCall request');
|
||||
try {
|
||||
const cs = sessionTracker.get(callSid);
|
||||
if (!cs) {
|
||||
logger.info(`updateCall: callSid not found ${callSid}`);
|
||||
return res.sendStatus(404);
|
||||
}
|
||||
res.sendStatus(202);
|
||||
cs.updateCall(req.body);
|
||||
} catch (err) {
|
||||
sysError(logger, res, err);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user