| 20 | # Communicate remote peers |
| 21 | @PluginManager.acceptPlugins |
| 22 | class Peer(object): |
| 23 | __slots__ = ( |
| 24 | "ip", "port", "site", "key", "connection", "connection_server", "time_found", "time_response", "time_hashfield", |
| 25 | "time_added", "has_hashfield", "is_tracker_connection", "time_my_hashfield_sent", "last_ping", "reputation", |
| 26 | "last_content_json_update", "hashfield", "connection_error", "hash_failed", "download_bytes", "download_time" |
| 27 | ) |
| 28 | |
| 29 | def __init__(self, ip, port, site=None, connection_server=None): |
| 30 | self.ip = ip |
| 31 | self.port = port |
| 32 | self.site = site |
| 33 | self.key = "%s:%s" % (ip, port) |
| 34 | |
| 35 | self.connection = None |
| 36 | self.connection_server = connection_server |
| 37 | self.has_hashfield = False # Lazy hashfield object not created yet |
| 38 | self.time_hashfield = None # Last time peer's hashfiled downloaded |
| 39 | self.time_my_hashfield_sent = None # Last time my hashfield sent to peer |
| 40 | self.time_found = time.time() # Time of last found in the torrent tracker |
| 41 | self.time_response = None # Time of last successful response from peer |
| 42 | self.time_added = time.time() |
| 43 | self.last_ping = None # Last response time for ping |
| 44 | self.is_tracker_connection = False # Tracker connection instead of normal peer |
| 45 | self.reputation = 0 # More likely to connect if larger |
| 46 | self.last_content_json_update = 0.0 # Modify date of last received content.json |
| 47 | |
| 48 | self.connection_error = 0 # Series of connection error |
| 49 | self.hash_failed = 0 # Number of bad files from peer |
| 50 | self.download_bytes = 0 # Bytes downloaded |
| 51 | self.download_time = 0 # Time spent to download |
| 52 | |
| 53 | def __getattr__(self, key): |
| 54 | if key == "hashfield": |
| 55 | self.has_hashfield = True |
| 56 | self.hashfield = PeerHashfield() |
| 57 | return self.hashfield |
| 58 | else: |
| 59 | return getattr(self, key) |
| 60 | |
| 61 | def log(self, text): |
| 62 | if not config.verbose: |
| 63 | return # Only log if we are in debug mode |
| 64 | if self.site: |
| 65 | self.site.log.debug("%s:%s %s" % (self.ip, self.port, text)) |
| 66 | else: |
| 67 | logging.debug("%s:%s %s" % (self.ip, self.port, text)) |
| 68 | |
| 69 | # Connect to host |
| 70 | def connect(self, connection=None): |
| 71 | if self.reputation < -10: |
| 72 | self.reputation = -10 |
| 73 | if self.reputation > 10: |
| 74 | self.reputation = 10 |
| 75 | |
| 76 | if self.connection: |
| 77 | self.log("Getting connection (Closing %s)..." % self.connection) |
| 78 | self.connection.close("Connection change") |
| 79 | else: |
no outgoing calls