Changeset - d72018278450
[Not reviewed]
default
0 3 1
Laman - 8 years ago 2017-05-07 17:38:09

nicer program entry point
4 files changed with 63 insertions and 15 deletions:
0 comments (0 inline, 0 general)
src/client.py
Show inline comments
 
@@ -29,7 +29,7 @@ class Connection:
 

	
 

	
 
def negotiate():
 
	localTree=HashTree.fromFile(open(filename,mode="rb"))
 
	localTree=HashTree.fromFile(filename)
 
	blocksToTransfer=[]
 
	nodeStack=collections.deque([0]) # root
 

	
src/hashtree.py
Show inline comments
 
@@ -14,15 +14,16 @@ class HashTree:
 
		self.leafCount=leafCount
 
		
 
	@classmethod
 
	def fromFile(cls,fd):
 
		stat=os.fstat(fd.fileno())
 
		size=stat.st_size # !! symlinks
 
		leafCount=(size-1)//HashTree.BLOCK_SIZE+1 # number of leaf blocks
 
		res=cls(leafCount)
 
		
 
		for i in range(leafCount):
 
			data=fd.read(HashTree.BLOCK_SIZE)
 
			res.insertLeaf(hashlib.sha256(data).digest()[HashTree.HASH_LEN:])
 
	def fromFile(cls,filename):
 
		with open(filename,"rb") as f:
 
			stat=os.fstat(f.fileno())
 
			size=stat.st_size # !! symlinks
 
			leafCount=(size-1)//HashTree.BLOCK_SIZE+1 # number of leaf blocks
 
			res=cls(leafCount)
 

	
 
			for i in range(leafCount):
 
				data=f.read(HashTree.BLOCK_SIZE)
 
				res.insertLeaf(hashlib.sha256(data).digest()[HashTree.HASH_LEN:])
 
		res.buildTree()
 
		
 
		return res
src/morevna.py
Show inline comments
 
new file 100644
 
import sys
 
import os.path
 
from argparse import ArgumentParser
 

	
 
from hashtree import HashTree
 

	
 

	
 
def buildTree(args):
 
	if not os.path.isfile(args.datafile):
 
		print("invalid file specified:",args.datafile,file=sys.stderr)
 
		return
 
	tree=HashTree.fromFile(args.datafile)
 
	tree.save(args.treefile)
 

	
 
def update(args):
 
	print("ready to update")
 
	print(args)
 

	
 
def serve(args):
 
	print("ready to serve")
 
	print(args)
 

	
 
parser=ArgumentParser()
 
subparsers=parser.add_subparsers()
 

	
 
pRebuild=subparsers.add_parser("build")
 
pRebuild.add_argument("treefile",help="stored hash tree location")
 
pRebuild.add_argument("datafile")
 
pRebuild.set_defaults(func=buildTree)
 

	
 
pUpdate=subparsers.add_parser("update")
 
pUpdate.add_argument("-p","--port",type=int)
 
pUpdate.add_argument("--host",default="127.0.0.1")
 
pUpdate.add_argument("-t","--tree",help="stored hash tree location")
 
pUpdate.add_argument("datafile")
 
pUpdate.set_defaults(func=update)
 

	
 
pServe=subparsers.add_parser("serve")
 
pServe.add_argument("-p","--port",type=int)
 
pServe.add_argument("--host",default="")
 
pServe.add_argument("-t","--tree",help="stored hash tree location")
 
pServe.add_argument("datafile")
 
pServe.set_defaults(func=serve)
 

	
 
args=parser.parse_args()
 
args.func(args)
src/server.py
Show inline comments
 
@@ -32,7 +32,7 @@ class Connection:
 
		self.socket.close()
 

	
 

	
 
localTree=HashTree.fromFile(open(filename,mode="rb"))
 
localTree=HashTree.fromFile(filename)
 

	
 
ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
ss.bind(("",conf.port))
 
@@ -53,8 +53,9 @@ while True:
 
			assert jsonData["blockSize"]==localTree.BLOCK_SIZE
 
			assert jsonData["blockCount"]==localTree.leafCount
 

	
 
		elif jsonData["command"]=="req": # !! index out of range
 
		elif jsonData["command"]=="req":
 
			log.info("received request for node #{0}".format(jsonData["index"]))
 
			assert jsonData["index"]<len(localTree.store)
 
			nodeHash=localTree.store[jsonData["index"]]
 

	
 
			jsonResponse={"command":"send", "index":jsonData["index"], "dataType":"hash"}
 
@@ -62,7 +63,7 @@ while True:
 

	
 
			outcoming.writeMsg(jsonResponse,binResponse)
 

	
 
		elif jsonData["command"]=="send" and jsonData["dataType"]=="data": # needlessly allow hashes and data in mixed order
 
		elif jsonData["command"]=="send" and jsonData["dataType"]=="data":
 
			log.info("received data block #{0}: {1}...{2}".format(jsonData["index"],binData[:5],binData[-5:]))
 

	
 
			i2=jsonData["index"]
 
@@ -77,7 +78,7 @@ while True:
 
			log.info("closing session...")
 
			break
 
	
 
		else: pass # !! error
 
		else:
 
			assert False, jsonData["command"]
 

	
 
dataFile.close()
 
sys.exit(0)
0 comments (0 inline, 0 general)