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 threading
3233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgtry:
3333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  import ujson as json
3433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgexcept:
3533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  import json
3633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
3733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgfrom . import constants
3833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgfrom ..objects import peer
3933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
4033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
4133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgSTARTUP_REQUEST = "V8 test peer starting up"
4233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgSTARTUP_RESPONSE = "Let's rock some tests!"
4333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgEXIT_REQUEST = "V8 testing peer going down"
4433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
4533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
4633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgdef GetOwnIP():
4733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
4833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  s.connect(("8.8.8.8", 80))
4933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  ip = s.getsockname()[0]
5033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  s.close()
5133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  return ip
5233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
5333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
5433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgclass PresenceHandler(SocketServer.BaseRequestHandler):
5533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
5633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  def handle(self):
5733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    data = json.loads(self.request[0].strip())
5833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
5933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    if data[0] == STARTUP_REQUEST:
6033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      jobs = data[1]
6133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      relative_perf = data[2]
6233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      pubkey_fingerprint = data[3]
6333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      trusted = self.server.daemon.IsTrusted(pubkey_fingerprint)
6433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      response = [STARTUP_RESPONSE, self.server.daemon.jobs,
6533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org                  self.server.daemon.relative_perf,
6633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org                  self.server.daemon.pubkey_fingerprint, trusted]
6733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      response = json.dumps(response)
6833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      self.server.SendTo(self.client_address[0], response)
6933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      p = peer.Peer(self.client_address[0], jobs, relative_perf,
7033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org                    pubkey_fingerprint)
7133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      p.trusted = trusted
7233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      self.server.daemon.AddPeer(p)
7333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
7433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    elif data[0] == STARTUP_RESPONSE:
7533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      jobs = data[1]
7633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      perf = data[2]
7733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      pubkey_fingerprint = data[3]
7833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      p = peer.Peer(self.client_address[0], jobs, perf, pubkey_fingerprint)
7933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      p.trusted = self.server.daemon.IsTrusted(pubkey_fingerprint)
8033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      p.trusting_me = data[4]
8133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      self.server.daemon.AddPeer(p)
8233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
8333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    elif data[0] == EXIT_REQUEST:
8433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      self.server.daemon.DeletePeer(self.client_address[0])
8533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      if self.client_address[0] == self.server.daemon.ip:
8633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        self.server.shutdown_lock.release()
8733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
8833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
8933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgclass PresenceDaemon(SocketServer.ThreadingMixIn, SocketServer.UDPServer):
9033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  def __init__(self, daemon):
9133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.daemon = daemon
9233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    address = (daemon.ip, constants.PRESENCE_PORT)
9333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    SocketServer.UDPServer.__init__(self, address, PresenceHandler)
9433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.shutdown_lock = threading.Lock()
9533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
9633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  def shutdown(self):
9733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.shutdown_lock.acquire()
9833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.SendToAll(json.dumps([EXIT_REQUEST]))
9933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.shutdown_lock.acquire()
10033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.shutdown_lock.release()
10133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    SocketServer.UDPServer.shutdown(self)
10233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
10333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  def SendTo(self, target, message):
10433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
10533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    sock.sendto(message, (target, constants.PRESENCE_PORT))
10633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    sock.close()
10733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
10833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  def SendToAll(self, message):
10933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
11033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    ip = self.daemon.ip.split(".")
11133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    for i in range(1, 254):
11233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      ip[-1] = str(i)
11333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      sock.sendto(message, (".".join(ip), constants.PRESENCE_PORT))
11433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    sock.close()
11533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org
11633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  def FindPeers(self):
11733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    request = [STARTUP_REQUEST, self.daemon.jobs, self.daemon.relative_perf,
11833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org               self.daemon.pubkey_fingerprint]
11933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    request = json.dumps(request)
12033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    self.SendToAll(request)
121