diff --git a/.gitignore b/.gitignore index 7d6f5a2e42f2fe0f04f3cd8e8ddf613d07134900..7260afd257b9a398676a703f40db285aeddb5d67 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,9 @@ # Unit testing test.py +# test file +test* + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/client.py b/client.py index f81a82e7da69c5a690f74c3ba22cb39656aaaff4..279026e81d45543bc9f61bc1c5da00d6e9280fe2 100644 --- a/client.py +++ b/client.py @@ -97,6 +97,13 @@ while (not(fin)): stage = "RECEIVE_DATA" elif (stage == "RECEIVE_DATA"): + ''' + Pemeriksaan apakah segmen yang diperoleh rusak atau tidak + Jika tidak rusak + Kirim acknowledgement kepada server + Jika rusak + abaikan + ''' data, address = s.recvfrom(32777) r = receiver.Receiver() @@ -114,6 +121,8 @@ while (not(fin)): if (seq_num0 == server_seq_num + n_data_received): #curr_ack_num masih sama karena sebelum ini, client belum menerima paket dengan payload received_data = rec_packet.getPayLoad() print("\nReceived data: "+received_data) + #print() + #print(hexa.byte(received_data,'utf-8')) ack_packet = segment.Segment() diff --git a/filehandler.py b/filehandler.py index a8a1771fa7fbaa637f381b66a472373771b3a40a..bd2a75882adf61ac974dea84e01199cfbd36cba1 100644 --- a/filehandler.py +++ b/filehandler.py @@ -20,4 +20,19 @@ class FileHandler: file_ = file.File(filePath) dumpRes = pickle.dumps(file_) # byte obj fileHexString = hexa.hexstring(dumpRes) #str - return fileHexString \ No newline at end of file + return fileHexString + + def restoreFile(self,filePath : str,fileHexString : str) -> str: + ''' + [DESC] + Method to restore a file class from a hexstring + [PARAMS] + filePath : str + fileHexString : str + [RETURNS] + str : filePath + ''' + file_ = file.File(filePath) + file_ = pickle.loads(hexa.hextobyte(fileHexString)) + file_.save() + return filePath \ No newline at end of file diff --git a/server.py b/server.py index 585852671b00a36abd04e45bb653194cad5fa006..06bbd21c5bdf8389c6ff9dd48da46e8218b4e609 100644 --- a/server.py +++ b/server.py @@ -29,16 +29,6 @@ Pseudocode: 6. Ulangi sampai pengguna menyelesaikan program server ''' -s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) -if (len(sys.argv) > 1): - port = int(sys.argv[1]) - print("Server started at port "+str(port)+"...") - -else: - print("Port number not specified. Run: 'python server.py <port-number>'") - exit() -s.bind(('127.0.0.1',port)) - def listen_broadcast(): print("Listening to broadcast address for clients.") @@ -135,8 +125,10 @@ def connect(client): elif (stage == "SEND_FILE"): print("\nSending file...") tm = transmitter.Transmitter(server_seq_num + n_data_received) - tm.prepareSegment("./test.txt") - + ''' + Go-Back-N-ARQ starts here + ''' + tm.prepareSegment("./test.mp4") message = tm.transmitSegment(0) s.sendto(message, (IP, port)) @@ -145,7 +137,9 @@ def connect(client): rec_packet.build(r.receiveSegment(data)) n_data_sent += len(tm.segmentQueue[0].getPayLoad()) - + ''' + Go-Back-N-ARQ ends here + ''' stage = "FIN" elif (stage == "FIN"): @@ -241,6 +235,22 @@ def connect(client): print("\nConnection with client "+IP+":"+str(port)+" closed.") -clients = listen_broadcast() +# Main Program +s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + +if (len(sys.argv) > 1): + port = int(sys.argv[1]) + print("Server started at port "+str(port)+"...") + +else: + print("Port number not specified. Run: 'python server.py <port-number>'") + exit() -send_and_connect(clients) +hostname = socket.gethostname() +print(hostname) +local_ip = socket.gethostbyname(hostname) +print(local_ip) + +s.bind(('127.0.0.1',port)) +clients = listen_broadcast() +send_and_connect(clients) \ No newline at end of file diff --git a/transmitter.py b/transmitter.py index a205c7d972ea8fda4abeb411eb139cda9a23e9b6..e8bdd855547fffaf94af2d737faa659298e11ec2 100644 --- a/transmitter.py +++ b/transmitter.py @@ -34,6 +34,7 @@ class Transmitter: s.loadPayLoad(fileHexString[i:i + segment.PAYLOAD_MAX_HEXLENGTH]) s.switchFlag("DATA") self.segmentQueue.append(s) + print("SEG",) counter += 1 def transmitSegment(self,index : int) -> bytes: