Skip to content

Commit

Permalink
Add URI and NameAddrHeader classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jmillan committed Jan 31, 2013
1 parent 47cdb66 commit 7b345bf
Show file tree
Hide file tree
Showing 19 changed files with 466 additions and 12,364 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Name: JsSIP
Maintainer: José Luis Millán <[email protected]>
Copyright (c) 2012-2013 José Luis Millán <[email protected]>
Copyright (c) 2012-2013 José Luis Millán - Versatica <http://www.versatica.com>


License: The MIT License
Expand Down
2 changes: 2 additions & 0 deletions grunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ module.exports = function(grunt) {
"src/Transport.js",
"src/Parser.js",
"src/SIPMessage.js",
"src/URI.js",
"src/NameAddrHeader.js",
"src/Transactions.js",
"src/Dialogs.js",
"src/RequestSender.js",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "jssip",
"title": "JsSIP",
"description": "Javascript SIP WebSocket library",
"version": "0.2.1",
"version": "devel",
"homepage": "http://jssip.net",
"author": "José Luis Millán <[email protected]>",
"contributors": [
Expand All @@ -21,7 +21,7 @@
"library"
],
"devDependencies": {
"grunt": "0.3.12"
"grunt": "0.3.17"
},
"license": "MIT"
}
8 changes: 4 additions & 4 deletions src/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ JsSIP.Message.prototype.send = function(target, body, contentType, options) {
}

// Check target validity
target = JsSIP.utils.normalizeUri(target, this.ua.configuration.domain);
target = JsSIP.utils.normalizeURI(target, this.ua.configuration.domain);
if (!target) {
throw new JsSIP.exceptions.InvalidTargetError();
}

// Message parameter initialization
this.direction = 'outgoing';
this.local_identity = this.ua.configuration.user;
this.local_identity = this.ua.configuration.from_uri;
this.remote_identity = target;

this.closed = false;
Expand Down Expand Up @@ -164,8 +164,8 @@ JsSIP.Message.prototype.init_incoming = function(request) {

this.direction = 'incoming';
this.request = request;
this.local_identity = request.s('to').uri;
this.remote_identity = request.s('from').uri;
this.local_identity = request.s('to').uri.toAor();
this.remote_identity = request.s('from').uri.toAor();

if (contentType && (contentType.match(/^text\/plain(\s*;\s*.+)*$/i) || contentType.match(/^text\/html(\s*;\s*.+)*$/i))) {
this.ua.emit('newMessage', this.ua, {
Expand Down
86 changes: 86 additions & 0 deletions src/NameAddrHeader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* @augments JsSIP
* @class Class creating a Name Address SIP header.
*
* @param {JsSIP.URI} uri
* @param {String} [display_name]
* @param {Object} [parameters]
*
*/
JsSIP.NameAddrHeader = function(uri, display_name, parameters) {
var param;

// Checks
if(!uri || !uri instanceof JsSIP.URI) {
console.warn('Missing or invalid "uri" in NameAddrHeader');
throw new JsSIP.exceptions.InvalidValueError();
}

// Initialize parameters
this.uri = uri;
this.parameters = {};

for (param in parameters) {
this.setParam(param, parameters[param]);
}

Object.defineProperties(this, {
display_name: {
get: function() { return display_name; },
set: function(value) {
display_name = value;
}
}
});
};
JsSIP.NameAddrHeader.prototype = {
setParam: function(key, value) {
if (key) {
this.parameters[key.toLowerCase()] = (typeof value === 'undefined' || value === null)? null : value.toString();
}
},

getParam: function(key) {
if(key) {
return this.parameters[key.toLowerCase()];
}
},

hasParam: function(key) {
if(key) {
return this.parameters.hasOwnProperty(key.toLowerCase()) && true || false;
}
},

deleteParam: function(parameter) {
parameter = parameter.toLowerCase();
if (this.parameters.hasOwnProperty(parameter)) {
delete this.parameters[parameter];
}
},

clearParams: function() {
this.parameters = {};
},

clone: function() {
return new JsSIP.NameAddrHeader(
this.uri.clone(),
this.display_name,
window.JSON.parse(window.JSON.stringify(this.parameters)));
},

toString: function() {
var body, parameter;

body = (this.display_name) ? '"' + this.display_name + '" ' : '';
body += (this.display_name) ? '<' + this.uri.toString() + '>' : this.uri.toString();


for (parameter in this.parameters) {
body += ';' + parameter;
body += (this.parameters[parameter] === null)? '' : '=' + this.parameters[parameter];
}
return body;
}
};
6 changes: 3 additions & 3 deletions src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ JsSIP.Parser = {
parsed = message.parseHeader('from');
if(parsed) {
message.from = parsed;
message.from_tag = parsed.tag;
message.from_tag = parsed.getParam('tag');
}
break;
case 'to':
Expand All @@ -82,7 +82,7 @@ JsSIP.Parser = {
parsed = message.parseHeader('to');
if(parsed) {
message.to = parsed;
message.to_tag = parsed.tag;
message.to_tag = parsed.getParam('tag');
}
break;
case 'record-route':
Expand Down Expand Up @@ -191,7 +191,7 @@ JsSIP.Parser = {
} else if(!parsed.status_code) {
message = new JsSIP.IncomingRequest();
message.method = parsed.method;
message.ruri = parsed;
message.ruri = parsed.uri;
} else {
message = new JsSIP.IncomingResponse();
message.status_code = parsed.status_code;
Expand Down
14 changes: 5 additions & 9 deletions src/Registrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ JsSIP.Registrator.prototype = {
while(contacts--) {
contact = response.parseHeader('contact', contacts);
if(contact.uri === this.ua.contact.uri) {
expires = contact.params.expires;
expires = contact.getParam('expires');
break;
}
}
Expand All @@ -106,10 +106,6 @@ JsSIP.Registrator.prototype = {

if(!expires) {
expires = this.expires;
} else if(expires < this.min_expires) {
// Set the expires value to min_expires in case it is slower
console.log(JsSIP.c.LOG_REGISTRATOR +'Received expires value: ' + expires + ' is smaller than the minimum expires time: ' + this.min_expires);
expires = this.min_expires;
}

// Re-Register before the expiration interval has elapsed.
Expand All @@ -119,11 +115,11 @@ JsSIP.Registrator.prototype = {
}, (expires * 1000) - 3000);

//Save gruu values
if (contact.params['temp-gruu']) {
this.ua.contact.temp_gruu = contact.params['temp-gruu'].replace(/"/g,'');
if (contact.hasParam('temp-gruu')) {
this.ua.contact.temp_gruu = contact.getParam('temp-gruu').replace(/"/g,'');
}
if (contact.params['pub-gruu']) {
this.ua.contact.pub_gruu = contact.params['pub-gruu'].replace(/"/g,'');
if (contact.hasParam('pub-gruu')) {
this.ua.contact.pub_gruu = contact.getParam('pub-gruu').replace(/"/g,'');
}

this.registered = true;
Expand Down
9 changes: 5 additions & 4 deletions src/SIPMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ JsSIP.IncomingRequest.prototype.reply = function(code, reason, extraHeaders, bod

if(!this.to_tag) {
to += ';tag=' + JsSIP.utils.newTag();
} else if(this.to_tag && !this.s('to').tag) {
} else if(this.to_tag && !this.s('to').hasParam('tag')) {
to += ';tag=' + this.to_tag;
}

Expand All @@ -398,7 +398,7 @@ JsSIP.IncomingRequest.prototype.reply = function(code, reason, extraHeaders, bod
response += 'Content-Length: ' + length + '\r\n\r\n';
response += body;
} else {
response += '\r\n';
response += 'Content-Length: ' + 0 + '\r\n\r\n';
}

this.server_transaction.receiveResponse(code, response, onSuccess, onFailure);
Expand Down Expand Up @@ -435,14 +435,15 @@ JsSIP.IncomingRequest.prototype.reply_sl = function(code, reason) {

if(!this.to_tag) {
to += ';tag=' + JsSIP.utils.newTag();
} else if(this.to_tag && !this.s('to').tag) {
} else if(this.to_tag && !this.s('to').hasParam('tag')) {
to += ';tag=' + this.to_tag;
}

response += 'To: ' + to + '\r\n';
response += 'From: ' + this.getHeader('From') + '\r\n';
response += 'Call-ID: ' + this.call_id + '\r\n';
response += 'CSeq: ' + this.cseq + ' ' + this.method + '\r\n\r\n';
response += 'CSeq: ' + this.cseq + ' ' + this.method + '\r\n';
response += 'Content-Length: ' + 0 + '\r\n\r\n';

this.transport.send(response);
};
Expand Down
2 changes: 1 addition & 1 deletion src/SanityCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ JsSIP.sanityCheck = (function() {

// Sanity Check functions for requests
function rfc3261_8_2_2_1() {
if(message.s('to').scheme !== 'sip') {
if(message.s('to').uri.scheme !== 'sip') {
reply(416);
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Session.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ JsSIP.Session.prototype.connect = function(target, options) {
}

// Check target validity
target = JsSIP.utils.normalizeUri(target, this.ua.configuration.domain);
target = JsSIP.utils.normalizeURI(target, this.ua.configuration.domain);
if (!target) {
throw new JsSIP.exceptions.InvalidTargetError();
}
Expand Down Expand Up @@ -746,10 +746,10 @@ JsSIP.Session.prototype.newSession = function(originator, request, target) {
session.direction = (originator === 'local') ? 'outgoing' : 'incoming';

if (originator === 'remote') {
session.local_identity = request.s('to').uri;
session.remote_identity = request.s('from').uri;
session.local_identity = request.s('to').uri.toAor();
session.remote_identity = request.s('from').uri.toAor();
} else if (originator === 'local'){
session.local_identity = session.ua.configuration.user;
session.local_identity = session.ua.configuration.from_uri;
session.remote_identity = target;
}

Expand Down
9 changes: 9 additions & 0 deletions src/Transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ JsSIP.Transport = function(ua, server) {
this.closed = false;
this.connected = false;
this.reconnectTimer = null;
this.lastTransportError = {};

this.ua.transport = this;

Expand Down Expand Up @@ -127,6 +128,8 @@ JsSIP.Transport.prototype = {
var connected_before = this.connected;

this.connected = false;
this.lastTransportError.code = e.code;
this.lastTransportError.reason = e.reason;
console.warn(JsSIP.c.LOG_TRANSPORT +'WebSocket disconnected: code=' + e.code + (e.reason? ', reason=' + e.reason : ''));

if(e.wasClean === false) {
Expand All @@ -140,6 +143,12 @@ JsSIP.Transport.prototype = {
// Reset reconnection_attempts
this.reconnection_attempts = 0;
this.reConnect();
} else {
this.ua.emit('disconnected', this.ua, {
transport: this,
code: this.lastTransportError.code,
reason: this.lastTransportError.reason
});
}
} else {
// This is the first connection attempt
Expand Down
12 changes: 9 additions & 3 deletions src/UA.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,12 @@ JsSIP.UA.prototype.onTransportError = function(transport) {
transport.server.status = JsSIP.c.WS_SERVER_ERROR;
console.log(JsSIP.c.LOG_UA +'connection status set to: '+ JsSIP.c.WS_SERVER_ERROR);

this.emit('disconnected', this, {
transport: transport,
code: transport.lastTransportError.code,
reason: transport.lastTransportError.reason
});

server = this.getNextWsServer();

if(server) {
Expand All @@ -310,9 +316,7 @@ JsSIP.UA.prototype.onTransportError = function(transport) {
if (!this.error || this.error !== JsSIP.c.UA_NETWORK_ERROR) {
this.status = JsSIP.c.UA_STATUS_NOT_READY;
this.error = JsSIP.c.UA_NETWORK_ERROR;
this.emit('disconnected');
}

// Transport Recovery process
this.recoverTransport();
}
Expand All @@ -339,7 +343,9 @@ JsSIP.UA.prototype.onTransportConnected = function(transport) {

this.status = JsSIP.c.UA_STATUS_READY;
this.error = null;
this.emit('connected', this);
this.emit('connected', this, {
transport: transport
});

if(this.configuration.register) {
if(this.registrator) {
Expand Down
Loading

0 comments on commit 7b345bf

Please sign in to comment.