This repository has been archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
135 lines (118 loc) · 3.52 KB
/
main.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
/**
(c) Copyright 2019 Locha Mesh developers
Licensed under the Apache License 2.0
*/
'use strict';
const node = require('child_process');
const api = node.fork(`${__dirname}/wServer/main.js`);
const pms = node.fork(`${__dirname}/pManager/main.js`);
/**
* define a new peerList and execute operations in each element
*/
class peerList {
constructor(data) {
// data.list: [{id: 0, options: {}}, {id: 1, options: {}}]
this.peer_list = data.list;
}
init() {
// TODO execute [this.peer_list] list of nodes.
// ;; this func. can be auto initialized when the list is created.
}
update(n) {
// TODO execute the (n) list of nodes.
// TODO update characteristics of the (n) list of nodes.
// each node in the list must be equal an a valid node.
}
delete(n) {
// TODO delete the (n) list of nodes.
}
purge() {
// TODO stop and delete every peer in [this.peer_list]
}
}
var ioHandler = function (msg) {
switch (msg.event) {
case 'peer_list':
// data.cmd : is a command to exec.
// for ex: put, update, delete.
// data.list: is a JSON containing definitions of each node to create, update or delete.
// pass to class peer_list.
console.log('nodes: ', msg);
break;
case 'client_app':
console.log('client_app', msg);
case 'connection':
console.log('client_connected:', msg.data);
// next: waiting for commands
break;
case 'disconnect':
console.log('client_disconnect:', msg.data);
// next: verify if each subprocess created was stopped.
break;
default:
console.log(msg);
//break;
}
};
var wsHandler = function (msg) {
switch (msg.event) {
case 'listen':
console.log('open: http://localhost:%s', msg.data.port);
break;
default:
console.log(msg);
break;
}
};
/* web-server process event handler */
api.on('close', (code, signal) => {
// TODO
// - verify if a list is created, if yes: stop and delete each process.
console.log(api.pid, 'web_server - close', code, signal);
});
api.on('disconnect', (code, signal) => {
// TODO
// - verify if a list is created, if yes: stop and delete each process.
console.log(api.pid, 'web_server - disconnect', code, signal);
});
api.on('error', (error) => {
// TODO
// - verify if a list is created, if yes: stop and delete each process.
console.log(api.pid, 'web_server - error', error);
});
api.on('exit', (code, signal) => {
// TODO
// - clean exit?
console.log(api.pid, 'web_server - exit', code, signal);
});
api.on('message', (msg) => {
switch (msg.type) {
case 'socket_io':
ioHandler(msg);
break;
case 'web_server':
wsHandler(msg);
break;
default:
console.warn('Unknow messge type: %s', msg.type);
break;
}
});
/*
// process manger service event handler
pms.on('close', (code, signal) => {
console.log(api.pid, 'pms- close', code, signal);
});
pms.on('disconnect', (code, signal) => {
console.log(api.pid, 'pms - disconnect', code, signal);
});
pms.on('error', (error) => {
console.log(api.pid, 'pms - error', error);
});
pms.on('exit', (code, signal) => {
console.log(api.pid, 'pms - exit', code, signal);
});
pms.on('message', (msg) => {
console.warn(msg);
});
*/