-
Notifications
You must be signed in to change notification settings - Fork 0
/
poolboy.js
128 lines (101 loc) · 3.17 KB
/
poolboy.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
var net = require('net');
var fs = require('fs');
var ports = module.exports.ports = {};
var poolExists = false;
var minPortRange = module.exports.minPortRange = 5000;
var maxPort = module.exports.maxPortRange = 5100;
var curAvailablePort = module.exports.curAvailablePort = minPortRange;
var jsonEmitEnabled = module.exports.jsonEmitEnabled = true;
var crossPolicyEnabled = module.exports.crossPolicyEnabled = true;
var crossPolicyPort = module.exports.crossPolicyPort = 3001;
var requests = 0;
var getOpenPort = function() {
curAvailablePort++;
var newPort = curAvailablePort;
if (ports[newPort]) {
newPort = getOpenPort();
}
if (newPort >= maxPort) { // if over max port allocation
curAvailablePort = minPort;
newPort = getOpenPort();
}
return newPort;
}
var addJSONEmitToSocket = function(socket) {
var getJSON = function(data) {
var json = false;
try {
json = JSON.parse(data);
console.log("parse: ",json);
} catch(err) {
console.log("can't parse~!",data);
}
return json;
}
socket.on('data', function(data) {
var json = getJSON(data);
if (json) {
socket.emit('json',json);
}
});
return socket;
}
var createServer = function(port,persist) {
if (!port) throw new Error("server needs a port!");
if (ports[port]) throw new Error("server port already taken!");
var server = net.createServer(function(c) {//'connection' listener
console.log('tcp server ' + port + ' connected');
c.on('data', function(data) {
requests++
console.log("request number: " + requests);
});
c.on('end', function() {
if (!persist) {
console.log('tcp server ' + port + ' disconnected');
remove(port);
}
});
if (jsonEmitEnabled) addJSONEmitToSocket(c);
});
server.listen(port, function() {//'listening' listener
console.log('tcp server ' + port + ' bound');
});
ports[port] = server;
return server;
}
var setupCrossDomainPort = function() {
var policyServer = createServer(crossPolicyPort,true);
policyServer.on('connection',function(socket) {
var policyXML = '<?xml version="1.0"?><cross-domain-policy><allow-access-from domain="*" to-ports="' + minPortRange + '-' + maxPortRange + '"/></cross-domain-policy>'
socket.write(policyXML);
});
}
var add = module.exports.add = function(port) {
if (!port) port = getOpenPort();
return createServer(port);
}
var remove = module.exports.remove = function(port) {
if (!port || !ports[port]) throw new Error("port doesn't exist damnit!");
var server = ports[port];
try {
server.close();
} catch(err) {
console.log(err);
}
ports[port] = null;
}
var setupConnectionPool = module.exports.setupConnectionPool = function(reqPort) {
if (poolExists) throw new Error("server connection pool already setup!");
if (!reqPort) throw new Error("server needs connection pool port!");
if (ports[reqPort]) throw new Error("server connection pool port already taken!");
var server = createServer(reqPort,true);
server.on('connection',function(poolSocket) {
var newServer = add();
var newPort = newServer.address().port;
poolSocket.write(String(newPort) + '\n');
server.emit('newport',newServer);
})
if (crossPolicyEnabled) setupCrossDomainPort();
poolExists = true;
return server;
};