Skip to content

Commit

Permalink
Enabled back PZP test in npm test
Browse files Browse the repository at this point in the history
Jira Issue: WP-794
  • Loading branch information
habibvirji committed Apr 2, 2013
1 parent 88a03a5 commit 078b21b
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
69 changes: 69 additions & 0 deletions webinos/core/pzp/lib/pzp_connectPeer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

var PzpClient = function () {
"use strict";
var PzpCommon = require("./pzp.js");
var logger = PzpCommon.wUtil.webinosLogging (__filename) || console;
/**
*
*/
function pzpClient_PeerCleanup() {
var path = require("path");
var fs = require("fs");
var existsSync = fs.existsSync || path.existsSync;
logger.log("Clean up SiB leftovers");
var own = path.join(parent.config.metaData.webinosRoot, "keys", "conn.pem");
var other = path.join(parent.config.metaData.webinosRoot, "keys", "otherconn.pem");
var exlist = path.join(parent.config.metaData.webinosRoot, "exCertList.json");
if(existsSync(own)) {
fs.unlink(own, function(err){
if(err) throw err;
logger.log("removed" + own);
});
}
if(existsSync(other)) {
fs.unlink(other, function(err){
if(err) throw err;
logger.log("removed" + other);
});
}
if(existsSync(exlist)) {
fs.unlink(exlist, function(err){
if(err) throw err;
logger.log("removed - " + exlist);
});
}
parent.pzp_state.connectingPeerAddr = "";
}
/**
* Connect Peer PZPs. This is either triggered by PZH sending PZPUpdate message or else from PZP local discovery
* @param msg - msg is an object containing port, address and name of PZP to be connected
*/
this.connectPeer = function (msg) {
parent.setConnectionParameters(function (options) {
var name = msg.name, n, client;
if(name && (n = name.indexOf("/"))) options.servername = name.substring(0, n);
client = require("tls").connect(parent.getPorts().pzp_tlsServer, msg.address, options, function () {
if (client.authorized) {
parent.handlePeerAuthorization(msg.name, client);
pzpClient_PeerCleanup();
} else {
logger.error("pzp client - connection failed, " + client.authorizationError);
}
});

client.on("data", function (buffer) {
parent.handleMsg(client, buffer);
});

client.on("end", function () {
parent.cleanUp(client.id);
});

client.on("error", function (err) {
logger.error(err.message);
});
});
}
};

module.exports = PzpClient;
71 changes: 71 additions & 0 deletions webinos/core/pzp/lib/pzp_connectPzh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

/**
* Connects with PZH and handle respective events
*/
var PzpOtherManager = require("./pzp_otherManager.js");
var PzpCommon = require("./pzp.js");

var PzpConnectHub = function () {
"use strict";
PzpOtherManager.call(this);
var self = this;
var logger = PzpCommon.wutil.webinosLogging (__filename) || console;

/**
* If PZP fails to connect to PZH, this tries to connect back to PZH
*/
function retryConnecting () {
if (self.getEnrolledStatus()) {
setTimeout (function () {
self.connect (function (status) {
logger.log ("retrying to connect back to the PZH " + (status ? "successful" : "failed"));
});
}, 60000);//increase time limit to suggest when it should retry connecting back to the PZH
}
}
/**
*
* @param callback
*/
this.connectHub = function (callback) {
var pzpClient;
try {
self.setConnectionParameters(function (certificateConfiguration) {
pzpClient = require("tls").connect(parent.getPorts().provider,
self.getServerAddress(),
certificateConfiguration, function() {
logger.log ("connection to pzh status: " + pzpClient.authorized);
if (pzpClient.authorized) {
authenticated (pzpClient, callback);
} else {
unauthenticated (pzpClient, callback);
}
});
pzpClient.setTimeout(100);

pzpClient.on ("data", function (buffer) {
handleMsg(pzpClient, buffer);
});

pzpClient.on ("close", function(had_error) {
if(had_error) {
logger.log("transmission error lead to disconnect");
}
});
pzpClient.on ("end", function() {
if (pzpClient.id) cleanUp(pzpClient.id);
retryConnecting();
});

pzpClient.on ("error", function(err) {
handlePzpError(err);
});
});
} catch (err) {
logger.error ("Connecting Personal Zone Hub Failed : " + err);
}
}
};

require("util").inherits(PzpConnectHub, PzpOtherManager);
module.exports = PzpConnectHub;

0 comments on commit 078b21b

Please sign in to comment.