-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge.js
52 lines (41 loc) · 1.3 KB
/
bridge.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
const Queue = require('bull');
const nrp = require('./modules/nrpModule');
const {matchEngine, Order, User} = require("./app");
const matchEngineQueue = new Queue('MEQ', {
redis: {
host: "127.0.0.1",
port: "6379",
},
});
const queue = 'addOrder';
const open = require('amqplib').connect('amqp://localhost');
open.then(function(conn) {
return conn.createChannel();
}).then(function(ch) {
return ch.assertQueue(queue,{durable: true}).then(function(ok) {
return ch.consume(queue, function(msg) {
if (msg !== null) {
let data = JSON.parse(msg.content.toString());
console.log(data)
orderAdder(data);
ch.ack(msg);
}
}, {
noAck: false
}
);
});
}).catch(console.warn);
function orderAdder(data) {
let {type, side, quantity,price=0, username} = data;
price = Number(price);
quantity = Number(quantity);
// console.log(req.body)
const user = new User(username);
const order = new Order(type, side, quantity, user, price, matchEngine);
if (order.type === 'limit')
matchEngine.match(order);
else if (order.type === 'market')
matchEngine.market(order);
}
// module.exports = {matchEngineQueue: matchEngineQueue};