Files
@ 5f1426df68a7
Branch filter:
Location: Morevna/src/server.py - annotation
5f1426df68a7
2.0 KiB
text/x-python
deleted obsolete file
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 59339cfb3d80 59339cfb3d80 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 92b513293c88 59339cfb3d80 59339cfb3d80 59339cfb3d80 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
while True:
jsonData,binData=incoming.get(timeout=2)
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:]))
dataFile=open(filename,mode="rb+")
dataFile.seek(jsonData["index"]*localTree.BLOCK_SIZE)
dataFile.write(binData)
dataFile.close()
# never update the hash tree
elif jsonData["command"]=="end":
print("closing...")
break
else: pass # !! error
# fr.close()
# fw.close()
conn.close()
sys.exit(0)
|