UNPKG

xapi-connector

Version:

Simple low level connector for xAPI written in Coffeescript

209 lines (187 loc) 6.17 kB
// Generated by CoffeeScript 1.8.0 (function() { var Connector, Emitter, dispatcher, print, tls, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; tls = require('tls'); dispatcher = require('./dispatcher.js'); Emitter = require('events').EventEmitter; print = function(msg) { console.log(msg + '\n'); }; Connector = (function() { function Connector(server_url, conn_port, stream_port, username, password) { this.server_url = server_url; this.conn_port = conn_port; this.stream_port = stream_port; this.username = username; this.password = password; this._onStreamChunk = __bind(this._onStreamChunk, this); this._onChunk = __bind(this._onChunk, this); this._msg = ''; this._stream_msg = ''; this._conn = {}; this._stream = {}; this._emitter = new Emitter(); this._streamEmitter = new Emitter(); } Connector.prototype.buildCommand = function(command, args, tag) { var com; com = { command: (function() { if (command != null) { return command; } else { throw new Error('Missing command'); } })(), "arguments": args != null ? args : void 0 }; if (tag != null) { com.customTag = tag; } return JSON.stringify(com); }; Connector.prototype.buildStreamCommand = function(command, stream_session_id, args) { var com, key, val; if (command == null) { throw new Error('Missing stream command'); } com = { command: command, streamSessionId: stream_session_id != null ? stream_session_id : void 0 }; if (args != null) { for (key in args) { val = args[key]; com[key] = val; } } return JSON.stringify(com); }; Connector.prototype.connect = function() { this._conn.socket = tls.connect(this.conn_port, this.server_url, (function(_this) { return function() { return _this._emitter.emit('open'); }; })(this)); this._conn.socket.setEncoding('utf-8'); this._conn.dispatcher = new dispatcher(this._conn.socket, 200); this.send = (function(_this) { return function(msg) { return _this._conn.dispatcher.add(msg); }; })(this); this._conn.socket.addListener('data', this._onChunk); this._conn.socket.addListener('error', (function(_this) { return function(err) { return _this._emitter.emit('error', err); }; })(this)); this._conn.socket.addListener('close', (function(_this) { return function() { return _this._emitter.emit('close'); }; })(this)); }; Connector.prototype._onChunk = function(data) { var res, responses, _i, _len; responses = data.split('\n\n'); if (responses.length === 1) { this._msg += responses[0]; } else { responses = (function() { var _i, _len, _ref, _results; _ref = data.split('\n\n'); _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { res = _ref[_i]; if (res !== '') { _results.push(res); } } return _results; })(); for (_i = 0, _len = responses.length; _i < _len; _i++) { res = responses[_i]; this._msg += res; this._emitter.emit('message', this._msg); this._msg = ''; } } }; Connector.prototype.disconnect = function() { if (this._conn.socket != null) { this._conn.socket.destroy(); } }; Connector.prototype.connectStream = function() { this._stream.socket = tls.connect(this.stream_port, this.server_url, (function(_this) { return function() { return _this._streamEmitter.emit('open'); }; })(this)); this._stream.socket.setEncoding('utf-8'); this._stream.dispatcher = new dispatcher(this._stream.socket, 200); this.sendStream = (function(_this) { return function(msg) { return _this._stream.dispatcher.add(msg); }; })(this); this._stream.socket.addListener('data', this._onStreamChunk); this._stream.socket.addListener('error', (function(_this) { return function(err) { return _this._streamEmitter.emit('error', err); }; })(this)); this._stream.socket.addListener('close', (function(_this) { return function() { return _this._streamEmitter.emit('close'); }; })(this)); }; Connector.prototype._onStreamChunk = function(data) { var res, responses, _i, _len; responses = data.split('\n\n'); if (responses.length === 1) { this._stream_msg += responses[0]; } else { responses = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = responses.length; _i < _len; _i++) { res = responses[_i]; if (res !== '') { _results.push(res); } } return _results; })(); for (_i = 0, _len = responses.length; _i < _len; _i++) { res = responses[_i]; this._stream_msg += res; this._streamEmitter.emit('message', this._stream_msg); this._stream_msg = ''; } } }; Connector.prototype.disconnectStream = function() { if (this._stream.socket != null) { this._stream.socket.destroy(); } }; Connector.prototype.on = function(event, callback) { this._emitter.on(event, callback); }; Connector.prototype.onStream = function(event, callback) { this._streamEmitter.on(event, callback); }; Connector.prototype.getQue = function() { return this._conn.dispatcher.getQue(); }; Connector.prototype.getStreamQue = function() { return this._stream.dispatcher.getQue(); }; return Connector; })(); module.exports = Connector; }).call(this);