Changeset - 9dbe43245cff
[Not reviewed]
default
0 1 0
Laman - 7 years ago 2017-10-19 11:56:59

fixed index bug on negotiating the last inner node
1 file changed with 2 insertions and 1 deletions:
0 comments (0 inline, 0 general)
client.py
Show inline comments
 
@@ -15,49 +15,50 @@ fr=s.makefile(mode='rb')
 
fw=s.makefile(mode='wb')
 
 
networkReader=NetworkReader(fr)
 
networkReader.start()
 
networkWriter=NetworkWriter(fw)
 
networkWriter.start()
 
 
blocksToTransfer=[]
 
nodeStack=collections.deque([0]) # root
 
incoming=networkReader.output # synchronized message queue
 
outcoming=networkWriter.input
 
 
 
# determine which blocks to send
 
while len(nodeStack)>0:
 
  i=nodeStack.pop()
 
  jsonData={"command":"req", "index":i}
 
  outcoming.put((jsonData,b""))
 
  
 
  jsonData,binData=incoming.get(timeout=2)
 
  assert jsonData["index"]==i
 
  assert jsonData["dataType"]=="hash"
 
  
 
  if localTree.store[i]!=binData:
 
    if 2*i+3<len(localTree.store): # inner node
 
    # ie. 0-6 nodes, 7-14 leaves. 2*6+2<15
 
    if 2*i+2<len(localTree.store): # inner node
 
      nodeStack.append(2*i+2)
 
      nodeStack.append(2*i+1)
 
    else: blocksToTransfer.append(i-localTree.leafStart) # leaf
 
 
 
# send the actual data
 
print(blocksToTransfer)
 
dataFile=open("clientFile.txt",mode="rb")
 
 
for i in blocksToTransfer:
 
  jsonData={"command":"send", "index":i, "dataType":"data"}
 
  dataFile.seek(i*localTree.BLOCK_SIZE)
 
  binData=dataFile.read(localTree.BLOCK_SIZE)
 
  
 
  print("block #{0}: {1}...{2}".format(i,binData[:5],binData[-5:]))
 
  
 
  outcoming.put((jsonData,binData),timeout=2)
 
 
jsonData={"command":"end"}
 
outcoming.put((jsonData,b""),timeout=2)
 
 
outcoming.put(None)
 
print("closing...")
 
dataFile.close()
0 comments (0 inline, 0 general)