Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 38 additions & 27 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function S101Socket(address, port) {
self.port = port;
self.socket = null;
self.keepaliveInterval = 10;
self.codec = new S101Codec();
self.codec = null;
self.status = "disconnected";

}
Expand Down Expand Up @@ -49,7 +49,7 @@ function S101Client(socket, server) {
if (root !== undefined) {
self.emit('emberTree', root);
}
} catch(e) {
} catch (e) {
self.emit("error", e);
}
});
Expand Down Expand Up @@ -90,7 +90,7 @@ function S101Server(address, port) {

util.inherits(S101Server, EventEmitter);

S101Server.prototype.listen = function() {
S101Server.prototype.listen = function () {
var self = this;
if (self.status !== "disconnected") {
return;
Expand All @@ -113,7 +113,7 @@ S101Server.prototype.listen = function() {
}


S101Server.prototype.addClient = function(socket) {
S101Server.prototype.addClient = function (socket) {
var client = new S101Client(socket, this);
this.emit("connection", client);
}
Expand All @@ -123,29 +123,29 @@ S101Server.prototype.addClient = function(socket) {
* Client
*****************************************************/

S101Client.prototype.remoteAddress = function() {
S101Client.prototype.remoteAddress = function () {
if (this.socket === undefined) {
return;
}
return `${this.socket.remoteAddress}:${this.socket.remotePort}`
}

S101Client.prototype.queueMessage = function(node) {
S101Client.prototype.queueMessage = function (node) {
const self = this;
this.addRequest(() => {
self.sendBERNode(node);
});
}

S101Client.prototype.makeRequest = function() {
if(this.activeRequest === null && this.pendingRequests.length > 0) {
S101Client.prototype.makeRequest = function () {
if (this.activeRequest === null && this.pendingRequests.length > 0) {
this.activeRequest = this.pendingRequests.shift();
this.activeRequest();
this.activeRequest = null;
}
};

S101Client.prototype.addRequest = function(cb) {
S101Client.prototype.addRequest = function (cb) {
this.pendingRequests.push(cb);
this.makeRequest();
}
Expand All @@ -155,7 +155,7 @@ S101Client.prototype.addRequest = function(cb) {
* Socket
*****************************************************/

S101Socket.prototype.connect = function(timeout = 2) {
S101Socket.prototype.connect = function (timeout = 2) {
var self = this;
if (self.status !== "disconnected") {
return;
Expand All @@ -171,17 +171,17 @@ S101Socket.prototype.connect = function(timeout = 2) {
}, 1000 * timeout);
}

self.codec = new S101Codec();
self.socket = net.createConnection(self.port, self.address, () => {
winston.debug('socket connected');

if (self._timer) {
clearTimeout(self._timer);
}


setInterval(() => {
self.keepaliveIntervalTimer = setInterval(() => {
self.sendKeepaliveRequest();
}, 1000 * self.keepaliveInterval );
}, 1000 * self.keepaliveInterval);

self.codec.on('keepaliveReq', () => {
self.sendKeepaliveResponse();
Expand All @@ -196,7 +196,7 @@ S101Socket.prototype.connect = function(timeout = 2) {
if (root !== undefined) {
self.emit('emberTree', root);
}
} catch(e) {
} catch (e) {
self.emit("error", e);
}
});
Expand All @@ -219,36 +219,48 @@ S101Socket.prototype.connect = function(timeout = 2) {

}

S101Socket.prototype.isConnected = function() {
S101Socket.prototype.isConnected = function () {
return ((this.socket !== null) && (this.socket !== undefined));
}

S101Socket.prototype.disconnect = function() {
S101Socket.prototype.disconnect = function () {
var self = this;
if (self.isConnected()) {
self.socket.destroy();
self.socket = null;
self.status = "disconnected";

if (!self.isConnected()) {
return Promise.resolve();
}
}
return new Promise((resolve, reject) => {
self.socket.once('close', () => {
self.codec = null;
self.socket = null;
resolve();
});
self.socket.once('error', reject);
clearInterval(self.keepaliveIntervalTimer);
clearTimeout(self._timeout);
self.socket.end();
self.status = "disconnected";
}
);
};

S101Socket.prototype.sendKeepaliveRequest = function() {
S101Socket.prototype.sendKeepaliveRequest = function () {
var self = this;
if (self.isConnected()) {
self.socket.write(self.codec.keepAliveRequest());
winston.debug('sent keepalive request');
}
}

S101Socket.prototype.sendKeepaliveResponse = function() {
S101Socket.prototype.sendKeepaliveResponse = function () {
var self = this;
if (self.isConnected()) {
self.socket.write(self.codec.keepAliveResponse());
winston.debug('sent keepalive response');
}
}

S101Socket.prototype.sendBER = function(data) {
S101Socket.prototype.sendBER = function (data) {
var self = this;
if (self.isConnected()) {
var frames = self.codec.encodeBER(data);
Expand All @@ -258,15 +270,14 @@ S101Socket.prototype.sendBER = function(data) {
}
}

S101Socket.prototype.sendBERNode = function(node) {
var self=this;
S101Socket.prototype.sendBERNode = function (node) {
var self = this;
if (!node) return;
var writer = new BER.Writer();
node.encode(writer);
self.sendBER(writer.buffer);
}



module.exports = { S101Socket, S101Server, S101Client };

Loading