diff --git a/src/client.py b/src/client.py --- a/src/client.py +++ b/src/client.py @@ -4,7 +4,7 @@ import logging as log from datetime import datetime import config as conf -from util import progress +from util import Progress from hashtree import HashTree from networkers import NetworkReader,NetworkWriter @@ -45,6 +45,7 @@ class Client: # 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}) @@ -59,8 +60,8 @@ class Client: nodeStack.append(2*i+1) else: blocksToTransfer.append(i-localTree.leafStart) # leaf - progress(i-localTree.leafStart, localTree.leafCount) - print("100%") + progress.p(i-localTree.leafStart) + progress.done() return blocksToTransfer @@ -71,6 +72,7 @@ class Client: print(datetime.now(), "sending data:") with Connection() as (incoming,outcoming): + progress=Progress(len(blocksToTransfer)) for (k,i2) in enumerate(blocksToTransfer): jsonData={"command":"send", "index":i2, "dataType":"data"} if i1+1!=i2: @@ -83,8 +85,8 @@ class Client: jsonData,binData=incoming.readMsg() assert jsonData["command"]=="ack" and jsonData["index"]==i2, jsonData i1=i2 - progress(k,len(blocksToTransfer)) - print("100%") + progress.p(k) + progress.done() with Connection() as (incoming,outcoming): outcoming.writeMsg({"command":"end"}) diff --git a/src/hashtree.py b/src/hashtree.py --- a/src/hashtree.py +++ b/src/hashtree.py @@ -2,7 +2,7 @@ import os from datetime import datetime -from util import progress +from util import Progress class HashTree: @@ -25,12 +25,13 @@ class HashTree: res=cls(leafCount) print(datetime.now(), "hashing file:") + progress=Progress(leafCount) for i in range(leafCount): data=f.read(HashTree.BLOCK_SIZE) res.insertLeaf(hashlib.sha256(data).digest()[HashTree.HASH_LEN:]) - progress(i, leafCount) - print("100%") + progress.p(i) + progress.done() res.buildTree() return res @@ -76,10 +77,11 @@ class HashTree: ## Fast construction of the tree over the leaves. O(n). def buildTree(self): print(datetime.now(), "building tree:") + progress=Progress(-1, self.leafStart-1) for i in range(self.leafStart-1,-1,-1): self.store[i]=hashlib.sha256(self.store[i*2+1]+self.store[i*2+2]).digest()[HashTree.HASH_LEN:] - progress(i, -1, self.leafStart - 1) - print() + progress.p(i) + progress.done() if __name__=="__main__": diff --git a/src/util.py b/src/util.py --- a/src/util.py +++ b/src/util.py @@ -1,11 +1,25 @@ -def progress(i, n, i0=0): - def _progress(i,n,i0): - return 100*(i+1-i0)//(n-i0) +class Progress: + def __init__(self,n,i0=0): + self._n=n + self._i0=i0 + self._i=i0 + self._last="" + + def p(self,i): + i0=self._i0 + n=self._n - if n=i0 else -1 + return 100*(i+_1-i0)//(n-i0)