Files
@ 6f31f20feb06
Branch filter:
Location: Morevna/src/server.py - annotation
6f31f20feb06
2.0 KiB
text/x-python
client organized into functions
59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 59339cfb3d80 | import socket
from hashtree import HashTree
import queue
from networkers import NetworkReader,NetworkWriter
import collections
import sys
# debug copy default file
import shutil
shutil.copyfile("serverFile_.txt","serverFile.txt")
localTree=HashTree.fromFile(open("serverFile.txt",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("serverFile.txt",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)
|