diff --git a/src/client.py b/src/client.py --- a/src/client.py +++ b/src/client.py @@ -7,11 +7,12 @@ import config as conf from networkers import NetworkReader,NetworkWriter +filename=sys.argv[1] + + def connect(): - HOST = conf.hosts[0] # The remote host - PORT = conf.port # The same port as used by the server s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.connect((HOST, PORT)) + s.connect((conf.hosts[0], conf.port)) fr=s.makefile(mode='rb') fw=s.makefile(mode='wb') @@ -27,7 +28,7 @@ def connect(): def negotiate(incoming,outcoming): - localTree=HashTree.fromFile(open("clientFile.txt",mode="rb")) + localTree=HashTree.fromFile(open(filename,mode="rb")) blocksToTransfer=[] nodeStack=collections.deque([0]) # root @@ -56,7 +57,7 @@ def negotiate(incoming,outcoming): def sendData(outcoming,blocksToTransfer): print(blocksToTransfer) - dataFile=open("clientFile.txt",mode="rb") + dataFile=open(filename,mode="rb") for i in blocksToTransfer: jsonData={"command":"send", "index":i, "dataType":"data"} diff --git a/src/config.py b/src/config.py --- a/src/config.py +++ b/src/config.py @@ -1,4 +1,4 @@ version=0 -hosts=["10.0.0.33"] +hosts=["127.0.0.1"] port=50009 diff --git a/src/server.py b/src/server.py --- a/src/server.py +++ b/src/server.py @@ -1,19 +1,20 @@ 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") +origFilename=sys.argv[1] +filename=origFilename+"_" +shutil.copyfile(origFilename,filename) -localTree=HashTree.fromFile(open("serverFile.txt",mode="rb")) +localTree=HashTree.fromFile(open(filename,mode="rb")) -HOST = '' # Symbolic name meaning all available interfaces -PORT = 50009 # Arbitrary non-privileged port +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) @@ -34,36 +35,36 @@ 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 + 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()