<commit_before>/*******************************************************************************
*  Code contributed to the webinos project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2011 University of Oxford
* Copyright 2011 <NAME>, Samsung Electronics (UK) Ltd
********************************************************************************/

// This file is a wrapper / facade for all the functionality that the PZH
// web interface is likely to need.

// Ideally, this would be accessed through the same messaging interface
// as everything else, as these need some access control too.
// In other words: due a refactor.

var pzhapis     = exports;

var path        = require("path");
var fs          = require("fs");
var util        = require("util");
var crypto      = require("crypto");

var webinos = require("webinos")(__dirname);
var session = webinos.global.require(webinos.global.pzp.location, "lib/session");
var qrcode  = require("./pzh_qrcode.js");
var revoke  = require("./pzh_revoke.js");

var farm    = require("./pzh_farm.js");

var pzh_internal_apis = exports;

// Synchronous method for getting information about a PZP with a certain ID.
function getPzpInfoSync(instance, pzpId) {
  "use strict";
  //find out whether we have this PZP in a session somewhere.
  var pzpConnected = false;
  var pzpName = pzpId;
  for (var i in instance.connectedPzp) {
    if (instance.connectedPzp.hasOwnProperty(i)) {
        //session IDs append the PZH to the front of the PZP ID.
      var splitId = i.split("/");
      if (splitId.length > 1 && splitId[1] !== null) {
        if (splitId[1] === pzpId) {
          pzpConnected = true;
          pzpName = i;
        }
      }
    }
  }

  return {
    id          : pzpId,
    cname       : pzpName,
    isConnected : pzpConnected
  };
}

// Synchronous method for getting information about a PZH with a certain ID.
function getPzhInfoSync(instance, pzhId) {
  "use strict";
  if (pzhId && instance && instance.config && instance.config.name &&
      pzhId.split("_")[0] === instance.config.name) {
    //we know that this PZH is alive
    return {
      id : pzhId + " (Your PZH)",
      url: pzhId,
      isConnected: true
    };
  } else {
    if(instance.connectedPzh) {
      for (var i in instance.connectedPzh) {
        if (instance.connectedPzh.hasOwnProperty(i)) {
          //session IDs append the PZH to the front of the PZP ID.
          if (i === pzhId.split('/')[1]) {
            return {
              id          : i,
              url         : pzhId,
              isConnected : true
            };
          }
        }
      }
    }
    // We did not find pzh connected
     return {
      id          : pzhId.split("/")[1],
      url         : pzhId,
      isConnected : false
    };
  }
}

// Wrapper for adding a new PZP to a personal zone through a short code
pzh_internal_apis.addPzpQR = function (instance, callback) {
  "use strict";
  qrcode.addPzpQRAgain(instance, callback);
};


pzh_internal_apis.listPzp = function(instance, callback) {
  "use strict";
  var result = {signedCert: [], revokedCert: []};
  if (instance && instance.config) {
    if(instance.config.signedCert) {
      for (var i in instance.config.signedCert){
        if (instance.config.signedCert.hasOwnProperty(i)) {
          result.signedCert.push(getPzpInfoSync(instance, i));
        }
      }
    }
    if (instance.config.revokedCert) {
      for (var i in instance.config.revokedCert) {
        if (instance.config.revokedCert.hasOwnProperty(i)) {
          result.revokedCert.push(getPzpInfoSync(instance, i));
        }
      }
    }
    if (instance.config.serverName) {
      var payload = {to: instance.config.serverName, cmd:"listPzp", payload:result};
      callback(payload);
    } else {
      callback();
    }
  }
};


// Get a list of all Personal zone devices.
pzh_internal_apis.listZoneDevices = function(instance, callback) {
  "use strict";
  var result = {pzps: [], pzhs: []};
  if (instance && instance.config) {
    if (instance.config.signedCert) {
      for (var i in instance.config.signedCert){
        if (instance.config.signedCert.hasOwnProperty(i)) {
          result.pzps.push(getPzpInfoSync(instance, i));
        }
      }
    }
    if(instance.config.otherCert) {
      for (var i in instance.config.otherCert) {
        if (instance.config.otherCert.hasOwnProperty(i)
        && instance.config.otherCert[i].cert !== "") {
          result.pzhs.push(getPzhInfoSync(instance, i));
        }
      }
    }
    result.pzhs.push(getPzhInfoSync(instance, instance.sessionId));
    if (instance.config.serverName) {
      var payload = {to: instance.config.serverName, cmd:"listDevices", payload:result};
      callback(payload);
    } else {
      callback();
    }
  }
};<commit_msg>Add service registration methods to PZH internal API<commit_after>/*******************************************************************************
*  Code contributed to the webinos project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2011 University of Oxford
* Copyright 2011 <NAME>, Samsung Electronics (UK) Ltd
********************************************************************************/

// This file is a wrapper / facade for all the functionality that the PZH
// web interface is likely to need.

// Ideally, this would be accessed through the same messaging interface
// as everything else, as these need some access control too.
// In other words: due a refactor.

var pzhapis     = exports;

var path        = require("path");
var fs          = require("fs");
var util        = require("util");
var crypto      = require("crypto");

var webinos = require("webinos")(__dirname);
var session = webinos.global.require(webinos.global.pzp.location, "lib/session");
var qrcode  = require("./pzh_qrcode.js");
var revoke  = require("./pzh_revoke.js");

var farm    = require("./pzh_farm.js");

var pzh_internal_apis = exports;

// Synchronous method for getting information about a PZP with a certain ID.
function getPzpInfoSync(instance, pzpId) {
  "use strict";
  //find out whether we have this PZP in a session somewhere.
  var pzpConnected = false;
  var pzpName = pzpId;
  for (var i in instance.connectedPzp) {
    if (instance.connectedPzp.hasOwnProperty(i)) {
        //session IDs append the PZH to the front of the PZP ID.
      var splitId = i.split("/");
      if (splitId.length > 1 && splitId[1] !== null) {
        if (splitId[1] === pzpId) {
          pzpConnected = true;
          pzpName = i;
        }
      }
    }
  }

  return {
    id          : pzpId,
    cname       : pzpName,
    isConnected : pzpConnected
  };
}

// Synchronous method for getting information about a PZH with a certain ID.
function getPzhInfoSync(instance, pzhId) {
  "use strict";
  if (pzhId && instance && instance.config && instance.config.name &&
      pzhId.split("_")[0] === instance.config.name) {
    //we know that this PZH is alive
    return {
      id : pzhId + " (Your PZH)",
      url: pzhId,
      isConnected: true
    };
  } else {
    if(instance.connectedPzh) {
      for (var i in instance.connectedPzh) {
        if (instance.connectedPzh.hasOwnProperty(i)) {
          //session IDs append the PZH to the front of the PZP ID.
          if (i === pzhId.split('/')[1]) {
            return {
              id          : i,
              url         : pzhId,
              isConnected : true
            };
          }
        }
      }
    }
    // We did not find pzh connected
     return {
      id          : pzhId.split("/")[1],
      url         : pzhId,
      isConnected : false
    };
  }
}

// Wrapper for adding a new PZP to a personal zone through a short code
pzh_internal_apis.addPzpQR = function (instance, callback) {
  "use strict";
  qrcode.addPzpQRAgain(instance, callback);
};


pzh_internal_apis.listPzp = function(instance, callback) {
  "use strict";
  var result = {signedCert: [], revokedCert: []};
  if (instance && instance.config) {
    if(instance.config.signedCert) {
      for (var i in instance.config.signedCert){
        if (instance.config.signedCert.hasOwnProperty(i)) {
          result.signedCert.push(getPzpInfoSync(instance, i));
        }
      }
    }
    if (instance.config.revokedCert) {
      for (var i in instance.config.revokedCert) {
        if (instance.config.revokedCert.hasOwnProperty(i)) {
          result.revokedCert.push(getPzpInfoSync(instance, i));
        }
      }
    }
    if (instance.config.serverName) {
      var payload = {to: instance.config.serverName, cmd:"listPzp", payload:result};
      callback(payload);
    } else {
      callback();
    }
  }
};

pzh_internal_apis.listAllServices = function(instance, callback) {
  "use strict";
  var result = {pzEntityList: []};
  result.pzEntityList.push({pzId:instance.sessionId});
  for (var i in instance.config.signedCert){
    if (typeof instance.config.signedCert[i] !== "undefined") {
      result.pzEntityList.push({pzId:getPzpInfoSync(instance, i).cname});
    }
  }

  result.services = instance.discovery.getAllServices();

  var payload = {to: instance.config.serverName, cmd:"listAllServices", payload:result};
  callback(payload);
};

pzh_internal_apis.listUnregServices = function(instance, at, callback) {
  "use strict";

  function runCallback(pzEntityId, modules) {
	  var result = {
	    "pzEntityId": pzEntityId,
	    "modules"   : modules
	  };
	  callback({to: instance.config.serverName, cmd:"listUnregServices", payload:result});
  }

  if (instance.sessionId !== at) {
    var id = instance.addMsgListener(function(modules) {
      runCallback(at, modules);
    });
    var msg = instance.prepMsg(instance.sessionId, at, "listUnregServices", {listenerId:id});
    instance.sendMessage(msg, at);
  } else {
    runCallback(instance.sessionId, instance.getInitModules());
  }
};

pzh_internal_apis.registerService = function(instance, at, name, callback) {
  "use strict";

  if (instance.sessionId !== at) {
    var msg = instance.prepMsg(instance.sessionId, at, "registerService", {name:name, params:{}});
    instance.sendMessage(msg, at);
  } else {
    instance.registry.loadModule({"name":name, "params":{}}, instance.rpcHandler);
  }
};

pzh_internal_apis.unregisterService = function(instance, at, svId, svAPI, callback) {
  "use strict";

  if (instance.sessionId !== at) {
    var msg = instance.prepMsg(instance.sessionId, at, "unregisterService", {svId:svId, svAPI:svAPI});
    instance.sendMessage(msg, at);
  } else {
    instance.registry.unregisterObject({"id":svId, "api":svAPI});
  }
};

// Get a list of all Personal zone devices.
pzh_internal_apis.listZoneDevices = function(instance, callback) {
  "use strict";
  var result = {pzps: [], pzhs: []};
  if (instance && instance.config) {
    if (instance.config.signedCert) {
      for (var i in instance.config.signedCert){
        if (instance.config.signedCert.hasOwnProperty(i)) {
          result.pzps.push(getPzpInfoSync(instance, i));
        }
      }
    }
    if(instance.config.otherCert) {
      for (var i in instance.config.otherCert) {
        if (instance.config.otherCert.hasOwnProperty(i)
        && instance.config.otherCert[i].cert !== "") {
          result.pzhs.push(getPzhInfoSync(instance, i));
        }
      }
    }
    result.pzhs.push(getPzhInfoSync(instance, instance.sessionId));
    if (instance.config.serverName) {
      var payload = {to: instance.config.serverName, cmd:"listDevices", payload:result};
      callback(payload);
    } else {
      callback();
    }
  }
};
{'fraction_non_alphanumeric': 0.145451648876852, 'fraction_numerical': 0.002867611916520631, 'mean_word_length': 2.8930232558139535, 'pattern_counts': {'":': 5, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5883689', 'n_tokens_mistral': 3901, 'n_tokens_neox': 3834, 'n_words': 1032}