# HG changeset patch # User Laman # Date 2017-10-18 16:22:02 # Node ID 6c8e994fd9066803092ad0c187adb540cb047932 # Parent 3d0876534e40585cb8e3cd1aa97b69b05bb362c6 using just one socket, server saving memory when not serving diff --git a/src/client.py b/src/client.py --- a/src/client.py +++ b/src/client.py @@ -12,9 +12,13 @@ from networkers import NetworkReader,Net class Connection: - def __init__(self,sslContext): + def __init__(self): sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sslContext=ssl.create_default_context(cafile=conf.peers) + sslContext.check_hostname=False + sslContext.load_cert_chain(conf.certfile,conf.keyfile) + self._socket=sslContext.wrap_socket(sock) self._socket.connect((conf.hosts[0], conf.port)) fr=self._socket.makefile(mode="rb") @@ -33,44 +37,43 @@ class Connection: class Client: def __init__(self,filename): + self._incoming=None + self._outcoming=None self._filename=filename - self._ssl=ssl.create_default_context(cafile=conf.peers) - self._ssl.check_hostname=False - self._ssl.load_cert_chain(conf.certfile,conf.keyfile) + print(datetime.now(), "initializing...") + self._localTree=HashTree.fromFile(self._filename) def negotiate(self): - print(datetime.now(), "initializing...") - localTree=HashTree.fromFile(self._filename) + localTree=self._localTree blocksToTransfer=[] nodeStack=collections.deque([0]) # root # initialize session - with Connection(self._ssl) as (incoming,outcoming): - jsonData={"command":"init", "blockSize":localTree.BLOCK_SIZE, "blockCount":localTree.leafCount, "version":conf.version} - outcoming.writeMsg(jsonData) - jsonData,binData=incoming.readMsg() - assert jsonData["command"]=="ack" + 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) - while len(nodeStack)>0: - i=nodeStack.pop() - outcoming.writeMsg({"command":"req", "index":i, "dataType":"hash"}) + # determine which blocks to send + print(datetime.now(), "negotiating:") + progress=Progress(localTree.leafCount) + while len(nodeStack)>0: + i=nodeStack.pop() + self._outcoming.writeMsg({"command":"req", "index":i, "dataType":"hash"}) - jsonData,binData=incoming.readMsg() - assert jsonData["index"]==i - assert jsonData["dataType"]=="hash" - stats.logExchangedNode() + jsonData,binData=self._incoming.readMsg() + assert jsonData["index"]==i + assert jsonData["dataType"]=="hash" + stats.logExchangedNode() - if localTree.store[i]!=binData: - if 2*i+3