Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / core / forwarders / __init__.py
1 # Copyright 2014 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
5 import collections
6
7
8 PortPair = collections.namedtuple('PortPair', ['local_port', 'remote_port'])
9 PortPairs = collections.namedtuple('PortPairs', ['http', 'https', 'dns'])
10
11
12 class ForwarderFactory(object):
13
14   def Create(self, port_pairs):
15     """Creates a forwarder that maps remote (device) <-> local (host) ports.
16
17     Args:
18       port_pairs: A PortPairs instance that consists of a PortPair mapping
19           for each protocol. http is required. https and dns may be None.
20     """
21     raise NotImplementedError()
22
23   @property
24   def host_ip(self):
25     return '127.0.0.1'
26
27   @property
28   def does_forwarder_override_dns(self):
29     return False
30
31
32 class Forwarder(object):
33
34   def __init__(self, port_pairs):
35     assert port_pairs.http, 'HTTP port mapping is required.'
36     self._port_pairs = PortPairs(*[
37         PortPair(p.local_port, p.remote_port or p.local_port)
38         if p else None for p in port_pairs])
39
40   @property
41   def host_port(self):
42     return self._port_pairs.http.remote_port
43
44   @property
45   def host_ip(self):
46     return '127.0.0.1'
47
48   @property
49   def port_pairs(self):
50     return self._port_pairs
51
52   @property
53   def url(self):
54     assert self.host_ip and self.host_port
55     return 'http://%s:%i' % (self.host_ip, self.host_port)
56
57   def Close(self):
58     self._port_pairs = None