summaryrefslogtreecommitdiff
path: root/components/proto_json/src/proto_json_rpc.erl
blob: 9f7ccc04a9a98362a5e694ecb24f87875e25e76e (plain)
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
%%
%% Copyright (C) 2014, Jaguar Land Rover
%%
%% This program is licensed under the terms and conditions of the
%% Mozilla Public License, version 2.0.  The full text of the
%% Mozilla Public License is at https://www.mozilla.org/MPL/2.0/
%%


-module(proto_json_rpc).
-behaviour(gen_server).

-export([handle_rpc/2,
	 handle_notification/2]).
-export([start_link/0]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
	 terminate/2, code_change/3]).


-include_lib("lager/include/log.hrl").
-include_lib("rvi_common/include/rvi_common.hrl").

-define(SERVER, ?MODULE).
-export([start_json_server/0]).
-export([send_message/8,
	 receive_message/3]).

-record(st, {
	  %% Component specification
	  queue = [],
	  cs = #component_spec{}
	  }).

start_link() ->
    gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).

init([]) ->
    ?debug("proto_json_rpc:init(): called."),
    {ok, #st { cs = rvi_common:get_component_specification() } }.

start_json_server() ->
    rvi_common:start_json_rpc_server(protocol, ?MODULE, proto_json_sup).



send_message(CompSpec,
	     TID,
	     ServiceName,
	     Timeout,
	     ProtoOpts,
	     DataLinkMod,
	     DataLinkOpts,
	     Parameters) ->
    rvi_common:request(protocol, ?MODULE, send_message,
		       [{ transaction_id, TID },
			{ service, ServiceName },
			{ timeout, Timeout },
			{ protocol_opts, ProtoOpts },
			{ data_link_mod, DataLinkMod },
			{ data_link_opts, DataLinkOpts },
			{ parameters, Parameters }],
			[ status ], CompSpec).

receive_message(CompSpec, {IP, Port}, Data) ->
    rvi_common:notification(protocol, ?MODULE, receive_message,
			    [ {data, Data },
                              {remote_ip, IP},
                              {remote_port, Port} ],
			    CompSpec).

%% JSON-RPC entry point

%% CAlled by local exo http server
handle_rpc("send_message", Args) ->
    LogId = rvi_common:get_json_log_id(Args),
    {ok, TID} = rvi_common:get_json_element(["transaction_id"], Args),
    {ok, ServiceName} = rvi_common:get_json_element(["service_name"], Args),
    {ok, Timeout} = rvi_common:get_json_element(["timeout"], Args),
    {ok, ProtoOpts} = rvi_common:get_json_element(["protocol_opts"], Args),
    {ok, DataLinkMod} = rvi_common:get_json_element(["data_link_mod"], Args),
    {ok, DataLinkOpts} = rvi_common:get_json_element(["data_link_opts"], Args),
    {ok, Parameters} = rvi_common:get_json_element(["parameters"], Args),
    [ ok ] = gen_server:call(?SERVER, { rvi, send_message,
					[TID,
					 ServiceName,
					 Timeout,
					 ProtoOpts,
					 DataLinkMod,
					 DataLinkOpts,
					 Parameters,
					 LogId]}),
    {ok, [ {status, rvi_common:json_rpc_status(ok)} ]};



handle_rpc(Other, _Args) ->
    ?warning("proto_json_rpc:handle_rpc(~p): Unknown~n", [ Other ]),
    { ok, [ { status, rvi_common:json_rpc_status(invalid_command)} ] }.


handle_notification("receive_message", Args) ->
    LogId = rvi_common:get_json_log_id(Args),
    {ok, Data} = rvi_common:get_json_element(["data"], Args),
    {ok, RemoteIP} = rvi_common:get_json_element(["remote_ip"], Args),
    {ok, RemotePort} = rvi_common:get_json_element(["remote_port"], Args),
    gen_server:cast(?SERVER, { rvi, receive_message, [Data,
						      RemoteIP,
						      RemotePort,
						      LogId]}),
    ok;

handle_notification(Other, _Args) ->
    ?debug("proto_json_rpc:handle_other(~p): unknown", [ Other ]),
    ok.


handle_call({rvi, send_message,
	     [TID,
	      ServiceName,
	      Timeout,
	      ProtoOpts,
	      DataLinkMod,
	      DataLinkOpts,
	      Parameters | _LogId]}, _From, St) ->
    ?debug("    protocol:send(): transaction id:  ~p~n", [TID]),
    ?debug("    protocol:send(): service name:    ~p~n", [ServiceName]),
    ?debug("    protocol:send(): timeout:         ~p~n", [Timeout]),
    ?debug("    protocol:send(): opts:            ~p~n", [ProtoOpts]),
    ?debug("    protocol:send(): data_link_mod:   ~p~n", [DataLinkMod]),
    ?debug("    protocol:send(): data_link_opts:  ~p~n", [DataLinkOpts]),
    ?debug("    protocol:send(): parameters:      ~p~n", [Parameters]),
    Data = jsx:encode([
		       { <<"tid">>, TID },
		       { <<"service">>, ServiceName },
		       { <<"timeout">>, Timeout },
		       { <<"parameters">>, Parameters }
		      ]),

    case use_frag(Parameters, DataLinkOpts) of
	{true, Window} ->
	    {Res, St1} =
		chunk_message(Window, TID, ServiceName, DataLinkMod,
			      DataLinkOpts, iolist_to_binary(Data), St),
	    {reply, Res, St1};
	false ->
	    Res = DataLinkMod:send_data(
		    St#st.cs, ?MODULE, ServiceName, DataLinkOpts, Data),
	    {reply, Res, St}
    end;

handle_call(Other, _From, St) ->
    ?warning("proto_json_rpc:handle_call(~p): unknown", [ Other ]),
    { reply, [ invalid_command ], St}.

%% Convert list-based data to binary.
handle_cast({rvi, receive_message, [Payload, IP, Port | _LogId]} = Msg, St) ->
    ?debug("~p:handle_cast(~p)", [?MODULE, Msg]),
    Elems = jsx:decode(iolist_to_binary(Payload)),

    case Elems of
	[{<<"frg">>, _}|_] ->
	    St1 = handle_frag(Elems, IP, Port, St),
	    {noreply, St1};
	_ ->
	    [ ServiceName, Timeout, Parameters ] =
		opts([<<"service">>, <<"timeout">>, <<"parameters">>],
		     Elems, undefined),

	    ?debug("    protocol:rcv(): service name:    ~p~n", [ServiceName]),
	    ?debug("    protocol:rcv(): timeout:         ~p~n", [Timeout]),
	    ?debug("    protocol:rcv(): remote IP/Port:  ~p~n", [{IP, Port}]),

	    service_edge_rpc:handle_remote_message(St#st.cs,
						   {IP, Port},
						   ServiceName,
						   Timeout,
						   Parameters),
	    {noreply, St}
    end;


handle_cast(Other, St) ->
    ?warning("proto_json_rpc:handle_cast(~p): unknown", [ Other ]),
    {noreply, St}.

handle_info(_Info, St) ->
    {noreply, St}.

terminate(_Reason, _St) ->
    ok.
code_change(_OldVsn, St, _Extra) ->
    {ok, St}.

opt(K, L, Def) ->
    case lists:keyfind(K, 1, L) of
	{_, V} -> V;
	false  -> Def
    end.

opts(Keys, Elems, Def) ->
    [ opt(K, Elems, Def) || K <- Keys].

use_frag(Params, DLinkOpts) ->
    case p_reliable(Params) of
	undefined ->
	    d_reliable(DLinkOpts);
	Other ->
	    Other
    end.

%% We use reliable send (i.e. fragmentation support) if:
%% - rvi.max_msg_size is set in the Params (overrides static config)
%% - rvi.reliable = true in the Params
%% - max_msg_size is set for the data link
%% - {reliable, true} defined for the data link
%%
%% If {reliable, true} and no max_message_size, we send a single packet
%% as one fragment (marking it as first and last fragment) and use the
%% ack mechanism to acknowledge successful delivery.
%%
p_reliable([{"rvi.max_msg_size", Sz}|_]) -> {true, Sz};
p_reliable([{"rvi.reliable", true}|_])   -> {true, infinity};
p_reliable([{"rvi.reliable", false}|_])  -> false;
p_reliable([_|T]) -> p_reliable(T);
p_reliable([])    -> undefined.

d_reliable([{max_msg_size, Sz}|_]) -> {true, Sz};
d_reliable([{reliable, true}|_])   -> {true, infinity};
d_reliable([{reliable, false}|_])  -> false;
d_reliable([_|T]) -> d_reliable(T);
d_reliable([])    -> false.

chunk_message(Window, TID, _ServiceName, _DLinkMod, _DLinkOpts, Data, St) ->
    _Frag = first_frag(Window, TID, Data),

    {ok, St}.

handle_frag(_Elems, _IP, _Port, _St) ->
    error(nyi).

first_frag(_Window, _TID, _Data) ->
    error(nyi).