Example #1
0
 def parse(self):
     """
     Read the data from the socket, split it by \r\n and send it to the
     parseData() method.
     
     """
     while True:
         try:
             # if the last element in the split isn't a '', then it didn't
             # end on a \r\n.
             stream = util.toUnicode(self.sock.recv(4096)).split('\r\n')
             for data in stream:
                 util.write(data)
                 self.parseData(data)
             time.sleep(0.1)
         except (socket.timeout, socket.error, BreakOutOfLoop):
             self.server.connect = False
             break
         except (KeyboardInterrupt, SystemExit):
             self.commandHandler.replyWithMessage('Shutting down the bot')
             self.server.connect = False
             break
         except UnicodeDecodeError:
             continue
         except UnicodeEncodeError:
             continue
Example #2
0
 def ident(self):
     """Send the identification and nickname to the server."""
     util.write("Sending ident")
     self.sendRawMessage("NICK %s" % self.server.nickname)
     self.sendRawMessage("USER %s %s +iw :%s" % (
         self.server.realname,
         self.server.address,
         self.server.realname
     ))
Example #3
0
    def LoSC_Bouillard_optimizeByDelayBound_rate(arrivals, sc: ServiceCurve,
                                                 weights, foi):
        assert (
            arrivals and len(arrivals) and weights and len(weights)
            and len(arrivals) == len(weights) and weights[foi]
        ), 'pre-conditions failed for GPS.LoSC_Bouillard_optimizeByDelayBound(...)'
        # re-indexing and sorting
        arr_foi = arrivals.pop(foi)
        weights_foi = weights.pop(foi)

        # heuristic for sorting here
        # r is the parameter we use here (descending)
        arrivals_weights = list(zip(arrivals, weights))
        arrivals_weights.sort(key=lambda x: x[0].r, reverse=True)
        arrivals_, weights_ = list(zip(*arrivals_weights))
        arrivals = list(arrivals_)
        weights = list(weights_)

        arrivals.append(arr_foi)
        weights.append(weights_foi)
        new_foi = len(arrivals) - 1

        beta_i = None
        min_delay = None
        # in terms of delay
        best_j = None
        _iter = 1
        start = datetime.datetime.now()
        for j in range(new_foi):
            if _iter % 5 <= 5:
                clear_last_line()
                logging.debug(f"j: {_iter} of {new_foi}")
                percentage = round(_iter / new_foi * 100)
                print(
                    f"calculating {'#'*percentage}{'-'*(abs(100-percentage))} {percentage}%"
                )

            beta_candidate = GPS.LoSC_Bouillard_new(arrivals, sc, weights,
                                                    new_foi, j)
            delay_candidate = NC.delay_bound_token_bucket_rate_latency(
                arrivals[new_foi], beta_candidate)
            if min_delay is None or delay_candidate <= min_delay:
                # we ignore negative delay bounds as they are not reasonable
                if delay_candidate >= 0:
                    beta_i = beta_candidate
                    min_delay = delay_candidate
                    best_j = j
            _iter += 1

        write(f"j: {_iter-1} of {new_foi}")
        duration = datetime.datetime.now() - start
        print_write(
            "total computation time: ", ":".join([
                str(round(float(i))).zfill(2) for i in str(duration).split(":")
            ]))
        return beta_i, f'best_j={best_j}'
Example #4
0
 def destroyBot(self, botObjName):
     """Gracefully shut down the bot and remove it from self.botObjects."""
     try:
         self.botObjects[botObjName].destroy()
         del self.botObjects[botObjName]
         util.write("Bot %s has been detroyed." % botObjName)
     except KeyError:
         util.write(
             "Bot %s does not exist." % botObjName, 
             outputType="Warning"
         )
Example #5
0
    def LoSC_Chang_optimizeByDelayBound(arrivals, sc: ServiceCurve, weights,
                                        foi, subsetlist):
        global _v
        assert (
            arrivals and len(arrivals) and weights and len(weights)
            and len(arrivals) == len(weights) and weights[foi]
        ), 'pre-conditions failed for GPS.LoSC_Chang_optimizeByDelayBound(...)'

        beta_i = None
        min_delay = None
        # in terms of delay
        best_m = None
        _iter = 1
        start = datetime.datetime.now()
        # mod = (((2 ** len(arrivals)) - 1) // 2500) if (((2 ** len(arrivals)) - 1) // 2500) != 0 \
        #         else 1
        mod = 100000
        for M in subsetlist:
            if len(M) == 0:
                continue
            if round(_iter % mod) == 0 and _v:
                clear_last_line()
                logging.debug(f"M: {_iter} of {(2**len(arrivals)) - 1}")
                percentage = round(_iter / ((2**len(arrivals)) - 1) * 100)
                print(
                    f"calculating {'#'*percentage}{'-'*(abs(100-percentage))} {percentage}%"
                )

            beta_candidate = GPS.LoSC_Chang(arrivals, sc, weights, foi, M)
            delay_candidate = NC.delay_bound_token_bucket_rate_latency(
                arrivals[foi], beta_candidate)

            if min_delay is None or delay_candidate <= min_delay:
                # we ignore negative delay bounds as they are not reasonable
                if delay_candidate >= 0:
                    beta_i = beta_candidate
                    min_delay = delay_candidate
                    best_m = M
            _iter += 1

        write(f"M: {_iter-1} of {(2**len(arrivals)) - 1}")
        duration = datetime.datetime.now() - start
        if _v:
            print_write(
                "total computation time: ", ":".join([
                    str(round(float(i))).zfill(2)
                    for i in str(duration).split(":")
                ]))
        return beta_i, f'best_m (len)={len(best_m)}', len(best_m), best_m
Example #6
0
    def reloadBot(self, botObjName):
        """First destroy the Bot object and then reinstantiate it."""
        try:
            info = self.botObjects[botObjName].info
        except KeyError:
            info = None
        if info is not None:
            self.destroyBot(botObjName)
            self.botObjects[botObjName] = src.irc.botObject.BotObject(info)
            util.write("Bot %s has been reloaded." % botObjName)
        else:
            util.write(
                "Bot %s does not exist." % botObjName, 
                outputType="Warning"
            )

            
Example #7
0
 def help(self, command):
     """
     Look up the docstring for a given module or method from a module.
     """
     command = command[5:]
     cmdClass, cmdMethod, cmdArgs = self._splitCommand(command)
     util.write("Finding docstring for %s.%s" % (cmdClass, cmdMethod))
     try:
         # If there is only a module (ie $test)
         if cmdClass is None or cmdClass == '':
             self.replyWithMessage(
                 "Usage: help module.command to get information on a specific command, or help module to just get info on the module."
             )
         elif cmdMethod is None:
             docString = util.getDocstring(
                 self.grabClass(cmdClass)
             )
             self.replyWithMessage(
                 "%s: %s" % (command.title(), docString)
             )
         # If there is also a method on the module, and it isn't a private
         # method (ie $test.testing)
         elif not cmdMethod.startswith('_'):
             docString = util.getDocstring(
                 cmdMethod,
                 targetClass=self.grabClass(cmdClass)
             )
             self.replyWithMessage(
                 "%s.%s: %s" % (cmdClass.title(), cmdMethod, docString)
             )
         else:
             self.replyWithMessage(
                 "Docstring: Methods starting with _ are private methods!"
             )
     except (AttributeError, KeyError):
         if cmdMethod is not None:
             self.replyWithMessage(
                 "Docstring: Command %s.%s was not found" % (cmdClass, cmdMethod)
             )
         else:
             self.replyWithMessage(
                 "Docstring: Module '%s' was not found" % cmdClass
             )
     except Exception as e:
         self.replyWithMessage("Docstring: Exception occured: %s " % e)
         util.writeException(sys.exc_info())
Example #8
0
 def __init__(self, generateConf=False):
     """Set the initial settings file name."""
     super(Settings, self).__init__()
     if generateConf:
         self.writeSettingsFile(DEFAULT_CONF, path=DEFAULT_SETTINGS_PATH)
     else:
         try:
             self.settingsFilePath = DEFAULT_SETTINGS_PATH
         except IOError:
             self.writeSettingsFile(
                 DEFAULT_CONF,
                 path=DEFAULT_SETTINGS_PATH
             )
             util.write(
                 "No configuration file found, a default one has been generated at %s." % (DEFAULT_SETTINGS_PATH,),
                 priority=9
             )
             sys.exit(0)
Example #9
0
 def replyWithMessage(self, text, msgType=None):
     """Send a message to the channel from which we received the command."""
     text = util.toUnicode(text)
     recipient = self.user
     if self.channel.startswith("#"):
         recipient = self.channel
     if msgType is None:
         msgType = self.msgType
     # Avoid flooding of a channel
     splitText = text.split("\n")
     timeout = 0.2
     if len(splitText) > 10:
         timeout = 0.5
     for txt in splitText:
         txt = "%s %s :%s\r\n" % (msgType, recipient, txt)
         util.write(txt)
         self.sock.send(util.toBytes(txt))
         time.sleep(timeout)
Example #10
0
    def execute(self, command):
        """
        Execute the command by seperating it on '.' and then taking the first
        index, (e.g. test.testing) and use the module 'src.modules.test.test'
        and the class Test, then pass the second index 'testing' as an arg to
        the __init__ of the class, which then executes the method.

        """
        cmdClass, cmdMethod, cmdArgs = self._splitCommand(command)
        util.write("Executing %s.%s with args: %s" % (
            cmdClass,
            cmdMethod,
            cmdArgs
        ))
        try:
            # If there is only a module (ie $test)
            if cmdMethod is None:
                publicMethods = util.publicMethods(
                    self.grabClass(cmdClass)
                )
                self.replyWithMessage(
                    "%s: %s" % (command.title(), publicMethods)
                )
            # If there is also a method on the module, and it isn't a private
            # method (ie $test.testing)
            elif not cmdMethod.startswith('_'):
                self.grabClass(cmdClass)(self, cmdName=cmdMethod, cmdArgs=cmdArgs)
            else:
                self.replyWithMessage(
                    "Methods starting with _ are private methods!"
                )
        except (AttributeError, KeyError):
            if cmdMethod is not None:
                self.replyWithMessage(
                    "Command %s.%s was not found" % (cmdClass, cmdMethod)
                )
            else:
                self.replyWithMessage(
                    "Module '%s' was not found" % cmdClass
                )
        except Exception as e:
            self.replyWithMessage("Exception occured during command execution: %s " % e)
            util.writeException(sys.exc_info())
Example #11
0
    def connectToServer(self):
        """Connect to the address and pass the socket to the listener."""
        if self.server.connect:
            util.write("Initializing connection to %s" % (self.server.address,))
        while self.server.connect:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            if self.server.ssl:
                sock = ssl.wrap_socket(sock)
            # Allow reuse of the socket
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            # Set timeout limit, so we can detect if we are disconnected
            sock.settimeout(300)
            try:
                sock.connect((self.server.address, self.server.port))
                self.sock = sock
            except socket.gaierror:
                util.write(
                    "Either wrong hostname or no connection. Trying again.",
                    outputType="Warning"
                )
                time.sleep(60)
                continue
            except ssl.SSLError:
                util.write(
                    "Problem has occured with SSL connecting to %s:%s !\
(check you're using the right port)" %
                    (self.server.address, self.server.port,),
                    outputType="Warning"
                )
                break
            else:
                # We have succesfully connected, so we can start parsing
                self.dataParser.parse()
Example #12
0
 def sendRawMessage(self, text):
     """Send a raw message to the socket."""
     text = "%s\r\n" % text
     util.write(text)
     self.sock.send(util.toBytes(text))
Example #13
0
    def LoSC_Bouillard_optimizeByDelayBound_new(arrivals, sc: ServiceCurve,
                                                weights, foi):
        assert (
            arrivals and len(arrivals) and weights and len(weights)
            and len(arrivals) == len(weights) and weights[foi]
        ), 'pre-conditions failed for GPS.LoSC_Bouillard_optimizeByDelayBound(...)'
        # re-indexing and sorting
        arr_foi = arrivals.pop(foi)
        weights_foi = weights.pop(foi)

        arrivals.insert(0, TokenBucket(r=0, b=0))
        weights.insert(0, 0)

        for i in range(len(arrivals) - 1):
            ti_p1, ti_p1_ix = GPS.Bouillard_ti_new(weights, sc, arrivals, i)
            # swap
            ai_p1 = arrivals[i + 1]
            wi_p1 = weights[i + 1]
            arrivals[i + 1] = arrivals[ti_p1_ix]
            arrivals[ti_p1_ix] = ai_p1
            weights[i + 1] = weights[ti_p1_ix]
            weights[ti_p1_ix] = wi_p1
            # after the loop "arrivals" and "weights" are already sorted

        arrivals.pop(0)
        weights.pop(0)

        arrivals.append(arr_foi)
        weights.append(weights_foi)
        new_foi = len(arrivals) - 1

        beta_i = None
        min_delay = None
        # in terms of delay
        best_j = None
        _iter = 1
        start = datetime.datetime.now()
        for j in range(new_foi):
            if _iter % 5 <= 5:
                clear_last_line()
                logging.debug(f"j: {_iter} of {new_foi}")
                percentage = round(_iter / new_foi * 100)
                print(
                    f"calculating {'#'*percentage}{'-'*(abs(100-percentage))} {percentage}%"
                )

            beta_candidate = GPS.LoSC_Bouillard_new(arrivals, sc, weights,
                                                    new_foi, j)
            delay_candidate = NC.delay_bound_token_bucket_rate_latency(
                arrivals[new_foi], beta_candidate)
            if min_delay is None or delay_candidate <= min_delay:
                # we ignore negative delay bounds as they are not reasonable
                if delay_candidate >= 0:
                    beta_i = beta_candidate
                    min_delay = delay_candidate
                    best_j = j
            _iter += 1

        write(f"j: {_iter-1} of {new_foi}")
        duration = datetime.datetime.now() - start
        print_write(
            "total computation time: ", ":".join([
                str(round(float(i))).zfill(2) for i in str(duration).split(":")
            ]))
        return beta_i, f'best_j={best_j}'
Example #14
0
    def LoSC_Bouillard_optimizeByDelayBound(arrivals, sc: ServiceCurve,
                                            weights, foi):
        assert (
            arrivals and len(arrivals) and weights and len(weights)
            and len(arrivals) == len(weights) and weights[foi]
        ), 'pre-conditions failed for GPS.LoSC_Bouillard_optimizeByDelayBound(...)'
        # re-indexing and sorting
        arr_foi = arrivals.pop(foi)
        weights_foi = weights.pop(foi)

        arrivals_ti_weight = []
        for i, a in enumerate(arrivals):
            ti = GPS.Bouillard_ti(weights, sc, arrivals, i)
            arrivals_ti_weight.append((a, ti, weights[i]))

        arrivals_ti_weight.sort(key=lambda x: x[1], reverse=False)
        arrivals_, _, weights_ = list(zip(*arrivals_ti_weight))
        arrivals = list(arrivals_)
        weights = list(weights_)

        arrivals.append(arr_foi)
        weights.append(weights_foi)
        new_foi = len(arrivals) - 1

        beta_i = None
        min_delay = None
        # in terms of delay
        best_j = None
        _iter = 1
        start = datetime.datetime.now()
        for j in range(new_foi):
            if _iter % 5 <= 5:
                clear_last_line()
                logging.debug(f"j: {_iter} of {new_foi}")
                percentage = round(_iter / new_foi * 100)
                print(
                    f"calculating {'#'*percentage}{'-'*(abs(100-percentage))} {percentage}%"
                )

            # ask whether all flows are included or not
            #if all included, then compare to PG result and they have to match
            # else throw error

            beta_candidate = GPS.LoSC_Bouillard(arrivals, sc, weights, new_foi,
                                                j)
            delay_candidate = NC.delay_bound_token_bucket_rate_latency(
                arrivals[new_foi], beta_candidate)
            if min_delay is None or delay_candidate <= min_delay:
                # we ignore negative delay bounds as they are not reasonable
                if delay_candidate >= 0:
                    beta_i = beta_candidate
                    min_delay = delay_candidate
                    best_j = j
            _iter += 1

        write(f"j: {_iter-1} of {new_foi}")
        duration = datetime.datetime.now() - start
        print_write(
            "total computation time: ", ":".join([
                str(round(float(i))).zfill(2) for i in str(duration).split(":")
            ]))
        return beta_i, f'best_j={best_j}'