forked from falconair/nodefix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Client.js
134 lines (114 loc) · 3.26 KB
/
Client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
var util = require('util');
var fs = require('fs');
var net = require('net');
var events = require('events');
var _ = require('underscore');
var OutgoingMessage = require("./OutgoingMessage");
var IncomingMessage = require("./IncomingMessage");
var Buffer = require("./Buffer");
var Session = require("./Session");
/**
* @constructor
* @param {object} settings
* @param {string} settings.host
* @param {number} settings.port
* @param {string} settings.fixVersion
* @param {string} settings.senderCompID
* @param {string} settings.targetCompID
* @param {string} settings.targetSubID
* @fires connect
* @fires logon
* @fires outgoing
* @fires incoming
* @fires error
* @fires end
*/
function Client(settings) {
this.settings = settings;
this.connect();
}
Client.prototype = new events.EventEmitter();
/**
* @public
*/
Client.prototype.connect = function () {
this.stream = net.createConnection(this.settings.port, this.settings.host);
this.buffer = new Buffer();
this.session = new Session(this.settings);
this.stream.on("connect", this._onConnected.bind(this));
this.stream.on("data", this._onIncomingData.bind(this));
this.stream.on("end", this._onDisconnected.bind(this));
this.stream.on("error", this._onStreamError.bind(this));
this.buffer.on("message", this._onIncomingMessage.bind(this));
this.buffer.on("fatal", this._onFatal.bind(this));
this.session.on("send", this._finalSend.bind(this));
this.session.on("logon", this._onLogon.bind(this));
this.session.on("fatal", this._onFatal.bind(this));
};
/**
* @public
* @param {[type]} username
* @param {[type]} password
*/
Client.prototype.logon = function (username, password) {
this.send("Logon", [
["EncryptMethod", "0"],
["HeartBtInt", "30"],
["ResetSeqNumFlag", "Y"]
]);
this.send("UserRequest", [
["Username", username],
["Password", password],
["UserRequestID", "1"],
["UserRequestType", "1"]
]);
};
/**
* @public
* @param {string} messageType
* @param {array} data
*/
Client.prototype.send = function (messageType, data) {
var message = new OutgoingMessage(messageType, data);
this.session.outgoing(message);
};
/**
* @public
* @param {string} reason
*/
Client.prototype.logoff = function (reason) {
this.send("Logout", [
["Text", reason]
]);
};
Client.prototype._onConnected = function () {
this.emit("connect");
};
Client.prototype._finalSend = function (message) {
this.stream.write(message.getFIX());
this.emit("outgoing", message);
};
Client.prototype._onIncomingData = function (raw) {
this.buffer.incoming(raw);
};
Client.prototype._onIncomingMessage = function (data) {
var message = new IncomingMessage(data);
this.session.incoming(message);
this.emit("incoming", message);
};
Client.prototype._onLogon = function () {
this.emit("logon");
};
Client.prototype._onFatal = function () {
this.logoff("Buffer or session failure");
this.stream.end();
};
Client.prototype._onStreamError = function (error) {
console.log("[ERROR] Could not connect:", error);
this.emit("error", error);
};
Client.prototype._onDisconnected = function () {
this.session.stopHeartbeat();
this.emit("end");
};
module.exports = Client;