1# Copyright 2016 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5"""Tester feedback backend service implementation.""" 6 7import SimpleXMLRPCServer 8import multiprocessing 9 10import feedback_delegate 11import request_multiplexer 12 13# TODO(garnold) Load query delegate implementations as they become available 14# (b/26769927). 15# pylint: disable=unused-import 16import audio_query_delegate_impl 17 18 19class FeedbackService(object): 20 """The feedback service main object.""" 21 22 def __init__(self): 23 self._multiplexer = None 24 self._server_port = 0 25 self._server_process = None 26 self._running = False 27 28 29 @property 30 def server_port(self): 31 """Returns the service listening port.""" 32 return self._server_port 33 34 35 def start(self): 36 """Starts the feedback service.""" 37 if self._running: 38 return 39 40 # Start the feedback request multiplexer. 41 self._multiplexer = request_multiplexer.FeedbackRequestMultiplexer() 42 self._multiplexer.start() 43 44 # Start the feedback delegate RPC server. 45 rpc_server = SimpleXMLRPCServer.SimpleXMLRPCServer(('localhost', 0)) 46 rpc_server.register_instance( 47 feedback_delegate.FeedbackDelegate(self._multiplexer)) 48 self._server_port = rpc_server.server_address[1] 49 self._server_process = multiprocessing.Process( 50 target=rpc_server.serve_forever) 51 self._server_process.start() 52 53 self._running = True 54 55 56 def stop(self): 57 """Stops the feedback service.""" 58 if not self._running: 59 return 60 61 # Stop the RPC server. 62 self._server_process.terminate() 63 self._server_process.join() 64 self._server_port = 0 65 66 # Stop the multiplexer. 67 self._multiplexer.stop() 68 69 self._running = False 70