Skip to content

Commit

Permalink
extract guid and nodeid in own files
Browse files Browse the repository at this point in the history
  • Loading branch information
erossignon committed Feb 13, 2014
1 parent 1733e23 commit 764725d
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 156 deletions.
123 changes: 13 additions & 110 deletions lib/encode_decode.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
var BinaryStream = require("./binaryStream").BinaryStream;
var assert = require("assert");
var Enum = require("enum");

var ec = exports;
var util = require("util");

var isValidGUID = require("./guid").isValidGUID;
var is_guid = require("./guid").is_guid;

var NodeIdType= exports.NodeIdType = require("./nodeid").NodeIdType;

var makeNodeId = exports.makeNodeId = require("./nodeid").makeNodeId;
exports.makeExpandedNodeId = require("./nodeid").makeExpandedNodeId;



exports.decodeUAString = function (stream) {
var value;
var length = stream.readInteger();
Expand Down Expand Up @@ -178,23 +189,12 @@ exports.decodeDateTime = function(stream) {
};


var regexGUID = /[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{8}/;

function is_guid(value) {
return regexGUID.test(value);
}

exports.is_guid = is_guid;

exports.isValidGUID = function (guid) {
assert(guid.length == 36);
return is_guid(guid);
};


exports.encodeGUID = function (guid, stream) {

exports.isValidGUID(guid);
assert(isValidGUID(guid));
// 1 2 3
// 0123456789012345678901234567890123456
// | | | | |
Expand Down Expand Up @@ -245,72 +245,10 @@ exports.decodeGUID = function (stream) {
return guid.toUpperCase();
};

var Enum = require("enum");
var NodeIdType = new Enum({
NUMERIC: 0x01,
STRING: 0x02,
GUID: 0x03,
BYTESTRING: 0x04
});
exports.NodeIdType = NodeIdType;



function NodeId(identifierType,value,namespace) {
this.identifierType =identifierType;
this.value = value;
this.namespace = namespace;
}

var re = new RegExp("\"", 'g');

NodeId.prototype.toString = function() {
switch(this.identifierType) {
case NodeIdType.NUMERIC:
return "ns="+ this.namespace +";i="+this.value;
break;
case NodeIdType.STRING:
return "ns="+ this.namespace +";s='"+this.value+"'";
break;
case NodeIdType.GUID:
return "ns="+ this.namespace +";g='"+this.value+"'";
break;
case NodeIdType.BYTESTRING:
return "ns="+ this.namespace +";b="+toHex(this.value)+"";
break;
default:
return JSON.stringify(this).replace(re,"");
}
};


var makeNodeId = function makeNodeId(value,namespace) {

value = value || 0;
namespace = namespace || 0;

var identifierType = NodeIdType.NUMERIC;
if (typeof value == "string" ) {
// 1 2 3
// 012345678901234567890123456789012345
// "72962B91-FA75-4AE6-8D28-B404DC7DAF63"
if (is_guid(value)) {
identifierType= NodeIdType.GUID;
} else {
identifierType= NodeIdType.STRING;
}

} else if ( value instanceof Buffer) {
identifierType= NodeIdType.BYTESTRING;
}

var nodeId = new NodeId(identifierType,value,namespace);

assert(nodeId.hasOwnProperty("identifierType"));

return nodeId;
};
exports.makeNodeId = makeNodeId;


var EnumNodeIdEncoding = new Enum({
Expand Down Expand Up @@ -444,42 +382,7 @@ exports.decodeNodeId = function(stream) {



function ExpandedNodeId(identifierType,value,namespace,namespaceUri,serverIndex) {
NodeId.apply(this,arguments);
this.namespaceUri = namespaceUri;
this.serverIndex = serverIndex;
}
util.inherits(ExpandedNodeId,NodeId);

//------
// An ExpandedNodeId extends the NodeId structure by allowing the NamespaceUri to be
// explicitly specified instead of using the NamespaceIndex. The NamespaceUri is optional. If it
// is specified then the NamespaceIndex inside the NodeId shall be ignored.
//
// The ExpandedNodeId is encoded by first encoding a NodeId as described in Clause 5 .2.2.9
// and then encoding NamespaceUri as a String.
//
// An instance of an ExpandedNodeId may still use the NamespaceIndex instead of the
// NamespaceUri. In this case, the NamespaceUri is not encoded in the stream. The presence of
// the NamespaceUri in the stream is indicated by setting the NamespaceUri flag in the encoding
// format byte for the NodeId.
//
// If the NamespaceUri is present then the encoder shall encode the NamespaceIndex as 0 in
// the stream when the NodeId portion is encoded. The unused NamespaceIndex is included in
// the stream for consistency,
//
// An ExpandedNodeId may also have a ServerIndex which is encoded as a UInt32 after the
// NamespaceUri. The ServerIndex flag in the NodeId encoding byte indicates whether the
// ServerIndex is present in the stream. The ServerIndex is omitted if it is equal to zero.
//
exports.makeExpandedNodeId = function makeExpandedNodeId(value,namespace) {

value = parseInt(value) || 0;
namespace = namespace || 0;
serverIndex = 0;
namespaceUri = null;
return new ExpandedNodeId(NodeIdType.NUMERIC,value,namespace,namespaceUri,serverIndex);
};


exports.encodeExpandedNodeId = function(expandedNodeId, stream) {

Expand Down
29 changes: 1 addition & 28 deletions lib/factories.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,7 @@ factories = {};
_enumerations = {};


/**
* Convert a value into a nodeId:
* - if nodeId is a string of form : i=1234 => nodeId({ ns: 0 , value=1234})
* @param value
*/
function coerceNodeId(value){

if (value instanceof Object) {
return ec.makeNodeId(value.value,value.namespace);
}
if (typeof value === "string") {

if ( value.substr(0,2) === "i=" ) {
var n = parseInt(value.substr(2));
return ec.makeNodeId(n,0);
}
var r = /ns=([0-9]+)\;i=([0-9]+)/
var matches = r.exec(value);
if (matches) {
var ns = matches[1];
var i = matches[2];
return ec.makeNodeId(parseInt(i),parseInt(ns));
}
}

return ec.makeNodeId(value,0);
}
exports.coerceNodeId = coerceNodeId;
coerceNodeId = require("./nodeid").coerceNodeId;


var _defaultType = [
Expand Down
15 changes: 15 additions & 0 deletions lib/guid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var assert = require("assert");


var regexGUID = /[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{8}/;

function is_guid(value) {
return regexGUID.test(value);
}

exports.is_guid = is_guid;

exports.isValidGUID = function (guid) {
assert(guid.length == 36);
return is_guid(guid);
};
156 changes: 156 additions & 0 deletions lib/nodeid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
var util = require("util");
var Enum = require("enum");
var assert = require("assert");

var is_guid = require("./guid").is_guid;

var NodeIdType = new Enum({
NUMERIC: 0x01,
STRING: 0x02,
GUID: 0x03,
BYTESTRING: 0x04
});
exports.NodeIdType = NodeIdType;

function NodeId(identifierType,value,namespace) {
this.identifierType =identifierType;
this.value = value;
this.namespace = namespace;
}

var re = new RegExp("\"", 'g');

NodeId.prototype.toString = function() {
switch(this.identifierType) {
case NodeIdType.NUMERIC:
return "ns="+ this.namespace +";i="+this.value;
break;
case NodeIdType.STRING:
return "ns="+ this.namespace +";s='"+this.value+"'";
break;
case NodeIdType.GUID:
return "ns="+ this.namespace +";g='"+this.value+"'";
break;
case NodeIdType.BYTESTRING:
return "ns="+ this.namespace +";b="+toHex(this.value)+"";
break;
default:
return JSON.stringify(this).replace(re,"");
}
};


/**
* Convert a value into a nodeId:
* - if nodeId is a string of form : i=1234 => nodeId({ ns: 0 , value=1234})
* @param value
* @param namespace {integer}
*/
function coerceNodeId(value,namespace){

value = value || 0;
namespace = namespace || 0;
var identifierType = NodeIdType.NUMERIC;

if (typeof value == "string" ) {
identifierType= NodeIdType.STRING;
if ( value.substr(0,2) === "i=" ) {

identifierType = NodeIdType.NUMERIC;
value = parseInt(value.substr(2));

} else if (is_guid(value)) {
identifierType= NodeIdType.GUID;
} else {
identifierType = NodeIdType.NUMERIC;
var r = /ns=([0-9]+)\;i=([0-9]+)/ ;
var matches = r.exec(value);
if (matches) {
namespace = parseInt(matches[1]);
value = parseInt(matches[2]);
}
}

} else if ( value instanceof Buffer) {
identifierType= NodeIdType.BYTESTRING;

} else if (value instanceof Object) {
var tmp= value;
value = tmp.value;
namespace = namespace || tmp.namespace;
return coerceNodeId(value,namespace);
}
var nodeId = new NodeId(identifierType,value,namespace);
return nodeId;
}
exports.coerceNodeId = coerceNodeId;



var makeNodeId = function makeNodeId(value,namespace) {

value = value || 0;
namespace = namespace || 0;

var identifierType = NodeIdType.NUMERIC;
if (typeof value == "string" ) {
// 1 2 3
// 012345678901234567890123456789012345
// "72962B91-FA75-4AE6-8D28-B404DC7DAF63"
if (is_guid(value)) {
identifierType= NodeIdType.GUID;
} else {
identifierType= NodeIdType.STRING;
}

} else if ( value instanceof Buffer) {
identifierType= NodeIdType.BYTESTRING;
}

var nodeId = new NodeId(identifierType,value,namespace);

assert(nodeId.hasOwnProperty("identifierType"));

return nodeId;
};

exports.makeNodeId = makeNodeId;


function ExpandedNodeId(identifierType,value,namespace,namespaceUri,serverIndex) {
NodeId.apply(this,arguments);
this.namespaceUri = namespaceUri;
this.serverIndex = serverIndex;
}
util.inherits(ExpandedNodeId,NodeId);
exports.ExpandedNodeId = ExpandedNodeId;

//------
// An ExpandedNodeId extends the NodeId structure by allowing the NamespaceUri to be
// explicitly specified instead of using the NamespaceIndex. The NamespaceUri is optional. If it
// is specified then the NamespaceIndex inside the NodeId shall be ignored.
//
// The ExpandedNodeId is encoded by first encoding a NodeId as described in Clause 5 .2.2.9
// and then encoding NamespaceUri as a String.
//
// An instance of an ExpandedNodeId may still use the NamespaceIndex instead of the
// NamespaceUri. In this case, the NamespaceUri is not encoded in the stream. The presence of
// the NamespaceUri in the stream is indicated by setting the NamespaceUri flag in the encoding
// format byte for the NodeId.
//
// If the NamespaceUri is present then the encoder shall encode the NamespaceIndex as 0 in
// the stream when the NodeId portion is encoded. The unused NamespaceIndex is included in
// the stream for consistency,
//
// An ExpandedNodeId may also have a ServerIndex which is encoded as a UInt32 after the
// NamespaceUri. The ServerIndex flag in the NodeId encoding byte indicates whether the
// ServerIndex is present in the stream. The ServerIndex is omitted if it is equal to zero.
//
exports.makeExpandedNodeId = function makeExpandedNodeId(value,namespace) {

value = parseInt(value) || 0;
namespace = namespace || 0;
serverIndex = 0;
namespaceUri = null;
return new ExpandedNodeId(NodeIdType.NUMERIC,value,namespace,namespaceUri,serverIndex);
};
3 changes: 2 additions & 1 deletion lib/opcua-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ OPCUAClient.prototype._activateSession = function(session,callback) {
*/
OPCUAClient.prototype.createSession = function(callback) {

assert(_.isFunction(callback));
var self = this;

self._createSession(function(err,session){
Expand All @@ -398,7 +399,7 @@ OPCUAClient.prototype.createSession = function(callback) {

OPCUAClient.prototype._closeSession= function(session,callback) {

assert(typeof(callback) === "function");
assert(_.isFunction(callback));
if (!this._secureChannel) {
callback(new Error(" No secure channel"));
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"optimist": "~0.6.0",
"prettyjson": "~0.11.1",
"treeify": "~1.0.1",
"coveralls": "~2.7.0",
"coveralls": "*",
"mocha-lcov-reporter": "0.0.1",
"node-int64": "~0.3.0",
"easy-table": "~0.3.0",
Expand Down

0 comments on commit 764725d

Please sign in to comment.