提交 94b8167a 编写于 作者: B Brian Johnson

Refactored arePortsAvailable to Utils and added support for passing in single port. GH #5674

上级 da473e0d
...@@ -12,7 +12,6 @@ import datetime ...@@ -12,7 +12,6 @@ import datetime
import sys import sys
import random import random
import json import json
import socket
import errno import errno
from core_symbol import CORE_SYMBOL from core_symbol import CORE_SYMBOL
...@@ -131,7 +130,7 @@ class Cluster(object): ...@@ -131,7 +130,7 @@ class Cluster(object):
producerFlag="--producers %s" % (totalProducers) producerFlag="--producers %s" % (totalProducers)
tries = 30 tries = 30
while not Cluster.arePortsAvailable(set(range(self.port, self.port+totalNodes+1))): while not Utils.arePortsAvailable(set(range(self.port, self.port+totalNodes+1))):
Utils.Print("ERROR: Another process is listening on nodeos default port. wait...") Utils.Print("ERROR: Another process is listening on nodeos default port. wait...")
if tries == 0: if tries == 0:
return False return False
...@@ -366,34 +365,6 @@ class Cluster(object): ...@@ -366,34 +365,6 @@ class Cluster(object):
return True return True
@staticmethod
def arePortsAvailable(ports):
"""Check if specified ports are available for listening on."""
assert(ports)
assert(isinstance(ports, set))
for port in ports:
if Utils.Debug: Utils.Print("Checking if port %d is available." % (port))
assert(isinstance(port, int))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
s.bind(("127.0.0.1", port))
except socket.error as e:
if e.errno == errno.EADDRINUSE:
Utils.Print("ERROR: Port %d is already in use" % (port))
else:
# something else raised the socket.error exception
Utils.Print("ERROR: Unknown exception while trying to listen on port %d" % (port))
Utils.Print(e)
return False
finally:
s.close()
return True
# Initialize the default nodes (at present just the root node) # Initialize the default nodes (at present just the root node)
def initializeNodes(self, defproduceraPrvtKey=None, defproducerbPrvtKey=None, onlyBios=False): def initializeNodes(self, defproduceraPrvtKey=None, defproducerbPrvtKey=None, onlyBios=False):
port=Cluster.__BiosPort if onlyBios else self.port port=Cluster.__BiosPort if onlyBios else self.port
......
...@@ -6,6 +6,7 @@ from collections import namedtuple ...@@ -6,6 +6,7 @@ from collections import namedtuple
import inspect import inspect
import json import json
import shlex import shlex
import socket
from sys import stdout from sys import stdout
from sys import exit from sys import exit
import traceback import traceback
...@@ -171,6 +172,35 @@ class Utils: ...@@ -171,6 +172,35 @@ class Utils:
cmdArr=shlex.split(cmd) cmdArr=shlex.split(cmd)
return Utils.runCmdArrReturnJson(cmdArr, trace=trace, silentErrors=silentErrors) return Utils.runCmdArrReturnJson(cmdArr, trace=trace, silentErrors=silentErrors)
@staticmethod
def arePortsAvailable(ports):
"""Check if specified port (as int) or ports (as set) is/are available for listening on."""
assert(ports)
if isinstance(ports, int):
ports={ports}
assert(isinstance(ports, set))
for port in ports:
if Utils.Debug: Utils.Print("Checking if port %d is available." % (port))
assert(isinstance(port, int))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
s.bind(("127.0.0.1", port))
except socket.error as e:
if e.errno == errno.EADDRINUSE:
Utils.Print("ERROR: Port %d is already in use" % (port))
else:
# something else raised the socket.error exception
Utils.Print("ERROR: Unknown exception while trying to listen on port %d" % (port))
Utils.Print(e)
return False
finally:
s.close()
return True
########################################################################################### ###########################################################################################
class Account(object): class Account(object):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册