Files
@ 8ad57a925d81
Branch filter:
Location: Morevna/src/server.py - annotation
8ad57a925d81
2.0 KiB
text/x-python
optimized some unnecessary disk seeks
59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 92b513293c88 92b513293c88 92b513293c88 59339cfb3d80 59339cfb3d80 92b513293c88 59339cfb3d80 92b513293c88 92b513293c88 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 8ad57a925d81 8ad57a925d81 59339cfb3d80 59339cfb3d80 92b513293c88 8ad57a925d81 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 8ad57a925d81 8ad57a925d81 8ad57a925d81 8ad57a925d81 92b513293c88 8ad57a925d81 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 59339cfb3d80 59339cfb3d80 59339cfb3d80 8ad57a925d81 59339cfb3d80 59339cfb3d80 | import socket
from hashtree import HashTree
from networkers import NetworkReader,NetworkWriter
import collections
import sys
# debug copy default file
import shutil
origFilename=sys.argv[1]
filename=origFilename+"_"
shutil.copyfile(origFilename,filename)
localTree=HashTree.fromFile(open(filename,mode="rb"))
HOST = '' # Symbolic name meaning all available interfaces
PORT = 50009 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print('Connected by', addr)
fr=conn.makefile(mode="rb")
fw=conn.makefile(mode="wb")
networkReader=NetworkReader(fr)
networkReader.start()
networkWriter=NetworkWriter(fw)
networkWriter.start()
blocksToTransfer=[]
nodeStack=collections.deque([0])
incoming=networkReader.output # synchronized message queue
outcoming=networkWriter.input
i1=-1
while True:
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
# fr.close()
# fw.close()
dataFile.close()
conn.close()
sys.exit(0)
|