diff options
-rw-r--r-- | components/proto_json/src/proto_json_rpc.erl | 4 | ||||
-rw-r--r-- | components/schedule/src/schedule_rpc.erl | 2 | ||||
-rw-r--r-- | components/service_edge/src/service_edge_rpc.erl | 15 | ||||
-rwxr-xr-x | python/rvi_call_ws.py | 39 | ||||
-rw-r--r-- | python/rvi_service_ws.py | 123 |
5 files changed, 173 insertions, 10 deletions
diff --git a/components/proto_json/src/proto_json_rpc.erl b/components/proto_json/src/proto_json_rpc.erl index 54ed2d3..7fd6b77 100644 --- a/components/proto_json/src/proto_json_rpc.erl +++ b/components/proto_json/src/proto_json_rpc.erl @@ -127,7 +127,7 @@ handle_call({rvi, send_message, [ { "service", ServiceName }, { "timeout", Timeout }, - { "parameters", {array, Parameters} }, + { "parameters", Parameters }, { "signature", Signature } ] }), @@ -148,7 +148,7 @@ handle_cast({rvi, receive_message, [Payload, IP, Port]}, St) when is_binary(Payl handle_cast({rvi, receive_message, [Payload, IP, Port]}, St) -> {ok, {struct, Elems}} = exo_json:decode_string(Payload), - [ ServiceName, Timeout, {array, Parameters}, Signature ] = + [ ServiceName, Timeout, Parameters, Signature ] = opts(["service", "timeout", "parameters", "signature"], Elems, undefined), ?debug(" protocol:rcv(): service name: ~p~n", [ServiceName]), diff --git a/components/schedule/src/schedule_rpc.erl b/components/schedule/src/schedule_rpc.erl index 4726521..eda8153 100644 --- a/components/schedule/src/schedule_rpc.erl +++ b/components/schedule/src/schedule_rpc.erl @@ -130,7 +130,7 @@ schedule_message(CompSpec, schedule_message, [{ service, SvcName }, { timeout, Timeout }, - { parameters, Parameters }, + { parameters, {struct, Parameters } }, { signature, Signature }], [status, transaction_id], CompSpec). diff --git a/components/service_edge/src/service_edge_rpc.erl b/components/service_edge/src/service_edge_rpc.erl index 354f724..c2f0503 100644 --- a/components/service_edge/src/service_edge_rpc.erl +++ b/components/service_edge/src/service_edge_rpc.erl @@ -195,7 +195,7 @@ handle_ws_json_rpc(WSock, "message", Params, _Arg ) -> ?debug("service_edge_rpc:handle_websocket(~p) parameters: ~p", [ WSock, Parameters ]), [ Res, TID ] = gen_server:call(?SERVER, { rvi, handle_local_message, - [ SvcName, Timeout, [{struct, Parameters}]]}), + [ SvcName, Timeout, Parameters]}), ?debug("service_edge_rpc:wse_message(~p) Res: ~p", [ WSock, Res ]), { ok, [ { status, rvi_common:json_rpc_status(Res) }, @@ -437,7 +437,7 @@ handle_call({ rvi, handle_local_message, ?debug("service_edge_rpc:local_msg(): Service is local. Forwarding."), Res = forward_message_to_local_service(URL, SvcName, - Parameters, + {struct, Parameters}, St#st.cs), { reply, Res , St}; @@ -492,6 +492,7 @@ handle_cast({rvi, handle_remote_message, %% Check if this is a local message. case ets:lookup(?SERVICE_TABLE, SvcName) of [ #service_entry { url = URL }] -> %% This is a local message + {struct, Parameters1} = Parameters, case authorize_rpc:authorize_remote_message( St#st.cs, SvcName, @@ -500,7 +501,7 @@ handle_cast({rvi, handle_remote_message, {service_name, SvcName}, {timeout, Timeout}, %% {parameters, [ {struct, Parameters}]}, - {parameters, Parameters}, + {parameters, Parameters1}, {signature, Signature}]) of [ ok ] -> forward_message_to_local_service(URL, SvcName, @@ -609,8 +610,8 @@ dispatch_to_local_service([ $w, $s, $: | WSPidStr], services_unavailable, ok; dispatch_to_local_service([ $w, $s, $: | WSPidStr], message, - {struct, [{ service_name, SvcName}, - { parameters, { struct, Parameters} } + {struct, [{ service_name, SvcName }, + { parameters, Parameters } ]} ) -> ?info("service_edge:dispatch_to_local_service(message, websock): ~p", @@ -618,7 +619,7 @@ dispatch_to_local_service([ $w, $s, $: | WSPidStr], message, wse_server:send(list_to_pid(WSPidStr), json_rpc_notification("message", [{ "service_name", SvcName}, - {parameters, { struct, Parameters}}])), + {parameters, Parameters}])), %% No response expected. ?debug("service_edge:dispatch_to_local_service(message, websock): Done"), ok; @@ -661,7 +662,7 @@ forward_message_to_local_service(URL,SvcName, Parameters, _CompSpec) -> dispatch_to_local_service(URL, message, {struct, [ { service_name, LocalSvcName }, - { parameters, { struct, Parameters }}]})) + { parameters, Parameters }]})) end), [ ok, -1 ]. diff --git a/python/rvi_call_ws.py b/python/rvi_call_ws.py new file mode 100755 index 0000000..ab87d52 --- /dev/null +++ b/python/rvi_call_ws.py @@ -0,0 +1,39 @@ +import websocket +import time +import sys +import getopt +import json + +opts, args = getopt.getopt(sys.argv[1:], "n:") + +host = 'ws://localhost:8808' + +for o, a in opts: + if o == "-n": + host = a + else: + usage() +if len(args) < 1: + usage() + +i = 0 +service = args[0] +rvi_args = {} +for i in args[1:]: + print i + [k, v] = i.split('=') + rvi_args[k] = v + +ws = websocket.create_connection(host) + +print "RVI Node: ", host +print "Service: ", service +print "args: ", rvi_args + +payload = {} +payload['jsonrpc'] = "2.0" +payload['params'] = {'service_name':service, 'timeout':(int(time.time())+60), 'parameters':rvi_args} +payload['id'] = "1" +payload['method'] = 'message' + +ws.send(json.dumps(payload))
\ No newline at end of file diff --git a/python/rvi_service_ws.py b/python/rvi_service_ws.py new file mode 100644 index 0000000..9ddfbf3 --- /dev/null +++ b/python/rvi_service_ws.py @@ -0,0 +1,123 @@ +#!/usr/bin/python + +# +# Copyright (C) 2015, 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/ +# +# +# Register a service specified by command line with an RVI node. +# Print out a message when the service gets invoked. +# +import sys +from rvilib import RVI +# pip-install websocket-client +import websocket +import json +import getopt + +def usage(): + print "Usage:", sys.argv[0], "[-n <rvi_url>] <service_name>" + print " <rvi_url> URL of Service Edge on a local RVI node." + print " Default: ws://localhost:8808" + print " <service_name> URL of Service to register" + print + print "The Service Edge URL is logged as a notice when the" + print "RVI node is started." + print + + print "Example: ./rvi_service_ws.py -n ws://rvi1.nginfotpdx.net:8808 /test/some_service" + sys.exit(255) + + +# +# Our general handler, registered with rvi.register_service() below. +# +# You can also explicitly name the arguments, but then +# the sender has to match the argument names. + +# For example: +# rvi_call.py http://localhost:8801 jlr.com/vin/test a=1 b=2 c=3 -> +# def service(a,b,c) +# +def service_invoked(**args): + print + print "Service invoked!" + print "args:", args + print + sys.stdout.write("Press enter to quit: ") + sys.stdout.flush() + return ['ok'] + +def services_available(**args): + print + print "Services available!" + print "args:", args + print + sys.stdout.write("Press enter to quit: ") + sys.stdout.flush() + return ['ok'] + +def services_unavailable(**args): + print + print "Services unavailable!" + print "args:", args + print + sys.stdout.write("Press enter to quit: ") + sys.stdout.flush() + return ['ok'] + + +# +# Check that we have the correct arguments +# +opts, args= getopt.getopt(sys.argv[1:], "n:") + +rvi_node_url = "ws://localhost:8808" +for o, a in opts: + if o == "-n": + rvi_node_url = a + else: + usage() + +if len(args) != 1: + usage() + +service_name = args[0] + + +ws = websocket.create_connection(rvi_node_url) + +tid = 1 +payload = {} +payload['json-rpc'] = "2.0" +payload['id'] =tid +payload['method'] = "register_service" +payload['params'] = {"service_name": service_name} + +# Register service +ws.send(json.dumps(payload)) +reg_res = json.loads(ws.recv()) + +full_service_name = reg_res['service'] +print "RVI node URL: ", rvi_node_url +print "Service: ", full_service_name + +while True: + print + print 'Press Ctrl-\ to quit.' + inp = json.loads(ws.recv()) + print inp + params=inp['params'] + if inp['method'] == 'services_available': + print "Services available:", params['services'] + + if inp['method'] == 'services_unavailable': + print "Service unavailable:", params['services'] + + if inp['method'] == 'message': + print "Service invoked." + print "service: ", params['service_name'] + print "parameters: ", params['parameters'] |