Commit 79740cfc authored by nanocryk's avatar nanocryk
Browse files

feature : WebOfTrust interface; MemoryWebOfTrust and AtomicFileWebOfTrust classes

parent 479f7082
let native: any = require('../native');
let WebOfTrust = native.WebOfTrust;
let NativeWebOfTrust = native.WebOfTrust;
interface WebOfTrust {
toFile(path: String): Boolean;
addNode(): Number;
removeNode(): Number;
getSentries(d_min: Number): Array<Number>;
getNonSentries(d_min: Number): Array<Number>;
getDisabled(): Array<Number>;
getPaths(from: Number, to: Number, k_max: Number): Array<Array<Number>>;
getWoTSize(): Number;
isEnabled(node: Number): Boolean;
setEnabled(node: Number, state: Boolean): Boolean;
addLink(from: Number, to: Number): Number;
removeLink(from: Number, to: Number): Number;
existsLink(from: Number, to: Number): Boolean;
isOutdistanced(node: Number, d_min: Number, d_max: Number, x_percent: Number): Boolean;
setMaxCert(max_cert: Number): void;
getMaxCert(): Number;
}
class MemoryWebOfTrust implements WebOfTrust {
private wot: any;
constructor (max_cert: Number) {
this.wot = new NativeWebOfTrust(max_cert);
}
toFile(path: String): Boolean {
return this.wot.toFile(path)
}
addNode(): Number {
return this.wot.addNode()
}
removeNode(): Number {
return this.wot.removeNode()
}
getSentries(d_min: Number): Array<Number> {
return this.wot.getSentries(d_min)
}
getNonSentries(d_min: Number): Array<Number> {
return this.wot.getNonSentries(d_min)
}
getDisabled(): Array<Number> {
return this.wot.getDisabled()
}
getPaths(from: Number, to: Number, k_max: Number): Array<Array<Number>> {
return this.wot.getPaths(from, to, k_max)
}
getWoTSize(): Number {
return this.wot.getWoTSize()
}
isEnabled(node: Number): Boolean {
return this.wot.isEnabled()
}
setEnabled(node: Number, state: Boolean): Boolean {
return this.wot.setEnabled(node, state)
}
addLink(from: Number, to: Number): Number {
return this.wot.addLink(from, to)
}
removeLink(from: Number, to: Number): Number {
return this.wot.removeLink(from, to)
}
existsLink(from: Number, to: Number): Boolean {
return this.wot.existsLink(from, to)
}
isOutdistanced(node: Number, d_min: Number, d_max: Number, x_percent: Number): Boolean {
return this.wot.isOutdistanced(node, d_min, d_max, x_percent)
}
setMaxCert(max_cert: Number): void {
return this.wot.setMaxCert(max_cert)
}
getMaxCert(): Number {
return this.wot.getMaxCert()
}
}
class AtomicFileWebOfTrust implements WebOfTrust {
private wot: any;
private path: String;
constructor (path: String) {
this.wot = new NativeWebOfTrust(path);
this.path = path;
}
toFile(path: String): Boolean {
return this.wot.toFile(path)
}
addNode(): Number {
let result = this.wot.addNode()
if (!this.toFile(this.path)) throw new Error("Error while writing file")
return result
}
removeNode(): Number {
let result = this.wot.removeNode()
if (!this.toFile(this.path)) throw new Error("Error while writing file")
return result
}
getSentries(d_min: Number): Array<Number> {
return this.wot.getSentries(d_min)
}
getNonSentries(d_min: Number): Array<Number> {
return this.wot.getNonSentries(d_min)
}
getDisabled(): Array<Number> {
return this.wot.getDisabled()
}
getPaths(from: Number, to: Number, k_max: Number): Array<Array<Number>> {
return this.wot.getPaths(from, to, k_max)
}
getWoTSize(): Number {
return this.wot.getWoTSize()
}
isEnabled(node: Number): Boolean {
return this.wot.isEnabled()
}
setEnabled(node: Number, state: Boolean): Boolean {
let result = this.wot.setEnabled(node, state)
if (!this.toFile(this.path)) throw new Error("Error while writing file")
return result
}
addLink(from: Number, to: Number): Number {
let result = this.wot.addLink(from, to)
if (!this.toFile(this.path)) throw new Error("Error while writing file")
return result
}
removeLink(from: Number, to: Number): Number {
let result = this.wot.removeLink(from, to)
if (!this.toFile(this.path)) throw new Error("Error while writing file")
return result
}
existsLink(from: Number, to: Number): Boolean {
return this.wot.existsLink(from, to)
}
isOutdistanced(node: Number, d_min: Number, d_max: Number, x_percent: Number): Boolean {
return this.wot.isOutdistanced(node, d_min, d_max, x_percent)
}
setMaxCert(max_cert: Number): void {
this.wot.setMaxCert(max_cert)
if (!this.toFile(this.path)) throw new Error("Error while writing file")
}
getMaxCert(): Number {
return this.wot.getMaxCert()
}
}
{
let wot = new WebOfTrust(3);
let wot = new MemoryWebOfTrust(3);
console.log(wot.getMaxCert())
wot.setMaxCert(4);
console.log(wot.getMaxCert())
......@@ -13,9 +190,11 @@ let WebOfTrust = native.WebOfTrust;
console.log("-----")
{
let wot = new WebOfTrust("hey.wot");
let wot = new AtomicFileWebOfTrust("hey.wot");
console.log(wot.getMaxCert())
console.log(wot.addNode());
console.log(wot.getWoTSize());
console.log(wot.toFile("hey.wot"));
//console.log(wot.toFile("hey.wot"));
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment