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
5import collections
6
7
8PortPair = collections.namedtuple('PortPair', ['local_port', 'remote_port'])
9PortPairs = collections.namedtuple('PortPairs', ['http', 'https', 'dns'])
10
11
12class 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
32class Forwarder(object):
33
34  def __init__(self, port_pairs):
35    assert port_pairs.http, 'HTTP port mapping is required.'
36    self._port_pairs = port_pairs
37
38  @property
39  def host_port(self):
40    return self._port_pairs.http.remote_port
41
42  @property
43  def host_ip(self):
44    return '127.0.0.1'
45
46  @property
47  def url(self):
48    assert self.host_ip and self.host_port
49    return 'http://%s:%i' % (self.host_ip, self.host_port)
50
51  def Close(self):
52    self._port_pairs = None
53