Changeset - b052f27e1cbc
[Not reviewed]
default
0 4 0
Laman - 8 years ago 2017-06-03 22:47:05

more progress info
4 files changed with 29 insertions and 13 deletions:
0 comments (0 inline, 0 general)
src/client.py
Show inline comments
 
from hashtree import HashTree
 
import collections
 
import collections
 
import socket
 
import logging as log
 
from datetime import datetime
 

	
 
import config as conf
 
from util import progress
 
from hashtree import HashTree
 
from networkers import NetworkReader,NetworkWriter
 

	
 

	
 
@@ -29,6 +31,7 @@ class Client:
 
		self.filename=filename
 

	
 
	def negotiate(self):
 
		print(datetime.now(), "initializing...")
 
		localTree=HashTree.fromFile(self.filename)
 
		blocksToTransfer=[]
 
		nodeStack=collections.deque([0]) # root
 
@@ -39,6 +42,7 @@ class Client:
 
			outcoming.writeMsg(jsonData)
 

	
 
		# determine which blocks to send
 
		print(datetime.now(), "negotiating:")
 
		while len(nodeStack)>0:
 
			with Connection() as (incoming,outcoming):
 
				i=nodeStack.pop()
 
@@ -52,7 +56,10 @@ class Client:
 
					if 2*i+3<len(localTree.store): # inner node
 
						nodeStack.append(2*i+2)
 
						nodeStack.append(2*i+1)
 
					else: blocksToTransfer.append(i-localTree.leafStart) # leaf
 
					else:
 
						blocksToTransfer.append(i-localTree.leafStart) # leaf
 
						progress(i-localTree.leafStart, localTree.leafCount)
 
		print("100%")
 

	
 
		return blocksToTransfer
 

	
 
@@ -61,7 +68,8 @@ class Client:
 
		dataFile=open(self.filename,mode="rb")
 
		i1=-1
 

	
 
		for i2 in blocksToTransfer:
 
		print(datetime.now(), "sending data:")
 
		for (k,i2) in enumerate(blocksToTransfer):
 
			with Connection() as (incoming,outcoming):
 
				jsonData={"command":"send", "index":i2, "dataType":"data"}
 
				if i1+1!=i2:
 
@@ -71,10 +79,12 @@ class Client:
 
				log.info("block #{0}: {1}...{2}".format(i2,binData[:5],binData[-5:]))
 

	
 
				outcoming.writeMsg(jsonData,binData)
 
				i1=i2
 
			i1=i2
 
			progress(k,len(blocksToTransfer))
 
		print("100%")
 

	
 
		with Connection() as (incoming,outcoming):
 
			outcoming.writeMsg({"command":"end"})
 

	
 
		log.info("closing session...")
 
		log.info(datetime.now(), "closing session...")
 
		dataFile.close()
src/config.py
Show inline comments
 
import datetime
 
import logging as log
 

	
 

	
 
log.basicConfig(level=log.INFO,format="%(asctime)s %(levelname)s: %(message)s",datefmt="%Y-%m-%d %H:%M:%S")
 
logName="/tmp/mor{0}.log".format(datetime.datetime.now().timestamp())
 
log.basicConfig(
 
	level=log.INFO,format="%(asctime)s %(levelname)s: %(message)s",datefmt="%Y-%m-%d %H:%M:%S",
 
	filename=logName
 
)
 

	
 
version=0
 

	
src/hashtree.py
Show inline comments
 
import hashlib
 
import os
 
from datetime import datetime
 

	
 
from util import progress
 

	
 
@@ -22,13 +23,14 @@ class HashTree:
 
			size=stat.st_size # !! symlinks
 
			leafCount=(size-1)//HashTree.BLOCK_SIZE+1 # number of leaf blocks
 
			res=cls(leafCount)
 
			print("hashing file:")
 
			print(datetime.now(), "hashing file:")
 

	
 
			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%")
 
		res.buildTree()
 
		
 
		return res
 
@@ -73,10 +75,11 @@ class HashTree:
 
			
 
	## Fast construction of the tree over the leaves. O(n).
 
	def buildTree(self):
 
		print("building tree:")
 
		print(datetime.now(), "building tree:")
 
		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()
 

	
 

	
 
if __name__=="__main__":
src/util.py
Show inline comments
 
@@ -6,8 +6,6 @@ def progress(i, n, i0=0):
 
		i*=-1
 
		n*=-1
 
		i0*=-1
 
	assert i0<=i<n
 
	assert i0<=i<n, (i0,i,n)
 
	percentage=_progress(i,n,i0)
 
	if percentage>_progress(i-1,n,i0):
 
		print(r"{0}%".format(percentage),end="")
 
		if percentage==100: print()
 
	print("{0}%".format(percentage),end="\r")
0 comments (0 inline, 0 general)