1b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch# Copyright 2012 the V8 project authors. All rights reserved.
2b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch# Redistribution and use in source and binary forms, with or without
3b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch# modification, are permitted provided that the following conditions are
4b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch# met:
5b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#
6b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#     * Redistributions of source code must retain the above copyright
7b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#       notice, this list of conditions and the following disclaimer.
8b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#     * Redistributions in binary form must reproduce the above
9b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#       copyright notice, this list of conditions and the following
10b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#       disclaimer in the documentation and/or other materials provided
11b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#       with the distribution.
12b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#     * Neither the name of Google Inc. nor the names of its
13b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#       contributors may be used to endorse or promote products derived
14b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#       from this software without specific prior written permission.
15b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch#
16b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
28b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
29b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochimport socket
30b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochimport SocketServer
31b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochimport threading
32b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochtry:
33b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  import ujson as json
34b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochexcept:
35b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  import json
36b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
37b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochfrom . import constants
38b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochfrom ..objects import peer
39b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
40b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
41b8a8cc1952d61a2f3a2568848933943a543b5d3eBen MurdochSTARTUP_REQUEST = "V8 test peer starting up"
42b8a8cc1952d61a2f3a2568848933943a543b5d3eBen MurdochSTARTUP_RESPONSE = "Let's rock some tests!"
43b8a8cc1952d61a2f3a2568848933943a543b5d3eBen MurdochEXIT_REQUEST = "V8 testing peer going down"
44b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
45b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
46b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochdef GetOwnIP():
47b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
48b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  s.connect(("8.8.8.8", 80))
49b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  ip = s.getsockname()[0]
50b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  s.close()
51b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  return ip
52b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
53b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
54b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochclass PresenceHandler(SocketServer.BaseRequestHandler):
55b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
56b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  def handle(self):
57b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    data = json.loads(self.request[0].strip())
58b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
59b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    if data[0] == STARTUP_REQUEST:
60b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      jobs = data[1]
61b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      relative_perf = data[2]
62b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      pubkey_fingerprint = data[3]
63b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      trusted = self.server.daemon.IsTrusted(pubkey_fingerprint)
64b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      response = [STARTUP_RESPONSE, self.server.daemon.jobs,
65b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                  self.server.daemon.relative_perf,
66b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                  self.server.daemon.pubkey_fingerprint, trusted]
67b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      response = json.dumps(response)
68b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      self.server.SendTo(self.client_address[0], response)
69b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      p = peer.Peer(self.client_address[0], jobs, relative_perf,
70b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch                    pubkey_fingerprint)
71b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      p.trusted = trusted
72b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      self.server.daemon.AddPeer(p)
73b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
74b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    elif data[0] == STARTUP_RESPONSE:
75b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      jobs = data[1]
76b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      perf = data[2]
77b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      pubkey_fingerprint = data[3]
78b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      p = peer.Peer(self.client_address[0], jobs, perf, pubkey_fingerprint)
79b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      p.trusted = self.server.daemon.IsTrusted(pubkey_fingerprint)
80b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      p.trusting_me = data[4]
81b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      self.server.daemon.AddPeer(p)
82b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
83b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    elif data[0] == EXIT_REQUEST:
84b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      self.server.daemon.DeletePeer(self.client_address[0])
85b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      if self.client_address[0] == self.server.daemon.ip:
86b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch        self.server.shutdown_lock.release()
87b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
88b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
89b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochclass PresenceDaemon(SocketServer.ThreadingMixIn, SocketServer.UDPServer):
90b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  def __init__(self, daemon):
91b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    self.daemon = daemon
92b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    address = (daemon.ip, constants.PRESENCE_PORT)
93b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    SocketServer.UDPServer.__init__(self, address, PresenceHandler)
94b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    self.shutdown_lock = threading.Lock()
95b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
96b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  def shutdown(self):
97b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    self.shutdown_lock.acquire()
98b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    self.SendToAll(json.dumps([EXIT_REQUEST]))
99b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    self.shutdown_lock.acquire()
100b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    self.shutdown_lock.release()
101b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    SocketServer.UDPServer.shutdown(self)
102b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
103b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  def SendTo(self, target, message):
104b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
105b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    sock.sendto(message, (target, constants.PRESENCE_PORT))
106b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    sock.close()
107b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
108b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  def SendToAll(self, message):
109b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
110b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    ip = self.daemon.ip.split(".")
111b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    for i in range(1, 254):
112b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      ip[-1] = str(i)
113b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      sock.sendto(message, (".".join(ip), constants.PRESENCE_PORT))
114b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    sock.close()
115b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
116b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  def FindPeers(self):
117b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    request = [STARTUP_REQUEST, self.daemon.jobs, self.daemon.relative_perf,
118b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch               self.daemon.pubkey_fingerprint]
119b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    request = json.dumps(request)
120b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch    self.SendToAll(request)
121