mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2025-12-20 08:40:38 +00:00
initial work on dial verb
This commit is contained in:
@@ -1,11 +1,6 @@
|
||||
const Emitter = require('events');
|
||||
/*
|
||||
const config = require('config');
|
||||
const {forwardInDialogRequests} = require('drachtio-fn-b2b-sugar');
|
||||
const {parseUri, SipError} = require('drachtio-srf');
|
||||
const debug = require('debug')('jambonz:sbc-inbound');
|
||||
const assert = require('assert');
|
||||
*/
|
||||
|
||||
class CallSession extends Emitter {
|
||||
constructor(req, res) {
|
||||
super();
|
||||
@@ -14,6 +9,11 @@ class CallSession extends Emitter {
|
||||
this.srf = req.srf;
|
||||
this.logger = req.locals.logger;
|
||||
this.application = req.locals.application;
|
||||
this.resources = new Map();
|
||||
|
||||
req.on('cancel', this._onCallerHangup.bind(this));
|
||||
|
||||
this.on('callStatusChange', this._onCallStatusChange.bind(this));
|
||||
}
|
||||
|
||||
async exec() {
|
||||
@@ -26,13 +26,69 @@ class CallSession extends Emitter {
|
||||
} catch (err) {
|
||||
this.logger.error({err, task}, 'Error executing task');
|
||||
}
|
||||
this.logger.info('finished all tasks');
|
||||
this.logger.info('CallSession: finished all tasks');
|
||||
if (!this.res.finalResponseSent) {
|
||||
this.logger.info('auto-generating non-success response to invite');
|
||||
this.logger.info('CallSession: auto-generating non-success response to invite');
|
||||
this.res.send(603);
|
||||
}
|
||||
this._clearResources();
|
||||
}
|
||||
}
|
||||
|
||||
addResource(name, resource) {
|
||||
this.logger.debug(`CallSession:addResource: adding ${name}`);
|
||||
this.resources.set(name, resource);
|
||||
}
|
||||
|
||||
getResource(name) {
|
||||
return this.resources.get(name);
|
||||
}
|
||||
|
||||
removeResource(name) {
|
||||
this.logger.debug(`CallSession:removeResource: removing ${name}`);
|
||||
this.resources.delete(name);
|
||||
}
|
||||
|
||||
async createOrRetrieveEpAndMs(remoteSdp) {
|
||||
const mrf = this.srf.locals.mrf;
|
||||
let ms = this.getResource('ms');
|
||||
let ep = this.getResource('epIn');
|
||||
if (ms && ep) return {ms, ep};
|
||||
|
||||
// get a media server
|
||||
if (!ms) {
|
||||
ms = await mrf.connect(config.get('freeswitch'));
|
||||
this.addResource('ms', ms);
|
||||
}
|
||||
if (!ep) {
|
||||
ep = await ms.createEndpoint({remoteSdp});
|
||||
this.addResource('epIn', ep);
|
||||
}
|
||||
return {ms, ep};
|
||||
}
|
||||
|
||||
/**
|
||||
* clear down resources
|
||||
* (note: we remove in reverse order they were added since mediaserver
|
||||
* is typically added first and I prefer to destroy it after any resources it holds)
|
||||
*/
|
||||
_clearResources() {
|
||||
for (const [name, resource] of Array.from(this.resources).reverse()) {
|
||||
try {
|
||||
this.logger.info(`CallSession:_clearResources: deleting ${name}`);
|
||||
if (resource.connected) resource.destroy();
|
||||
} catch (err) {
|
||||
this.logger.error(err, `CallSession:_clearResources: error deleting ${name}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_onCallerHangup(evt) {
|
||||
this.logger.debug('CallSession: caller hung before connection');
|
||||
}
|
||||
_onCallStatusChange(evt) {
|
||||
this.logger.debug(evt, 'CallSession:_onCallStatusChange');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = CallSession;
|
||||
|
||||
Reference in New Issue
Block a user