From 8832c5a890975a2959480dfc8b581a33d131bf7a Mon Sep 17 00:00:00 2001 From: Benedikt Kristinsson Date: Fri, 29 Dec 2017 02:49:49 +0100 Subject: [PATCH] main() logic moved to ncpoc.py file --- ncpoc.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ network.py | 30 +----------------------------- 2 files changed, 45 insertions(+), 29 deletions(-) create mode 100644 ncpoc.py diff --git a/ncpoc.py b/ncpoc.py new file mode 100644 index 0000000..77ecbe9 --- /dev/null +++ b/ncpoc.py @@ -0,0 +1,44 @@ + +import sys +import argparse +from datetime import datetime + +from twisted.internet import reactor +from twisted.internet.endpoints import TCP4ServerEndpoint, TCP4ClientEndpoint +from twisted.internet.error import CannotListenError +from twisted.internet.endpoints import connectProtocol + +import network +from network import NCFactory, NCProtocol + +def _print(*args): + # double, make common module + time = datetime.now().time().isoformat()[:8] + print time, + print " ".join(map(str, args)) + + +if __name__ == "__main__": + if len(sys.argv) == 2: + port = int(sys.argv[1]) + else: + port = 5005 + try: + endpoint = TCP4ServerEndpoint(reactor, port, interface="151.217.219.153") + _print(" [ ] LISTEN:", port) + ncfactory = NCFactory() + endpoint.listen(ncfactory) + except CannotListenError: + _print("[!] Address in use") + sys.exit(1) + + + # connect to bootstrap addresses + _print(" [ ] Trying to connect to bootstrap hosts:") + for bootstrap in network.BOOTSTRAP_NODES: + _print(" [*]", bootstrap) + host, port = bootstrap.split(":") + point = TCP4ClientEndpoint(reactor, host, int(port)) + d = connectProtocol(point, NCProtocol(ncfactory, "SENDHELLO", "SPEAKER")) + d.addCallback(network.gotProtocol) + reactor.run() diff --git a/network.py b/network.py index e9084cd..ec97523 100644 --- a/network.py +++ b/network.py @@ -1,13 +1,11 @@ -import sys from datetime import datetime from time import time from functools import partial from twisted.internet import reactor from twisted.internet.protocol import Protocol, Factory -from twisted.internet.endpoints import TCP4ServerEndpoint, TCP4ClientEndpoint +from twisted.internet.endpoints import TCP4ClientEndpoint from twisted.internet.endpoints import connectProtocol -from twisted.internet.error import CannotListenError from twisted.internet.task import LoopingCall import messages @@ -193,29 +191,3 @@ class NCFactory(Factory): def gotProtocol(p): # ClientFactory instead? p.send_HELLO() - -if __name__ == "__main__": - # start listener - if len(sys.argv) == 2: - port = int(sys.argv[1]) - else: - port = 5005 - try: - endpoint = TCP4ServerEndpoint(reactor, port, interface="151.217.219.153") - _print(" [ ] LISTEN:", port) - ncfactory = NCFactory() - endpoint.listen(ncfactory) - except CannotListenError: - _print("[!] Address in use") - sys.exit(1) - - - # connect to bootstrap addresses - _print(" [ ] Trying to connect to bootstrap hosts:") - for bootstrap in BOOTSTRAP_NODES: - _print(" [*]", bootstrap) - host, port = bootstrap.split(":") - point = TCP4ClientEndpoint(reactor, host, int(port)) - d = connectProtocol(point, NCProtocol(ncfactory, "SENDHELLO", "SPEAKER")) - d.addCallback(gotProtocol) - reactor.run()