Repository navigation
Expand file tree
/
Copy pathPeer.java
More file actions
166 lines (120 loc) · 4.05 KB
/
Copy pathPeer.java
File metadata and controls
166 lines (120 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Models the state of a peer
* @author MayurM
* @version 0.3
*/
import java.util.*;
public class Peer
{
//public InputStream input;
//public Logger log;
//public OutputStream output;
//public PeerDriver peerDriver;
//public peerInfo thisPeer;
int id; //unique idenifier for each peer
//list of connected peers. for simplicity this will be every other peer in the network
ArrayList<Peer> connectedPeers;
//collection of messages intended for this peer
ArrayList<Message> inbox;
//messages sent from this peer
ArrayList<Message> sent;
//the amount of computation time for proof of work. avg 4 min per message
int PowTime;
int numConnectedPeers;
/**
public PeerDriver(int peerID) {
// TODO Auto-generated constructor stub
this.peerID = peerID;
this.server= new ServerConnection(this.peerID, this);
this.nieghbors= new ArrayList<Peer>();
this.done= false;
}
public void startPeer(){
startServer();
startClients();
}
**/
public Peer(){
this.id = 0;
this.connectedPeers = new ArrayList<Peer>();
this.inbox = new ArrayList<Message>();
this.sent = new ArrayList<Message>();
this.PowTime = 0;
this.numConnectedPeers = 0;
}
public Peer(int id){
this.id = id;
this.connectedPeers = new ArrayList<Peer>();
this.inbox = new ArrayList<Message>();
this.sent = new ArrayList<Message>();
this.PowTime = 0;
this.numConnectedPeers = 0;
}
public int getId(){
return this.id;
}
public void setId(int id){
this.id = id;
}
public int getPowTime(){
return this.PowTime;
}
public void setPowTime(int PowTime){
this.PowTime = PowTime;
}
public int getInboxSize(){
int total = 0;
for(Message m : this.inbox){//add up message size for all received messages
total = total + m.getSize();
}
return total;
}
public int getSentSize(){
int total = 0;
for(Message m : this.sent){//add up message size for all sent messages
total = total + m.getSize();
}
return total;
}
public void add(Peer peer){//new peer connectss to this peer
this.connectedPeers.add(peer);
this.numConnectedPeers++;
}
public void sendMessage(int size, int toId){
//send message of given size
Message m = new Message(size, this.getId(), toId); //create message
this.sent.add(m); //add this message to this peers sent list
//for all connected peers add this message to the inbox
//this is done in one step for simplicity
for(Peer p : this.connectedPeers){
p.receiveMessage(m);
}
}
public void receiveMessage(Message m){
//add the input message to this peers inbox
this.inbox.add(m);
//update the received status of message
if(m.getTo() == this.getId()){//acknowledgement
m.setReceived(true);
}
}
public String getState(){ //represents the current state of peer
String s = "Peer ID: " + this.id + "\n";
s = s + "Received: " + this.inbox.size() + " messages totaling " + this.getInboxSize() + " KB \n";
s = s + "Sent: " + this.sent.size() + " messages totaling " + this.getSentSize() + " KB \n";
s = s + "POW time: " + this.totalPow() + " min(s) \n";
return s;
}
public int totalPow(){
return this.sent.size() * 4; //avg 4 min of pow per message
}
public String toString(){
String s = "name: " + this.id + "\n";
s = s + "connections: ";
//print the id of all peers that are connected to this peer
for (Peer p : this.connectedPeers){
s = s + p.getId() + " ";
}
return s;
}
}