diff --git a/.hgignore b/.hgignore --- a/.hgignore +++ b/.hgignore @@ -1,4 +1,4 @@ /__pycache__/ ^\..* ^certs/ -^config.json +^config.json$ diff --git a/config.json b/config.json --- a/config.json +++ b/config.json @@ -1,5 +1,5 @@ { "hosts": ["127.0.0.1"], - "port": 9021, + "port": 9901, "batchSize": 256 } diff --git a/src/client.py b/src/client.py --- a/src/client.py +++ b/src/client.py @@ -1,4 +1,3 @@ -import sys import collections import socket import ssl @@ -9,7 +8,7 @@ import config as conf import stats from util import Progress from hashtree import HashTree,hashBlock -from netnode import BaseConnection,NetNode +from netnode import BaseConnection,NetNode,FailedConnection class Connection(BaseConnection): @@ -22,13 +21,20 @@ class Connection(BaseConnection): sslContext.load_cert_chain(conf.certfile,conf.keyfile) self._socket=sslContext.wrap_socket(sock) + try: self._socket.connect((host,port)) - except ConnectionRefusedError: - print("Couldn't connect to {0}:{1}".format(conf.hosts[0],conf.port)) - sys.exit(1) + except ConnectionRefusedError as e: + log.exception(e) + print("Couldn't connect to {0}:{1}".format(host,port)) + raise FailedConnection() + except ssl.SSLError as e: + log.exception(e) + print("Error creating SSL connection to {0}:{1}".format(host,port)) + raise FailedConnection() self.createNetworkers() + print("Connected to {0}".format(host)) class Client(NetNode): diff --git a/src/morevna.py b/src/morevna.py --- a/src/morevna.py +++ b/src/morevna.py @@ -7,7 +7,7 @@ from util import spawnDaemon, splitHost import config as conf import stats from hashtree import HashTree -from client import Client, Connection as ClientConnection +from client import Client, Connection as ClientConnection, FailedConnection from server import Miniserver @@ -37,13 +37,16 @@ def push(args): if args.port: conf.port=args.port c=Client(args.datafile,args.tree) - 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()) + for h in conf.hosts: + host=splitHost(h,conf.port) + try: + with ClientConnection(*host) as con: + c.setConnection(con) + blocksToTransfer=c.negotiate() + c.sendData(blocksToTransfer) + print() + print(stats.report()) + except FailedConnection: continue def pull(args): _checkFile(args.datafile) @@ -53,12 +56,15 @@ def pull(args): if args.port: conf.port=args.port c=Client(args.datafile,args.tree) - with ClientConnection(*splitHost(conf.hosts[0],conf.port)) as con: - c.setConnection(con) - blocksToTransfer=c.negotiate() - c.pullData(blocksToTransfer) - print() - print(stats.report()) + host=splitHost(conf.hosts[0],conf.port) + try: + with ClientConnection(*host) as con: + c.setConnection(con) + blocksToTransfer=c.negotiate() + c.pullData(blocksToTransfer) + print() + print(stats.report()) + except FailedConnection: pass def serve(args): _checkFile(args.datafile) @@ -92,7 +98,7 @@ pUpdate.set_defaults(func=push) pUpdate=subparsers.add_parser("pull") pUpdate.add_argument("-p","--port",type=int) -pUpdate.add_argument("--host",default="127.0.0.1") +pUpdate.add_argument("--host") pUpdate.add_argument("-t","--tree",help="stored hash tree location") pUpdate.add_argument("datafile") pUpdate.set_defaults(func=pull) diff --git a/src/netnode.py b/src/netnode.py --- a/src/netnode.py +++ b/src/netnode.py @@ -5,6 +5,9 @@ from networkers import NetworkReader,Net from hashtree import HashTree +class FailedConnection(Exception): pass + + class BaseConnection: # abstract def __init__(self): self._socket=None