-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnectorServer.js
150 lines (136 loc) · 3.69 KB
/
connectorServer.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
const inc = require('./inc.js')
const { makeLogger } = inc('log')
const logger = makeLogger({
mode: Symbol.for('console')
})
const config = require('./config/connect.js')
const net = require('net')
const fs = require('fs')
console.info(`[ > ] Making sockets socketing...`)
console.info(config)
class connectorServer {
constructor(logger, container, path) {
this.logger = logger
this.container = container
this.path = path
this.connections = []
this.connectionsCount = 0
this.clearPath()
this.makeServer()
}
clearPath() {
if(fs.existsSync(this.path)) fs.rmSync(this.path)
}
assignPair(anotherServer) {
this.anotherServer = anotherServer
}
makeServer() {
this.anotherServer = null
this.server = null
this.server = net.createServer()
this.server.on('connection', (x)=>{
this.connectionsCount++
this.connections[this.connectionsCount-1] = x
this.connectionListener(this.connectionsCount, x)
if(this.anotherServer!==null&&this.anotherServer.connectionsCount>0) {
this.data(JSON.stringify({
context: "connectionEstablished",
}))
this.anotherServer.data(JSON.stringify({
context: "connectionEstablished",
}))
}
})
this.server.on('error', (error)=>{
this.logger.write(error)
})
}
connectionListener(count, x) {
x.on('data', (xx)=>{
this.dataListener(xx)
})
x.on('error', (error)=>{
this.logger.write(error)
})
x.on('end', ()=>{
delete this.connections[count-1]
})
}
dataListener(data) {
this.anotherServer.data(data)
}
listen() {
this.server.listen(this.path)
}
data(data) {
this.connections.forEach((x)=>{
x.write(data)
})
}
close() {
this.server.close()
}
}
const makeConnectorServer = (logger, container, path)=>{
return new connectorServer(logger, container, path)
}
class connectorPair {
constructor(one, two) {
this.one = one
this.two = two
this.assignEach()
}
assignEach() {
this.one.assignPair(this.two)
this.two.assignPair(this.one)
}
}
const makePair = (one, two)=>{
return new connectorPair(one, two)
}
class connectorContainer {
constructor(logger, config) {
this.logger = logger
this.config = config
this.ends = []
this.makeServers()
this.gracefullTermination()
}
makeServers() {
this.servers = {}
Object.entries(this.config).forEach((x)=>{
this.servers[x[0]] = {}
let pair = Object.entries(x[1]).map((xx)=>{
let server = makeConnectorServer(this.logger, this, xx[1])
this.servers[x[0]][xx[0]] = server
this.ends.push(server)
return server
})
makePair(...pair)
})
}
listen() {
this.ends.forEach((x)=>{
x.listen()
})
}
gracefullTermination() {
process.on('SIGTERM', ()=>{
this.ends.forEach((x)=>{
x.close()
})
process.exit()
})
process.on('SIGINT', ()=>{
this.ends.forEach((x)=>{
x.close()
})
process.exit()
})
}
}
const makeContainer = (logger, config)=>{
return new connectorContainer(logger, config)
}
let container = makeContainer(logger, config)
container.listen()