forked from falconair/nodefix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix.js
157 lines (125 loc) · 6.33 KB
/
fix.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
var sys = require('sys');
var fs = require('fs');
var net = require('net');
var events = require('events');
var path = require('path');
var pipe = require('pipe');
var _ = require('underscore');
//TODO
//Improve 'error' events. If sender/target exist, add them
//Clean up direct use of msg fields. Prefer the use of sender/target from context rather
//than trying to get fields directly (or do the opposite?)
//If no logon is established x seconds after connection, kill connection and notify client
//-----------------------------Expose server API-----------------------------
exports.createServer = function(opt, func) {
var server = new Server(opt, func);
return server;
};
//TODO: handle error event, for example, when the listening port is already being used
function Server(opt, func) {
events.EventEmitter.call(this);
this.sessions = {};
this.port = null;
var self = this;
this.server = net.createServer(function(stream) {
stream.setTimeout(2 * 60 * 1000);//if no traffic for two minutes, kill connection!
var session = this;
this.senderCompID = null;
this.targetCompID = null;
this.fixVersion = null;
this.p = null;
function SessionEmitterObj(){
events.EventEmitter.call(this);
this.write = function(data){session.p.pushOutgoing({data:data, type:'data'});};
}
sys.inherits(SessionEmitterObj,events.EventEmitter);
this.sessionEmitter = new SessionEmitterObj();
session.sessionEmitter.emit('connect', stream.remoteAddress, self.port, 'acceptor');
session.p = pipe.makePipe(stream);
session.p.addHandler(require('./handlers/fixFrameDecoder.js').newFixFrameDecoder());
session.p.addHandler(require('./handlers/outMsgEvtInterceptor.js').newOutMsgEvtInterceptor(session));
session.p.addHandler(require('./handlers/sessionProcessor.js').newSessionProcessor(true,opt));
session.p.addHandler(require('./handlers/inMsgEvtInterceptor.js').newInMsgEvtInterceptor(session));
stream.on('data', function(data) { session.p.pushIncoming({data:data, type:'data'}); });
stream.on('timeout', function(){ stream.end(); });
stream.on('end', function(){ session.sessionEmitter.emit('end');})
//stream.on('connect', function(){ session.sessionEmitter.emit('connect');})
func(session.sessionEmitter);
});
self.server.on('error', function(err){ self.emit('error', err); });
this.listen = function(port, host, callback) {
self.port = port;
self.server.listen(port, host, callback);
};
this.write = function(targetCompID, data) { self.sessions[targetCompID].write({data:data, type:'data'}); };
this.logoff = function(targetCompID, logoffReason) { self.sessions[targetCompID].write({data:{35:5, 58:logoffReason}, type:'data'}); };
this.kill = function(targetCompID, reason){ self.sessions[targetCompID].end(); };
/*this.getMessages = function(callback){
var fileName = './traffic/' + session.fixVersion + '-' + session.senderCompID + '-' + session.targetCompID + '.log';
fs.readFile(fileName, encoding='ascii', function(err,data){
if(err){
callback(err,null);
}
else{
var transactions = data.split('\n');
callback(null,transactions);
}
});
};*/
}
sys.inherits(Server, events.EventEmitter);
//-----------------------------Expose client API-----------------------------
exports.createClient = function(fixVersion, senderCompID, targetCompID, opt) {
//return new Client({'8': fixVersion, '56': targetCompID, '49': senderCompID, '35': 'A', '90': '0', '108': '10'}, port, host, callback);
return new Client(fixVersion, senderCompID, targetCompID, opt);
};
/*exports.createConnectionWithLogonMsg = function(logonmsg, port, host, callback) {
return new Client(logonmsg, port, host, callback);
};*/
function Client(fixVersion, senderCompID, targetCompID, opt) {
events.EventEmitter.call(this);
var stream = null;
this.port = null;
this.host = null;
var self = this;
//--CLIENT METHODS--
//this.write = function(data) { self.p.pushOutgoing(data); };
this.write = function(data) { self.p.pushOutgoing({data:data, type:'data'}); };
this.connect = function(port, host, callback){
//self.p.state.session['remoteAddress'] = host;
self.stream = net.createConnection(port, host, callback);
self.p = pipe.makePipe(self.stream);
self.p.addHandler(require('./handlers/fixFrameDecoder.js').newFixFrameDecoder());
self.p.addHandler(require('./handlers/outMsgEvtInterceptor.js').newOutMsgEvtInterceptor({'sessionEmitter':self}));
self.p.addHandler(require('./handlers/sessionProcessor.js').newSessionProcessor(false, opt));
self.p.addHandler(require('./handlers/inMsgEvtInterceptor.js').newInMsgEvtInterceptor({'sessionEmitter':self}));
self.stream.on('connect', function(){ self.emit('connect', self.host, self.port, 'initiator');});
self.stream.on('data', function(data) { self.p.pushIncoming({data:data, type:'data'}); });
self.stream.on('end', function(){ self.emit('end');});
self.stream.on('error', function(err){ self.emit('error',err);});
}
this.logon = function(logonmsg){
logonmsg = _.isUndefined(logonmsg)? {'8':fixVersion, '49':senderCompID, '56': targetCompID, '35': 'A', '90': '0', '108': '10'} : logonmsg;
self.p.pushOutgoing({data:logonmsg, type:'data'});
}
this.connectAndLogon = function(port, host){
self.port = port;
self.host = host;
self.connect(port, host);
self.on('connect', function(){ self.logon(); });
}
this.logoff = function(logoffReason){ self.p.pushOutgoing({data:{35:5, 58:logoffReason}, type:'data'}) };
/*this.getMessages = function(callback){
var fileName = './traffic/' + self.fixVersion + '-' + self.senderCompID + '-' + self.targetCompID + '.log';
fs.readFile(fileName, encoding='ascii', function(err,data){
if(err){
callback(err,null);
}
else{
var transactions = data.split('\n');
callback(null,transactions);
}
});
};*/
}
sys.inherits(Client, events.EventEmitter);