Files
@ 1c3d9df86fb1
Branch filter:
Location: Morevna/src/server.py - annotation
1c3d9df86fb1
2.2 KiB
text/x-python
switched from persistent to transient sockets
59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 1c3d9df86fb1 1c3d9df86fb1 59339cfb3d80 59339cfb3d80 92b513293c88 92b513293c88 92b513293c88 59339cfb3d80 59339cfb3d80 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 92b513293c88 59339cfb3d80 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 8ad57a925d81 8ad57a925d81 59339cfb3d80 59339cfb3d80 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 1c3d9df86fb1 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")
networkReader=NetworkReader(fr)
networkReader.start()
networkWriter=NetworkWriter(fw)
networkWriter.start()
self.incoming=networkReader.output # synchronized message queue
self.outcoming=networkWriter.input
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.get(timeout=2)
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.put((jsonResponse,binResponse),timeout=2)
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)
|