Repository navigation
Expand file tree
/
Copy pathDriver.java
More file actions
122 lines (93 loc) · 3.56 KB
/
Copy pathDriver.java
File metadata and controls
122 lines (93 loc) · 3.56 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
/**
* Driver to start simulation
*
* @author MayurM
* @version 0.3
*/
import java.util.*;
import java.io.*;
public class Driver
{
public static void main(String [] args){
// check if there are enough arguments
if(args.length != 2){
String usage = "Usage: java Driver [num of peers] [total messages to send] \n";
System.out.println(usage);
return;
}
//start simulation
System.out.println("running...");
//initialize new netowork
Network network = new Network();
//create list for peers
ArrayList<Peer> test = new ArrayList<Peer>();
int numOfPeers = Integer.parseInt(args[0]);
int numOfMessages = Integer.parseInt(args[1]);
//create all peers
for(int i = 0; i < numOfPeers; i++){
Peer p = new Peer(i);
test.add(p);
}
//add each newly created peers to network
network.addPeers(test);
//used to send messages from a random peer to a random peer
Random rand = new Random();
//represents size of all messages in network
//each new connecting peer will have to decrypt all messages to determine if there is a new message
int messageTotal = 0;
//stores log of each sent message
String log = "";
for(int i = 0; i < numOfMessages; i++){
int to = rand.nextInt(test.size());
int from = rand.nextInt(test.size());
//sets message size randomly from 1 to 128kB
int messageSize = rand.nextInt(128) + 1;
//compute total size of messages in network
messageTotal = messageTotal + messageSize;
while(from == to){
//make sure that no peer can send a message to itself
from = rand.nextInt(test.size());
}
//send out message
test.get(from).sendMessage(messageSize, to);
//log the sent message
log = log + "peer " + from + " sent message of size " + messageSize
+ " KB to peer " + to + "\n";
//System.out.println(messageTotal);
}
//holds the stats of each peer
String stat = "";
for(Peer p : network.peers){
//creates the stat string
stat = stat + p.getState() + "\n";
}
//appends the total message sum to end of stat
stat = stat + "Total messages size in network: " + messageTotal + " KB \n";
System.out.println("writing results to file...");
//write stat to file
toFile(stat, "stat.txt");
System.out.println("writing log to file...");
//write log to file
toFile(log, "log.txt");
System.out.println("done");
}
public static void toFile(String s, String name ){
try{
File f = new File(name);
// make the file if its not there
if (!f.exists()){
f.createNewFile();
}
FileWriter w = new FileWriter(f.getAbsoluteFile());
BufferedWriter b = new BufferedWriter(w);
b.write(s);
b.close();
//finished writing
}
catch(IOException e)
{
System.out.println("exception: " + e);
e.printStackTrace( );
}
}
}