diff --git a/src/client.py b/src/client.py --- a/src/client.py +++ b/src/client.py @@ -11,6 +11,9 @@ from hashtree import HashTree,hashBlock from netnode import BaseConnection,NetNode,FailedConnection +class DeniedConnection(Exception): pass + + class Connection(BaseConnection): def __init__(self,host,port): super().__init__() @@ -42,6 +45,14 @@ class Client(NetNode): print(datetime.now(), "initializing...") super().__init__(filename,treeFile) + def init(self,action): + jsonData={"command":"init", "blockSize":self._tree.BLOCK_SIZE, "blockCount":self._tree.leafCount, "version":conf.version, "action":action} + self._outcoming.writeMsg(jsonData) + jsonData,binData=self._incoming.readMsg() + if jsonData["command"]=="deny": + raise DeniedConnection() + assert jsonData["command"]=="init" + ## Asks server for node hashes to determine which are to be transferred. # # Uses a binary HashTree, where item at k is hash of items at 2k+1, 2k+2. @@ -52,12 +63,6 @@ class Client(NetNode): blocksToTransfer=[] nodeStack=collections.deque([0]) # root - # initialize session - jsonData={"command":"init", "blockSize":localTree.BLOCK_SIZE, "blockCount":localTree.leafCount, "version":conf.version} - self._outcoming.writeMsg(jsonData) - jsonData,binData=self._incoming.readMsg() - assert jsonData["command"]=="ack" - # determine which blocks to send print(datetime.now(), "negotiating:") progress=Progress(localTree.leafCount) @@ -115,12 +120,13 @@ class Client(NetNode): progress.p(k) progress.done() - self._outcoming.writeMsg({"command":"end"}) + self._outcoming.writeMsg({"command":"end","action":"push"}) log.info("closing session...") dataFile.close() def pullData(self,blocksToTransfer): + self._lock() log.info(blocksToTransfer) dataFile=open(self._filename, mode="rb+") i1=-1 @@ -150,6 +156,7 @@ class Client(NetNode): log.info("closing session...") dataFile.close() + self._unlock() if self._treeFile: self._updateTree()