-
Notifications
You must be signed in to change notification settings - Fork 0
/
TMNet.js
135 lines (108 loc) · 3.38 KB
/
TMNet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* TMNet.js */
'use strict';
var fs = require('fs');
const TMNode = require('./TMNode')
const TMutils = require('./TMutils')
const MODE = TMutils.MODE
/***** Constructor *****/
//TODO: add 'options' parameter {numnodes}
function TMNet(tm){
if(!tm) throw "Missing TM instance"
this.tm = tm
this.tmnodes = {}
this.log = this.tm.tms.log
this.loadTMNet()
}
/* Load nodes data frome file */
TMNet.prototype.loadTMNet = function(){
var dir = this.tm.dir
var files = fs.readdirSync(dir)
var self = this
/* Read node files (.dat) in the data folder */
files.forEach(function(file){
if(TMutils.getFileExtension(file) == 'dat')
try {
var nodeData = TMutils.loadObject(dir+'/'+file)
self.addTMNode(nodeData, MODE.DEFAULT)
} catch(e) { self.log.warn("Failed to load "+file+": "+e); }
})
this.log.info("'"+this.tm.name+"' network loaded")
if(Object.keys(this.tmnodes).length == 0)
this.log.warn("Network is empty")
}
/* Returns the status of the network */
TMNet.prototype.getStatus = function(){
var status = {}
status.name = this.tm.name
status.nodes = []
for(var id in this.tmnodes)
status.nodes.push({id:id, address: this.tmnodes[id].addr})
return status
}
/* Creates a new dynamic node ID */
TMNet.prototype._generateID = function(base){
if(!base) base = 'N'
var maxn = 1
for(var id in this.tmnodes){
if(id.startsWith(base)){
var idnum = id.substring(base.length,id.length)
if(TMutils.isNum(idnum) && idnum >= maxn) maxn = parseInt(idnum)+1
}
}
return base+maxn
}
/* Adds a TMNode to this network */
TMNet.prototype.addTMNode = function(nodeData, mode){
if(!nodeData.id){
if(mode == MODE.TMP) nodeData.id = this._generateID('TMP')
else nodeData.id = this._generateID()
}
var node = new TMNode(this, nodeData, mode)
this.tmnodes[nodeData.id] = node
return {id:node.id, address:node.addr}
}
/* Remove a TMNode of this network */
//TODO: opt MODE.KEEP (keep file)
TMNet.prototype.removeTMNode = function(id){
if(!this.isTMNodeID(id)) throw "ERR: Invalid ID"
this.getNode(id).destroy()
delete this.tmnodes[id]
}
/* Returns the node object */
TMNet.prototype.getNode = function(id){
if(!id) throw "ERR: ID is required"
if(this.tmnodes[id]) return this.tmnodes[id];
else return null
}
/* Return the node ID for a specific BTC address */
TMNet.prototype.getNodeID = function(addr){
if(!addr) throw "ERR: addr is required"
for(var id in this.tmnodes)
if(this.tmnodes[id].getAddr() == addr) return id
return null
};
/* Return the BTC address of a node */
TMNet.prototype.getNodeAddress = function(id){
if(!id) throw "ERR: ID is required"
if(this.isTMNodeAddr(id)) return id
if(this.tmnodes[id])
return this.tmnodes[id].getAddr()
return null
};
/* Returns true if 'node' corresponds to a node (ID/address) in this network */
TMNet.prototype.isTMNode = function(node){
if(this.isTMNodeAddr(node) || this.isTMNodeID(node)) return true
return false
}
/* Returns true if the ID corresponds to a node in this network */
TMNet.prototype.isTMNodeID = function(id){
if(this.getNode(id)) return true
return false
}
/* Returns true if the address corresponds to a node in this network */
TMNet.prototype.isTMNodeAddr = function(addr){
if(this.getNodeID(addr)) return true
return false
}
/*****************************************************************************/
module.exports = TMNet;