Skip to content
This repository was archived by the owner on Sep 22, 2023. It is now read-only.

Commit 23bfab1

Browse files
committed
Data implementation: First setup.
1 parent f54fc54 commit 23bfab1

File tree

4 files changed

+123
-6
lines changed

4 files changed

+123
-6
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,4 @@ typings/
6060
# next.js build output
6161
.next
6262
dist
63+
clienttest.js

Diff for: package-lock.json

+8-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
},
3030
"homepage": "https://showcomposer.de",
3131
"devDependencies": {
32-
"@types/node": "^10.12.25",
32+
"@types/node": "^10.12.26",
3333
"tslint": "^5.12.0",
3434
"typescript": "^3.1.1"
3535
},
36-
"dependencies": {}
36+
"dependencies": {
37+
"uuid": "^3.3.2"
38+
}
3739
}

Diff for: src/broker.ts

+110-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,116 @@
11
// SC broker
22
// Init global variables
3-
const config = {port: 6789};
3+
const config = { port: 6789 };
44
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"];
59

610
// import settings
711
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

Comments
 (0)