Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Energy, onHostDestruction: ensured ptr existence
[simgrid.git] / examples / java / bittorrent / Connection.java
1 /* Copyright (c) 2006-2014. 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 bittorrent;
8
9 import java.util.Arrays;
10 public class Connection {
11         /**
12          * Remote peer id
13          */
14         public int id;
15         /**
16          * Remote peer bitfield.
17          */
18         public char bitfield[];
19         /**
20          * Remote peer mailbox
21          */
22         public String mailbox;
23         /**
24          * Indicates if we are interested in something this peer has
25          */
26         public boolean amInterested = false;
27         /**
28          * Indicates if the peer is interested in one of our pieces
29          */
30         public boolean interested = false;
31         /**
32          * Indicates if the peer is choked for the current peer
33          */
34         public boolean chokedUpload = true;
35         /**
36          * Indicates if the peer has choked the current peer
37          */
38         public boolean chokedDownload = true;
39         /**
40          * Number of messages we have received from the peer
41          */
42         public int messagesCount = 0;
43         /**
44          * Peer speed.
45          */
46         public double peerSpeed = 0;
47         /**
48          * Last time the peer was unchoked
49          */
50         public double lastUnchoke = 0;
51         /**
52          * Constructor
53          */
54         public Connection(int id) {
55                 this.id = id;
56                 this.mailbox = Integer.toString(id);
57         }
58         /**
59          * Add a new value to the peer speed average
60          */
61         public void addSpeedValue(double speed) {
62                 peerSpeed = peerSpeed * 0.55 + speed * 0.45;
63                 //              peerSpeed = (peerSpeed * messagesCount + speed) / (++messagesCount);            
64         }
65                 
66         @Override
67         public String toString() {
68                 return "Connection [id=" + id + ", bitfield="
69                                 + Arrays.toString(bitfield) + ", mailbox=" + mailbox
70                                 + ", amInterested=" + amInterested + ", interested="
71                                 + interested + ", chokedUpload=" + chokedUpload
72                                 + ", chokedDownload=" + chokedDownload + "]";
73         }
74         
75         
76 }
77