major revamp of http client functionalit

This commit is contained in:
Dave Horton
2020-02-14 12:45:28 -05:00
parent ff531e6964
commit 446000ee97
35 changed files with 906 additions and 433 deletions

View File

@@ -3,6 +3,11 @@ const {CallStatus} = require('../utils/constants');
const moment = require('moment');
const assert = require('assert');
/**
* @classdesc Subclass of CallSession. This represents a CallSession that is
* established for an inbound call.
* @extends CallSession
*/
class InboundCallSession extends CallSession {
constructor(req, res) {
super({
@@ -34,46 +39,9 @@ class InboundCallSession extends CallSession {
}
}
async connectInboundCallToIvr(earlyMedia = false) {
// check for a stable inbound call already connected to the ivr
if (this.ep && this.dlg) {
this.logger.debug('CallSession:connectInboundCallToIvr - inbound call already connected to IVR');
return {ep: this.ep, dlg: this.dlg};
}
// check for an early media connection, where caller wants same
if (this.ep && earlyMedia) {
this.logger.debug('CallSession:connectInboundCallToIvr - inbound call already has early media connection');
return {ep: this.ep};
}
// ok, we need to connect the inbound call to the ivr
try {
assert(!this.req.finalResponseSent);
this.logger.debug('CallSession:connectInboundCallToIvr - creating endpoint for inbound call');
const {ep} = await this.createOrRetrieveEpAndMs();
this.ep = ep;
if (earlyMedia) {
this.res.send(183, {body: ep.local.sdp});
this.emit('callStatusChange', {sipStatus: 183, callStatus: CallStatus.EarlyMedia});
return {ep, res: this.res};
}
const dlg = await this.srf.createUAS(this.req, this.res, {localSdp: ep.local.sdp});
dlg.on('destroy', this._callerHungup.bind(this));
dlg.connectTime = moment();
this.emit('callStatusChange', {sipStatus: 200, callStatus: CallStatus.InProgress});
this.logger.debug(`CallSession:connectInboundCallToIvr - answered callSid ${this.callSid}`);
this.ep = ep;
this.dlg = dlg;
return {ep, dlg};
} catch (err) {
this.logger.error(err, 'CallSession:connectInboundCallToIvr error');
throw err;
}
}
/**
* Answer the call, if it has not already been answered.
*/
async propagateAnswer() {
if (!this.dlg) {
assert(this.ep);
@@ -85,6 +53,9 @@ class InboundCallSession extends CallSession {
}
}
/**
* This is invoked when the caller hangs up, in order to calculate the call duration.
*/
_callerHungup() {
assert(this.dlg.connectTime);
const duration = moment().diff(this.dlg.connectTime, 'seconds');
@@ -92,7 +63,6 @@ class InboundCallSession extends CallSession {
this.logger.debug('InboundCallSession: caller hung up');
this._callReleased();
}
}
module.exports = InboundCallSession;