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 74 insertions and 61 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,8 +25,12 @@ class Connection:
 
		self.socket.close()
 

	
 

	
 
def negotiate():
 
	localTree=HashTree.fromFile(filename)
 
class Client:
 
	def __init__(self,filename):
 
		self.filename=filename
 

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

	
 
@@ -56,10 +57,9 @@ def negotiate():
 

	
 
	return blocksToTransfer
 

	
 

	
 
def sendData(blocksToTransfer):
 
	def sendData(self,blocksToTransfer):
 
	log.info(blocksToTransfer)
 
	dataFile=open(filename,mode="rb")
 
		dataFile=open(self.filename,mode="rb")
 
	i1=-1
 

	
 
	for i2 in blocksToTransfer:
 
@@ -79,10 +79,3 @@ def sendData(blocksToTransfer):
 

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

	
 

	
 
if __name__=="__main__":
 
	blocksToTransfer=negotiate()
 
	sendData(blocksToTransfer)
 

	
 
	sys.exit(0)
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])
 

	
 
i1=-1
 
		self._lastWrite=-1
 

	
 
	def serve(self):
 
		while self._serveOne():
 
			pass
 

	
 
while True:
 
	with Connection(ss) as (incoming,outcoming):
 
	def _serveOne(self):
 
		with Connection(self.ss) as (incoming,outcoming):
 
		jsonData,binData=incoming.readMsg()
 
		dataFile=open(filename,mode="rb+")
 

	
 
		if jsonData["command"]=="init":
 
			assert jsonData["blockSize"]==localTree.BLOCK_SIZE
 
			assert jsonData["blockCount"]==localTree.leafCount
 
				assert jsonData["blockSize"]==self.BLOCK_SIZE
 
				assert jsonData["blockCount"]==self.tree.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"]=="end":
 
				log.info("closing session...")
 
				return False
 

	
 
			else:
 
				assert False, jsonData["command"]
 

	
 
			return True
 

	
 
	def _requestHash(self,jsonData):
 
			log.info("received request for node #{0}".format(jsonData["index"]))
 
			assert jsonData["index"]<len(localTree.store)
 
			nodeHash=localTree.store[jsonData["index"]]
 
		assert jsonData["index"]<len(self.tree.store)
 
		nodeHash=self.tree.store[jsonData["index"]]
 

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

	
 
			outcoming.writeMsg(jsonResponse,binResponse)
 
		return (jsonResponse,binResponse)
 

	
 
		elif jsonData["command"]=="send" and jsonData["dataType"]=="data":
 
	def _receiveData(self,jsonData,binData):
 
			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)
 
		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)
 
			i1=i2
 
		self._lastWrite=i
 

	
 
			# never update the hash tree
 

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

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