Changeset - 92b513293c88
[Not reviewed]
default
0 3 0
Laman - 8 years ago 2017-05-04 22:56:48

reading filename from command line, fixed indentation
3 files changed with 43 insertions and 41 deletions:
0 comments (0 inline, 0 general)
src/client.py
Show inline comments
 
@@ -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"}
src/config.py
Show inline comments
 
version=0
 

	
 
hosts=["10.0.0.33"]
 
hosts=["127.0.0.1"]
 
port=50009
src/server.py
Show inline comments
 
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()
0 comments (0 inline, 0 general)