Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b6d88d4017f8e20d0368a901248c893a35230bae
[simgrid.git] / examples / java / app / bittorrent / Connection.java
1 /* Copyright (c) 2006-2018. 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   }
36
37   @Override
38   public String toString() {
39     return "Connection [id=" + id + ", bitfield=" + Arrays.toString(bitfield) + ", mailbox=" + mailbox
40         + ", amInterested=" + amInterested + ", interested=" + interested + ", chokedUpload=" + chokedUpload
41         + ", chokedDownload=" + chokedDownload + "]";
42   }
43 }