-
Notifications
You must be signed in to change notification settings - Fork 54
/
servers.js
161 lines (121 loc) · 4.04 KB
/
servers.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
// Load modules
const StringDecoder = require('string_decoder').StringDecoder;
var net = require('net');
let trackingServer
let potentialServer
/*
* Create TCP server for source tracking
*/
let remainingTrack = '';
exports.startTrackingServer = (odasStudio) => {
trackingServer = net.createServer();
trackingServer.on('connection', handleConnection);
trackingServer.listen(9000, function() {
console.log('server listening to %j', trackingServer.address());
});
function handleConnection(conn) {
var remoteAddress = conn.remoteAddress + ':' + conn.remotePort;
console.log('new client connection from %s', remoteAddress);
conn.on('data', onConnData);
conn.once('close', onConnClose);
conn.on('error', onConnError);
function onConnData(d) {
var decoder = new StringDecoder();
// Decode received string
var stream = remainingTrack + decoder.write(d);
strs = stream.split("}\n{");
if(strs.length < 2) {
remainingTrack = stream;
return;
}
strs.forEach((str,index) => {
if(index == strs.length-1) {
remainingTrack = str;
return;
}
if(str.charAt(0) !== '{') {
str = '{' + str;
}
if(str.charAt(str.length-2) !== '}') {
if(str.charAt(str.length-3)!== '}') {
str = str + '}';
}
}
try {
odasStudio.mainWindow.webContents.send('newTracking',str);
if(typeof odasStudio.odas.odas_process == 'undefined') {
odasStudio.mainWindow.webContents.send('remote-online');
}
}
catch(err) {
console.log('Window was closed');
}
});
}
function onConnClose() {
console.log('connection from %s closed', remoteAddress);
odasStudio.mainWindow.webContents.send('remote-offline');
}
function onConnError(err) {
console.log('Connection %s error: %s', remoteAddress, err.message);
}
}
}
/*
* Create TCP server for potential sources
*/
let remainingPot = '';
exports.startPotentialServer = (odasStudio) => {
potentialServer = net.createServer();
potentialServer.on('connection', handlePotConnection);
potentialServer.listen(9001, function() {
console.log('server listening to %j', potentialServer.address());
});
function handlePotConnection(conn) {
var remoteAddress = conn.remoteAddress + ':' + conn.remotePort;
console.log('new client connection from %s', remoteAddress);
conn.on('data', onConnData);
conn.once('close', onConnClose);
conn.on('error', onConnError);
function onConnData(d) {
var decoder = new StringDecoder();
// Decode received string
var stream = remainingPot + decoder.write(d);
strs = stream.split("}\n{");
if(strs.length < 2) {
remainingPot = stream;
return;
}
strs.forEach((str,index) => {
if(index == strs.length-1) {
remainingPot = str;
return;
}
try {
if(str.charAt(0) !== '{') {
str = '{' + str;
}
if(str.charAt(str.length-2) !== '}') {
if(str.charAt(str.length-3)!== '}') {
str = str + '}';
}
}
odasStudio.mainWindow.webContents.send('newPotential',str);
if(typeof odasStudio.odas.odas_process == 'undefined') {
odasStudio.mainWindow.webContents.send('remote-online');
}
}
catch(err) {
console.log('Window was closed');
}
});
}
function onConnClose() {
console.log('connection from %s closed', remoteAddress);
odasStudio.mainWindow.webContents.send('remote-offline');
}
function onConnError(err) {
console.log('Connection %s error: %s', remoteAddress, err.message);
}
}
}