Skip to content

Commit

Permalink
add bytesRead bytesWritten on client.
Browse files Browse the repository at this point in the history
fix channel deletion ( by calling removeAllListeners)
  • Loading branch information
erossignon committed Aug 17, 2014
1 parent 7a655a9 commit a43667b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
29 changes: 27 additions & 2 deletions lib/client/client_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,12 @@ OPCUAClientBase.prototype.connect = function (endpoint_url, callback) {
debugLog(" OPCUAClientBase emitting close".yellow.bold,err);
/// console.log("xxxx OPCUAClientBase emitting close".yellow.bold,err);
/**
* @@event close
* @event close
* @param error {Error}
*/
self.emit("close",err);

self._secureChannel.removeAllListeners();
self._secureChannel = null;
});

Expand All @@ -143,13 +145,16 @@ OPCUAClientBase.prototype.connect = function (endpoint_url, callback) {

self._secureChannel.create(endpoint_url, function (err) {
if (err) {
//console.log("XXXXXXXXXX".yellow.bold,err);
self._secureChannel.removeAllListeners();
self._secureChannel = null;
}
_inner_callback(err);
});
},
//------------------------------------------------- STEP 3 : GetEndpointsRequest
function (_inner_callback) {
assert(self._secureChannel !== null);
self.getEndPointRequest(function (err, endpoints) {
_inner_callback(err);
});
Expand Down Expand Up @@ -210,7 +215,10 @@ OPCUAClientBase.prototype.getEndPointRequest = function (callback) {

var self = this;
if (!self._secureChannel) {
return callback(new Error("Invalid Secure Channel"));
setImmediate(function() {
callback(new Error("Invalid Secure Channel"));
});
return;
}
assert(self._secureChannel); // must have a secure channel
// OpenSecureChannel
Expand Down Expand Up @@ -270,6 +278,8 @@ OPCUAClientBase.prototype.disconnect = function (callback) {
var self = this;
if (self._secureChannel) {
var tmp_channel = self._secureChannel;

self._secureChannel.removeAllListeners();
self._secureChannel = null;
tmp_channel.close(function () {
/**
Expand All @@ -282,4 +292,19 @@ OPCUAClientBase.prototype.disconnect = function (callback) {
callback();
}
};

OPCUAClientBase.prototype.__defineGetter__("bytesRead",function() {
var self = this;
return self._secureChannel ? self._secureChannel.bytesRead :0;
});

OPCUAClientBase.prototype.__defineGetter__("bytesWritten",function() {
var self = this;
return self._secureChannel ? self._secureChannel.bytesWritten :0;
});
OPCUAClientBase.prototype.__defineGetter__("transactionsPerformed", function() {
var self = this;
return self._secureChannel ? self._secureChannel.transactionsPerformed :0;
});

exports.OPCUAClientBase = OPCUAClientBase;
10 changes: 8 additions & 2 deletions lib/client/opcua_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,14 @@ OPCUAClient.prototype._closeSession = function (session, callback) {
callback(err, null);
});
} else {
//xx s.CloseSessionResponse
self._secureChannel.close(callback);
if (self._secureChannel) {
//xx s.CloseSessionResponse
self._secureChannel.close(callback);
} else {
// -> _secureChannel may have been closed already
// this could happen if _closeSession is called twice
callback(new Error(" Close session already called "))
}
}
});
};
Expand Down
37 changes: 37 additions & 0 deletions test/client/test_client_secure_channel_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,41 @@ describe("testing ClientSecureChannelLayer ",function(){

});

it("should expose the total number of bytes read and written",function(done){

var mock = new MockTransport([
// ---------------------------------------------------- Transaction 1
// Client will send a HEl_message
// Server will reply with this :
opcua.packTcpMessage("ACK", fake_AcknowledgeMessage),

// ---------------------------------------------------- Transaction 2
// client will send a "OPN" OpenSecureChannelRequest
// Server will reply with this:
require("../fixtures/fixture_full_tcp_packets").packet_sc_2,

// ---------------------------------------------------- Transaction 3
// client will send a "CLO" CloseSecureChannelRequest
// Server will close the socket, without sending a response
function() { this.fake_socket.server.end(); }

],done);

require("../../lib/transport/tcp_transport").setFakeTransport(mock.fake_socket.client);
var secureChannel = new ClientSecureChannelLayer();

secureChannel.bytesRead.should.equal(0);
secureChannel.bytesWritten.should.equal(0);

secureChannel.create("fake://localhost:2033/SomeAddress",function(err){

secureChannel.bytesRead.should.be.greaterThan(0);
secureChannel.bytesWritten.should.be.greaterThan(0);

secureChannel.close(function(err){
done(err);
});
});

});
});

0 comments on commit a43667b

Please sign in to comment.