# HG changeset patch # User Laman # Date 2017-05-07 23:58:18 # Node ID b73a5d69a11bb236c6248e304ca457073721747b # Parent d720182784506b804ee418f9b81b33fd5e88f333 wrapped client and server into objects diff --git a/src/client.py b/src/client.py --- a/src/client.py +++ b/src/client.py @@ -8,9 +8,6 @@ import config as conf from networkers import NetworkReader,NetworkWriter -filename=sys.argv[1] - - class Connection: def __init__(self): self.socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM) @@ -28,61 +25,57 @@ class Connection: self.socket.close() -def negotiate(): - localTree=HashTree.fromFile(filename) - blocksToTransfer=[] - nodeStack=collections.deque([0]) # root +class Client: + def __init__(self,filename): + self.filename=filename - # initialize session - with Connection() as (incoming,outcoming): - jsonData={"command":"init", "blockSize":localTree.BLOCK_SIZE, "blockCount":localTree.leafCount, "version":conf.version} - outcoming.writeMsg(jsonData) + def negotiate(self): + localTree=HashTree.fromFile(self.filename) + blocksToTransfer=[] + nodeStack=collections.deque([0]) # root - # determine which blocks to send - while len(nodeStack)>0: + # initialize session with Connection() as (incoming,outcoming): - i=nodeStack.pop() - outcoming.writeMsg({"command":"req", "index":i}) - - jsonData,binData=incoming.readMsg() - assert jsonData["index"]==i - assert jsonData["dataType"]=="hash" + jsonData={"command":"init", "blockSize":localTree.BLOCK_SIZE, "blockCount":localTree.leafCount, "version":conf.version} + outcoming.writeMsg(jsonData) - if localTree.store[i]!=binData: - if 2*i+30: + with Connection() as (incoming,outcoming): + i=nodeStack.pop() + outcoming.writeMsg({"command":"req", "index":i}) - return blocksToTransfer - + jsonData,binData=incoming.readMsg() + assert jsonData["index"]==i + assert jsonData["dataType"]=="hash" -def sendData(blocksToTransfer): - log.info(blocksToTransfer) - dataFile=open(filename,mode="rb") - i1=-1 + if localTree.store[i]!=binData: + if 2*i+3