# HG changeset patch # User Laman # Date 2017-10-31 19:45:36 # Node ID 9f2b0a4f3538dcc90633e26ee0ae65644a20ce88 # Parent 09590815939340a512449f156768ceed3d13aed6 push to multiple servers diff --git a/src/client.py b/src/client.py --- a/src/client.py +++ b/src/client.py @@ -13,7 +13,7 @@ from netnode import BaseConnection,NetNo class Connection(BaseConnection): - def __init__(self): + def __init__(self,host,port): super().__init__() sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -23,7 +23,7 @@ class Connection(BaseConnection): self._socket=sslContext.wrap_socket(sock) try: - self._socket.connect((conf.hosts[0], conf.port)) + self._socket.connect((host,port)) except ConnectionRefusedError: print("Couldn't connect to {0}:{1}".format(conf.hosts[0],conf.port)) sys.exit(1) diff --git a/src/morevna.py b/src/morevna.py --- a/src/morevna.py +++ b/src/morevna.py @@ -3,7 +3,7 @@ import os.path import logging as log from argparse import ArgumentParser -from util import spawnDaemon +from util import spawnDaemon, splitHost import config as conf import stats from hashtree import HashTree @@ -33,26 +33,27 @@ def push(args): _checkFile(args.datafile) if args.tree: _checkFile(args.tree) - if args.host: conf.hosts.insert(0,args.host) + if args.host: conf.hosts=[args.host] if args.port: conf.port=args.port c=Client(args.datafile,args.tree) - with ClientConnection() as con: - c.setConnection(con) - blocksToTransfer=c.negotiate() - c.sendData(blocksToTransfer) - print() - print(stats.report()) + for host in conf.hosts: + with ClientConnection(*splitHost(host,conf.port)) as con: + c.setConnection(con) + blocksToTransfer=c.negotiate() + c.sendData(blocksToTransfer) + print() + print(stats.report()) def pull(args): _checkFile(args.datafile) if args.tree: _checkFile(args.tree) - if args.host: conf.hosts.insert(0,args.host) + if args.host: conf.hosts=[args.host] if args.port: conf.port=args.port c=Client(args.datafile,args.tree) - with ClientConnection() as con: + with ClientConnection(*splitHost(conf.hosts[0],conf.port)) as con: c.setConnection(con) blocksToTransfer=c.negotiate() c.pullData(blocksToTransfer) diff --git a/src/netnode.py b/src/netnode.py --- a/src/netnode.py +++ b/src/netnode.py @@ -26,7 +26,7 @@ class BaseConnection: # abstract self._socket.shutdown(socket.SHUT_RDWR) self._socket.close() except OSError: - log.warning("encountered an error when shutting down the connection") + log.warning("broken connection") class NetNode: diff --git a/src/server.py b/src/server.py --- a/src/server.py +++ b/src/server.py @@ -64,7 +64,7 @@ class Server(NetNode): def serve(self): try: while self._serveOne(): pass - except AssertionError as e: + except (AssertionError,ConnectionResetError) as e: log.warning(e) def _serveOne(self): diff --git a/src/tests/test_overall.py b/src/tests/test_overall.py --- a/src/tests/test_overall.py +++ b/src/tests/test_overall.py @@ -68,7 +68,7 @@ class TestMorevna(TestCase): for clientFile in ("test2.img","test3.img","test4.img"): clientFile=os.path.join(dataDir,clientFile) c=Client(clientFile) - with ClientConnection() as con: + with ClientConnection("127.0.0.1",config.port) as con: c.setConnection(con) blocksToTransfer=c.negotiate() c.sendData(blocksToTransfer) @@ -85,7 +85,7 @@ class TestMorevna(TestCase): p.start() c=Client(filename) - with ClientConnection() as con: + with ClientConnection("127.0.0.1",config.port) as con: c.setConnection(con) blocksToTransfer=c.negotiate() c.pullData(blocksToTransfer) diff --git a/src/util.py b/src/util.py --- a/src/util.py +++ b/src/util.py @@ -33,6 +33,12 @@ def spawnDaemon(fun): os._exit(os.EX_OK) +def splitHost(host,defaultPort=0): + address,_,port=host.partition(":") + if not port: port=defaultPort + return (address,port) + + class Progress: def __init__(self,n,i0=0): self._n=n