1# Copyright 2012 the V8 project authors. All rights reserved.
2# Redistribution and use in source and binary forms, with or without
3# modification, are permitted provided that the following conditions are
4# met:
5#
6#     * Redistributions of source code must retain the above copyright
7#       notice, this list of conditions and the following disclaimer.
8#     * Redistributions in binary form must reproduce the above
9#       copyright notice, this list of conditions and the following
10#       disclaimer in the documentation and/or other materials provided
11#       with the distribution.
12#     * Neither the name of Google Inc. nor the names of its
13#       contributors may be used to endorse or promote products derived
14#       from this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28
29class Peer(object):
30  def __init__(self, address, jobs, rel_perf, pubkey):
31    self.address = address  # string: IP address
32    self.jobs = jobs  # integer: number of CPUs
33    self.relative_performance = rel_perf
34    self.pubkey = pubkey # string: pubkey's fingerprint
35    self.shells = set()  # set of strings
36    self.needed_work = 0
37    self.assigned_work = 0
38    self.tests = []  # list of TestCase objects
39    self.trusting_me = False  # This peer trusts my public key.
40    self.trusted = False  # I trust this peer's public key.
41
42  def __str__(self):
43    return ("Peer at %s, jobs: %d, performance: %.2f, trust I/O: %s/%s" %
44            (self.address, self.jobs, self.relative_performance,
45             self.trusting_me, self.trusted))
46
47  def AddTests(self, shell):
48    """Adds tests from |shell| to this peer.
49
50    Stops when self.needed_work reaches zero, or when all of shell's tests
51    are assigned."""
52    assert self.needed_work > 0
53    if shell.shell not in self.shells:
54      self.shells.add(shell.shell)
55    while len(shell.tests) > 0 and self.needed_work > 0:
56      t = shell.tests.pop()
57      self.needed_work -= t.duration
58      self.assigned_work += t.duration
59      shell.total_duration -= t.duration
60      self.tests.append(t)
61
62  def ForceAddOneTest(self, test, shell):
63    """Forcibly adds another test to this peer, disregarding needed_work."""
64    if shell.shell not in self.shells:
65      self.shells.add(shell.shell)
66    self.needed_work -= test.duration
67    self.assigned_work += test.duration
68    shell.total_duration -= test.duration
69    self.tests.append(test)
70
71
72  def Pack(self):
73    """Creates a JSON serializable representation of this Peer."""
74    return [self.address, self.jobs, self.relative_performance]
75
76  @staticmethod
77  def Unpack(packed):
78    """Creates a Peer object built from a packed representation."""
79    pubkey_dummy = ""  # Callers of this don't care (only the server does).
80    return Peer(packed[0], packed[1], packed[2], pubkey_dummy)
81