-
Notifications
You must be signed in to change notification settings - Fork 0
/
erq.erl
186 lines (154 loc) · 6.66 KB
/
erq.erl
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
-module(erq).
-export([start/0, start/1, serve/1]).
-include_lib("erqconf.hrl").
-define(DEFAULT_PORT, 2345).
%% Read a line of data from the socket, for the control messages (get, set, etc).
read_line_of_data(Socket) ->
%% Active lets us poll for data so we can switch between line and buffer mode.
inet:setopts(Socket, [{active, false},
{packet, line}]),
gen_tcp:recv(Socket, 0).
%% Read a fixed size buffer of data from the socket, for the values.
read_fixed_data(Socket, Size) ->
inet:setopts(Socket, [{packet, raw}]),
Result = gen_tcp:recv(Socket, Size),
read_line_of_data(Socket),
Result.
update_config([], Conf) ->
Conf;
update_config(Opts, Conf) ->
[Opt|RemainingOpts] = Opts,
{Name, Value} = Opt,
NewConf = case Name of
address -> Conf#erqconf{address=Value};
port -> Conf#erqconf{port=Value}
end,
update_config(RemainingOpts, NewConf).
read_config(Filename) ->
{ok, Opts} = file:consult(Filename),
update_config(Opts, #erqconf{}).
start() ->
start("erq.conf").
start(ConfigFilename) ->
Config = read_config(ConfigFilename),
{ok, Listen} = gen_tcp:listen(Config#erqconf.port, [list,
{reuseaddr, true},
{packet, line},
{ip, Config#erqconf.address}
]),
io:format("Listening on ~p:~p", [Config#erqconf.address, Config#erqconf.port]),
spawn(fun() -> erq:serve(Listen) end),
self().
serve(Listen) ->
{ok, Socket} = gen_tcp:accept(Listen),
spawn(fun() -> erq:serve(Listen) end),
loop(Socket, none).
handle_set(Args, Socket) ->
[QueueName, _FlagsString, _ExpiryString, SizeString] = Args,
Size = list_to_integer(SizeString),
erqutils:debug("Reading data of size ~p.", [Size]),
case read_fixed_data(Socket, Size) of
{ok, Data} ->
case mqueue:enqueue(QueueName, Data) of
ack -> "STORED\r\n";
{error, ErrorMessage} -> lists:flatten(io_lib:format("SERVER_ERROR ~p\r\n",
[ErrorMessage]));
_ -> "SERVER_ERROR invalid response from queue process\r\n"
end;
Result ->
erqutils:unexpected_result(Result, "from read_fixed_data in handle_set")
end.
is_open_requested(Args) ->
lists:any(fun(X) -> X == "open" end, Args).
is_close_requested(Args) ->
lists:any(fun(X) -> X == "close" end, Args).
get_timeout_from_args([]) ->
nonblocking;
get_timeout_from_args(Args) ->
[First|Rest] = Args,
erqutils:debug("First/Rest: ~p/~p", [First,Rest]),
case string:sub_string(First, 1, 2) of
"t=" ->
{Timeout, _Rest} = string:to_integer(string:substr(First, 3)),
Timeout;
_ ->
get_timeout_from_args(Rest)
end.
parse_queuename_with_args(QueueNameWithArgs) ->
[QueueName|Args] = string:tokens(QueueNameWithArgs, "/"),
{QueueName, is_open_requested(Args), is_close_requested(Args), get_timeout_from_args(Args)}.
handle_get(_QueueName, false, true, _Timeout, _) ->
{"END\r\n", none};
handle_get(_QueueName, true, false, _Timeout, CheckedOutMessage) when CheckedOutMessage =/= none ->
{"ERROR must close old message before opening a new one\r\n", CheckedOutMessage}; % TODO: Is this a legit way to report the specific error?
handle_get(QueueName, OpenRequested, CloseRequested, Timeout, CheckedOutMessage) ->
case Timeout of
nonblocking ->
Result = mqueue:dequeue(QueueName);
_ ->
Result = mqueue:dequeue(QueueName, Timeout)
end,
erqutils:debug("get requested from queue: ~p", [QueueName]),
case Result of
{ok, Data} ->
erqutils:debug("Dequeued data: ~p", [Data]),
case OpenRequested of
true ->
NewCheckedOutMessage = {QueueName, Data};
false ->
case CloseRequested of
true -> NewCheckedOutMessage = none;
false -> NewCheckedOutMessage = CheckedOutMessage
end
end,
Response = lists:flatten(io_lib:format("VALUE ~s 0 ~.10B\r\n",
[QueueName, length(Data)]) ++ Data ++ "\r\nEND\r\n");
empty ->
erqutils:debug("Queue was empty, so nothing to dequeue.", []),
NewCheckedOutMessage = CheckedOutMessage,
Response = "END\r\n";
Other ->
erqutils:unexpected_result(Other, "mqueue:dequeue in handle_get"),
NewCheckedOutMessage = CheckedOutMessage,
Response = "ERROR\r\n"
%% should we use SERVER_ERROR with a message?
end,
{Response, NewCheckedOutMessage}.
handle_get(Args, CheckedOutMessage) ->
[QueueNameWithArgs] = Args,
{QueueName, OpenRequested, CloseRequested, Timeout} = parse_queuename_with_args(QueueNameWithArgs),
erqutils:debug("QueueName/OpenRequested/CloseRequested/Timeout: ~p/~p/~p/~p", [QueueName, OpenRequested, CloseRequested, Timeout]),
handle_get(QueueName, OpenRequested, CloseRequested, Timeout, CheckedOutMessage).
return_checked_out_message_if_necessary(none) -> ok;
return_checked_out_message_if_necessary(CheckedOutMessage) ->
{QueueName, Data} = CheckedOutMessage,
mqueue:return(QueueName, Data).
loop(Socket, CheckedOutMessage) ->
case read_line_of_data(Socket) of
{ok, StrWithNewline} ->
Str = erqutils:chomp(StrWithNewline),
erqutils:debug("Server received: = ~p", [Str]),
[Command|Args] = string:tokens(Str, " "),
erqutils:debug("command: ~p", [Command]),
case Command of
"get" ->
{Response, NewCheckedOutMessage} = handle_get(Args, CheckedOutMessage);
"set" ->
NewCheckedOutMessage = CheckedOutMessage,
Response = handle_set(Args, Socket);
_ ->
NewCheckedOutMessage = CheckedOutMessage,
Response = "ERROR\r\n"
end,
gen_tcp:send(Socket, Response),
loop(Socket, NewCheckedOutMessage);
{tcp_closed, Socket} ->
return_checked_out_message_if_necessary(CheckedOutMessage),
erqutils:debug("Server socket closed", []);
{error, closed} ->
return_checked_out_message_if_necessary(CheckedOutMessage),
{ok, closed};
Other ->
return_checked_out_message_if_necessary(CheckedOutMessage),
io:format("Dunno whats going on: ~p~n", [Other])
end.