diff options
author | Magnus Feuer <mfeuer@jaguarlandrover.com> | 2015-03-24 11:28:09 -0700 |
---|---|---|
committer | Magnus Feuer <mfeuer@jaguarlandrover.com> | 2015-03-24 11:28:09 -0700 |
commit | abb9c0b4686470a8d9b175a91b73ce0dfce5ab05 (patch) | |
tree | 77798c9c59cd17095d3d8a4c4eac554a043d0672 /components/rvi_common | |
parent | c766984479e4dcd1db585cb6d0d84b7d14422f50 (diff) | |
download | rvi_core-abb9c0b4686470a8d9b175a91b73ce0dfce5ab05.tar.gz |
Temp
Diffstat (limited to 'components/rvi_common')
-rw-r--r-- | components/rvi_common/src/exoport_exo_http.erl | 21 | ||||
-rw-r--r-- | components/rvi_common/src/rvi_common.erl | 77 |
2 files changed, 86 insertions, 12 deletions
diff --git a/components/rvi_common/src/exoport_exo_http.erl b/components/rvi_common/src/exoport_exo_http.erl index fcbf0a3..193b730 100644 --- a/components/rvi_common/src/exoport_exo_http.erl +++ b/components/rvi_common/src/exoport_exo_http.erl @@ -44,11 +44,14 @@ handle_body(Socket, Request, Body, AppMod) when Request#http_request.method == ' {error, Error} -> error_response(Socket, Id, Error) end; - {notification, _Method, _Args} -> - %% FIXME: Notification. + + {notification, Method, Args} -> + handle_notification(AppMod, Method, Args), exo_http_server:response(Socket, undefined, 200, "OK", ""); + {error, _} -> error_response(Socket, parse_error) + catch error:_ -> exo_http_server:response(Socket, undefined, 501, @@ -86,6 +89,20 @@ handle_rpc(Mod, Method, Args) -> {error, {internal_error, Crash}} end. +handle_notification(Mod, Method, Args) -> + ?debug("exoport_exo_http_server:handle_notification(): Mod: ~p", [Mod]), + ?debug("exoport_exo_http_server:handle_notification(): Method: ~p", [Method]), + ?debug("exoport_exo_http_server:handle_notification(): Args: ~p", [Args]), + + try Mod:handle_notification(Method, Args) of + _ -> ok + catch + error:Crash -> + ?error("rpc_callback() CRASHED: Reason: ~p", [Crash]), + ?error("post_request() CRASHED: Stack: ~p", [erlang:get_stacktrace()]), + {error, {internal_error, Crash}} + end. + success_response(Socket, Id, Reply) -> JSON = {struct, [{"jsonrpc", "2.0"}, {"id", Id}, diff --git a/components/rvi_common/src/rvi_common.erl b/components/rvi_common/src/rvi_common.erl index 9881a20..b7a5243 100644 --- a/components/rvi_common/src/rvi_common.erl +++ b/components/rvi_common/src/rvi_common.erl @@ -14,8 +14,10 @@ -include_lib("rvi_common/include/rvi_common.hrl"). --export([send_http_request/3]). +-export([send_json_request/3]). +-export([send_json_notification/3]). -export([request/6]). +-export([notification/6]). -export([json_rpc_status/1]). -export([get_json_element/2]). -export([sanitize_service_string/1]). @@ -171,7 +173,7 @@ request(Component, ?debug("Sending ~p:~p(~p) -> ~p.", [Module, Function, InArg, JSONArg]), case get_request_result( - send_http_request(URL, atom_to_list(Function), JSONArg) + send_json_request(URL, atom_to_list(Function), JSONArg) ) of { ok, JSONBody} -> @@ -183,7 +185,36 @@ request(Component, Err1 -> Err1 end. -send_http_request(Url,Method, Args) -> +notification(Component, + Module, + Function, + InArgPropList, + OutArgSpec, + CompSpec) -> + + %% Split [ { network_address, "127.0.0.1:888" } , { timeout, 34 } ] to + %% [ "127.0.0.1:888", 34] [ network_address, timeout ] + InArg = [ Val || { _Key, Val } <- InArgPropList ], + InArgSpec = [ Key || { Key, _Val } <- InArgPropList ], + %% Figure out how we are to invoke this MFA. + case get_module_type(Component, Module, CompSpec) of + %% We have a gen_server + { ok, gen_server } -> + ?debug("Sending ~p - ~p:~p(~p)", [Component, Module, Function, InArg]), + gen_server:cast(Module, { rvi_call, Function, InArg}), + ok; + + %% We have a JSON-RPC server + { ok, json_rpc } -> + URL = get_module_json_rpc_url(Component, Module, CompSpec), + ?debug("Sending ~p:~p(~p) -> ~p.", [Module, Function, InArg, URL]), + JSONArg = json_argument(InArg, InArgSpec), + ?debug("Sending ~p:~p(~p) -> ~p.", [Module, Function, InArg, JSONArg]), + send_json_notification(URL, atom_to_list(Function), JSONArg), + ok + end. + +send_json_request(Url,Method, Args) -> Req = binary_to_list( iolist_to_binary( @@ -195,17 +226,43 @@ send_http_request(Url,Method, Args) -> }))), Hdrs = [{'Content-Type', "application/json"} ], - %%?debug("rvi_common:send_http_request() Sending: ~p", [Req]), + %%?debug("rvi_common:send_json_request() Sending: ~p", [Req]), + try + exo_http:wpost(Url, {1,1}, Hdrs, Req, 1000) + catch + Type:Reason -> + ?error("rvi_common:send_json_request() CRASHED: URL: ~p", [Url]), + ?error("rvi_common:send_json_request() CRASHED: Hdrs: ~p", [Hdrs]), + ?error("rvi_common:send_json_request() CRASHED: Body: ~p", [Req]), + ?error("rvi_common:send_json_request() CRASHED: Type: ~p", [Type]), + ?error("rvi_common:send_json_request() CRASHED: Reason: ~p", [Reason]), + ?error("rvi_common:send_json_request() CRASHED: Stack: ~p", [ erlang:get_stacktrace()]), + {error, internal} + end. + + +send_json_notification(Url,Method, Args) -> + + Req = binary_to_list( + iolist_to_binary( + exo_json:encode({struct, [{"jsonrpc", "2.0"}, + {"method", Method}, + {"params", Args} + ] + }))), + + Hdrs = [{'Content-Type', "application/json"} ], + %%?debug("rvi_common:send_json_notification() Sending: ~p", [Req]), try exo_http:wpost(Url, {1,1}, Hdrs, Req, 1000) catch Type:Reason -> - ?error("rvi_common:send_http_request() CRASHED: URL: ~p", [Url]), - ?error("rvi_common:send_http_request() CRASHED: Hdrs: ~p", [Hdrs]), - ?error("rvi_common:send_http_request() CRASHED: Body: ~p", [Req]), - ?error("rvi_common:send_http_request() CRASHED: Type: ~p", [Type]), - ?error("rvi_common:send_http_request() CRASHED: Reason: ~p", [Reason]), - ?error("rvi_common:send_http_request() CRASHED: Stack: ~p", [ erlang:get_stacktrace()]), + ?error("rvi_common:send_json_notification() CRASHED: URL: ~p", [Url]), + ?error("rvi_common:send_json_notification() CRASHED: Hdrs: ~p", [Hdrs]), + ?error("rvi_common:send_json_notification() CRASHED: Body: ~p", [Req]), + ?error("rvi_common:send_json_notification() CRASHED: Type: ~p", [Type]), + ?error("rvi_common:send_json_notification() CRASHED: Reason: ~p", [Reason]), + ?error("rvi_common:send_json_notification() CRASHED: Stack: ~p", [ erlang:get_stacktrace()]), {error, internal} end. |