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
 
@@ -26,13 +26,13 @@ class Connection:
 

	
 
	def __exit__(self, exc_type, exc_val, exc_tb):
 
		self.socket.close()
 

	
 

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

	
 
	# initialize session
 
	with Connection() as (incoming,outcoming):
 
		jsonData={"command":"init", "blockSize":localTree.BLOCK_SIZE, "blockCount":localTree.leafCount, "version":conf.version}
src/hashtree.py
Show inline comments
 
@@ -11,21 +11,22 @@ class HashTree:
 
		self.store=[b""]*(leafCount*2-1)
 
		self.leafStart=leafCount-1
 
		self.index=self.leafStart
 
		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
 

	
 
	@classmethod
 
	def load(cls,filename):
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
 
@@ -29,13 +29,13 @@ class Connection:
 
		return self.incoming,self.outcoming
 

	
 
	def __exit__(self, exc_type, exc_val, exc_tb):
 
		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))
 
ss.listen(1)
 

	
 
blocksToTransfer=[]
 
@@ -50,22 +50,23 @@ while True:
 
		dataFile=open(filename,mode="rb+")
 

	
 
		if jsonData["command"]=="init":
 
			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"}
 
			binResponse=nodeHash
 

	
 
			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"]
 
			if i1+1!=i2:
 
				dataFile.seek(i2*localTree.BLOCK_SIZE)
 
			dataFile.write(binData)
 
@@ -74,10 +75,10 @@ while True:
 
			# never update the hash tree
 

	
 
		elif jsonData["command"]=="end":
 
			log.info("closing session...")
 
			break
 
	
 
		else: pass # !! error
 
		else:
 
			assert False, jsonData["command"]
 

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