diff --git a/src/client.py b/src/client.py --- a/src/client.py +++ b/src/client.py @@ -58,15 +58,18 @@ def negotiate(incoming,outcoming): def sendData(outcoming,blocksToTransfer): print(blocksToTransfer) dataFile=open(filename,mode="rb") + i1=-1 - for i in blocksToTransfer: - jsonData={"command":"send", "index":i, "dataType":"data"} - dataFile.seek(i*HashTree.BLOCK_SIZE) + for i2 in blocksToTransfer: + jsonData={"command":"send", "index":i2, "dataType":"data"} + if i1+1!=i2: + dataFile.seek(i2*HashTree.BLOCK_SIZE) binData=dataFile.read(HashTree.BLOCK_SIZE) - print("block #{0}: {1}...{2}".format(i,binData[:5],binData[-5:])) + print("block #{0}: {1}...{2}".format(i2,binData[:5],binData[-5:])) outcoming.put((jsonData,binData),timeout=2) + i1=i2 jsonData={"command":"end"} outcoming.put((jsonData,b""),timeout=2) diff --git a/src/server.py b/src/server.py --- a/src/server.py +++ b/src/server.py @@ -33,9 +33,12 @@ nodeStack=collections.deque([0]) incoming=networkReader.output # synchronized message queue outcoming=networkWriter.input +i1=-1 + while True: jsonData,binData=incoming.get(timeout=2) + dataFile=open(filename,mode="rb+") if jsonData["command"]=="init": assert jsonData["blockSize"]==localTree.BLOCK_SIZE @@ -52,11 +55,12 @@ while True: elif jsonData["command"]=="send" and jsonData["dataType"]=="data": # needlessly allow hashes and data in mixed order print("received data block #{0}: {1}...{2}".format(jsonData["index"],binData[:5],binData[-5:])) - - dataFile=open(filename,mode="rb+") - dataFile.seek(jsonData["index"]*localTree.BLOCK_SIZE) + + i2=jsonData["index"] + if i1+1!=i2: + dataFile.seek(i2*localTree.BLOCK_SIZE) dataFile.write(binData) - dataFile.close() + i1=i2 # never update the hash tree @@ -68,5 +72,6 @@ while True: # fr.close() # fw.close() +dataFile.close() conn.close() sys.exit(0)