コード例 #1
0
ファイル: nidtnhttpgateway.py プロジェクト: skunkwerks/netinf
    def __init__(self, config, logger, ndo_cache, redis_conn, http_server):
        """
        @brief Constructor for NetInf HTTP<->DTN gateway component of niserver.
        @param config object ConfigParser instance pointing to config file
        @param logger object logger instance to output messages
        @param ndo_cache object instance of NetInfCache with local (HTTP) NDOs
        @param nrs_redis object StrictRedis instance used for communication
                                between the gateway and the Redis database.
        @param http_server object instance of NIHTTPServer that instantiated
                                  this gateway.
        """

        self.config = config
        self.logger = logger
        self.ndo_cache = ndo_cache
        self.redis_conn = redis_conn
        self.http_server = http_server
        self.mprocs = None
        self.per_req_limit = None

        # Get mprocs and per_req_limit from config if specified
        conf_section = "gateway"
        if ((config is not None) and (config.has_section(conf_section))):
            if (self.mprocs is None):
                conf_option = "mprocs"
                if config.has_option(conf_section, conf_option):
                    try:
                        self.mprocs = config.getint(conf_section,
                                                    conf_option)
                    except ValueError:
                        self.logerror("Value supplied for %s is not an "
                                      "acceptable integer representation - "
                                      "using default %d" %
                                      (conf_option, self.DEFAULT_MPROCS))
                        self.mprocs = None
            if (self.per_req_limit is None):
                conf_option = "per-req-limit"
                if config.has_option(conf_section, conf_option):
                    try:
                        self.per_req_limit = config.getint(conf_section,
                                                           conf_option)
                    except ValueError:
                        self.logerror("Value supplied for %s is not an "
                                      "acceptable integer representation - "
                                      "using default %d" %
                                      (conf_option, self.DEFAULT_PER_REQ_LIMIT))
                        self.per_req_limit = None

        # Use defaults if not in configuration
        if self.mprocs is None:
            self.mprocs = self.DEFAULT_MPROCS
        if self.per_req_limit is None:
            self.per_req_limit = self.DEFAULT_PER_REQ_LIMIT

        # Create Queue for requests to be sent into DTN network
        self.response_q = Queue()

        # Create HTTP action thread
        self.http_action = HTTPAction(self.response_q,
                                      ndo_cache.get_temp_path(), logger,
                                      redis_conn, ndo_cache,
                                      mprocs=self.mprocs,
                                      per_req_limit=self.per_req_limit)
        self.http_action.setDaemon(True)

        # Create DTN send and receive threads
        self.dtn_send = DtnSend(self.response_q, logger)
        self.dtn_send.setDaemon(True)
        self.dtn_receive = DtnReceive(self.http_action, logger)
        self.dtn_receive.setDaemon(True)
        
        return
コード例 #2
0
ファイル: nidtnhttpgateway.py プロジェクト: skunkwerks/netinf
class DtnHttpGateway():
    """
    @brief Controller for HTTP<->DTN gateway.

    Records parameters passed on startup.
    Starts up various threads as described above.
    Provides shutdown routine to close down the threads on request. 
    
    """
    #--------------------------------------------------------------------------#
    # CLASS CONSTANTS

    # === Defaults for single/multi-processing setup ===

    ##@var DEFAULT_MPROCS
    # integer number of processes to provide gateway forwarding operations
    #         If 1 (or 0) will run synchronously within the gateway HTTP thread
    DEFAULT_MPROCS = 1

    ##@var DEFAULT_PER_REQ_LIMIT
    # integer number of outstanding forwarded HTTP requests allowed per item
    DEFAULT_PER_REQ_LIMIT = 1
    
    #--------------------------------------------------------------------------#
    # INSTANCE VARIABLES

    ##@var redis_conn
    # object StrictRedis instance used for communication between the NRS server
    # and the Redis database.

    ##@var logger
    # object logger instance to output messages

    ##@var ndo_cache
    # object instance of NDO cache handler

    ##@var http_server
    # object instance of NIHTTPServer that created this gateway
    
    #--------------------------------------------------------------------------#
    def __init__(self, config, logger, ndo_cache, redis_conn, http_server):
        """
        @brief Constructor for NetInf HTTP<->DTN gateway component of niserver.
        @param config object ConfigParser instance pointing to config file
        @param logger object logger instance to output messages
        @param ndo_cache object instance of NetInfCache with local (HTTP) NDOs
        @param nrs_redis object StrictRedis instance used for communication
                                between the gateway and the Redis database.
        @param http_server object instance of NIHTTPServer that instantiated
                                  this gateway.
        """

        self.config = config
        self.logger = logger
        self.ndo_cache = ndo_cache
        self.redis_conn = redis_conn
        self.http_server = http_server
        self.mprocs = None
        self.per_req_limit = None

        # Get mprocs and per_req_limit from config if specified
        conf_section = "gateway"
        if ((config is not None) and (config.has_section(conf_section))):
            if (self.mprocs is None):
                conf_option = "mprocs"
                if config.has_option(conf_section, conf_option):
                    try:
                        self.mprocs = config.getint(conf_section,
                                                    conf_option)
                    except ValueError:
                        self.logerror("Value supplied for %s is not an "
                                      "acceptable integer representation - "
                                      "using default %d" %
                                      (conf_option, self.DEFAULT_MPROCS))
                        self.mprocs = None
            if (self.per_req_limit is None):
                conf_option = "per-req-limit"
                if config.has_option(conf_section, conf_option):
                    try:
                        self.per_req_limit = config.getint(conf_section,
                                                           conf_option)
                    except ValueError:
                        self.logerror("Value supplied for %s is not an "
                                      "acceptable integer representation - "
                                      "using default %d" %
                                      (conf_option, self.DEFAULT_PER_REQ_LIMIT))
                        self.per_req_limit = None

        # Use defaults if not in configuration
        if self.mprocs is None:
            self.mprocs = self.DEFAULT_MPROCS
        if self.per_req_limit is None:
            self.per_req_limit = self.DEFAULT_PER_REQ_LIMIT

        # Create Queue for requests to be sent into DTN network
        self.response_q = Queue()

        # Create HTTP action thread
        self.http_action = HTTPAction(self.response_q,
                                      ndo_cache.get_temp_path(), logger,
                                      redis_conn, ndo_cache,
                                      mprocs=self.mprocs,
                                      per_req_limit=self.per_req_limit)
        self.http_action.setDaemon(True)

        # Create DTN send and receive threads
        self.dtn_send = DtnSend(self.response_q, logger)
        self.dtn_send.setDaemon(True)
        self.dtn_receive = DtnReceive(self.http_action, logger)
        self.dtn_receive.setDaemon(True)
        
        return
    
    #--------------------------------------------------------------------------#
    def start_gateway(self):
        """
        @brief start the threads running HTTP actions plus DTN send and receive
        """
        self.http_action.start()
        self.dtn_send.start()
        self.dtn_receive.start()
        return
    
    #--------------------------------------------------------------------------#
    def shutdown_gateway(self):
        """
        @brief terminate the threads running DTN receive and the HTTP
               action routine

        @detail Terminating the HTTP action thread will automatically terminate
        the DTN recive thread by sending it an 'end' message.
        """
        self.dtn_receive.end_run()
        self.http_action.end_run()
        return