Files
@ fa1be84731e2
Branch filter:
Location: Morevna/src/server.py - annotation
fa1be84731e2
2.0 KiB
text/x-python
simplified networkers for dealing with a single request-response
59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 1c3d9df86fb1 1c3d9df86fb1 59339cfb3d80 59339cfb3d80 92b513293c88 92b513293c88 92b513293c88 59339cfb3d80 59339cfb3d80 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 fa1be84731e2 fa1be84731e2 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 92b513293c88 59339cfb3d80 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 8ad57a925d81 8ad57a925d81 59339cfb3d80 59339cfb3d80 1c3d9df86fb1 fa1be84731e2 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 fa1be84731e2 8ad57a925d81 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 92b513293c88 1c3d9df86fb1 59339cfb3d80 8ad57a925d81 59339cfb3d80 | import socket
from hashtree import HashTree
from networkers import NetworkReader,NetworkWriter
import collections
import sys
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()
print('Connected by', address)
fr=self.socket.makefile(mode="rb")
fw=self.socket.makefile(mode="wb")
self.incoming=NetworkReader(fr)
self.outcoming=NetworkWriter(fw)
def __enter__(self):
return self.incoming,self.outcoming
def __exit__(self, exc_type, exc_val, exc_tb):
self.socket.close()
localTree=HashTree.fromFile(open(filename,mode="rb"))
ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ss.bind(("",conf.port))
ss.listen(1)
blocksToTransfer=[]
nodeStack=collections.deque([0])
i1=-1
while True:
with Connection(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
elif jsonData["command"]=="req": # !! index out of range
print("received request for node #{0}".format(jsonData["index"]))
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
print("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)
i1=i2
# never update the hash tree
elif jsonData["command"]=="end":
print("closing...")
break
else: pass # !! error
dataFile.close()
sys.exit(0)
|