Changeset - b73a5d69a11b
[Not reviewed]
default
0 3 0
Laman - 8 years ago 2017-05-07 23:58:18

wrapped client and server into objects
3 files changed with 114 insertions and 101 deletions:
0 comments (0 inline, 0 general)
src/client.py
Show inline comments
 
@@ -8,9 +8,6 @@ import config as conf
 
from networkers import NetworkReader,NetworkWriter
 

	
 

	
 
filename=sys.argv[1]
 

	
 

	
 
class Connection:
 
	def __init__(self):
 
		self.socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
 
@@ -28,61 +25,57 @@ class Connection:
 
		self.socket.close()
 

	
 

	
 
def negotiate():
 
	localTree=HashTree.fromFile(filename)
 
	blocksToTransfer=[]
 
	nodeStack=collections.deque([0]) # root
 
class Client:
 
	def __init__(self,filename):
 
		self.filename=filename
 

	
 
	# initialize session
 
	with Connection() as (incoming,outcoming):
 
		jsonData={"command":"init", "blockSize":localTree.BLOCK_SIZE, "blockCount":localTree.leafCount, "version":conf.version}
 
		outcoming.writeMsg(jsonData)
 
	def negotiate(self):
 
		localTree=HashTree.fromFile(self.filename)
 
		blocksToTransfer=[]
 
		nodeStack=collections.deque([0]) # root
 

	
 
	# determine which blocks to send
 
	while len(nodeStack)>0:
 
		# initialize session
 
		with Connection() as (incoming,outcoming):
 
			i=nodeStack.pop()
 
			outcoming.writeMsg({"command":"req", "index":i})
 

	
 
			jsonData,binData=incoming.readMsg()
 
			assert jsonData["index"]==i
 
			assert jsonData["dataType"]=="hash"
 
			jsonData={"command":"init", "blockSize":localTree.BLOCK_SIZE, "blockCount":localTree.leafCount, "version":conf.version}
 
			outcoming.writeMsg(jsonData)
 

	
 
			if localTree.store[i]!=binData:
 
				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
 
		# determine which blocks to send
 
		while len(nodeStack)>0:
 
			with Connection() as (incoming,outcoming):
 
				i=nodeStack.pop()
 
				outcoming.writeMsg({"command":"req", "index":i})
 

	
 
	return blocksToTransfer
 

	
 
				jsonData,binData=incoming.readMsg()
 
				assert jsonData["index"]==i
 
				assert jsonData["dataType"]=="hash"
 

	
 
def sendData(blocksToTransfer):
 
	log.info(blocksToTransfer)
 
	dataFile=open(filename,mode="rb")
 
	i1=-1
 
				if localTree.store[i]!=binData:
 
					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
 

	
 
	for i2 in blocksToTransfer:
 
		with Connection() as (incoming,outcoming):
 
			jsonData={"command":"send", "index":i2, "dataType":"data"}
 
			if i1+1!=i2:
 
				dataFile.seek(i2*HashTree.BLOCK_SIZE)
 
			binData=dataFile.read(HashTree.BLOCK_SIZE)
 
		return blocksToTransfer
 

	
 
			log.info("block #{0}: {1}...{2}".format(i2,binData[:5],binData[-5:]))
 
	def sendData(self,blocksToTransfer):
 
		log.info(blocksToTransfer)
 
		dataFile=open(self.filename,mode="rb")
 
		i1=-1
 

	
 
			outcoming.writeMsg(jsonData,binData)
 
			i1=i2
 
		for i2 in blocksToTransfer:
 
			with Connection() as (incoming,outcoming):
 
				jsonData={"command":"send", "index":i2, "dataType":"data"}
 
				if i1+1!=i2:
 
					dataFile.seek(i2*HashTree.BLOCK_SIZE)
 
				binData=dataFile.read(HashTree.BLOCK_SIZE)
 

	
 
	with Connection() as (incoming,outcoming):
 
		outcoming.writeMsg({"command":"end"})
 
				log.info("block #{0}: {1}...{2}".format(i2,binData[:5],binData[-5:]))
 

	
 
	log.info("closing session...")
 
	dataFile.close()
 

	
 
				outcoming.writeMsg(jsonData,binData)
 
				i1=i2
 

	
 
if __name__=="__main__":
 
	blocksToTransfer=negotiate()
 
	sendData(blocksToTransfer)
 
		with Connection() as (incoming,outcoming):
 
			outcoming.writeMsg({"command":"end"})
 

	
 
	sys.exit(0)
 
		log.info("closing session...")
 
		dataFile.close()
src/morevna.py
Show inline comments
 
@@ -3,22 +3,41 @@ import os.path
 
from argparse import ArgumentParser
 

	
 
from hashtree import HashTree
 
from client import Client
 
from server import Server
 

	
 

	
 
def _checkDatafile(datafile):
 
	if not os.path.isfile(datafile):
 
		print("invalid file specified:",args.datafile,file=sys.stderr)
 
		sys.exit(1)
 

	
 

	
 
def buildTree(args):
 
	if not os.path.isfile(args.datafile):
 
		print("invalid file specified:",args.datafile,file=sys.stderr)
 
		return
 
	_checkDatafile(args.datafile)
 

	
 
	tree=HashTree.fromFile(args.datafile)
 
	tree.save(args.treefile)
 

	
 
def update(args):
 
	print("ready to update")
 
	print(args)
 
	_checkDatafile(args.datafile)
 

	
 
	c=Client(args.datafile)
 
	blocksToTransfer=c.negotiate()
 
	c.sendData(blocksToTransfer)
 

	
 
def serve(args):
 
	print("ready to serve")
 
	print(args)
 
	_checkDatafile(args.datafile)
 

	
 
	# debug copy default file
 
	import shutil
 
	origFilename=args.datafile
 
	filename=origFilename+"_"
 
	shutil.copyfile(origFilename,filename)
 

	
 
	s=Server(filename)
 
	s.serve()
 

	
 

	
 
parser=ArgumentParser()
 
subparsers=parser.add_subparsers()
src/server.py
Show inline comments
 
import socket
 
from hashtree import HashTree
 
from networkers import NetworkReader,NetworkWriter
 
import collections
 
import sys
 
import logging as log
 

	
 
import config as conf
 

	
 

	
 
# debug copy default file
 
import shutil
 
origFilename=sys.argv[1]
 
filename=origFilename+"_"
 
shutil.copyfile(origFilename,filename)
 

	
 

	
 
class Connection:
 
	def __init__(self,server_socket):
 
		self.socket, address = server_socket.accept()
 
@@ -32,53 +23,63 @@ class Connection:
 
		self.socket.close()
 

	
 

	
 
localTree=HashTree.fromFile(filename)
 
class Server:
 
	def __init__(self,filename):
 
		self.filename=filename
 
		self.tree=HashTree.fromFile(filename)
 
		self.BLOCK_SIZE=self.tree.BLOCK_SIZE
 

	
 
ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
ss.bind(("",conf.port))
 
ss.listen(1)
 
		self.ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
		self.ss.bind(("",conf.port))
 
		self.ss.listen(1)
 

	
 
blocksToTransfer=[]
 
nodeStack=collections.deque([0])
 
		self._lastWrite=-1
 

	
 
i1=-1
 
	def serve(self):
 
		while self._serveOne():
 
			pass
 

	
 
	def _serveOne(self):
 
		with Connection(self.ss) as (incoming,outcoming):
 
			jsonData,binData=incoming.readMsg()
 

	
 
while True:
 
	with Connection(ss) as (incoming,outcoming):
 
		jsonData,binData=incoming.readMsg()
 
		dataFile=open(filename,mode="rb+")
 
			if jsonData["command"]=="init":
 
				assert jsonData["blockSize"]==self.BLOCK_SIZE
 
				assert jsonData["blockCount"]==self.tree.leafCount
 

	
 
		if jsonData["command"]=="init":
 
			assert jsonData["blockSize"]==localTree.BLOCK_SIZE
 
			assert jsonData["blockCount"]==localTree.leafCount
 
			elif jsonData["command"]=="req":
 
				outcoming.writeMsg(*self._requestHash(jsonData))
 

	
 
			elif jsonData["command"]=="send" and jsonData["dataType"]=="data":
 
				self._receiveData(jsonData,binData)
 

	
 
		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"]]
 
			elif jsonData["command"]=="end":
 
				log.info("closing session...")
 
				return False
 

	
 
			else:
 
				assert False, jsonData["command"]
 

	
 
			jsonResponse={"command":"send", "index":jsonData["index"], "dataType":"hash"}
 
			binResponse=nodeHash
 
			return True
 

	
 
			outcoming.writeMsg(jsonResponse,binResponse)
 

	
 
		elif jsonData["command"]=="send" and jsonData["dataType"]=="data":
 
			log.info("received data block #{0}: {1}...{2}".format(jsonData["index"],binData[:5],binData[-5:]))
 
	def _requestHash(self,jsonData):
 
		log.info("received request for node #{0}".format(jsonData["index"]))
 
		assert jsonData["index"]<len(self.tree.store)
 
		nodeHash=self.tree.store[jsonData["index"]]
 

	
 
			i2=jsonData["index"]
 
			if i1+1!=i2:
 
				dataFile.seek(i2*localTree.BLOCK_SIZE)
 
			dataFile.write(binData)
 
			i1=i2
 
		jsonResponse={"command":"send", "index":jsonData["index"], "dataType":"hash"}
 
		binResponse=nodeHash
 

	
 
			# never update the hash tree
 
		return (jsonResponse,binResponse)
 

	
 
	def _receiveData(self,jsonData,binData):
 
		log.info("received data block #{0}: {1}...{2}".format(jsonData["index"],binData[:5],binData[-5:]))
 

	
 
		elif jsonData["command"]=="end":
 
			log.info("closing session...")
 
			break
 
	
 
		else:
 
			assert False, jsonData["command"]
 
		i=jsonData["index"]
 
		with open(self.filename,mode="rb+") as dataFile:
 
			if self._lastWrite+1!=i:
 
				dataFile.seek(i*self.BLOCK_SIZE)
 
			dataFile.write(binData)
 
		self._lastWrite=i
 

	
 
dataFile.close()
 
		# never update the hash tree
0 comments (0 inline, 0 general)