def main(server): parser = argparse.ArgumentParser() bosdyn.client.util.add_common_arguments(parser) parser.add_argument('--my-host', help='Address to register into the directory with') parser.add_argument( '--directory-host', help= 'Host running the directory service. Omit to skip directory registration' ) parser.add_argument( '--port', default=0, type=int, help='Listening port for server. Omit to choose a port at random') parser.add_argument('--servicer', default='HelloWorld', help='Type of servicer to launch.') options = parser.parse_args() sdk = bosdyn.client.create_standard_sdk('run-mission-service') sdk.load_app_token(options.app_token) level = logging.DEBUG if options.verbose else logging.INFO logging.basicConfig(level=level, format='%(message)s (%(filename)s:%(lineno)d)') logger = logging.getLogger() # Map options.servicer to a function that will build the appropriate instance. string_to_servicer = { 'HelloWorld': lambda: HelloWorldServicer(logger), 'PowerOff': lambda: PowerOffServicer(logger, sdk, options.hostname, options. username, options.password) } # Build whatever service the user specified. servicer = string_to_servicer[options.servicer]() # Start the server, talking over an insecure channel. remote_service_pb2_grpc.add_RemoteMissionServiceServicer_to_server( servicer, server) port = server.add_insecure_port('[::]:{}'.format(options.port)) server.start() logger.info('Starting server on port %i', port) dir_keepalive = None # If a directory host was specified, register with the directory there. # This will often be the robot. For example, if this program is running on a payload computer # like the Spot CORE, directory_host would be 192.168.50.3. # Registering with the directory will be important when running with a robot, but it's useful # to skip directory registration when you're just testing your service locally. if options.directory_host: # Register with the directory. robot = sdk.create_robot(options.directory_host) robot.authenticate(options.username, options.password) dir_reg_client = robot.ensure_client( DirectoryRegistrationClient.default_service_name) dir_keepalive = DirectoryRegistrationKeepAlive(dir_reg_client, logger=logger) dir_keepalive.start(SERVICE_NAME_IN_DIRECTORY, SERVICE_TYPE, AUTHORITY, options.my_host, port) try: # Nothing for this thread to do. The server / servicer pair handles all incoming # requests. while not _STOP_RUNNING.wait(1): pass except KeyboardInterrupt: logger.info('Cancelled by keyboard interrupt') if dir_keepalive: dir_keepalive.shutdown() dir_keepalive.unregister() logger.info('Stopping server.') return 0
def main(): """Main remote mission service function""" parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter) bosdyn.client.util.add_common_arguments(parser) parser.add_argument('--my-host', help='Address to register into the directory with') parser.add_argument( '--directory-host', help= 'Host running the directory service. Omit to skip directory registration' ) parser.add_argument( '--port', default=0, type=int, help='Listening port for server. Omit to choose a port at random') parser.add_argument('--theta-client', dest='theta_client', action='store_true', help='Use theta client or direct mode ip settings') parser.add_argument('--payload-token', dest='payload_token', action='store_true', help='Use limited-access user token') options = parser.parse_args() level = logging.DEBUG if options.verbose else logging.INFO logging.basicConfig(level=level, format='%(message)s (%(filename)s:%(lineno)d)') logger = logging.getLogger() sdk = bosdyn.client.create_standard_sdk('run-ricoh-service') guid = None secret = None if options.payload_token: guid, secret = get_guid_and_secret() server = grpc.server(futures.ThreadPoolExecutor()) service = RicohThetaServicer(logger, options.theta_client) remote_service_pb2_grpc.add_RemoteMissionServiceServicer_to_server( service, server) port = server.add_insecure_port('[::]:{}'.format(options.port)) server.start() logger.info('Starting server on port %i', port) dir_keepalive = None if options.directory_host: robot = sdk.create_robot(options.directory_host) # Check authentication method if guid: payload_registration_client = robot.ensure_client( PayloadRegistrationClient.default_service_name) limited_token = payload_registration_client.get_payload_auth_token( guid, secret) robot.update_user_token(limited_token) else: robot.authenticate(options.username, options.password) # Register with the directory dir_reg_client = robot.ensure_client( DirectoryRegistrationClient.default_service_name) dir_keepalive = DirectoryRegistrationKeepAlive(dir_reg_client, logger=logger) dir_keepalive.start(DIRECTORY_NAME, SERVICE_TYPE, AUTHORITY, options.my_host, port) try: while True: # Nothing for this thread to do. time.sleep(100) except KeyboardInterrupt: logger.info('Cancelled by keyboard interrupt') if dir_keepalive: dir_keepalive.shutdown() dir_keepalive.unregister() logger.info('Stopping server.') # Stop with no grace period. shutdown_complete = server.stop(None) # Wait up to 1 second for a clean shutdown. shutdown_complete.wait(1) return 0