|
1 | 1 | // SC broker
|
2 | 2 | // Init global variables
|
3 |
| -const config = {port: 6789}; |
| 3 | +const config = { port: 6789 }; |
4 | 4 | const data = {};
|
| 5 | +const subscriber = {}; |
| 6 | + |
| 7 | +const sendTypes = ["INIT","INIT_REUSE","PING","SET","DEL","SUB","UNSUB","DUMP"]; |
| 8 | +const responseTypes = ["INIT_ACK","PONG","SET_RES","DEL_RES","SUB_RES","UNSUB_RES","DUMP_RES"]; |
5 | 9 |
|
6 | 10 | // import settings
|
7 | 11 | import * as net from "net";
|
| 12 | +import * as readline from "readline"; |
| 13 | +import * as uuid from "uuid/v4"; |
| 14 | + |
| 15 | +// Client class, handling incoming connections |
| 16 | +class Client { |
| 17 | + socket: net.Socket; |
| 18 | + uuid: any; |
| 19 | + pingInt: any; |
| 20 | + reqId = 0; |
| 21 | + reqWait = 0; |
| 22 | + reqArray = {}; |
| 23 | + inputReader: any; |
| 24 | + pingResponse: Number; |
| 25 | + pingSuccess: Number; |
| 26 | + // Constructor run by every new connection |
| 27 | + constructor(socket: net.Socket) { |
| 28 | + this.uuid = uuid(); |
| 29 | + this.socket = socket; |
| 30 | + // Setup regular ping |
| 31 | + this.pingInt = setInterval(() => {this.ping();},2000); |
| 32 | + |
| 33 | + // Prepare Input processing |
| 34 | + this.inputReader = readline.createInterface({ |
| 35 | + input: this.socket |
| 36 | + }); |
| 37 | + |
| 38 | + this.inputReader.on('line', (l)=> { |
| 39 | + this.handleLine(l); |
| 40 | + }); |
| 41 | + |
| 42 | + // Add this receiver to subscibers |
| 43 | + subscriber[this.uuid] = this; |
| 44 | + |
| 45 | + this.socket.setNoDelay(); |
| 46 | + // Handle closing |
| 47 | + socket.on('close', () => { |
| 48 | + console.log('client disconnected'); |
| 49 | + this.close(); |
| 50 | + }); |
| 51 | + } |
| 52 | + // Periodically Ping |
| 53 | + ping() { |
| 54 | + const start = process.hrtime()[1]; |
| 55 | + this.send("PING","",(c) => { |
| 56 | + if(c[1] === "PONG") { |
| 57 | + this.pingResponse = (process.hrtime()[1]-start)/1000; |
| 58 | + this.pingSuccess = Date.now(); |
| 59 | + } |
| 60 | + }); |
| 61 | + } |
| 62 | + // Close everything |
| 63 | + close() { |
| 64 | + delete subscriber[this.uuid]; |
| 65 | + clearInterval(this.pingInt); |
| 66 | + if(!this.socket.destroyed) { |
| 67 | + this.socket.destroy(); |
| 68 | + } |
| 69 | + } |
| 70 | + // Handle input |
| 71 | + handleLine(c) { |
| 72 | + const m=c.toString('utf8').split(' '); |
| 73 | + if(m.length<2) { |
| 74 | + return; |
| 75 | + } |
| 76 | + const type=parseInt(m[0],10) |
| 77 | + if(isNaN(type)) { |
| 78 | + return; |
| 79 | + } |
| 80 | + if(!m[2]) { |
| 81 | + m[2] = ""; |
| 82 | + } |
| 83 | + // Determine if it's new req or response |
| 84 | + if(responseTypes.includes(m[1])) { |
| 85 | + // It's a response |
| 86 | + // Check if id exists and handle |
| 87 | + if(this.reqArray[m[0]]) { |
| 88 | + this.reqArray[m[0]](m); |
| 89 | + delete this.reqArray[m[0]]; |
| 90 | + } |
| 91 | + } |
| 92 | + if(sendTypes.includes(m[1])) { |
| 93 | + // ToDo: Handle Requests |
| 94 | + } |
| 95 | + // Else: drop |
| 96 | + } |
| 97 | + // Build pkg |
| 98 | + send(type = "PING", payload = "", cb = (res: any) => {}) { |
| 99 | + this.reqId++; |
| 100 | + this.socket.write(this.reqId+" "+type+" "+payload+"\r\n"); |
| 101 | + this.reqArray[this.reqId] = cb; |
| 102 | + } |
| 103 | + // Build pkg res |
| 104 | + sendRes(id = 0, type = "PONG", payload = "") { |
| 105 | + this.socket.write(id+" "+type+" "+payload+"\r\n"); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// Create Server |
| 110 | +var server = new net.Server(); |
| 111 | +server.on('connection', (s) => { |
| 112 | + new Client(s); |
| 113 | +}); |
| 114 | +server.listen(config.port, () => { |
| 115 | + console.log("Listening on port " + config.port); |
| 116 | +}); |
0 commit comments