Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
668283a762b1cccfd5f5248ca5c674fbee038787
[simgrid.git] / examples / java / app / bittorrent / Connection.java
1 /* Copyright (c) 2006-2014, 2016. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 package app.bittorrent;
8 import java.util.Arrays;
9
10 public class Connection {
11   protected int id;
12   protected char[] bitfield;
13   protected String mailbox;
14   // Indicates if we are interested in something this peer has
15   protected boolean amInterested = false;
16   // Indicates if the peer is interested in one of our pieces
17   protected boolean interested = false;
18   // Indicates if the peer is choked for the current peer
19   protected boolean chokedUpload = true;
20   // Indicates if the peer has choked the current peer
21   protected boolean chokedDownload = true;
22   // Number of messages we have received from the peer
23   protected int messagesCount = 0;
24   protected double peerSpeed = 0;
25   protected double lastUnchoke = 0;
26
27   public Connection(int id) {
28     this.id = id;
29     this.mailbox = Integer.toString(id);
30   }
31
32   // Add a new value to the peer speed average
33   public void addSpeedValue(double speed) {
34     peerSpeed = peerSpeed * 0.55 + speed * 0.45;
35     // peerSpeed = (peerSpeed * messagesCount + speed) / (++messagesCount);    
36   }
37
38   @Override
39   public String toString() {
40     return "Connection [id=" + id + ", bitfield=" + Arrays.toString(bitfield) + ", mailbox=" + mailbox
41         + ", amInterested=" + amInterested + ", interested=" + interested + ", chokedUpload=" + chokedUpload
42         + ", chokedDownload=" + chokedDownload + "]";
43   }
44 }