diff options
| author | Magnus Feuer <mfeuer@jaguarlandrover.com> | 2015-03-04 19:14:16 -0800 |
|---|---|---|
| committer | Magnus Feuer <mfeuer@jaguarlandrover.com> | 2015-03-04 19:14:16 -0800 |
| commit | a59e0d6f1a4c51fc249309de9158395a2bed92c5 (patch) | |
| tree | 66b6edb65df0b24a388ec1c0225e9196ae808014 /python | |
| parent | 9f11329836f8710274102b6d57d1ebc65a3ec31b (diff) | |
| download | rvi_core-a59e0d6f1a4c51fc249309de9158395a2bed92c5.tar.gz | |
Added register/unregister. Added get_services.py to list all available services
Diffstat (limited to 'python')
| -rwxr-xr-x | python/rvi_get_services.py | 59 | ||||
| -rwxr-xr-x | python/rvi_service.py | 21 | ||||
| -rw-r--r-- | python/rvilib.py | 55 |
3 files changed, 130 insertions, 5 deletions
diff --git a/python/rvi_get_services.py b/python/rvi_get_services.py new file mode 100755 index 0000000..508480c --- /dev/null +++ b/python/rvi_get_services.py @@ -0,0 +1,59 @@ +#!/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/ +# +# +# Simple RVI service caller +# + +import sys +from rvilib import RVI +import threading +import time + +def usage(): + print "Return the name of all available services that can be reached" + print "through an RVI node." + print + print "Usage:", sys.argv[0], " RVI-node" + print + print "Example: ./callrvi.py http://rvi1.nginfotpdx.net:8801" + print + sys.exit(255) + + +# +# Check that we have the correct arguments +# +if len(sys.argv) <2: + usage() + +progname = sys.argv[0] +rvi_node = sys.argv[1] + + +# +# Setup an outbound JSON-RPC connection to the backend RVI node +# Service Edge. +# +rvi = RVI(rvi_node) + + +print "RVI Node: ", rvi_node + + +# +# Send the messge. +# +print rvi.get_available_services() + + + + + + diff --git a/python/rvi_service.py b/python/rvi_service.py index 36817ce..e1b705c 100755 --- a/python/rvi_service.py +++ b/python/rvi_service.py @@ -49,6 +49,23 @@ def service_invoked(**args): 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'] if len(sys.argv) != 3: @@ -65,6 +82,9 @@ rvi = RVI(rvi_node_url) rvi.start_serve_thread() +rvi.set_services_available_callback(services_available) +rvi.set_services_unavailable_callback(services_unavailable) + # Register our service and invoke 'service_invoked' if we # get an incoming JSON-RPC call to it from the RVI node # @@ -76,5 +96,6 @@ print "RVI node URL: ", rvi_node_url print "Service: ", full_service_name raw_input('Press enter to quit: ') +rvi.unregister_service(service_name) rvi.shutdown() sys.exit(0) diff --git a/python/rvilib.py b/python/rvilib.py index d40ec64..ba43bf9 100644 --- a/python/rvilib.py +++ b/python/rvilib.py @@ -30,6 +30,7 @@ class RVI(SimpleJSONRPCServer): SimpleJSONRPCServer.__init__(self,addr=((self.rvi_address, self.rvi_port)), logRequests=False) self.rvi_client = Server(rvi_node_url) self.serve_thread = False + self.registered_services = dict() # Set the callback to invoke when RVI reports that one or more new # services are available for invocation somewhere in the network. @@ -43,7 +44,13 @@ class RVI(SimpleJSONRPCServer): # Arguments will be an array of fully qualified service names no longer available. def set_services_unavailable_callback(self, function): self.register_function(function, 'services_unavailable') - + + def get_available_services(self): + # We need at least one dummy argument for the RPC to go + # through on the RVI side. + res = self.rvi_client.get_available_services(1) + return res['services'] + # Register a service in this python program that should # be reached from the rest of the network. # @@ -83,10 +90,39 @@ class RVI(SimpleJSONRPCServer): if self.serve_thread == False: self.start_serve_thread() + full_service_name = res['service'] + self.registered_services[service_name] = full_service_name # Return the fully qualified service name return res['service'] + # Unregister a service + # Unregisters a service previously registered with register_service + # The provided 'service_name' is identical to that provided tgo + # retister_service() + # + def unregister_service(self, service_name): + + # Check that the service has been previously registered + # If not just return + if service_name not in self.registered_services: + return False + + + # Retrieve the fully qualified service name that + # we need to unregister from rvi + full_service_name = self.registered_services[service_name] + + # Delete dictionary entry + del self.registered_services[service_name] + + # + # Unregister the service from RVI + # + res = self.rvi_client.unregister_service(service=full_service_name) + + return True + def start_serve_thread(self): self.serve_thread = threading.Thread(target=self.serve_forever) self.serve_thread.start() @@ -104,11 +140,20 @@ class RVI(SimpleJSONRPCServer): # service that registered 'service_name' in the network. # def message(self, service_name, parameters, timeout = int(time.time()) + 60 ): - self.rvi_client.message(calling_service= "not_used", - service_name = service_name, - timeout = timeout, - parameters = parameters) + self.rvi_client.message(service_name = service_name, + timeout = timeout, + parameters = parameters) + + # + # Redefined shutdown method that first unregisters all services. + # + def shutdown(self): + for service in self.registered_services: + self.unregister_service(service) + + SimpleJSONRPCServer.shutdown(self) + # # Check if method is 'message', if so dispatch on # name 'service_name' instead. |
