Logo AND Algorithmique Numérique Distribuée

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