Files
@ 3ba409cd6541
Branch filter:
Location: Morevna/src/client.py - annotation
3ba409cd6541
1.9 KiB
text/x-python
rebuild command to get the original file
59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 | from hashtree import HashTree
import collections
from networkers import NetworkReader,NetworkWriter
import socket
import sys
localTree=HashTree.fromFile(open("clientFile.txt",mode="rb"))
HOST = '127.0.0.1' # The remote host
PORT = 50009 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
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
# initialize session
jsonData={"command":"init", "blockSize":localTree.BLOCK_SIZE, "blockCount":localTree.leafCount}
outcoming.put((jsonData,b""))
# 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
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()
# fr.close()
# fw.close()
s.close()
sys.exit(0)
|