def get(self, count: int=1, readOnly: bool=False, recurlvl=0): with open(self.FILE, "r+") as file: portalocker.lock(file, portalocker.LOCK_EX) ports = [] while len(ports) < count: file.seek(0) port = int(file.readline()) if readOnly: return port port += 1 if port > self.maxPort: port = self.minPort file.seek(0) file.write(str(port)) try: checkPortAvailable(("",port)) ports.append(port) self.logger.debug("new port dispensed: {}".format(port)) except: if recurlvl < self.maxportretries: self.logger.debug("port {} unavailable, trying again...". format(port)) else: self.logger.debug("port {} unavailable, max retries {} " "reached". format(port, self.maxportretries)) raise return ports
def __init__(self, *args, **kwargs): checkPortAvailable(kwargs['ha']) basedirpath = kwargs.get('basedirpath') keep = RoadKeep(basedirpath=basedirpath, stackname=kwargs['name'], auto=kwargs.get('auto'), baseroledirpath=basedirpath) # type: RoadKeep kwargs['keep'] = keep localRoleData = keep.loadLocalRoleData() sighex = kwargs.pop('sighex', None) or localRoleData['sighex'] if not sighex: (sighex, _), (prihex, _) = getEd25519AndCurve25519Keys() else: prihex = ed25519SkToCurve25519(sighex, toHex=True) kwargs['sigkey'] = sighex kwargs['prikey'] = prihex self.msgHandler = kwargs.pop('msgHandler', None) # type: Callable super().__init__(*args, **kwargs) if self.ha[1] != kwargs['ha'].port: error("the stack port number has changed, likely due to " "information in the keep. {} passed {}, actual {}".format( kwargs['name'], kwargs['ha'].port, self.ha[1])) self.created = time.perf_counter() self.coro = None config = getConfig() try: self.messageTimeout = config.RAETMessageTimeout except AttributeError: # if no timeout is set then message will never timeout self.messageTimeout = 0
def get(self, count: int = 1, readOnly: bool = False, recurlvl=0): with open(self.FILE, "r+") as file: portalocker.lock(file, portalocker.LOCK_EX) ports = [] while len(ports) < count: file.seek(0) port = int(file.readline()) if readOnly: return port port += 1 if port > self.maxPort: port = self.minPort file.seek(0) file.write(str(port)) try: checkPortAvailable(("", port)) ports.append(port) self.logger.debug("new port dispensed: {}".format(port)) except: if recurlvl < self.maxportretries: self.logger.debug( "port {} unavailable, trying again...".format( port)) else: self.logger.debug( "port {} unavailable, max retries {} " "reached".format(port, self.maxportretries)) raise return ports
def __init__(self, *args, **kwargs): checkPortAvailable(kwargs['ha']) basedirpath = kwargs.get('basedirpath') keep = RoadKeep(basedirpath=basedirpath, stackname=kwargs['name'], auto=kwargs.get('auto'), baseroledirpath=basedirpath) # type: RoadKeep kwargs['keep'] = keep localRoleData = keep.loadLocalRoleData() sighex = kwargs.pop('sighex', None) or localRoleData['sighex'] if not sighex: (sighex, _), (prihex, _) = getEd25519AndCurve25519Keys() else: prihex = ed25519SkToCurve25519(sighex, toHex=True) kwargs['sigkey'] = sighex kwargs['prikey'] = prihex self.msgHandler = kwargs.pop('msgHandler', None) # type: Callable super().__init__(*args, **kwargs) if self.ha[1] != kwargs['ha'].port: error("the stack port number has changed, likely due to " "information in the keep. {} passed {}, actual {}". format(kwargs['name'], kwargs['ha'].port, self.ha[1])) self.created = time.perf_counter() self.coro = None config = getConfig() try: self.messageTimeout = config.RAETMessageTimeout except AttributeError: # if no timeout is set then message will never timeout self.messageTimeout = 0
def nextAvailableClientAddr(self, curClientPort=8100): self.curClientPort = self.curClientPort or curClientPort self.curClientPort += 1 host = "127.0.0.1" try: checkPortAvailable((host,self.curClientPort)) return host, self.curClientPort except Exception as ex: tokens = [(Token.Error, "Cannot bind to port {}: {}, " "trying another port.".format( self.curClientPort, ex))] self.printTokens(tokens) return self.nextAvailableClientAddr(self.curClientPort)
def newStack(cls, stack): """ Create a new instance of the given RoadStackClass :param stack: a dictionary of Roadstack constructor arguments. :return: the new instance of stack created. """ checkPortAvailable(stack['ha']) stk = cls(**stack) if stk.ha[1] != stack['ha'].port: error("the stack port number has changed, likely due to " "information in the keep") logger.info("stack {} starting at {} in {} mode" .format(stk.name, stk.ha, stk.keep.auto.name), extra={"cli": False}) return stk
def nextAvailableClientAddr(self, curClientPort=8100): self.curClientPort = self.curClientPort or curClientPort self.curClientPort += 1 host = "127.0.0.1" if checkPortAvailable((host, self.curClientPort)): return host, self.curClientPort else: tokens = [(Token.Error, "Port {} already in use, " "trying another port.".format( self.curClientPort))] self.printTokens(tokens) return self.nextAvailableClientAddr(self.curClientPort)
def get(self, count: int=1, readOnly: bool=False): with open(self.FILE, "r+") as file: fcntl.flock(file.fileno(), fcntl.LOCK_EX) ports = [] while len(ports) < count: file.seek(0) port = int(file.readline()) if readOnly: return port port += 1 if port > self.maxPort: port = self.minPort file.seek(0) file.write(str(port)) if checkPortAvailable(HA(self.ip, port)): ports.append(port) print("new port dispensed: {}".format(port)) else: print("new port not available: {}".format(port)) return ports
def chk(): for port in ports: checkPortAvailable(("", port))