Repository navigation
Expand file tree
/
Copy pathNetwork.java
More file actions
99 lines (73 loc) · 2.27 KB
/
Copy pathNetwork.java
File metadata and controls
99 lines (73 loc) · 2.27 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
/**
* This class represents the state of the network.
* Keep track of each peer in the network.
*
* @author MayurM
* @version 0.3
*
*/
import java.util.*;
public class Network
{
ArrayList<Peer> peers; //list of peers currently in network
int numOfPeers;
public Network(){
this.peers = new ArrayList<Peer> ();
this.numOfPeers = 0;
}
public void setPeerList(ArrayList<Peer> peers){
this.peers = peers;
this.numOfPeers = peers.size();
}
public void setNumOfPeers(int numOfPeers){
this.numOfPeers = numOfPeers;
}
public ArrayList<Peer> getPeerList(){
return this.peers;
}
public int getNumOfPeers (){
return this.numOfPeers;
}
public boolean empty(){//checks if there are no peers in network
if(this.getNumOfPeers() == 0){
return true;
}
else{
return false;
}
}
public Peer getPeer(int index){//returns peer in network based on index
if (index >= 0 && index <= this.peers.size()){
return peers.get(index);
}
else{
return null;
}
}
public void addPeer(Peer peer){//add single peer
// try {
// peer.server.join();
// //peer.connection.join();
// // peer2.server.join();
// //peer2.connection.join();
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
if(!this.empty()){
for(Peer p: this.peers){//add current peers to new peers peer list
peer.add(p);
}
for(Peer p: this.peers){// add new peer to every existing peers peer list
p.add(peer);
}
}
this.peers.add(peer);
this.numOfPeers++;//increment number of peers
}
public void addPeers(ArrayList<Peer> peers){//add multiple peers
for (Peer p : peers){
this.addPeer(p);
}
}
}