Example #1
0
class Rcon:
    host = ()
    password = None
    lock = thread.allocate_lock()
    socket = None
    queue = None
    console = None
    socket_timeout = 0.80
    rconsendstring = '\377\377\377\377rcon "%s" %s\n'
    rconreplystring = '\377\377\377\377print\n'
    qserversendstring = '\377\377\377\377%s\n'

    #caching options
    cache_opts = {
        'cache.data_dir': 'b3/cache/data',
        'cache.lock_dir': 'b3/cache/lock',
    }
    #create cache
    cache = CacheManager(**parse_cache_config_options(cache_opts))
    #default expiretime for the status cache in seconds and cache type
    statusCacheExpireTime = 2
    statusCacheType = 'memory'

    def __init__(self, console, host, password):
        self.console = console
        self.queue = Queue.Queue()

        if self.console.config.has_option('caching', 'status_cache_type'):
            self.statusCacheType = self.console.config.get(
                'caching', 'status_cache_type').lower()
            if self.statusCacheType not in ['file', 'memory']:
                self.statusCacheType = 'memory'
        if self.console.config.has_option('caching', 'status_cache_expire'):
            self.statusCacheExpireTime = abs(
                self.console.config.getint('caching', 'status_cache_expire'))
            if self.statusCacheExpireTime > 5:
                self.statusCacheExpireTime = 5
        self.console.bot('rcon status Cache Expire Time: [%s sec] Type: [%s]' %
                         (self.statusCacheExpireTime, self.statusCacheType))

        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.host = host
        self.password = password
        self.socket.settimeout(2)
        self.socket.connect(self.host)

        self._stopEvent = threading.Event()
        thread.start_new_thread(self._writelines, ())

    def encode_data(self, data, source):
        try:
            if isinstance(data, str):
                data = unicode(data, errors='ignore')
            data = data.encode(self.console.encoding, 'replace')
        except Exception, msg:
            self.console.warning('%s: ERROR encoding data: %r', source, msg)
            data = 'Encoding error'

        return data
    def __init__(self, app, config=None, environ_key='beaker.cache', **kwargs):
        """Initialize the Cache Middleware
        
        The Cache middleware will make a Cache instance available
        every request under the ``environ['beaker.cache']`` key by
        default. The location in environ can be changed by setting
        ``environ_key``.
        
        ``config``
            dict  All settings should be prefixed by 'cache.'. This
            method of passing variables is intended for Paste and other
            setups that accumulate multiple component settings in a
            single dictionary. If config contains *no cache. prefixed
            args*, then *all* of the config options will be used to
            intialize the Cache objects.
        
        ``environ_key``
            Location where the Cache instance will keyed in the WSGI
            environ
        
        ``**kwargs``
            All keyword arguments are assumed to be cache settings and
            will override any settings found in ``config``

        """
        self.app = app
        config = config or {}
        
        self.options = {}
        
        # Update the options with the parsed config
        self.options.update(parse_cache_config_options(config))
        
        # Add any options from kwargs, but leave out the defaults this
        # time
        self.options.update(
            parse_cache_config_options(kwargs, include_defaults=False))
                
        # Assume all keys are intended for cache if none are prefixed with
        # 'cache.'
        if not self.options and config:
            self.options = config
        
        self.options.update(kwargs)
        self.cache_manager = CacheManager(**self.options)
        self.environ_key = environ_key
Example #3
0
    def __init__(self, app, config=None, environ_key='beaker.cache', **kwargs):
        """Initialize the Cache Middleware
        
        The Cache middleware will make a Cache instance available
        every request under the ``environ['beaker.cache']`` key by
        default. The location in environ can be changed by setting
        ``environ_key``.
        
        ``config``
            dict  All settings should be prefixed by 'cache.'. This
            method of passing variables is intended for Paste and other
            setups that accumulate multiple component settings in a
            single dictionary. If config contains *no cache. prefixed
            args*, then *all* of the config options will be used to
            intialize the Cache objects.
        
        ``environ_key``
            Location where the Cache instance will keyed in the WSGI
            environ
        
        ``**kwargs``
            All keyword arguments are assumed to be cache settings and
            will override any settings found in ``config``

        """
        self.app = app
        config = config or {}

        self.options = {}

        # Update the options with the parsed config
        self.options.update(parse_cache_config_options(config))

        # Add any options from kwargs, but leave out the defaults this
        # time
        self.options.update(
            parse_cache_config_options(kwargs, include_defaults=False))

        # Assume all keys are intended for cache if none are prefixed with
        # 'cache.'
        if not self.options and config:
            self.options = config

        self.options.update(kwargs)
        self.cache_manager = CacheManager(**self.options)
        self.environ_key = environ_key
Example #4
0
class Rcon:
    host = ()
    password = None
    lock = thread.allocate_lock()
    socket = None
    queue = None
    console = None
    socket_timeout = 0.80
    rconsendstring = '\377\377\377\377rcon "%s" %s\n'
    rconreplystring = '\377\377\377\377print\n'
    qserversendstring = '\377\377\377\377%s\n'

    #caching options
    cache_opts = {
    'cache.data_dir': 'b3/cache/data',
    'cache.lock_dir': 'b3/cache/lock',
    }
    #create cache
    cache = CacheManager(**parse_cache_config_options(cache_opts))
    #default expiretime for the status cache in seconds and cache type
    statusCacheExpireTime = 2
    statusCacheType = 'memory'

    def __init__(self, console, host, password):
        self.console = console
        self.queue = Queue.Queue()

        if self.console.config.has_option('caching', 'status_cache_type'):
            self.statusCacheType = self.console.config.get('caching', 'status_cache_type').lower()
            if self.statusCacheType not in ['file', 'memory']:
                self.statusCacheType = 'memory'
        if self.console.config.has_option('caching', 'status_cache_expire'):
            self.statusCacheExpireTime = abs(self.console.config.getint('caching', 'status_cache_expire'))
            if self.statusCacheExpireTime > 5:
                self.statusCacheExpireTime = 5
        self.console.bot('rcon status Cache Expire Time: [%s sec] Type: [%s]' %(self.statusCacheExpireTime, self.statusCacheType))

        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.host = host
        self.password = password
        self.socket.settimeout(2)
        self.socket.connect(self.host)

        self._stopEvent = threading.Event()
        thread.start_new_thread(self._writelines, ())

    def send(self, data, maxRetries=None, socketTimeout=None):
        if socketTimeout is None:
            socketTimeout = self.socket_timeout
        if maxRetries is None:
            maxRetries = 2
            
        data = data.strip()
        self.console.verbose('QSERVER sending (%s:%s) %s', self.host[0], self.host[1], data)
        startTime = time.time()

        retries = 0
        while time.time() - startTime < 5:
            readables, writeables, errors = select.select([], [self.socket], [self.socket], socketTimeout)

            if len(errors) > 0:
                self.console.warning('QSERVER: %s', str(errors))
            elif len(writeables) > 0:
                try:
                    writeables[0].send(self.qserversendstring % data)
                except Exception, msg:
                    self.console.warning('QSERVER: ERROR sending: %s', msg)
                else:
                    try:
                        data = self.readSocket(self.socket, socketTimeout=socketTimeout)
                        self.console.verbose2('QSERVER: Received %s' % data)
                        return data
                    except Exception, msg:
                        self.console.warning('QSERVER: ERROR reading: %s', msg)
                    
            else: