Changeset - dad65188b1a0
[Not reviewed]
default
0 3 0
Laman - 8 years ago 2017-06-05 23:59:32

from transient back to persistent sockets
3 files changed with 22 insertions and 10 deletions:
0 comments (0 inline, 0 general)
src/client.py
Show inline comments
 
@@ -37,17 +37,18 @@ class Client:
 
		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}
 
			outcoming.writeMsg(jsonData)
 
			jsonData,binData=incoming.readMsg()
 
			assert jsonData["command"]=="ack"
 

	
 
		# determine which blocks to send
 
		print(datetime.now(), "negotiating:")
 
		while len(nodeStack)>0:
 
			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"
 
@@ -66,25 +67,27 @@ class Client:
 
	def sendData(self,blocksToTransfer):
 
		log.info(blocksToTransfer)
 
		dataFile=open(self.filename,mode="rb")
 
		i1=-1
 

	
 
		print(datetime.now(), "sending data:")
 
		with Connection() as (incoming,outcoming):
 
		for (k,i2) in enumerate(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)
 

	
 
				log.info("block #{0}: {1}...{2}".format(i2,binData[:5],binData[-5:]))
 

	
 
				outcoming.writeMsg(jsonData,binData)
 
				jsonData,binData=incoming.readMsg()
 
				assert jsonData["command"]=="ack" and jsonData["index"]==i2, jsonData
 
			i1=i2
 
			progress(k,len(blocksToTransfer))
 
		print("100%")
 

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

	
 
		log.info(datetime.now(), "closing session...")
 
		log.info("closing session...")
 
		dataFile.close()
src/networkers.py
Show inline comments
 
@@ -4,20 +4,22 @@
 
class NetworkReader:
 
	def __init__(self,stream):
 
		self.stream=stream
 

	
 
	def readMsg(self):
 
		data=self.stream.readline()
 
		if not data: pass # !! raise something
 
		assert data
 
		jsonLength=int(data.split(b":")[1].strip()) # "json-length: length" -> length
 
		data=self.stream.readline()
 
		if not data: pass # !! raise something
 
		assert data
 
		binLength=int(data.split(b":")[1].strip()) # "bin-length: length" -> length
 
		jsonData=self.stream.read(jsonLength)
 
		assert len(jsonData)==jsonLength
 
		jsonData=json.loads(str(jsonData,encoding="utf-8"))
 
		binData=self.stream.read(binLength)
 
		assert len(binData)==binLength
 
		
 
		return (jsonData,binData)
 
		
 

	
 
class NetworkWriter:
 
	def __init__(self,stream):
 
@@ -28,7 +30,8 @@ class NetworkWriter:
 
		self.stream.flush()
 

	
 
	def prepMsg(self,jsonData,binData=b""):
 
		jsonData=bytes(json.dumps(jsonData)+"\n",encoding="utf-8")
 
		jsonLength=bytes("json-length: "+str(len(jsonData))+"\n",encoding="utf-8")
 
		binLength=bytes("bin-length: "+str(len(binData))+"\n",encoding="utf-8")
 

	
 
		return b"".join((jsonLength,binLength,jsonData,binData))
src/server.py
Show inline comments
 
@@ -39,28 +39,33 @@ class Server:
 
		self.ss.listen(1)
 

	
 
		self._lastWrite=-1
 
		self.dataFile=None
 

	
 
	def serve(self):
 
		while self._serveOne():
 
			pass
 
		while True:
 
			with Connection(self.ss) as (incoming,outcoming):
 
				try:
 
					while True:
 
						if not self._serveOne(incoming,outcoming): return
 
				except AssertionError:
 
					continue
 

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

	
 
			if jsonData["command"]=="init":
 
				assert jsonData["blockSize"]==self.BLOCK_SIZE
 
				assert jsonData["blockCount"]==self.tree.leafCount
 
			outcoming.writeMsg({"command": "ack"})
 

	
 
			elif jsonData["command"]=="req":
 
				outcoming.writeMsg(*self._requestHash(jsonData))
 

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

	
 
			elif jsonData["command"]=="end":
 
				log.info("closing session...")
 
				if self.dataFile:
 
					self.dataFile.close()
 
				return False
 
@@ -88,7 +93,8 @@ class Server:
 
		i=jsonData["index"]
 
		if self._lastWrite+1!=i:
 
			self.dataFile.seek(i*self.BLOCK_SIZE)
 
		self.dataFile.write(binData)
 
		self._lastWrite=i
 

	
 
		return ({"command": "ack", "index": i},)
 
		# never update the hash tree
0 comments (0 inline, 0 general)