OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2015 The Chromium 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 """The discovery server used to register clients. | |
5 | |
6 The discovery server is started by the host controller and allows the clients | |
7 to register themselves when they start. Authentication of the client controllers | |
8 is based on an OTP passed to the client controller binary on startup. | |
9 """ | |
10 | |
11 import logging | |
12 import threading | |
13 import xmlrpclib | |
14 import SimpleXMLRPCServer | |
15 | |
16 SERVER_ADDRESS = '' | |
17 SERVER_PORT = 31710 | |
18 | |
19 | |
20 class DiscoveryServer(object): | |
21 """Discovery server run on the host.""" | |
22 | |
23 def __init__(self): | |
24 self._expected_clients = {} | |
25 self._rpc_server = None | |
26 self._thread = None | |
27 | |
28 def _RegisterClientRPC(self, otp, ip): | |
29 """The RPC used by a client to register with the discovery server. | |
30 | |
31 Args: | |
32 otp: The one time token issued by the host. | |
33 ip: The ip address of the client. | |
34 | |
35 Raises: | |
36 KeyError if the OTP isn't found in _expected_clients | |
37 """ | |
38 if otp not in self._expected_clients: | |
39 raise KeyError('OTP not found') | |
40 cb = self._expected_clients[otp] | |
Marc-Antoine Ruel (Google)
2015/01/30 21:58:40
You mean:
cb = self._expected_clients.pop(otp)
cb
Mike Meade
2015/02/03 01:18:09
Done.
| |
41 del self._expected_clients[otp] | |
42 cb(ip) | |
43 | |
44 def RegisterClientCallback(self, otp, callback): | |
45 """Registers a callback associated with an OTP. | |
46 | |
47 Args: | |
48 otp: A one time token used by the client to authenticate. | |
49 callback: The callback used when the client connects. | |
50 | |
51 Raises: | |
52 TypeError if the callback is not callable. | |
53 """ | |
54 if not callable(callback): | |
Marc-Antoine Ruel (Google)
2015/01/30 21:58:40
use an assert, it's not something normal.
Mike Meade
2015/02/03 01:18:09
Done.
| |
55 raise TypeError('callback is not callable') | |
56 self._expected_clients[otp] = callback | |
57 | |
58 def Start(self, address=None, port=None): | |
Marc-Antoine Ruel (Google)
2015/01/30 21:58:40
*args, **kwargs again
Mike Meade
2015/02/03 01:18:09
Changed this around. I hard coded the address and
| |
59 """Starts the discovery server. | |
60 | |
61 Args: | |
62 address: The address to run the server on ('', 'localhost', etc). | |
63 port: The port to run the server on. | |
64 """ | |
65 address = address or SERVER_ADDRESS | |
Marc-Antoine Ruel (Google)
2015/01/30 21:58:40
I don't like these to be sprinkled at multiple pla
Mike Meade
2015/02/03 01:18:09
Moved all references to address and port to common
| |
66 port = port = SERVER_PORT | |
67 logging.debug('Starting discovery server') | |
68 self._rpc_server = SimpleXMLRPCServer.SimpleXMLRPCServer( | |
69 (address, port), allow_none=True, logRequests=False) | |
70 self._rpc_server.register_function( | |
71 self._RegisterClientRPC, 'RegisterClient') | |
72 self._thread = threading.Thread(target=self._rpc_server.serve_forever) | |
73 self._thread.start() | |
74 | |
75 def Shutdown(self): | |
76 """Shuts the discovery server down.""" | |
77 if self._thread and self._thread.is_alive(): | |
78 logging.debug('Shutting down discovery server') | |
79 self._rpc_server.shutdown() | |
80 | |
81 @staticmethod | |
82 def Connect(host): | |
Marc-Antoine Ruel (Google)
2015/01/30 21:58:40
What's the relationship with the class? Why is it
Mike Meade
2015/02/03 01:18:09
Moved this and the other "Connect" method to commo
| |
83 """Create a connection to the discovery server.""" | |
84 server = 'http://%s:%s' % (host, SERVER_PORT) | |
85 logging.info('Connecting to a discovery server at %s', server) | |
86 return xmlrpclib.Server(server) | |
OLD | NEW |