Skip to content

Commit

Permalink
Extend the use of the 'options' argument
Browse files Browse the repository at this point in the history
..to

- Message.accept()
- Message.reject()
- Session.cancel()
- Session.answer()
- Session.terminate()
- Session.reject()
  • Loading branch information
jmillan committed Feb 13, 2013
1 parent 77fc70a commit f67872b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 37 deletions.
38 changes: 27 additions & 11 deletions src/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,24 @@ JsSIP.Message.prototype.init_incoming = function(request) {
* Accept the incoming Message
* Only valid for incoming Messages
*/
JsSIP.Message.prototype.accept = function() {
JsSIP.Message.prototype.accept = function(options) {
options = options || {};

var
status_code = options.status_code || 200,
reason_phrase = options.reason_phrase,
extraHeaders = options.extraHeaders || [],
body = options.body;

if (this.direction !== 'incoming') {
throw new TypeError('Invalid method "accept" for an outgoing message');
}

this.request.reply(200);
if (status_code < 200 || status_code >= 300) {
throw new TypeError('Invalid status_code: '+ status_code);
}

this.request.reply(status_code, reason_phrase, extraHeaders, body);
};

/**
Expand All @@ -208,18 +220,22 @@ JsSIP.Message.prototype.accept = function() {
* @param {Number} status_code
* @param {String} [reason_phrase]
*/
JsSIP.Message.prototype.reject = function(status_code, reason_phrase) {
JsSIP.Message.prototype.reject = function(options) {
options = options || {};

var
status_code = options.status_code || 480,
reason_phrase = options.reason_phrase,
extraHeaders = options.extraHeaders || [],
body = options.body;

if (this.direction !== 'incoming') {
throw new TypeError('Invalid method "reject" for an outgoing message');
}

if (status_code) {
if ((status_code < 300 || status_code >= 700)) {
throw new TypeError('Invalid status_code: '+ status_code);
} else {
this.request.reply(status_code, reason_phrase);
}
} else {
this.request.reply(480);
if (status_code < 300 || status_code >= 700) {
throw new TypeError('Invalid status_code: '+ status_code);
}

this.request.reply(status_code, reason_phrase, extraHeaders, body);
};
93 changes: 68 additions & 25 deletions src/Session.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,17 @@ JsSIP.Session.prototype.receiveInitialRequest = function(ua, request) {
* @param {HTMLVideoElement} selfView
* @param {HTMLVideoElement} remoteView
*/
this.answer = function(selfView, remoteView) {
var offer, onSuccess, onMediaFailure, onSdpFailure;
this.answer = function(selfView, remoteView, options) {
options = options || {};

var offer, onSuccess, onMediaFailure, onSdpFailure,
status_code = options.status_code || 200,
reason_phrase = options.reason_phrase,
extraHeaders = options.extraHeaders || [];

if (status_code < 200 || status_code >= 300) {
throw new TypeError('Invalid status_code: '+ status_code);
}

// Check Session Status
if (this.status !== JsSIP.C.SESSION_WAITING_FOR_ANSWER) {
Expand All @@ -415,7 +424,8 @@ JsSIP.Session.prototype.receiveInitialRequest = function(ua, request) {
return;
}

request.reply(200, null, ['Contact: <' + session.contact + '>'],
extraHeaders.push('Contact: <' + session.contact + '>');
request.reply(status_code, reason_phrase, extraHeaders,
sdp,
// onSuccess
function(){
Expand Down Expand Up @@ -549,7 +559,7 @@ JsSIP.Session.prototype.receiveResponse = function(response) {
return;
}

this.acceptAndTerminate(response,'SIP ;cause=400 ;text= "Missing session description"');
this.acceptAndTerminate(response, 400, 'Missing session description');
this.failed('remote', response, JsSIP.C.causes.BAD_MEDIA_DESCRIPTION);

break;
Expand Down Expand Up @@ -584,7 +594,7 @@ JsSIP.Session.prototype.receiveResponse = function(response) {
*/
function(e) {
console.warn(e);
session.acceptAndTerminate(response, 'SIP ;cause=488 ;text="Not Acceptable Here"');
session.acceptAndTerminate(response, 488, 'Not Acceptable Here');
session.failed('remote', response, JsSIP.C.causes.BAD_MEDIA_DESCRIPTION);
}
);
Expand Down Expand Up @@ -673,11 +683,14 @@ JsSIP.Session.prototype.userNoAnswerTimeout = function(request) {
/**
* @private
*/
JsSIP.Session.prototype.acceptAndTerminate = function(response, reason) {
JsSIP.Session.prototype.acceptAndTerminate = function(response, status_code, reason_phrase) {
// Send ACK and BYE
if (this.dialog || this.createConfirmedDialog(response, 'UAC')) {
this.sendACK();
this.sendBye(reason);
this.sendBye({
status_code: status_code,
reason_phrase: reason_phrase
});
}
};

Expand All @@ -693,10 +706,24 @@ JsSIP.Session.prototype.sendACK = function() {
/**
* @private
*/
JsSIP.Session.prototype.sendBye = function(reason) {
var
extraHeaders = (reason) ? ['Reason: '+ reason] : [],
request = this.dialog.createRequest(JsSIP.C.BYE, extraHeaders);
JsSIP.Session.prototype.sendBye = function(options) {
options = options || {};

var request, reason,
status_code = options.status_code,
reason_phrase = options.reason_phrase || JsSIP.C.REASON_PHRASE[status_code] || '',
extraHeaders = options.extraHeaders || [],
body = options.body;

if (status_code && (status_code < 200 || status_code >= 700)) {
throw new TypeError('Invalid status_code: '+ status_code);
} else if (status_code) {
reason = 'SIP ;cause=' + status_code + '; text="' + reason_phrase + '"';
extraHeaders.push('Reason: '+ reason);
}

request = this.dialog.createRequest(JsSIP.C.BYE, extraHeaders);
request.body = body;

this.sendRequest(request);
};
Expand Down Expand Up @@ -842,7 +869,7 @@ JsSIP.Session.prototype.failed = function(originator, response, cause) {
* Terminate the call.
* @param {String} [reason]
*/
JsSIP.Session.prototype.terminate = function() {
JsSIP.Session.prototype.terminate = function(options) {

// Check Session Status
if (this.status === JsSIP.C.SESSION_TERMINATED) {
Expand All @@ -854,16 +881,16 @@ JsSIP.Session.prototype.terminate = function() {
case JsSIP.C.SESSION_NULL:
case JsSIP.C.SESSION_INVITE_SENT:
case JsSIP.C.SESSION_1XX_RECEIVED:
this.cancel();
this.cancel(options);
break;
// - UAS -
case JsSIP.C.SESSION_WAITING_FOR_ANSWER:
this.reject();
this.reject(options);
break;
case JsSIP.C.SESSION_WAITING_FOR_ACK:
case JsSIP.C.SESSION_CONFIRMED:
// Send Bye
this.sendBye();
this.sendBye(options);

this.ended('local', null, JsSIP.C.causes.BYE);
break;
Expand All @@ -879,24 +906,28 @@ JsSIP.Session.prototype.terminate = function() {
* @param {Number} status_code
* @param {String} [reason_phrase]
*/
JsSIP.Session.prototype.reject = function(status_code, reason_phrase) {
JsSIP.Session.prototype.reject = function(options) {
options = options || {};

var
status_code = options.status_code || 480,
reason_phrase = options.reason_phrase,
extraHeaders = options.extraHeaders || [],
body = options.body;

// Check Session Direction and Status
if (this.direction !== 'incoming') {
throw new TypeError('Invalid method "reject" for an outgoing call');
} else if (this.status !== JsSIP.C.SESSION_WAITING_FOR_ANSWER) {
throw new JsSIP.Exceptions.InvalidStateError(this.status);
}

if (status_code) {
if ((status_code < 300 || status_code >= 700)) {
throw new TypeError('Invalid status_code: '+ status_code);
} else {
this.request.reply(status_code, reason_phrase);
}
} else {
this.request.reply(480);
if (status_code < 300 || status_code >= 700) {
throw new TypeError('Invalid status_code: '+ status_code);
}

this.request.reply(status_code, reason_phrase, extraHeaders, body);

this.failed('local', null, JsSIP.C.causes.REJECTED);
};

Expand All @@ -905,12 +936,24 @@ JsSIP.Session.prototype.reject = function(status_code, reason_phrase) {
*
* @param {String} [reason]
*/
JsSIP.Session.prototype.cancel = function(reason) {
JsSIP.Session.prototype.cancel = function(options) {
options = options || {};

var reason,
status_code = options.status_code,
reason_phrase = options.reason_phrase || JsSIP.C.REASON_PHRASE[status_code] || '';

// Check Session Direction
if (this.direction !== 'outgoing') {
throw new TypeError('Invalid method "cancel" for an incoming call');
}

if (status_code && (status_code < 200 || status_code >= 700)) {
throw new TypeError('Invalid status_code: '+ status_code);
} else if (status_code) {
reason = 'SIP ;cause=' + status_code + ' ;text="' + reason_phrase + '"';
}

// Check Session Status
if (this.status === JsSIP.C.SESSION_NULL) {
this.isCanceled = true;
Expand Down
2 changes: 1 addition & 1 deletion src/Transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ var InviteClientTransactionPrototype = function() {
' CANCEL\r\n';

if(reason) {
this.cancel += 'Reason:' + 'SIP ;cause=200 ;text=' + reason + '\r\n';
this.cancel += 'Reason: ' + reason + '\r\n';
}

this.cancel += 'Content-Length: 0\r\n\r\n';
Expand Down

3 comments on commit f67872b

@ibc
Copy link
Member

@ibc ibc commented on f67872b Feb 14, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sag, how to write here a cake emoticon?

@saghul
Copy link
Contributor

@saghul saghul commented on f67872b Feb 14, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it is! 🍰

@ibc
Copy link
Member

@ibc ibc commented on f67872b Feb 14, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opss, just write : and 5 emoticons appear but not 🍰
Thanks!

Please sign in to comment.