Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 56be3dff rédigé par Kubat's avatar Kubat
Parcourir les fichiers

First command: the status command is working

parent b43917fe
Branches
Aucune étiquette associée trouvée
2 requêtes de fusion!3Dev kubat,!1Create basic lib
const logger = require.main.require('./common/logger.js'); const logger = require.main.require('./common/logger.js');
const net = require('net'); const net = require('net');
/* The sleep function */
const { promisify } = require('util')
const sleep = promisify(setTimeout)
class LktClient { class LktClient {
/***************************************
* Not static methods, do not use them *
***************************************/
/* The constructor /* The constructor
* - host: String * - host: String
* - port: String * - port: String
* *
* Private members * Private members
* - m_socket: net.socket */ * - m_socket: net.socket
*
* Node: You should not use that directly, prefere the use of static
* methods */
constructor(port = 6600) { constructor(port = 6600) {
this.m_online = false; this.m_online = false;
this.m_port = port; this.m_port = port;
this.m_socket = new net.Socket(); this.m_socket = new net.Socket();
this.m_closed = true;
const sockopt = { const sockopt = {
port: this.m_port, port: this.m_port,
host: "localhost", host: "localhost",
...@@ -21,7 +33,7 @@ class LktClient { ...@@ -21,7 +33,7 @@ class LktClient {
}; };
logger.debug("Creating the lektor client"); logger.debug("Creating the lektor client");
this.m_socket.setTimeout(10); this.m_socket.setTimeout(3000);
this.m_socket.setEncoding('utf8'); this.m_socket.setEncoding('utf8');
this.m_socket.on("timeout", () => { this.m_socket.on("timeout", () => {
logger.error(`Got timeout while connecting to localhost:${this.m_port}`); logger.error(`Got timeout while connecting to localhost:${this.m_port}`);
...@@ -33,10 +45,6 @@ class LktClient { ...@@ -33,10 +45,6 @@ class LktClient {
this.m_online = true; this.m_online = true;
}); });
this.m_socket.on('data', (data) => {
logger.debug(`Recieved "${data.replace(/(\r\n|\n|\r)/gm, "")}"`);
});
this.m_socket.on("end", () => { this.m_socket.on("end", () => {
logger.info(`Disconnected from server localhost:${this.m_port}`); logger.info(`Disconnected from server localhost:${this.m_port}`);
this.m_online = false; this.m_online = false;
...@@ -59,17 +67,12 @@ class LktClient { ...@@ -59,17 +67,12 @@ class LktClient {
logger.info(`Socket connected to localhost:${this.m_port}`); logger.info(`Socket connected to localhost:${this.m_port}`);
this.m_online = true; this.m_online = true;
}); });
}
/* Wait for the socket to be connected */ return this;
wait() {
logger.debug(`Waiting to connect to localhost:${this.m_port}`);
var i = 0;
while (! this.m_online) {
}
} }
/* Close the client */ /* Close the client.
* Note: Prefere using the static methods. */
close() { close() {
if (this.m_online) { if (this.m_online) {
logger.debug("Requesting socket shutdown"); logger.debug("Requesting socket shutdown");
...@@ -77,7 +80,48 @@ class LktClient { ...@@ -77,7 +80,48 @@ class LktClient {
} else { } else {
logger.debug("Socket already closed"); logger.debug("Socket already closed");
} }
this.m_closed = true;
} }
/*****************************************
* Static methods, to be used by clients *
*****************************************/
/* The status command */
static commandStatus() {
var client = new this();
var once = false;
var result = {};
function __getResult(client) {
return new Promise(resolv => {
client.m_socket.on("data", (data) => {
if (!once) {
client.m_socket.write("status\n");
once = true;
return null;
} else {
client.close();
result = __mpdToObject(data);
resolv(result);
}
});
})
};
return __getResult(client);
}
}
function __mpdToObject(string) {
var ret = {};
string
.split("\n")
.forEach( (line) => {
if (line.length <= 1)
return;
var couple = line.split(/: (.+)/).slice(0, 2);
ret[couple[0]] = couple[1];
});
return ret;
} }
module.exports = LktClient; module.exports = LktClient;
...@@ -30,7 +30,8 @@ const __loggerCustomLevels = { ...@@ -30,7 +30,8 @@ const __loggerCustomLevels = {
}; };
const __myFormat = winston.format.printf(({ level, message, label, timestamp }) => { const __myFormat = winston.format.printf(({ level, message, label, timestamp }) => {
return `[${timestamp}] ${level}\t ${message}`; // return `[${timestamp}] ${level}\t ${message}`;
return `[${timestamp}] ${message}`;
}); });
module.exports = global.logger = module.exports = winston.createLogger({ module.exports = global.logger = module.exports = winston.createLogger({
......
const logger = require.main.require('./common/logger.js'); const logger = require.main.require('./common/logger.js');
const lkt = require.main.require('./common/lkt.js'); const lkt = require.main.require('./common/lkt.js');
var client = new lkt(); lkt
.commandStatus()
// client.wait(); .then( (res) => {
console.log(res);
})
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter