|
15 | 15 | import exceptions.PacketException; |
16 | 16 | import exceptions.UtilByteException; |
17 | 17 | import exceptions.UtilDatagramException; |
| 18 | +import me.tongfei.progressbar.ProgressBar; |
| 19 | +import me.tongfei.progressbar.ProgressBarStyle; |
18 | 20 | import network.Packet; |
19 | 21 | import network.TransportLayer; |
20 | 22 | import protocol.FileTransferProtocol; |
@@ -296,21 +298,39 @@ public void waitForInitiate() { |
296 | 298 | * Transfer the byte[] of the File to the downloader, contained in Packets. |
297 | 299 | */ |
298 | 300 | public void transferBytes() { |
299 | | - // TODO when running on client, also here use a progress bar (first solve repeating issue) |
300 | | - |
301 | | - while (!(filePointer >= fileContents.length && totalAckPackets == totalPackets)) { |
302 | | - // while not (reached end of the file AND all packets are acknowledged) |
303 | 301 |
|
304 | | - if ((currentPacketToSend <= LAR + SWS // inside send window size = send the packet |
305 | | - && currentPacketToSend < totalPackets) |
306 | | - && !this.paused) { // if paused only listen |
307 | | - this.sendNextPacket(); |
308 | | - } else { |
309 | | - this.listenForAck(); |
| 302 | + if (!waitForInitiate) { // running on a client: show progress bar |
| 303 | + try (ProgressBar pb = new ProgressBar(this.fileToRead.getName(), this.totalPackets, 1, |
| 304 | + System.out, ProgressBarStyle.COLORFUL_UNICODE_BLOCK, " Bytes", 1, false, null)) { |
| 305 | + pb.setExtraMessage("Uploading..."); |
| 306 | + while (!(filePointer >= fileContents.length && totalAckPackets == totalPackets)) { |
| 307 | + // while not (reached end of the file AND all packets are acknowledged) |
| 308 | + if ((currentPacketToSend <= LAR + SWS // inside send window size = send packet |
| 309 | + && currentPacketToSend < totalPackets) |
| 310 | + && !this.paused) { // if paused only listen |
| 311 | + this.sendNextPacket(); |
| 312 | + } else { |
| 313 | + this.listenForAck(); |
| 314 | + pb.stepTo(this.totalAckPackets); |
| 315 | + } |
| 316 | + pb.setExtraMessage("Done!"); |
| 317 | + |
| 318 | + } |
| 319 | + } |
| 320 | + } else { // running on server: more textual output |
| 321 | + while (!(filePointer >= fileContents.length && totalAckPackets == totalPackets)) { |
| 322 | + // while not (reached end of the file AND all packets are acknowledged) |
| 323 | + if ((currentPacketToSend <= LAR + SWS // inside send window size = send the packet |
| 324 | + && currentPacketToSend < totalPackets) |
| 325 | + && !this.paused) { // if paused only listen |
| 326 | + this.sendNextPacket(); |
| 327 | + } else { |
| 328 | + this.listenForAck(); |
| 329 | + } |
310 | 330 | } |
311 | 331 | } |
312 | 332 | this.showNamedMessage("Sending completed!"); |
313 | | - |
| 333 | + |
314 | 334 | this.complete = true; |
315 | 335 | } |
316 | 336 |
|
|
0 commit comments