Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / core / forwarders / cros_forwarder.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 logging
6 import subprocess
7
8 from telemetry.core import forwarders
9 from telemetry.core import util
10 from telemetry.core.forwarders import do_nothing_forwarder
11
12
13 class CrOsForwarderFactory(forwarders.ForwarderFactory):
14
15   def __init__(self, cri):
16     super(CrOsForwarderFactory, self).__init__()
17     self._cri = cri
18
19   def Create(self, port_pairs, forwarding_flag='R'):  # pylint: disable=W0221
20     if self._cri.local:
21       return do_nothing_forwarder.DoNothingForwarder(port_pairs)
22     return CrOsSshForwarder(self._cri, forwarding_flag, port_pairs)
23
24
25 class CrOsSshForwarder(forwarders.Forwarder):
26
27   def __init__(self, cri, forwarding_flag, port_pairs):
28     super(CrOsSshForwarder, self).__init__(port_pairs)
29     self._cri = cri
30     self._proc = None
31     self._forwarding_flag = forwarding_flag
32
33     if self._forwarding_flag == 'R':
34       command_line = ['-%s%i:%s:%i' % (self._forwarding_flag,
35                                        port_pair.remote_port,
36                                        self.host_ip,
37                                        port_pair.local_port)
38                       for port_pair in port_pairs if port_pair]
39     else:
40       command_line = ['-%s%i:%s:%i' % (self._forwarding_flag,
41                                        port_pair.local_port,
42                                        self.host_ip,
43                                        port_pair.remote_port)
44                       for port_pair in port_pairs if port_pair]
45     self._proc = subprocess.Popen(
46         self._cri.FormSSHCommandLine(['sleep', '999999999'], command_line),
47         stdout=subprocess.PIPE,
48         stderr=subprocess.PIPE,
49         stdin=subprocess.PIPE,
50         shell=False)
51     util.WaitFor(
52         lambda: self._cri.IsHTTPServerRunningOnPort(self.host_port), 60)
53     logging.debug('Server started on %s:%d' % (self.host_ip, self.host_port))
54
55   @property
56   def host_port(self):
57     return self._port_pairs.http.remote_port
58
59   def Close(self):
60     if self._proc:
61       self._proc.kill()
62       self._proc = None
63     super(CrOsSshForwarder, self).Close()