# HG changeset patch # User Laman # Date 2017-02-02 21:53:26 # Node ID 6f31f20feb06826d93fa1e67a392683ef528ca9b # Parent 3ba409cd6541a61be60b9dad961996a0d7ce5ae5 client organized into functions diff --git a/src/client.py b/src/client.py --- a/src/client.py +++ b/src/client.py @@ -1,70 +1,84 @@ 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') +import config as conf +from networkers import NetworkReader,NetworkWriter -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"")) +def connect(): + HOST = conf.hosts[0] # The remote host + PORT = conf.port # 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') -# 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+30: + 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