forked from Enfernuz/quik-lua-rpc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice.lua
418 lines (324 loc) · 10.6 KB
/
service.lua
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
local scriptPath = getScriptPath()
package.path = scriptPath .. '/?.lua;' .. package.path
local string = string
local zmq = require("lzmq")
local zmq_poller = require("lzmq.poller")
local zap = require("auth.zap")
local config_parser = require("utils.config_parser")
local event_data_converter = require("impl.event_data_converter")
local procedure_wrappers = require("impl.procedure_wrappers")
local utils = require("utils.utils")
local json = require("utils.json")
local uuid = require("utils.uuid")
local service = {}
service._VERSION = "v2.0-alpha"
service.QUIK_VERSION = "7.16.1.36"
service.event_callbacks = {}
local zmq_ctx = nil
local rpc_sockets = {}
local pub_sockets = {}
local poller = nil
local is_running = false
local initialized = false
local request_response_serde = {
-- initialized on demand
json = nil,
protobuf = nil
}
local publishers = {
json = nil,
protobuf = nil
}
local protobuf_context = {
is_initialized = false
}
function protobuf_context:init (context_path)
require("qlua.qlua_pb_init")(context_path)
self.is_initialized = true
end
local function pub_poll_out_callback ()
-- TODO: add reading from a message queue
-- Polling out is not implemented at the moment: messages are being sent regardless of the POLLOUT event.
end
local function send_data (data, socket)
local ok, err = pcall(function ()
local msg = zmq.msg_init_data(data)
msg:send(socket)
msg:close()
end)
-- if not ok then (log the error somehow, maybe to a file...) end
end
local function gen_error_obj (code, msg)
local err = {code = code}
if msg then
err.message = msg
end
return err
end
local function create_rpc_poll_in_callback (socket, serde_protocol)
local sd_proto = string.lower(serde_protocol)
local handler
if "json" == sd_proto then
-- TODO: remove this message
message("DEBUG: JSON message protocol detected")
if not request_response_serde.json then
request_response_serde.json = require("impl.json_request_response_serde"):new()
end
handler = request_response_serde.json
elseif "protobuf" == sd_proto then -- TODO: make explicit check on protobuf
-- TODO: remove this message
message("DEBUG: PROTOBUF message protocol detected")
if not request_response_serde.protobuf then
if not protobuf_context.is_initialized then
protobuf_context:init(scriptPath)
end
request_response_serde.protobuf = require("impl.protobuf_request_response_serde"):new()
end
handler = request_response_serde.protobuf
else
error( string.format("Неподдерживаемый протокол сериализации/десериализации: %s. Поддерживаемые протоколы: json, protobuf.", serde_protocol) )
end
local callback = function ()
local ok, res = pcall(function()
local recv = zmq.msg_init():recv(socket)
local result
if recv and recv ~= -1 then
-- request deserialization
local method, args = handler:deserialize_request( recv:data() )
recv:close()
local response = {
method = method
}
local proc_wrapper = procedure_wrappers[method]
if not proc_wrapper then
response.error = gen_error_obj(404, string.format("QLua-функция с именем '%s' не найдена.", method))
else
-- procedure call
local ok, proc_result = pcall(function() return proc_wrapper(args) end)
if ok then
response.proc_result = proc_result
else
response.error = gen_error_obj(1, res) -- the err code 1 is for errors inside the QLua functions' wrappers
end
end
result = response
end
return result
end)
local response
if ok then
if res then response = res end
else
response = {}
response.error = gen_error_obj(500, string.format("Ошибка при обработке входящего запроса: '%s'.", res))
end
if response then
-- response serialization
local serialized_response = handler:serialize_response(response)
-- response sending
send_data(serialized_response, socket)
end
end
return callback
end
local function publish (event_type, event_data)
if not is_running then return end
local converted_event_data = event_data_converter.convert(event_type, event_data)
for _, publisher in pairs(publishers) do
publisher:publish(event_type, converted_event_data)
end
end
-- TODO: make the publishing depending on the serde protocol being used
local function create_event_callbacks()
return {
OnClose = function ()
publish("OnClose")
service.terminate()
end,
OnStop = function (signal)
publish("OnStop", {signal = signal})
service.terminate()
end,
OnFirm = function (firm)
publish("OnFirm", firm)
end,
OnAllTrade = function (alltrade)
publish("OnAllTrade", alltrade)
end,
OnTrade = function (trade)
publish("OnTrade", trade)
end,
OnOrder = function (order)
publish("OnOrder", order)
end,
OnAccountBalance = function (acc_bal)
publish("OnAccountBalance", acc_bal)
end,
OnFuturesLimitChange = function (fut_limit)
publish("OnFuturesLimitChange", fut_limit)
end,
OnFuturesLimitDelete = function (lim_del)
publish("OnFuturesLimitDelete", lim_del)
end,
OnFuturesClientHolding = function (fut_pos)
publish("OnFuturesClientHolding", fut_pos)
end,
OnMoneyLimit = function (mlimit)
publish("OnMoneyLimit", mlimit)
end,
OnMoneyLimitDelete = function (mlimit_del)
publish("OnMoneyLimitDelete", mlimit_del)
end,
OnDepoLimit = function (dlimit)
publish("OnDepoLimit", dlimit)
end,
OnDepoLimitDelete = function (dlimit_del)
publish("OnDepoLimitDelete", dlimit_del)
end,
OnAccountPosition = function (acc_pos)
publish("OnAccountPosition", acc_pos)
end,
OnNegDeal = function (neg_deal)
publish("OnNegDeal", neg_deal)
end,
OnNegTrade = function (neg_trade)
publish("OnNegTrade", neg_trade)
end,
OnStopOrder = function (stop_order)
publish("OnStopOrder", stop_order)
end,
OnTransReply = function (trans_reply)
publish("OnTransReply", trans_reply)
end,
OnParam = function (class_code, sec_code)
publish("OnParam", {class_code = class_code, sec_code = sec_code})
end,
OnQuote = function (class_code, sec_code)
publish("OnQuote", {class_code = class_code, sec_code = sec_code})
end,
OnDisconnected = function ()
publish("OnDisconnected")
end,
OnConnected = function (flag)
publish("OnConnected", {flag = flag})
end,
OnCleanUp = function ()
publish("OnCleanUp")
end,
OnDataSourceUpdate = function (update_info)
publish("OnDataSourceUpdate", update_info)
end
}
end
local function create_socket (endpoint)
local socket
local sockets
if endpoint.type == "RPC" then
socket = zmq_ctx:socket(zmq.REP)
poller:add(socket, zmq.POLLIN, create_rpc_poll_in_callback(socket, endpoint.serde_protocol))
sockets = rpc_sockets
elseif endpoint.type == "PUB" then
socket = zmq_ctx:socket(zmq.PUB)
poller:add(socket, zmq.POLLIN, pub_poll_out_callback)
sockets = pub_sockets
else
error( string.format("Указан неподдерживаемый тип '%s' для точки подключения. Поддерживаемые типы: RPC и PUB.", endpoint.type) )
end
if zap.has_auth(endpoint) then
if not zap.is_initialized() then zap.init(zmq_ctx, poller) end
zap.setup_auth(socket, endpoint)
end
socket:bind( string.format("tcp://%s:%d", endpoint.address.host, endpoint.address.port) )
if endpoint.type == "PUB" then
local serde_protocol = string.lower(endpoint.serde_protocol)
local publisher
if "protobuf" == serde_protocol then
if not publishers.protobuf then
publishers.protobuf = require("impl.protobuf_event_publisher"):new()
end
publisher = publishers.protobuf
elseif "json" == serde_protocol then
if not publishers.json then
publishers.json = require("impl.json_event_publisher"):new()
end
publisher = publishers.json
end
publisher:add_pub_socket(socket)
-- Как координировать PUB и SUB правильно (сложно): http://zguide.zeromq.org/lua:all#Node-Coordination
-- Как не совсем правильно (просто): использовать sleep
utils.sleep(0.25) -- in seconds
local next = next
if not next(service.event_callbacks) then
service.event_callbacks = create_event_callbacks()
end
end
table.sinsert(sockets, socket)
return socket
end
local function reg_endpoint (endpoint)
create_socket(endpoint)
end
local function check_if_initialized ()
if not initialized then error("The service is not initialized.") end
end
function service.init ()
if initialized then return end
local config = config_parser.parse(scriptPath.."/config.json")
zmq_ctx = zmq.context()
poller = zmq_poller.new()
for i, endpoint in ipairs(config.endpoints) do
if endpoint.active then
endpoint.id = i
reg_endpoint(endpoint)
end
end
uuid.seed()
initialized = true
end
function service.start ()
check_if_initialized()
if is_running then
return
else
is_running = true
end
-- Does nothing useful at the moment, because the polling has not yet been started at the time it executes.
-- Issue #13.
publish("PublisherOnline")
xpcall(
function()
return poller:start()
end,
function()
message("Ошибка в poller:start. Стек вызовов:\n"..debug.traceback())
end
)
end
function service.stop ()
check_if_initialized()
if is_running then
poller:stop()
is_running = false
end
end
function service.terminate ()
check_if_initialized()
if is_running then
service.stop()
end
poller = nil
-- Set non-negative linger to prevent termination hanging in case if there's a message pending for a disconnected subscriber
for _i, socket in ipairs(rpc_sockets) do
socket:close(0)
end
rpc_sockets = {}
for _i, socket in ipairs(pub_sockets) do
socket:close(0)
end
pub_sockets = {}
zap.destroy()
zmq_ctx:term(1)
zmq_ctx = nil
initialized = false
end
return service