-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransferClient.java
More file actions
123 lines (107 loc) · 3.78 KB
/
TransferClient.java
File metadata and controls
123 lines (107 loc) · 3.78 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
/**
* <p>Title: CPSC 433/533 Programming Assignment</p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: Yale University</p>
*
* @author Hao Wang
* @version 1.0
*/
/**
* <p> A transfer client using Fishnet socket API </p>
*/
public class TransferClient extends FishThread {
private TCPSock sock;
private long interval;
private byte[] buf;
public static final long DEFAULT_CLIENT_INTERVAL = 1000;
public static final int DEFAULT_BUFFER_SZ = 65536;
// number of bytes to send
private int amount;
// starting and finishing time in milliseconds
private long startTime;
private long finishTime;
private int pos;
public TransferClient(Manager manager, Node node, TCPSock sock, int amount,
long interval, int sz) {
super(manager, node);
this.sock = sock;
this.interval = interval;
this.buf = new byte[sz];
this.amount = amount;
this.startTime = 0;
this.finishTime = 0;
this.pos = 0;
this.setInterval(this.interval);
}
public TransferClient(Manager manager, Node node, TCPSock sock, int amount) {
this(manager, node, sock, amount,
DEFAULT_CLIENT_INTERVAL,
DEFAULT_BUFFER_SZ);
}
public void execute() {
if (sock.isConnectionPending()) {
//node.logOutput("connecting...");
return;
} else if (sock.isConnected()) {
if (startTime == 0) {
// record starting time
startTime = manager.now();
node.logOutput("time = " + startTime + " msec");
node.logOutput("started");
node.logOutput("bytes to send = " + amount);
}
if (amount == 0) {
// sending completed, initiate closure of connection
node.logOutput("time = " + manager.now());
node.logOutput("sending completed");
node.logOutput("closing connection...");
sock.close();
return;
}
//node.logOutput("sending...");
int index = pos % buf.length;
if (index == 0) {
// generate new data
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) i;
}
}
int len = Math.min(buf.length - index, amount);
int count = sock.write(buf, index, len);
if (count == -1) {
// on error, release the socket immediately
node.logError("time = " + manager.now() + " msec");
node.logError("sending aborted");
node.logError("position = " + pos);
node.logError("releasing connection...");
sock.release();
this.stop();
return;
}
pos += count;
amount -= count;
//node.logOutput("time = " + manager.now());
//node.logOutput("bytes sent = " + count);
return;
} else if (sock.isClosurePending()) {
//node.logOutput("closing connection...");
return;
} else if (sock.isClosed()) {
finishTime = manager.now();
node.logOutput("time = " + manager.now() + " msec");
node.logOutput("connection closed");
node.logOutput("total bytes sent = " + pos);
node.logOutput("time elapsed = " +
(finishTime - startTime) + " msec");
node.logOutput("Bps = " + pos * 1000.0 / (finishTime - startTime));
// release the socket
sock.release();
this.stop();
return;
}
node.logError("shouldn't reach here");
System.exit(1);
}
}