-
Notifications
You must be signed in to change notification settings - Fork 9
/
rabbitmqstomp.lua
executable file
·159 lines (129 loc) · 3.71 KB
/
rabbitmqstomp.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
-- lua-resty-rabbitmqstomp: Opinionated RabbitMQ (STOMP) client lib
-- Copyright (C) 2013 Rohit 'bhaisaab' Yadav, Wingify
-- Opensourced at Wingify in New Delhi under the MIT License
local concat = table.concat
local find = string.find
local insert = table.insert
local len = string.len
local pairs = pairs
local setmetatable = setmetatable
local sub = string.sub
local skynet = require "skynet"
local socketchannel = require "skynet.socketchannel"
local _M = {
_VERSION = "0.1"
}
_M.__index = _M
local mt = {__index = _M}
local LF = "\x0a"
local EOL = "\x0d\x0a"
local NULL_BYTE = "\x00"
function _M:_build_frame(command, headers, body)
local frame = {command, EOL}
if body then
headers["content-length"] = len(body)
end
for key, value in pairs(headers) do
insert(frame, key)
insert(frame, ":")
insert(frame, value)
insert(frame, EOL)
end
insert(frame, EOL)
if body then
insert(frame, body)
end
insert(frame, NULL_BYTE)
insert(frame, EOL)
return concat(frame, "")
end
local function __dispatch_resp(self)
return function(sock)
local frame
if self.opts.trailing_lf == nil or self.opts.trailing_lf == true then
frame = sock:readline(NULL_BYTE .. LF)
else
frame = sock:readline(NULL_BYTE)
end
if not frame then
return false
end
-- We successfully received a frame, but it was an ERROR frame
if sub(frame, 1, len("ERROR")) == "ERROR" then
skynet.error("rabbitmq error:", frame)
end
skynet.error("resp frame:", frame)
return true, frame
end
end
function _M:_send_frame(frame)
local dispatch_resp = __dispatch_resp(self)
return self.__sock:request(frame, dispatch_resp)
end
local function rabbitmq_login(self)
return function(sc)
local headers = {}
headers["accept-version"] = "1.2"
headers["login"] = self.opts.username
headers["passcode"] = self.opts.password
headers["host"] = self.opts.vhost
return self:_send_frame(self:_build_frame("CONNECT", headers, nil))
end
end
function _M.connect(conf, opts)
if opts == nil then
opts = {username = "guest", password = "guest", vhost = "/", trailing_lf = true}
end
local obj = {
opts = opts
}
obj.__sock =
socketchannel.channel {
auth = rabbitmq_login(obj),
host = conf.host or "127.0.0.1",
port = conf.port or 61613,
nodelay = true,
overload = conf.overload
}
setmetatable(obj, _M)
obj.__sock:connect(true)
return obj
end
function _M:send(smsg, headers)
local f = nil
if headers["receipt"] ~= nil then
f = __dispatch_resp(self)
end
return self.__sock:request(self:_build_frame("SEND", headers, smsg), f)
end
function _M:subscribe(headers, cb)
self.__cb = cb
return self:_send_frame(self:_build_frame("SUBSCRIBE", headers))
end
function _M:unsubscribe(headers)
return self:_send_frame(self:_build_frame("UNSUBSCRIBE", headers))
end
function _M:receive()
local so = self.__sock
while so do
local dispatch_resp = __dispatch_resp(self)
local data, err = so:response(dispatch_resp)
skynet.error("receive:", data, err)
if not data then
return nil, err
end
local idx = find(data, "\n\n", 2)
self.__cb(sub(data, idx + 2))
end
end
function _M:close()
if self.state then
-- Graceful shutdown
local headers = {}
headers["receipt"] = "disconnect"
self:_send_frame(self:_build_frame("DISCONNECT", headers, nil))
end
self.__sock:close()
setmetatable(self, nil)
end
return _M