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']) 9PortSet = collections.namedtuple('PortSet', ['http', 'https', 'dns']) 10 11 12 13class ForwarderFactory(object): 14 15 def Create(self, port_pair): 16 """Creates a forwarder that maps remote (device) <-> local (host) ports. 17 18 Args: 19 port_pair: A PortPairs instance that consists of a PortPair mapping 20 for each protocol. http is required. https and dns may be None. 21 """ 22 raise NotImplementedError() 23 24 @property 25 def host_ip(self): 26 return '127.0.0.1' 27 28 29class Forwarder(object): 30 31 def __init__(self, port_pair): 32 assert port_pair, 'Port mapping is required.' 33 self._port_pair = port_pair 34 self._forwarding = True 35 36 @property 37 def host_port(self): 38 return self._port_pair.remote_port 39 40 @property 41 def host_ip(self): 42 return '127.0.0.1' 43 44 @property 45 def port_pair(self): 46 return self._port_pair 47 48 @property 49 def url(self): 50 assert self.host_ip and self.host_port 51 return 'http://%s:%i' % (self.host_ip, self.host_port) 52 53 def Close(self): 54 self._port_pair = None 55 self._forwarding = False 56