|
@@ -25,6 +25,11 @@ const secureOp = [
|
|
|
"ping",
|
|
"ping",
|
|
|
"close"];
|
|
"close"];
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ *Class representing connection with client
|
|
|
|
|
+ *
|
|
|
|
|
+ * @class Connection
|
|
|
|
|
+ */
|
|
|
class Connection {
|
|
class Connection {
|
|
|
id: number;
|
|
id: number;
|
|
|
socket: Socket;
|
|
socket: Socket;
|
|
@@ -32,6 +37,12 @@ class Connection {
|
|
|
queue: Queue<string>;
|
|
queue: Queue<string>;
|
|
|
session: any;
|
|
session: any;
|
|
|
ended: boolean;
|
|
ended: boolean;
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Creates an instance of Connection.
|
|
|
|
|
+ * @param {number} id - Conection's id
|
|
|
|
|
+ * @param {Socket} socket - Connection's socket
|
|
|
|
|
+ * @memberof Connection
|
|
|
|
|
+ */
|
|
|
constructor(id: number, socket: Socket) {
|
|
constructor(id: number, socket: Socket) {
|
|
|
console.log("New connection", id);
|
|
console.log("New connection", id);
|
|
|
|
|
|
|
@@ -94,6 +105,12 @@ class Connection {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Execute operator by running corresponding method of Connection
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param {string} op - operator
|
|
|
|
|
+ * @memberof Connection
|
|
|
|
|
+ */
|
|
|
async execute(op: string) {
|
|
async execute(op: string) {
|
|
|
if (secureOp.includes(op)) {
|
|
if (secureOp.includes(op)) {
|
|
|
await this[<secureOp>op]();
|
|
await this[<secureOp>op]();
|
|
@@ -105,6 +122,13 @@ class Connection {
|
|
|
|
|
|
|
|
// Session`s Operations
|
|
// Session`s Operations
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * register new App with name from queue then reply with id and authcode
|
|
|
|
|
+ *
|
|
|
|
|
+ * Should only be used by protocol
|
|
|
|
|
+ *
|
|
|
|
|
+ * @memberof Connection
|
|
|
|
|
+ */
|
|
|
async register() {
|
|
async register() {
|
|
|
const name = await this.queue.remove();
|
|
const name = await this.queue.remove();
|
|
|
let instance = await db.App.create({
|
|
let instance = await db.App.create({
|
|
@@ -113,6 +137,14 @@ class Connection {
|
|
|
this.write(`${instance.id}&${instance.auth};`);
|
|
this.write(`${instance.id}&${instance.auth};`);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * login App with authcode from queue.
|
|
|
|
|
+ * Reply "OK" on success or "ERR" and message on error;
|
|
|
|
|
+ *
|
|
|
|
|
+ * Should only be used by protocol
|
|
|
|
|
+ *
|
|
|
|
|
+ * @memberof Connection
|
|
|
|
|
+ */
|
|
|
async login() {
|
|
async login() {
|
|
|
const auth = await this.queue.remove();
|
|
const auth = await this.queue.remove();
|
|
|
if (typeof this.session !== "undefined") {
|
|
if (typeof this.session !== "undefined") {
|
|
@@ -135,6 +167,14 @@ class Connection {
|
|
|
this.write("OK;");
|
|
this.write("OK;");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * send message from queue to telegraf bot.
|
|
|
|
|
+ * Reply "OK" on success or "ERR" and message on error;
|
|
|
|
|
+ *
|
|
|
|
|
+ * Should only be used by protocol
|
|
|
|
|
+ *
|
|
|
|
|
+ * @memberof Connection
|
|
|
|
|
+ */
|
|
|
async notify() {
|
|
async notify() {
|
|
|
const message = await this.queue.remove();
|
|
const message = await this.queue.remove();
|
|
|
if (typeof this.session === "undefined") {
|
|
if (typeof this.session === "undefined") {
|
|
@@ -146,10 +186,24 @@ class Connection {
|
|
|
this.write("OK;")
|
|
this.write("OK;")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Reply with "pong".
|
|
|
|
|
+ *
|
|
|
|
|
+ * Should only be used by protocol
|
|
|
|
|
+ *
|
|
|
|
|
+ * @memberof Connection
|
|
|
|
|
+ */
|
|
|
ping() {
|
|
ping() {
|
|
|
this.write("pong;");
|
|
this.write("pong;");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Close connection
|
|
|
|
|
+ *
|
|
|
|
|
+ * Should only be used by protocol
|
|
|
|
|
+ *
|
|
|
|
|
+ * @memberof Connection
|
|
|
|
|
+ */
|
|
|
close() {
|
|
close() {
|
|
|
this.write("Closing connection;");
|
|
this.write("Closing connection;");
|
|
|
this.socket.end();
|
|
this.socket.end();
|
|
@@ -157,12 +211,23 @@ class Connection {
|
|
|
|
|
|
|
|
// Connection methods
|
|
// Connection methods
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Write message to connection's Socket
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param {(string | Buffer)} input - Message
|
|
|
|
|
+ * @memberof Connection
|
|
|
|
|
+ */
|
|
|
write(input: string | Buffer) {
|
|
write(input: string | Buffer) {
|
|
|
if (!this.socket.destroyed && !this.ended) {
|
|
if (!this.socket.destroyed && !this.ended) {
|
|
|
this.socket.write(input);
|
|
this.socket.write(input);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Clear connection from map
|
|
|
|
|
+ *
|
|
|
|
|
+ * @memberof Connection
|
|
|
|
|
+ */
|
|
|
end() {
|
|
end() {
|
|
|
delete Connections[id];
|
|
delete Connections[id];
|
|
|
console.log("Connection closed", this.id);
|
|
console.log("Connection closed", this.id);
|
|
@@ -171,6 +236,11 @@ class Connection {
|
|
|
|
|
|
|
|
let id = 0;
|
|
let id = 0;
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * Handle new connection with new Connection
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param {Socket} socket
|
|
|
|
|
+ */
|
|
|
function connectionHandler(socket: Socket): void {
|
|
function connectionHandler(socket: Socket): void {
|
|
|
id++;
|
|
id++;
|
|
|
new Connection(id, socket);
|
|
new Connection(id, socket);
|