-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource.java
104 lines (95 loc) · 2.56 KB
/
Source.java
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
/*
* Source
*
* Send a bunch of bytes to specified port using
* a bunch of threads.
*
* You must follow the coding standards distributed
* on the class web page.
*
* (C) 2007 Mike Dahlin
*
*/
import java.net.Socket;
import java.io.OutputStream;
import java.io.IOException;
import java.io.EOFException;
public class Source{
SourceWorker workers[];
//-------------------------------------------------
// Constructor
//-------------------------------------------------
public Source(int nFlows,
int bytesToSendPerFlow,
NWScheduler sched,
float weight[],
Stats stats,
int testPort)
{
assert(nFlows < 1000);
assert(bytesToSendPerFlow > 1000);
assert(weight.length == nFlows);
Socket s = null;
OutputStream os = null;
workers = new SourceWorker[nFlows];
int ii;
for(ii = 0; ii < nFlows; ii++){
try{
s = new Socket("localhost", testPort);
os = s.getOutputStream();
ScheduledOutputStream sos = new ScheduledOutputStream(os,
ii,
stats,
weight[ii],
sched);
workers[ii] = new SourceWorker(bytesToSendPerFlow, sos);
}
catch(IOException ioe){
System.out.println(ioe.toString());
ioe.printStackTrace();
try{
if(os != null){
os.close();
}
}
catch(IOException e){
// ignore
}
try{
if(s != null){
s.close();
}
}
catch(IOException e){
// ignore
}
System.exit(-1);
}
}
}
//-------------------------------------------------
// start() -- tell all the worker threads to start
//-------------------------------------------------
public void start()
{
int ii;
for(ii = 0; ii < workers.length; ii++){
workers[ii].start();
}
}
//-------------------------------------------------
// waitFor() -- join with all threads
//-------------------------------------------------
public void waitFor()
{
int ii;
for(ii = 0; ii < workers.length; ii++){
try{
workers[ii].join();
}
catch(InterruptedException ie){
// ignore
}
}
}
}