Exemple #1
0
class BasicEncoder(object):
    """Abstract Encoder for the BasicPacketEncoding Layer"""
    def __init__(self, logger_name="BasicEncoder", log_level=255):
        self.__logger_name = logger_name
        self.__log_level = log_level
        self.logger = Logger(self.__logger_name, self.__log_level)

    def set_log_level(self, log_level):
        self.logger.setLevel(log_level)

    @abc.abstractmethod
    def encode(self, packet: Packet):
        """encode a packet to wireformat"""

    @abc.abstractmethod
    def decode(self, wire_data) -> Packet:
        """decode a packet to Packet data structure"""

    def __getstate__(self):
        d = dict(self.__dict__)
        if 'logger' in d:
            del d['logger']
        return d

    def __setstate__(self, d):
        self.__dict__.update(
            d
        )  #need to store logger parameter and recreate logger here, since it cannot be pickled
        self.logger = Logger(self.__logger_name, self.__log_level)
Exemple #2
0
def main(args):
    logger = Logger(
        "ICNPushRepo",
        logging.DEBUG)  # note: set later according to cli/config arguments
    logger.info("Starting a Push Repository...")

    # Parse Configuration file
    conf = None
    if args.config != "none":
        try:
            conf = ConfigParser(args.config)
            logger.info("Successfully parsed configuration file.")
        except CouldNotOpenConfigError:
            conf = None
            logger.warning(
                "Could not open configuration file. Proceed with command line arguments or default values."
            )
        except CouldNotParseError:
            logger.warning(
                "Could not parse configuration file. Proceed with command line arguments or default values."
            )
        except MalformedConfigurationError as e:
            logger.warning(
                "Invalid configuration file. Proceed with command line arguments or default values. Hint: "
                + str(e))

    # Choose command line arguments before config file arguments before default values
    if not args.port:
        if conf and conf.udp_port:
            args.port = conf.udp_port
        else:
            args.port = default_port

    if not args.format:
        if conf and conf.format:
            args.format = conf.format
        else:
            args.format = default_format

    if not args.logging:
        if conf and conf.logging:
            args.logging = conf.logging
        else:
            args.logging = default_logging

    # Log Level
    if args.logging == 'error':
        log_level = logging.ERROR
    elif args.logging == 'warning':
        log_level = logging.WARNING
    elif args.logging == 'info':
        log_level = logging.INFO
    elif args.logging == 'debug':
        log_level = logging.DEBUG
    else:
        log_level = 255
    logger.setLevel(log_level)

    # Info
    logger.info("UDP Port:       " + str(args.port))
    logger.info("Log Level:      " + args.logging)
    logger.info("Packet Format:  " + args.format)
    logger.info("Database:       " + args.database_path)
    logger.info("Flush DB:       " + str(args.flush_database))

    # Packet encoder
    encoder = NdnTlvEncoder(
        log_level) if args.format == 'ndntlv' else SimpleStringEncoder(
            log_level)

    # Start
    forwarder = PiCN.ProgramLibs.ICNPushRepository.ICNPushRepository(
        args.database_path, args.port, log_level, encoder, args.flush_database)
    forwarder.start_forwarder()
    forwarder.linklayer.process.join()