diff options
author | Magnus <mfeuer@jaguarlandrover.com> | 2014-07-26 13:03:00 -0700 |
---|---|---|
committer | Magnus <mfeuer@jaguarlandrover.com> | 2014-07-26 13:03:00 -0700 |
commit | db63795114903f01da6584f02a7e910bfe56e9bf (patch) | |
tree | b255d51395119b85820775250f345efee3db01dc | |
parent | 22c75d36cedb85e8a663d5fb79081c5e2fdaf33d (diff) | |
download | rvi_core-db63795114903f01da6584f02a7e910bfe56e9bf.tar.gz |
Initial gerrit commit
Signed-off-by: Magnus <mfeuer@jaguarlandrover.com>
-rw-r--r-- | Makefile | 40 | ||||
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | curl_scripts/register_service.sh | 31 | ||||
-rw-r--r-- | curl_scripts/resolve_service.sh | 26 | ||||
-rwxr-xr-x | find_setup_gen.sh | 17 | ||||
-rw-r--r-- | priv/setup.config | 46 | ||||
-rw-r--r-- | rebar.config | 9 | ||||
-rw-r--r-- | src/service_discovery.app.src | 14 | ||||
-rw-r--r-- | src/service_discovery_app.erl | 22 | ||||
-rw-r--r-- | src/service_discovery_lib.erl | 38 | ||||
-rw-r--r-- | src/service_discovery_rpc.erl | 108 | ||||
-rw-r--r-- | src/service_discovery_sup.erl | 35 |
12 files changed, 387 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..322421e --- /dev/null +++ b/Makefile @@ -0,0 +1,40 @@ +.PHONY: all deps compile setup clean doc + + +NAME=service_discovery +export KVDB_BACKENDS=ets + +SETUP_GEN=$(shell ./find_setup_gen.sh) + +all: deps compile + +deps: + rebar get-deps + +compile: + rebar compile + +recomp: + rebar compile skip_deps=true + +setup: + ERL_LIBS=$(PWD)/deps:$(ERL_LIBS):$(PWD) \ + $(SETUP_GEN) $(NAME) priv/setup.config setup + +target: + ERL_LIBS=$(PWD)/deps:$(ERL_LIBS) \ + $(SETUP_GEN) $(NAME) priv/setup.config setup -pz $(PWD)/ebin \ + -target rel -vsn 0.1 + +run: setup + erl -boot setup/start -config setup/sys + +doc: + REBAR_DOC=1 rebar skip_deps=true get-deps doc + +clean: + rebar clean + + + + diff --git a/README.md b/README.md new file mode 100644 index 0000000..9c0852f --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Better documentation will come. diff --git a/curl_scripts/register_service.sh b/curl_scripts/register_service.sh new file mode 100644 index 0000000..7441eb4 --- /dev/null +++ b/curl_scripts/register_service.sh @@ -0,0 +1,31 @@ +#!/bin/sh +# Create new accounts, like the ga account +# But other accounts may be create +. $HOME/.exodmrc + + #if [ $# != 2 ] +#then +# echo "Usage: $0 temperature" +# exit 255 +#fi +# the password (actually erlang node cookie) must be 100% hidden +# so this is only for testing!!!!! + +URL=http://localhost:8801/exodm/rpc +curl -u $USER_AUTH -k -X POST $URL -d @- << EOF +{ + "jsonrpc": "2.0", + + "method": "service_discovery:register_service", + "id": "1", + "params": + { + "service": "hvac", + "address": "http://localhost:8901", + "methods": [ + { "access_type": "rpc", "method": "set_temperature" }, + { "access_type": "rpc", "method": "set_fan_speed" } + ] + } +} +EOF diff --git a/curl_scripts/resolve_service.sh b/curl_scripts/resolve_service.sh new file mode 100644 index 0000000..948c834 --- /dev/null +++ b/curl_scripts/resolve_service.sh @@ -0,0 +1,26 @@ +#!/bin/sh +# Create new accounts, like the ga account +# But other accounts may be create +. $HOME/.exodmrc + + #if [ $# != 2 ] +#then +# echo "Usage: $0 temperature" +# exit 255 +#fi +# the password (actually erlang node cookie) must be 100% hidden +# so this is only for testing!!!!! + +URL=http://localhost:8801/exodm/rpc +curl -u $USER_AUTH -k -X POST $URL -d @- << EOF +{ + "jsonrpc": "2.0", + + "method": "service_discovery:resolve_service", + "id": "1", + "params": + { + "service": "rpc://jaguarlandrover.com/vin/1234/services/hvac/set_speed" + } +} +EOF diff --git a/find_setup_gen.sh b/find_setup_gen.sh new file mode 100755 index 0000000..72cc213 --- /dev/null +++ b/find_setup_gen.sh @@ -0,0 +1,17 @@ +#!/bin/sh + +if [ -z $ERL_LIBS ]; then + L=`pwd`/deps +else + L=`pwd`/deps:$ERL_LIBS +fi + +dirs=`echo $L | sed 's/\:/ /g'` + +for d in $dirs; do + f=$d/setup/setup_gen + if [ -f $f ]; then + echo "$f" + exit 0 + fi +done diff --git a/priv/setup.config b/priv/setup.config new file mode 100644 index 0000000..843382c --- /dev/null +++ b/priv/setup.config @@ -0,0 +1,46 @@ +%% -*- erlang -*- +[ + %% Put include first, making it possible to override any defaults below + {include_lib, "exoport/priv/setup.config"}, + %% + %% Add our own app(s) + {add_apps, [asn1, + ssl, + rvi_common, + service_discovery]}, + %% + %% Custom environment settings + {env, + [ + {setup, [{data_dir, "db"}]}, + %% Tell exoport where to find our config file + {exoport, + [ + {bert_port, 9991}, %% 9999 was taken. + {config, filename:join(CWD, "exoport.config")}, + {access, + [{redirect, [{service_discovery, service_discovery_rpc}]}, + {accept, service_discovery_rpc} + ]}, + {exo_http, + [{port, 8801}, + {appmod, {exoport_exo_http, service_discovery_rpc}}]}, + {kvdb_databases, + [{kvdb_conf, + [{file,"$DATA_DIR/kvdb_conf.db"}, + {backend,ets}, + {log_dir, "$DATA_DIR/kvdb_conf.log"}, + {log_threshold, [{writes, 1000}]}, + {save_mode, [on_switch, on_close]}, + {tables,[data]}, + {encoding,{raw,term,term}}, + {schema,kvdb_schema_events}]} + ]} + ]} + %% %% We run with a logging ETS backend in the database + %% {kvdb, + %% [ + %% ]} + %% ]} + ]} +]. diff --git a/rebar.config b/rebar.config new file mode 100644 index 0000000..a85e8ea --- /dev/null +++ b/rebar.config @@ -0,0 +1,9 @@ +%% -*- erlang -*- + +{erl_opts, [debug_info]}. + +{deps, + [ + {setup, ".*", {git, "https://github.com/uwiger/setup.git", "HEAD"}}, + {rvi_common, ".*", {git, "https://gerrit.automotivelinux.org/gerrit/RVI/rvi_common", "HEAD"}} + ]}. diff --git a/src/service_discovery.app.src b/src/service_discovery.app.src new file mode 100644 index 0000000..92037b9 --- /dev/null +++ b/src/service_discovery.app.src @@ -0,0 +1,14 @@ +%% -*- erlang -*- +{application, service_discovery, + [ + {description, ""}, + {vsn, "0.1"}, + {registered, []}, + {applications, [ + kernel, + stdlib, + rvi_common + ]}, + {mod, { service_discovery_app, []}}, + {start_phases, [{init, []}]} + ]}. diff --git a/src/service_discovery_app.erl b/src/service_discovery_app.erl new file mode 100644 index 0000000..f8523d1 --- /dev/null +++ b/src/service_discovery_app.erl @@ -0,0 +1,22 @@ +-module(service_discovery_app). + +-behaviour(application). + +%% Application callbacks +-export([start/2, + start_phase/3, + stop/1]). + +%% =================================================================== +%% Application callbacks +%% =================================================================== + +start(_StartType, _StartArgs) -> + service_discovery_sup:start_link(). + +start_phase(init, _, _) -> + service_discovery_rpc:init(), + ok. + +stop(_State) -> + ok. diff --git a/src/service_discovery_lib.erl b/src/service_discovery_lib.erl new file mode 100644 index 0000000..c09bdf1 --- /dev/null +++ b/src/service_discovery_lib.erl @@ -0,0 +1,38 @@ +-module(service_discovery_lib). + +-export([timestamp/0, + ts_to_datetime/1, + make_decimal/1, + can_data_value/2, + find_val/3]). + +-define(EPOCH, 62167219200). + +timestamp() -> + {_,_,US} = Now = os:timestamp(), + MS = round(US/1000), + %% ?EPOCH is 1970-1-1, 0:0:0 in gregorian seconds + (calendar:datetime_to_gregorian_seconds( + calendar:now_to_universal_time(Now)) - ?EPOCH) * 1000 + MS. + +ts_to_datetime(TS) -> + MS = TS rem 1000, + S = TS div 1000, + {calendar:gregorian_seconds_to_datetime(S + ?EPOCH), MS}. + + +make_decimal(MS) -> + S = MS div 1000, + Rem = MS rem 1000, + integer_to_list(S) ++ "." ++ integer_to_list(Rem). + +can_data_value(Len, Bin) -> + <<Val:Len/integer-unit:8>> = Bin, + Val. + +find_val(K, [{K,_,V}|_], _) -> + list_to_integer(binary_to_list(V)); +find_val(K, [_|T], Default) -> + find_val(K, T, Default); +find_val(_, [], Default) -> + Default. diff --git a/src/service_discovery_rpc.erl b/src/service_discovery_rpc.erl new file mode 100644 index 0000000..e08c2c3 --- /dev/null +++ b/src/service_discovery_rpc.erl @@ -0,0 +1,108 @@ +-module(service_discovery_rpc). + +-export([handle_rpc/2]). +-export([init/0]). + +-include_lib("lager/include/log.hrl"). +-define(SERVICE_TABLE, rvi_service_discovery). + + +-record(service_entry, { + service = [], + network_address = undefined %% Address where service can be found + }). + +%% Called by service_discovery_app:start_phase(). +init() -> + ets:new(?SERVICE_TABLE, [set, public, named_table, {keypos,2}]), + case rvi_common:get_component_config(service_discovery, exo_http_opts) of + { ok, ExoHttpOpts } -> + exoport_exo_http:instance(service_discovery_sup, + service_discovery_rpc, + ExoHttpOpts); + Err -> Err + end, + ok. + + +register_service(Service, NetworkAddress) -> + ?debug("service_discovery_rpc:register_service(): service: ~p ~n", [Service]), + ?debug("service_discovery_rpc:register_service(): network_address: ~p ~n", [NetworkAddress]), + + ets:insert(?SERVICE_TABLE, + #service_entry { + service = rvi_common:service_to_string(Service), + network_address = NetworkAddress + }), + + {ok, [ { status, rvi_common:json_rpc_status(ok)}]}. + + +resolve_service(RawService) -> + Service = rvi_common:sanitize_service_string(RawService), + ?debug("service_discovery_rpc:resolve_service(~p): raw(~p)~n", [Service, RawService]), + Svcs = ets:foldl(fun({service_entry, ServiceName, ServiceAddr}, Acc) -> + [ {ServiceName, ServiceAddr} | Acc ] end, + [], ?SERVICE_TABLE), + + ?debug("service_discovery_rpc:resolve_service(~p): ~p~n", [Service, Svcs]), + + case ets:lookup(?SERVICE_TABLE, Service) of + [] -> + ?debug("service_discovery_rpc:resolve_service(~p): Service not found in ets~n", + [Service]), + + %% Check if this is a service residing on the backend server + case rvi_common:is_backend_service(Service) of + false -> %% Not found + ?debug("service_discovery_rpc:resolve_service(~p): Service not found backend~n", [Service]), + + { ok, [ { status, rvi_common:json_rpc_status(not_found) }]}; + + true -> %% FOund + ?debug("service_discovery_rpc:resolve_service(~p): Service is backend~n", [Service]), {ok, [ { status, rvi_common:json_rpc_status(ok) }, + { network_address, rvi_common:backend_address_string() }]} + end; + + [#service_entry { network_address = NetworkAddress }] -> + ?debug("service_discovery_rpc:resolve_service(): service: ~p -> ~p~n", + [ Service, NetworkAddress ]), + + {ok, [ { status, rvi_common:json_rpc_status(ok) }, + { network_address, NetworkAddress }]} + end. + + +get_services() -> + Services = ets:foldl(fun({service_entry, ServiceName, ServiceAddr}, Acc) -> + [ {struct, + [ + {service, ServiceName}, + {address, ServiceAddr} + ] + } | Acc ] end, + [], ?SERVICE_TABLE), + + ?debug("service_discovery_rpc:get_services(): ~p~n", [ Services]), + {ok, [ { status, rvi_common:json_rpc_status(ok) }, + { services, {array, Services }}]}. + + +%% JSON-RPC entry point +%% CAlled by local exo http server +handle_rpc("register_service", Args) -> + {ok, Service} = rvi_common:get_json_element(["service"], Args), + {ok, Address} = rvi_common:get_json_element(["network_address"], Args), + register_service(Service, Address); + +handle_rpc("resolve_service", Args) -> + {ok, Service} = rvi_common:get_json_element(["service"], Args), + resolve_service(Service); + +handle_rpc("get_services", _Args) -> + get_services(); + +handle_rpc( Other, _Args) -> + ?debug("service_discovery_rpc:handle_rpc(~p)~n", [ Other ]), + { ok, [ { status, rvi_common:json_rpc_status(invalid_command)} ] }. + diff --git a/src/service_discovery_sup.erl b/src/service_discovery_sup.erl new file mode 100644 index 0000000..30bd866 --- /dev/null +++ b/src/service_discovery_sup.erl @@ -0,0 +1,35 @@ +-module(service_discovery_sup). + +-behaviour(supervisor). + +%% API +-export([start_link/0]). + +%% Supervisor callbacks +-export([init/1]). + +%% Helper macro for declaring children of supervisor +-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}). + +%% =================================================================== +%% API functions +%% =================================================================== + +start_link() -> + supervisor:start_link({local, ?MODULE}, ?MODULE, []). + +%% =================================================================== +%% Supervisor callbacks +%% =================================================================== + +init([]) -> + {ok, { {one_for_one, 5, 10}, + [ +%% ?CHILD(service_discovery_alarms, worker), +%% ?CHILD(service_discovery_log, worker), +%% ?CHILD(service_discovery_can, worker), +%% ?CHILD(service_discovery_waypoints, worker) + %% ?CHILD(service_discovery_gps, worker), + %% ?CHILD(exodmo_config, worker) + ]} }. + |