-
Notifications
You must be signed in to change notification settings - Fork 1
/
communications.js
205 lines (166 loc) · 5.04 KB
/
communications.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const EventEmitter = require('events');
const electron = require('electron');
const address = require('address');
const Netmask = require('netmask').Netmask;
const http = require('http');
const firstOpenPort = require('first-open-port');
const got = require('got');
const Polo = require('polo');
/// Distributed event emitter that can announce itself via zero-config service discovery across a local network
class Communications {
constructor(options) {
this.state = 'idle';
this.options = options = Object.assign({
service: null, // Name to announce to other services as
namespace: false, // (String or Null) Filter remote events by namespace (Unimplemented)
onlyToSameServices: false // (Boolean) Limit remote communications only to services with same name
}, options);
this.service = {
id: Math.random().toString(16).substr(2),
name: options.service || 'anonymous',
port: null
};
this.locally = new Locally();
this.discovery = new Discovery(this);
this.remotely = new Remotely(this);
}
start() {
this.state = 'starting';
return this.remotely.start()
.then(() => this.discovery.start())
.then(() => this.state = 'ready');
}
twoway(events) {
events.forEach(type => {
this.locally.on(type, event => this.remotely.send(type, event));
this.remotely.on(type, event => this.locally.send(type, event));
});
}
}
/// Inter Process Communication
class Locally {
constructor() {
this.ipc = electron.ipcMain;
this.webContents = electron.webContents;
}
send(type, event) {
this.webContents.getAllWebContents().forEach(webContent => webContent.send(type, event));
}
on(type, callback) {
this.ipc.on(type, (event, ...args) => {
event.returnValue = callback.apply(null, args);
});
}
}
/// Announces self and discovers other services using UDP broadcasting
class Discovery extends EventEmitter {
constructor(comms) {
super();
this.comms = comms;
this.broadcastAddress = null;
this.polo = null;
this.clients = [];
this.handleUp = this.handleUp.bind(this);
this.handleDown = this.handleDown.bind(this);
}
start() {
return new Promise((resolve, reject) => {
const networkInterface = address.interface('IPv4');
if(!networkInterface) return reject({ type: 'no-interface' });
const block = new Netmask(networkInterface.address, networkInterface.netmask);
this.broadcastAddress = block.broadcast;
const polo = this.polo = Polo({
multicast: this.broadcastAddress,
heartbeat: 5*1000
});
polo.on('up', this.handleUp);
polo.on('down', this.handleDown);
polo.put(this.comms.service);
resolve();
});
}
handleUp(name, marco) {
const Service = this.comms.service;
if(marco.id === Service.id) return;
this.clients.push(marco);
this.emit('found', { clients: this.clients, client: marco });
this.emit('updated', { clients: this.clients });
}
handleDown(name, marco) {
const Service = this.comms.service;
if(marco.id === Service.id) return;
const index = this.clients.findIndex(client => client === marco);
if(index === -1) return;
this.clients.splice(index);
this.emit('lost', { clients: this.clients, client: marco });
this.emit('updated', { clients: this.clients, client: marco });
}
}
/// Handles external/remote communications to other applications/services
class Remotely extends EventEmitter {
constructor(comms) {
super();
this.comms = comms;
this.server = http.createServer();
this.handleRequest = this.handleRequest.bind(this);
this.server.on('request', this.handleRequest);
}
start() {
const Service = this.comms.service;
return new Promise((resolve, reject) => {
firstOpenPort(20000).then(port => {
Service.port = port;
this.server.listen(port, resolve);
});
});
}
handleRequest(req, res) {
if(!req.url.startsWith('/event/')) return;
const type = req.url.split('/')[2];
if(!type) return;
const mime = req.headers['content-type'];
let body = '';
req.on('data', chunk => body += chunk.toString());
req.on('end', () => {
res.writeHead(200, 'OK', { 'Content-Type': 'text/plain' });
res.end();
if(mime === 'text/plain'); // Let it be
else if(mime === 'application/json') body = JSON.parse(body);
else; // Unhandled Content-Type!
this.emit(type, body);
});
}
send(type, event) {
const Service = this.comms.service;
let clients = this.comms.discovery.clients;
if(this.comms.options.onlyToSameServices)
{
clients = clients.filter(client => client.name === Service.name);
}
clients.forEach(client => {
let request = {
host: client.host,
port: client.port,
path: '/event/' + type,
method: 'PUT',
headers: {},
body: null,
json: true
};
if(typeof event === 'object')
{
request.headers['content-type'] = 'application/json';
request.body = JSON.stringify(event);
}
else if(typeof event === 'string')
{
request.headers['content-type'] = 'text/plain';
request.body = event;
}
got(request);
});
}
}
module.exports = function(options) {
return new Communications(options);
}