-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.lua
More file actions
59 lines (49 loc) · 1.73 KB
/
client.lua
File metadata and controls
59 lines (49 loc) · 1.73 KB
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
local zmq = require 'lzmq'
local zloop = require 'lzmq.loop'
local Connection = require 'somata.connection'
local Client = {}
Client.__index = Client
function Client.create(loop, registry_host)
local client = {}
setmetatable(client, Client)
client.ctx = zmq.context()
if loop == nil then
client.loop = zloop.new(1, client.ctx)
else
client.loop = loop
end
if registry_host == nil then
client.registry_host = "localhost"
else
client.registry_host = registry_host
end
client.service_connections = {}
client.registry_connection = Connection.create(client.ctx, client.loop, "tcp://" .. client.registry_host .. ":8420", 'registry')
return client
end
function Client:getConnection(service_name, cb)
if self.service_connections[service_name] then
cb(nil, self.service_connections[service_name])
else
self.registry_connection:sendMethod("getService", {service_name}, function(err, service)
if service ~= nil then
local service_connection = Connection.create(self.ctx, self.loop, "tcp://" .. self.registry_host .. ":" .. service.port)
self.service_connections[service_name] = service_connection
cb(nil, service_connection)
else
cb("No such service")
end
end)
end
end
function Client:remote(service_name, method, args, cb)
self:getConnection(service_name, function (err, service_connection)
if service_connection ~= nil then
service_connection:sendMethod(method, args, cb)
else
print("[remote] Can't get service connection:", err)
cb("Can't get service connection")
end
end)
end
return Client