diff options
author | Magnus <mfeuer@jaguarlandrover.com> | 2014-08-19 16:25:31 -0700 |
---|---|---|
committer | Magnus <mfeuer@jaguarlandrover.com> | 2014-08-19 16:25:31 -0700 |
commit | 47f8f849e619675cdc0904cc11bc94a6468b4f48 (patch) | |
tree | cbaa2b820313bacf4ead539a92e52dbe56d73d4f | |
parent | 7474acf395c77bfe58b329f68f2c03d9478f6f77 (diff) | |
download | rvi_core-47f8f849e619675cdc0904cc11bc94a6468b4f48.tar.gz |
Removed subscription service, making the demo a true p2p setup.
Signed-off-by: Magnus <mfeuer@jaguarlandrover.com>
-rwxr-xr-x | hvac_demo/hvac_emulator.py | 157 | ||||
-rwxr-xr-x | hvac_demo/mobile_emulator.py | 89 | ||||
-rwxr-xr-x | hvac_demo/subscription_service.py | 173 |
3 files changed, 147 insertions, 272 deletions
diff --git a/hvac_demo/hvac_emulator.py b/hvac_demo/hvac_emulator.py index e368fb2..6f48ef0 100755 --- a/hvac_demo/hvac_emulator.py +++ b/hvac_demo/hvac_emulator.py @@ -57,23 +57,26 @@ import jsonrpclib import random import threading -# The subscriber service (MQTT server equivalent), that -# manages subscriptions and distributes received publish commands to -# the relevant subscribers. -# -SUBSCRIPTION_SERVICE_BASE='jlr.com/backend/subscription_service' -SUBSCRIBE_SERVICE=SUBSCRIPTION_SERVICE_BASE+'/subscribe' -PUBLISH_SERVICE=SUBSCRIPTION_SERVICE_BASE+'/publish' +def usage(): + print "Usage:", sys.argv[0], "<rvi_url>" + print " <rvi_url> URL of Service Edge on a local RVI node" + print + print "The RVI Service Edge URL can be found in" + print "[backend,vehicle].config as" + print "env -> rvi -> components -> service_edge -> url" + print + print "The Service Edge URL is also logged as a notice when the" + print "RVI node is started." + sys.exit(255) + # -# Publish command is invoked by the -# subscriber server above when it receives a publish command -# from a VIN that this emulator subscribes to. +# Publish command is invoked by the mobile emulator when it sends +# messages to jlr.com/vin/1234/hvac/publish service. # -def publish(vin, key, value): +def publish(key, value): print print "Publish invoked!" - print "vin:", vin print "key:", key print "value:", value print @@ -81,20 +84,47 @@ def publish(vin, key, value): sys.stdout.flush() return ['ok'] -def usage(): - print "Usage:", sys.argv[0], "<rvi_url>" - print " <rvi_url> URL of Service Edge on a local RVI node" - print - print "The RVI Service Edge URL can be found in" - print "[backend,vehicle].config as" - print "env -> rvi -> components -> service_edge -> url" +# +# Subscribe command is invoked by the mobile emulator when it sends +# messages to the jlr.com/vin/1234/hvac/subscribe service. +# The single argument provided is the subscribing service to be invoked when +# an value is updated on the command line of the HVAC emulator. +# +def subscribe(subscribing_service): print - print "The Service Edge URL is also logged as a notice when the" - print "RVI node is started." - sys.exit(255) + print "Got subscription" + print "vin:", vin + print "subscribing_service:", subscribing_service + print + # Add the subscribing service to list of subscribers, + # given that is is not already there. + if not subscribing_service in subscribers: + subscribers.append(subscribing_service) + else: + print subscribing_service," already subscribing" + sys.stdout.write("Enter <key> <val> or q to quit: ") + sys.stdout.flush() + return ['ok'] -# Setup self's server that we will receive incoming calls from service edge on. +# +# Publish an updated HVAC value, entered at the command line of the +# HVAC emulator, to all services who have set themselves up as +# subscribers through the jlr.com/vin/1234/hvac/subscribe service. +# +def publish_to_subscribers(vin, key, val): + for subscriber in subscribers: + print "Sending:",vin," key:", key," val:",val," to:", subscriber + rvi_server.message(calling_service = emulator_publish_service_name, + target = subscriber, + timeout = 0, + parameters = [{u'vin': vin, u'key': key, u'value': val}]) + +# +# A list of service names that should be notified when a value is +# updated on the HVAC emulator's command line. +# +subscribers = [] # # Setup a localhost URL, using a random port, that we will listen to @@ -115,51 +145,65 @@ if len(sys.argv) != 2: [ progname, rvi_url ] = sys.argv -# setup the service name we will register with -# The complete service name will be: jlr.com/vin/<vin>/hvac/publish -emulator_service_name = '/hvac/publish' +# setup the service names we will register with +# The complete service name will be: +# jlr.com/vin/1234/hvac/publish +# - and - +# jlr.com/vin/1234/hvac/subscribe +# +# Replace 1234 with the VIN number setup in the +# node_service_prefix entry in vehicle.config +emulator_publish_service_name = '/hvac/publish' +emulator_subscribe_service_name = '/hvac/subscribe' # Setup an outbound JSON-RPC connection to the RVI Service Edeg. rvi_server = jsonrpclib.Server(rvi_url) -# Register our HVAC emulator service with the RVI Service Edge, -# allowing the RVI to forward requests to the service name to the -# given network addresss (URL): -res = rvi_server.register_service(service = emulator_service_name, +# +# Register our HVAC emulator service with the vehicle RVI node's Service Edge. +# We register both services using our own URL as a callback. +# +res = rvi_server.register_service(service = emulator_publish_service_name, + + network_address = emulator_service_url) + +# +# Record the returned full service name so that we can print it out +# below. +# +full_emulator_publish_service_name = res['service'] + +res = rvi_server.register_service(service = emulator_subscribe_service_name, network_address = emulator_service_url) -# The returned full service name contains the VIN number that we want: -# jlr.com/vin/<vin>/hvac/publish -# We need to dig out the <vin> bit -full_emulator_service_name = res['service'] +# +# Record the returned full service name so that we can print it out +# below. +# +full_emulator_subscribe_service_name = res['service'] -[ t1, t2, vin, t3, t4] = full_emulator_service_name.split('/') +[ t1, t2, vin, t3, t4] = full_emulator_subscribe_service_name.split('/') # We are in mobile device mode. Setup the vin numbers for publish pub_vin = "hvac_"+vin sub_vin = "mobile_"+vin print "HVAC Emulator." -print "Vehicle RVI node URL: ", rvi_url -print "Emulator URL: ", emulator_service_url -print "VIN: ", vin -print "Full service name ", full_emulator_service_name - -# Regsiter self's service with the backend server RVI node -# See rvi_json_rpc_server.py._dispatch() for details on how -# incoming JSON-RPC requests are mapped to local funcitons. -# -emulator_service = RVIJSONRPCServer(addr=((emulator_service_host, emulator_service_port)), logRequests=False) -emulator_service.register_function(publish, emulator_service_name) +print "Vehicle RVI node URL: ", rvi_url +print "Emulator URL: ", emulator_service_url +print "VIN: ", vin +print "Full publish service name ", full_emulator_publish_service_name +print "Full subscribe service name ", full_emulator_subscribe_service_name -# Send of a subscribe to the subscription service running on the -# backend. See hvac_subscription_service.py.subscribe() for details. +emulator_service = RVIJSONRPCServer(addr=((emulator_service_host, emulator_service_port)), + logRequests=False) -rvi_server.message(calling_service = emulator_service_name, - target = SUBSCRIBE_SERVICE, - timeout = 0, - parameters = [{ u'vin': sub_vin, - u'subscribing_service': full_emulator_service_name}]) +# +# Regsiter callbacks for incoming JSON-RPC calls delivered to +# the HVAC emulator from the vehicle RVI node's Service Edge. +# +emulator_service.register_function(publish, emulator_publish_service_name) +emulator_service.register_function(subscribe, emulator_subscribe_service_name) # Create a thread to handle incoming stuff so that we can do input # in order to get new values @@ -181,11 +225,6 @@ while True: [k, v] = line.split(' ') # Send out update to the subscriber - rvi_server.message(calling_service= emulator_service_name, - target = PUBLISH_SERVICE, - timeout = 0, - parameters = [{ u'vin': pub_vin, - u'key': k, - u'value': v}]) + publish_to_subscribers(vin, k, v) print('Key {} set to {} for vin{}'. format(k, v, vin)) diff --git a/hvac_demo/mobile_emulator.py b/hvac_demo/mobile_emulator.py index 60d9cbf..eabb4fc 100755 --- a/hvac_demo/mobile_emulator.py +++ b/hvac_demo/mobile_emulator.py @@ -69,14 +69,6 @@ import jsonrpclib import random import threading -# The subscriber service (MQTT server equivalent), that -# manages subscriptions and distributes received publish commands to -# the relevant subscribers. -# -SUBSCRIPTION_SERVICE_BASE='jlr.com/backend/subscription_service' -SUBSCRIBE_SERVICE=SUBSCRIPTION_SERVICE_BASE+'/subscribe' -PUBLISH_SERVICE=SUBSCRIPTION_SERVICE_BASE+'/publish' - # # Publish command is invoked by the # subscriber server above when it receives a publish command @@ -108,6 +100,14 @@ def usage(): sys.exit(255) +# +# Check that we have the correct arguments +# +if len(sys.argv) != 4: + usage() + +[ progname, rvi_url, phone_number, vin ] = sys.argv + # Setup self's server that we will receive incoming calls from service edge on. # @@ -119,36 +119,54 @@ emulator_service_host = 'localhost' emulator_service_port = random.randint(20001, 59999) emulator_service_url = 'http://'+emulator_service_host + ':' + str(emulator_service_port) -# -# Check that we have the correct arguments # -if len(sys.argv) != 4: - usage() - -[ progname, rvi_url, phone_number, vin ] = sys.argv - -# We are in mobile device mode. Setup the vin numbers for publish -pub_vin = "mobile_"+vin -sub_vin = "hvac_"+vin +# Setup the target service, the vehicle hvac app emulator, +# that will receive 'publish' and 'subscribe' commands from us. +# +target_hvac_publish_service = 'jlr.com/vin/'+vin+'/hvac/publish' +target_hvac_subscribe_service = 'jlr.com/vin/'+vin+'/hvac/subscribe' +# # Setup the service name we will register with. -# The complete service name will be: jlr.com/backend/mobile/<phone_nr>/hvac/publish +# The complete service name will be: +# jlr.com/backend/mobile/<phone_nr>/hvac/publish +# emulator_service_name = '/mobile/'+phone_number+'/hvac/publish' - -# Setup an outbound JSON-RPC connection to the RVI Service Edeg. +# +# Setup an outbound JSON-RPC connection to the backend RVI node +# Service Edge. +# rvi_server = jsonrpclib.Server(rvi_url) -# Register our HVAC mobile emulator service with the RVI Service Edge, -# allowing the RVI to forward requests to the service name to the -# given network addresss (URL): +# +# Register our HVAC mobile emulator service with backend RVI node +# Service Edge. +# print "Emulator service URL", emulator_service_url res = rvi_server.register_service(service = emulator_service_name, network_address = emulator_service_url) +# Service Edge will return the full, global service name that the +# registration was recorded as: +# +# jlr.com/backend/mobile/[phone_number]/hvac/publish +# full_emulator_service_name = res['service'] +# +# Send of a subscribe to the hvac emulator running on the +# vehicle. +# +# We provide our own full service name to be invoked when +# a value is updated on the command line of the hvac emulator. +# +rvi_server.message(calling_service = emulator_service_name, + target = target_hvac_subscribe_service, + timeout = 0, # Not yet implemented + parameters = [ { u'subscribing_service': full_emulator_service_name}]) + print "Mobile Device Emulator." print "Backend RVI node URL: ", rvi_url print "Emulator URL: ", emulator_service_url @@ -156,23 +174,15 @@ print "Phone Number: ", phone_number print "VIN: ", vin print "Full Service Name: ", full_emulator_service_name +emulator_service = RVIJSONRPCServer(addr=((emulator_service_host, emulator_service_port)), + logRequests=False) -# Regsiter self's service with the backend server RVI node -# See rvi_json_rpc_server.py._dispatch() for details on how -# ncoming JSON-RPC requests are mapped to local funcitons. +# Register the publish function with the publish service +# so that publish() gets called when we receive a message +# to /mobile/[phone_nr]/hvac/publish # -emulator_service = RVIJSONRPCServer(addr=((emulator_service_host, emulator_service_port)), logRequests = False) emulator_service.register_function(publish, emulator_service_name) -# Send of a subscribe to the subscription service running on the -# backend. See hvac_subscription_service.py.subscribe() for details. - -rvi_server.message(calling_service = emulator_service_name, - target = SUBSCRIBE_SERVICE, - timeout = 0, - parameters = [{ u'vin': sub_vin, - u'subscribing_service': full_emulator_service_name}]) - # Create a thread to handle incoming stuff so that we can do input # in order to get new values thr = threading.Thread(target=emulator_service.serve_forever) @@ -194,10 +204,9 @@ while True: # Send out update to the subscriber rvi_server.message(calling_service= emulator_service_name, - target = PUBLISH_SERVICE, + target = target_hvac_publish_service, timeout = 0, - parameters = [{ u'vin': pub_vin, - u'key': k, + parameters = [{ u'key': k, u'value': v}]) print('Key {} set to {} for vin{}'. format(k, v, vin)) diff --git a/hvac_demo/subscription_service.py b/hvac_demo/subscription_service.py deleted file mode 100755 index 44f1644..0000000 --- a/hvac_demo/subscription_service.py +++ /dev/null @@ -1,173 +0,0 @@ -#!/usr/bin/python -# -# 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/ -# - -# -# This is an extremely simple subscription service that connects to -# a central backend RVI node. The node is well known by all -# other RVI nodes in a network, who has the backend node configured -# as a "static" node. See vehicle.config for an example. -# -# The subscription service will register the following services -# with the backend RVI node: -# -# /subscription_service/subscribe -# (Full name: jlr.com/backend/subscription_service/subscribe) -# Parameters: -# vin - The VIN number for which updates are subscribed to. -# subscribing_service - The service to send a message to when -# the given vin is updated. -# -# Adds the given service to the list of subscribers to notify -# -# When a message is recieved by the /subscribing_service/publish -# service, all subscribers who have specified a vin -# matching that provided with the publish message will have -# the message forwarded to them. -# -# /subscription_service/unsubscribe -# (Full name: jlr.com/backend/subscription_service/unsubscribe) -# Parameters: -# vin - The VIN number from which the service unsubscribes. -# subscribing_service - The service that unsubscribes., -# -# Removes a service, previously subscribing to the service through -# a subscribe command, from the given vin. Future updates -# to the vin will not be forwarded to the given service. -# -# /subscription_service/publish -# (Full name: jlr.com/backend/subscription_service/publish) -# Parameters: -# vin - The VIN number from which the service unsubscribes. -# key - The key that has an updated value -# value - The new value assigned to key -# -# Distributes the key/value pair to all services that have previosuly -# subscribed to updates for the given vin. -# -# For RVI Milestone 1, the mobile and Tizen IVI HVAC UI remains to be -# integrated with the RVI system itself. Meanwhile, hvac_emulator.py -# is provded as a simple tester that can emulate either an IVI or -# a mobile device. -# - -from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer -import jsonrpclib -from rvi_json_rpc_server import RVIJSONRPCServer -import sys -RVI_SERVICE_EDGE='http://localhost:8801' -HVAC_SERVER=('localhost', 8901) - -# vin_subs is a simple dictionary with vin numbers as keys and arrays -# of services as values. -# During a subscribe operation, the provided service is added -# as a list element in value, where the list hangs under the key -# with value 'vin' -# -# Thus: -# { '1234': [ 'jlr.com/vin/5555/hvac_update_ui', 'jlr.com/vin/4711/hvac_update_ui'] -vin_subs = {} - -def subscribe(vin, subscribing_service): - print - print "Got subscription" - print "vin:", vin - print "subscribing_service:", subscribing_service - print - # Delete any existing service with the same name - # Add the subscribing service - if not vin in vin_subs: - vin_subs[vin] = [subscribing_service] - else: - if vin_subs[vin].count(subscribing_service) == 0: - vin_subs[vin].append(subscribing_service) - - return ['ok'] - - -def unsubscribe(vin, subscribing_service): - print - print "Got unsubscribe" - print "vin:", vin - print "subscribing_service:", subscribing_service - print - # Delete any existing service with the same name - - if vin in vin_subs and vin_subs[vin].count(subscribing_service) > 0: - vin_subs[vin].remove(subscribing_service) - - return ['ok'] - -def publish(vin, key, value): - print - print "Got publish" - print "Publish" - print "vin:", vin - print "key:", key - print "value:", value - - # Distribute - try: - subs = vin_subs[vin] - except Err: - print "No subscribers for vin:", vin - return ['ok'] - - for sub in subs: - print "Sending publish to", sub - rvi_server.message(calling_service = '/hvac/publish', - target = sub, - timeout = 0, - parameters = [{ u'vin': vin, u'key': key}, {u'value': value }]) - print - -if len(sys.argv) == 2: - [ progname, rvi_url ] = sys.argv -else: - print "Usage:", sys.argv[0], "<rvi_url>" - print " <rvi_url> URL of RVI Service Edge on local host" - print - print "The RVI Service Edge URL can be found in" - print "[backend,backend].config as" - print "env -> rvi -> components -> service_edge -> url" - print - print "The Service Edge URL is also logged as a notice when the" - print "RVI node is started." - sys.exit(255) - -print "Will register with RVI at", rvi_url, "using", HVAC_SERVER, "as my own URL." - -# Setup self's server that we will receive incoming calls from service edge on. -hvac_server = RVIJSONRPCServer(HVAC_SERVER, logRequests=False) - -hvac_server.register_function(subscribe, '/subscription_service/subscribe') -hvac_server.register_function(unsubscribe, '/subscription_service/unsubscribe') -hvac_server.register_function(publish, '/subscription_service/publish') - - -# Register our services with the service edge and have -# it associate our service name with the URL that the hvac_server is listening on -rvi_server = jsonrpclib.Server(rvi_url) - -res = rvi_server.register_service(service = '/subscription_service/subscribe', - network_address = 'http://localhost:8901') - -print "Registered service", res['service'] - -res = rvi_server.register_service(service = '/subscription_service/unsubscribe', - network_address = 'http://localhost:8901') - -print "Registered service", res['service'] - -res = rvi_server.register_service(service = '/subscription_service/publish', - network_address = 'http://localhost:8901') - -print "Registered service", res['service'] - - -hvac_server.serve_forever() |