-
Notifications
You must be signed in to change notification settings - Fork 50
/
cloudi_service_udp.erl
179 lines (160 loc) · 7.34 KB
/
cloudi_service_udp.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
%-*-Mode:erlang;coding:utf-8;tab-width:4;c-basic-offset:4;indent-tabs-mode:()-*-
% ex: set ft=erlang fenc=utf-8 sts=4 ts=4 sw=4 et nomod:
%%%
%%%------------------------------------------------------------------------
%%% @doc
%%% ==Basic CloudI UDP Integration==
%%% @end
%%%
%%% MIT License
%%%
%%% Copyright (c) 2013-2021 Michael Truog <mjtruog at protonmail dot com>
%%%
%%% Permission is hereby granted, free of charge, to any person obtaining a
%%% copy of this software and associated documentation files (the "Software"),
%%% to deal in the Software without restriction, including without limitation
%%% the rights to use, copy, modify, merge, publish, distribute, sublicense,
%%% and/or sell copies of the Software, and to permit persons to whom the
%%% Software is furnished to do so, subject to the following conditions:
%%%
%%% The above copyright notice and this permission notice shall be included in
%%% all copies or substantial portions of the Software.
%%%
%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
%%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
%%% DEALINGS IN THE SOFTWARE.
%%%
%%% @author Michael Truog <mjtruog at protonmail dot com>
%%% @copyright 2013-2021 Michael Truog
%%% @version 2.0.3 {@date} {@time}
%%%------------------------------------------------------------------------
-module(cloudi_service_udp).
-author('mjtruog at protonmail dot com').
-behaviour(cloudi_service).
%% external interface
%% cloudi_service callbacks
-export([cloudi_service_init/4,
cloudi_service_handle_info/3,
cloudi_service_terminate/3]).
-include_lib("cloudi_core/include/cloudi_logger.hrl").
-define(DEFAULT_INTERFACE, {127,0,0,1}). % ip address
-define(DEFAULT_PORT, 8081).
-define(DEFAULT_DESTINATION, undefined). % service name
-record(state,
{
socket,
interface,
interface_formatted,
port,
port_formatted,
destination,
requests = #{}
}).
-record(request,
{
source_address,
source_port
}).
%%%------------------------------------------------------------------------
%%% External interface functions
%%%------------------------------------------------------------------------
%%%------------------------------------------------------------------------
%%% Callback functions from cloudi_service
%%%------------------------------------------------------------------------
cloudi_service_init(Args, _Prefix, _Timeout, Dispatcher) ->
Defaults = [
{ip, ?DEFAULT_INTERFACE},
{port, ?DEFAULT_PORT},
{destination, ?DEFAULT_DESTINATION}],
[Interface, Port, Destination] =
cloudi_proplists:take_values(Defaults, Args),
1 = cloudi_service:process_count_max(Dispatcher),
true = cloudi_service:duo_mode(Dispatcher),
true = is_integer(Port),
true = is_list(Destination),
case gen_udp:open(Port, [{ip, Interface}, {active, true}, binary]) of
{ok, Socket} ->
case inet:sockname(Socket) of
{ok, {InterfaceUsed, PortUsed}} ->
InterfaceFormatted =
cloudi_ip_address:to_binary(InterfaceUsed),
PortFormatted = erlang:integer_to_binary(PortUsed),
{ok, #state{socket = Socket,
interface = InterfaceUsed,
interface_formatted = InterfaceFormatted,
port = PortUsed,
port_formatted = PortFormatted,
destination = Destination}};
{error, _} = Error ->
{stop, Error, #state{socket = Socket}}
end;
{error, _} = Error ->
{stop, Error, #state{}}
end.
cloudi_service_handle_info({udp, Socket, SourceAddress, SourcePort, Request},
#state{socket = Socket,
interface_formatted =
DestinationAddressFormatted,
port_formatted =
DestinationPortFormatted,
destination = Name,
requests = Requests} = State, Dispatcher) ->
SourceAddressFormatted = cloudi_ip_address:to_binary(SourceAddress),
SourcePortFormatted = erlang:integer_to_binary(SourcePort),
RequestInfo = cloudi_request_info:key_value_new(
[{<<"source-address">>, SourceAddressFormatted},
{<<"source-port">>, SourcePortFormatted},
{<<"destination-address">>, DestinationAddressFormatted},
{<<"destination-port">>, DestinationPortFormatted}]),
case cloudi_service:send_async_active(Dispatcher, Name,
RequestInfo, Request,
undefined, undefined) of
{ok, TransId} ->
RequestData = #request{source_address = SourceAddress,
source_port = SourcePort},
{noreply, State#state{requests = maps:put(TransId, RequestData,
Requests)}};
{error, Reason} ->
?LOG_ERROR("dropped incoming udp packet: ~p", [Reason]),
{noreply, State}
end;
cloudi_service_handle_info({return_async_active, _Name, _Pattern,
_ResponseInfo, Response, _Timeout, TransId},
#state{socket = Socket,
requests = Requests} = State, _Dispatcher) ->
{#request{source_address = SourceAddress,
source_port = SourcePort},
NewRequests} = maps:take(TransId, Requests),
send(Socket, SourceAddress, SourcePort, Response),
{noreply, State#state{requests = NewRequests}};
cloudi_service_handle_info({timeout_async_active, TransId},
#state{socket = Socket,
requests = Requests} = State, _Dispatcher) ->
{#request{source_address = SourceAddress,
source_port = SourcePort},
NewRequests} = maps:take(TransId, Requests),
send(Socket, SourceAddress, SourcePort, <<>>),
{noreply, State#state{requests = NewRequests}};
cloudi_service_handle_info(Request, State, _Dispatcher) ->
{stop, cloudi_string:format("Unknown info \"~w\"", [Request]), State}.
cloudi_service_terminate(_Reason, _Timeout, undefined) ->
ok;
cloudi_service_terminate(_Reason, _Timeout,
#state{socket = Socket}) ->
catch gen_udp:close(Socket),
ok.
%%%------------------------------------------------------------------------
%%% Private functions
%%%------------------------------------------------------------------------
send(Socket, SourceAddress, SourcePort, Response) ->
case gen_udp:send(Socket, SourceAddress, SourcePort, Response) of
ok ->
ok;
{error, Reason} ->
?LOG_ERROR("dropped outgoing udp packet: ~p", [Reason]),
ok
end.