11
11
import java .net .DatagramPacket ;
12
12
import java .net .DatagramSocket ;
13
13
import java .net .InetAddress ;
14
+ import java .util .Collections ;
14
15
import java .util .HashMap ;
15
16
import java .util .Map ;
16
17
import java .util .concurrent .TimeUnit ;
@@ -43,11 +44,13 @@ public class FileCopyClient extends Thread {
43
44
44
45
FileInputStream inputstream ;
45
46
46
- Map <Integer , FCpacket > sendbuffer ;
47
+ static Map <Long , FCpacket > sendbuffer ;
47
48
48
49
FCpacket pinit ;
49
50
50
51
static DatagramSocket clientSocket ;
52
+
53
+ long sendbase = 0 ;
51
54
// ... ToDo
52
55
53
56
// Constructor
@@ -61,12 +64,10 @@ public FileCopyClient(String serverArg, String sourcePathArg, String destPathArg
61
64
62
65
}
63
66
64
- public void runFileCopyClient () throws IOException , InterruptedException {
67
+ public void runFileCopyClient () throws IOException {
65
68
inputstream = new FileInputStream (sourcePath );
66
- sendbuffer = new HashMap <Integer , FCpacket >();
67
- // clientSocket = new DatagramSocket();
68
- int sendbase = 0 ;
69
- int nextSeqNum = 1 ;
69
+ // clientSocket = new DatagramSocket();
70
+ long nextSeqNum = 1 ;
70
71
byte [] buf = new byte [1000 ];
71
72
72
73
pinit = makeControlPacket ();
@@ -76,9 +77,7 @@ public void runFileCopyClient() throws IOException, InterruptedException {
76
77
SERVER_PORT );
77
78
clientSocket .send (packet );
78
79
while (true ) {
79
- TimeUnit .SECONDS .sleep (1 );
80
- while (inputstream .available () != 0 && sendbuffer .size () <= windowSize ) {
81
- System .out .println (sendbuffer .size ());
80
+ while (inputstream .available () != 0 && sendbuffer .size () < windowSize ) {
82
81
inputstream .read (buf );
83
82
pinit = new FCpacket (nextSeqNum , buf , buf .length );
84
83
packet = new DatagramPacket (pinit .getSeqNumBytesAndData (), pinit .getLen () + 8 , address , SERVER_PORT );
@@ -153,30 +152,30 @@ public void testOut(String out) {
153
152
public static void main (String argv []) throws Exception {
154
153
FileCopyClient myClient = new FileCopyClient (argv [0 ], argv [1 ], argv [2 ], argv [3 ], argv [4 ]);
155
154
clientSocket = new DatagramSocket ();
155
+ sendbuffer = new HashMap <Long , FCpacket >();
156
156
(new FileCopyClient (argv [0 ], argv [1 ], argv [2 ], argv [3 ], argv [4 ])).start ();
157
157
myClient .runFileCopyClient ();
158
158
}
159
159
160
160
@ Override
161
161
public void run () {
162
162
try {
163
- System .out .println ("Hello from Thread." );
164
163
byte [] data = new byte [FileCopyClient .UDP_PACKET_SIZE ];
165
164
while (true ) {
166
165
DatagramPacket udpReceivePacket = new DatagramPacket (data , data .length );
167
166
// Wait for data packet
168
- System .out .println (clientSocket .getLocalPort ());
169
167
clientSocket .receive (udpReceivePacket );
170
- System .out .println ("testing" );
171
- FCpacket ackpack = new FCpacket (udpReceivePacket .getData (), udpReceivePacket .getLength ());
172
- long receivedSeqNumber = ackpack .getSeqNum ();
168
+ FCpacket ackpack = new FCpacket (udpReceivePacket .getData (), udpReceivePacket .getLength ());
169
+ long receivedSeqNumber = ackpack .getSeqNum ();
173
170
if (sendbuffer .containsKey (receivedSeqNumber )) {
174
171
sendbuffer .remove (receivedSeqNumber );
172
+ // if(sendbuffer.size() >= 0)
173
+ // sendbase = Collections.min(sendbuffer.keySet());
174
+ System .out .println (sendbase );
175
175
}
176
- sleep (1 );
177
176
}
178
177
} catch (Exception e ) {
179
-
178
+ System . out . println ( e );
180
179
}
181
180
}
182
181
}
0 commit comments