Changeset - 8ad57a925d81
[Not reviewed]
default
0 2 0
Laman - 8 years ago 2017-05-06 19:44:40

optimized some unnecessary disk seeks
2 files changed with 16 insertions and 8 deletions:
0 comments (0 inline, 0 general)
src/client.py
Show inline comments
 
@@ -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)
src/server.py
Show inline comments
 
@@ -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)
0 comments (0 inline, 0 general)