-
Notifications
You must be signed in to change notification settings - Fork 1
/
multicast.js
142 lines (132 loc) · 4.65 KB
/
multicast.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
136
137
138
139
140
141
142
var dgram = require('dgram');
const os = require("os");
module.exports = class Multicast {
constructor(app) {
this.app = app;
this.address = "224.0.255.255";
this.port = 3000;
this.socket = null;
this.uid = "robot_" + (new Date().getTime());
}
init(){
this.socket = dgram.createSocket({
type: 'udp4',
reuseAddr: true
})
//Check IP
let hasNetwork = false;
const interfaces = os.networkInterfaces();
for (let key in interfaces) {
let ips = interfaces[key].filter((iface) => {
return iface.family === "IPv4" && iface.internal === false;
}).map((iface) => { return iface.address; });
for(let ip of ips){
hasNetwork=true;
this.app.logger.log("IP "+ip);
}
}
if(!hasNetwork){
this.app.logger.log("No Network for multicast");
return;
}
this.socket.bind(this.port)
this.socket.on('message', (msg, remote) => {
this.onMessage(msg, remote);
})
this.socket.on("listening", () => {
//this.socket.setBroadcast(true)
this.socket.setMulticastTTL(128)
this.socket.addMembership(this.address)
console.log('Multicast listening . . . ')
})
this.socket.on('error', (exception) => {
console.log('Multicast Error :', exception);
});
}
async close(){
try{
if(this.socket) this.socket.dropMembership(this.address);
}catch(e){}
try{
if(this.socket) this.socket.close();
}catch(e){}
}
onMessage(packet, remote){
//Parse input message
let msg = null;
try{
msg = JSON.parse(packet.toString());
} catch(e){
console.log("Failed to parse multicast message", ""+packet.toString())
msg = null;
}
if(msg === null) return;
if(!msg.command) return;
if(!msg.uid || msg.uid == this.uid) return;
console.log("Multicast IN:", packet.toString());
delete msg.uid;
if(msg.command == "removeComponent"){
if(!msg.name) return;
if(!msg.type) return;
let cmp = this.app.map.getComponentByName(msg.name, msg.type);
if(cmp) this.app.map.removeComponent(cmp)
console.log("removing", msg.name, msg.type);
}
if(msg.command == "addComponent"){
if(!msg.name || !msg.type || !msg.shape) return;
delete msg.command;
this.app.map.addComponent(msg);
}
if(msg.command == "updateComponent"){
if(!msg.component || !msg.component.name || !msg.component.type || !msg.diff) return;
delete msg.command;
let cmp = this.app.map.getComponentByName(msg.component.name, msg.component.type);
if(cmp) Object.assign(cmp, msg.diff);
console.log("updating", msg.component.name, msg.component.type);
}
}
sendRemoveComponent(parameters){
if(!this.socket || !parameters.name) return false;
let cmd = {
uid: this.uid,
command: "removeComponent",
name: parameters.name,
type: parameters.type || "",
team: parameters.team || ""
}
const data = JSON.stringify(cmd);
//console.log("Multicast send", data);
try{
this.socket.send(data, 0, data.length, this.port, this.address);
}catch(e){}
return true;
}
sendAddComponent(parameters){
if(!this.socket || !parameters.name || !parameters.type || !parameters.shape) return false;
let cmd = Object.assign({}, parameters);
cmd.uid = this.uid;
cmd.command = "addComponent";
const data = JSON.stringify(cmd);
//console.log("Multicast send", data);
try{
this.socket.send(data, 0, data.length, this.port, this.address);
}catch(e){}
return true;
}
sendUpdateComponent(parameters){
if(!this.socket || !parameters.component || !parameters.diff) return false;
let cmd = Object.assign({}, parameters);
cmd.uid = this.uid;
cmd.command = "updateComponent";
const data = JSON.stringify(cmd);
//console.log("Multicast send", data);
try{
this.socket.send(data, 0, data.length, this.port, this.address);
}catch(e){}
return true;
}
}
/*setInterval(()=>{
let message = 'Hi! ' + new Date().getTime()
socket.send(message, 0, message.length, port, address)
}, 500)*/