Skip to content

Commit d181f88

Browse files
committed
Add progressbar to upload (from client)
1 parent 5906e45 commit d181f88

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

src/main/java/client/FileTransferClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ public boolean discoverServer() {
376376
*/
377377
public void clientRunning() {
378378
while (this.running) {
379-
String userInputString = textUI.getString("Please input:");
379+
String userInputString = textUI.getString("Please input (or type 'help'):");
380380
this.processUserInput(userInputString);
381381
}
382382
}

src/main/java/helpers/DownloadHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public void run() {
249249
this.showNamedMessage("Receiving...");
250250

251251
if (initiate) { // running on a client: show progress bar
252-
try (ProgressBar pb = new ProgressBar("Test", this.totalFileSize, 1,
252+
try (ProgressBar pb = new ProgressBar(this.fileToWrite.getName(), this.totalFileSize, 1,
253253
System.out, ProgressBarStyle.COLORFUL_UNICODE_BLOCK, " Bytes", 1, false, null)) {
254254
pb.setExtraMessage("Downloading...");
255255

src/main/java/helpers/UploadHelper.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import exceptions.PacketException;
1616
import exceptions.UtilByteException;
1717
import exceptions.UtilDatagramException;
18+
import me.tongfei.progressbar.ProgressBar;
19+
import me.tongfei.progressbar.ProgressBarStyle;
1820
import network.Packet;
1921
import network.TransportLayer;
2022
import protocol.FileTransferProtocol;
@@ -296,21 +298,39 @@ public void waitForInitiate() {
296298
* Transfer the byte[] of the File to the downloader, contained in Packets.
297299
*/
298300
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)
303301

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+
}
310330
}
311331
}
312332
this.showNamedMessage("Sending completed!");
313-
333+
314334
this.complete = true;
315335
}
316336

src/main/java/network/Packet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public class Packet {
6363
private int byteOffset;
6464

6565
/**
66-
* Create a Packet
66+
* Create a Packet.
6767
* @param id of new Packet
6868
* @param sourceAddress of new Packet
6969
* @param sourcePort of new Packet

0 commit comments

Comments
 (0)