133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# Copyright 2012 the V8 project authors. All rights reserved.
233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# Redistribution and use in source and binary forms, with or without
333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# modification, are permitted provided that the following conditions are
433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# met:
533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#
633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#     * Redistributions of source code must retain the above copyright
733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#       notice, this list of conditions and the following disclaimer.
833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#     * Redistributions in binary form must reproduce the above
933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#       copyright notice, this list of conditions and the following
1033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#       disclaimer in the documentation and/or other materials provided
1133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#       with the distribution.
1233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#     * Neither the name of Google Inc. nor the names of its
1333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#       contributors may be used to endorse or promote products derived
1433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#       from this software without specific prior written permission.
1533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org#
1633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
2833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
2933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgimport socket
3033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgimport SocketServer
3133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgimport StringIO
3233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
3333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgfrom . import compression
3433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgfrom . import constants
3533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
3633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
3733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgdef LocalQuery(query):
3833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
3933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  code = sock.connect_ex(("localhost", constants.CLIENT_PORT))
4033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  if code != 0: return None
4133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  compression.Send(query, sock)
4233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  compression.Send(constants.END_OF_STREAM, sock)
4333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  rec = compression.Receiver(sock)
4433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  data = None
4533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  while not rec.IsDone():
4633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    data = rec.Current()
4733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    assert data[0] == query[0]
4833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    data = data[1]
4933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    rec.Advance()
5033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  sock.close()
5133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  return data
5233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
5333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
5433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgclass LocalHandler(SocketServer.BaseRequestHandler):
5533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  def handle(self):
5633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    rec = compression.Receiver(self.request)
5733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    while not rec.IsDone():
5833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      data = rec.Current()
5933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      action = data[0]
6033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
6133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      if action == constants.REQUEST_PEERS:
6233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        with self.server.daemon.peer_list_lock:
6333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org          response = [ p.Pack() for p in self.server.daemon.peers
6433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org                       if p.trusting_me ]
6533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        compression.Send([action, response], self.request)
6633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
6733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      elif action == constants.UNRESPONSIVE_PEER:
6833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        self.server.daemon.DeletePeer(data[1])
6933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
7033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      elif action == constants.REQUEST_PUBKEY_FINGERPRINT:
7133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        compression.Send([action, self.server.daemon.pubkey_fingerprint],
7233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org                         self.request)
7333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
7433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      elif action == constants.REQUEST_STATUS:
7533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        compression.Send([action, self._GetStatusMessage()], self.request)
7633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
7733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      elif action == constants.ADD_TRUSTED:
7833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        fingerprint = self.server.daemon.CopyToTrusted(data[1])
7933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        compression.Send([action, fingerprint], self.request)
8033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
8133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      elif action == constants.INFORM_DURATION:
8233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        test_key = data[1]
8333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        test_duration = data[2]
8433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        arch = data[3]
8533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        mode = data[4]
8633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        self.server.daemon.AddPerfData(test_key, test_duration, arch, mode)
8733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
8833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      elif action == constants.UPDATE_PERF:
8933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        address = data[1]
9033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        perf = data[2]
9133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        self.server.daemon.UpdatePeerPerformance(data[1], data[2])
9233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
9333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      rec.Advance()
9433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    compression.Send(constants.END_OF_STREAM, self.request)
9533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
9633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  def _GetStatusMessage(self):
9733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    sio = StringIO.StringIO()
9833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    sio.write("Peers:\n")
9933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    with self.server.daemon.peer_list_lock:
10033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      for p in self.server.daemon.peers:
10133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        sio.write("%s\n" % p)
10233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    sio.write("My own jobs: %d, relative performance: %.2f\n" %
10333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org              (self.server.daemon.jobs, self.server.daemon.relative_perf))
10433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    # Low-priority TODO: Return more information. Ideas:
10533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    #   - currently running anything,
10633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    #   - time since last job,
10733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    #   - time since last repository fetch
10833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    #   - number of workpackets/testcases handled since startup
10933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    #   - slowest test(s)
11033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    result = sio.getvalue()
11133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    sio.close()
11233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    return result
11333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
11433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
11533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgclass LocalSocketServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
11633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  def __init__(self, daemon):
11733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    SocketServer.TCPServer.__init__(self, ("localhost", constants.CLIENT_PORT),
11833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org                                    LocalHandler)
11933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.daemon = daemon
120