Skip to content
client.py 2.1 KiB
Newer Older
Antoine Millet's avatar
Antoine Millet committed
#!/usr/bin/env python
#coding=utf8

Antoine Millet's avatar
Antoine Millet committed
'''
Local client representation classes.
'''

from threading import Lock
Antoine Millet's avatar
Antoine Millet committed
from datetime import datetime

Antoine Millet's avatar
Antoine Millet committed
class CCClient(object):
    '''
    Represent a single client connected to the server.
    '''

    def __init__(self, login, role, server, connection):

        # The login of the client:
Antoine Millet's avatar
Antoine Millet committed

        # The role of the client:
Antoine Millet's avatar
Antoine Millet committed

        # The server binded to this client:
Antoine Millet's avatar
Antoine Millet committed

        # The connection of the client (public attribute):
        self.connection = connection

Antoine Millet's avatar
Antoine Millet committed
        # The date of connection of the client:
        self._connection_date = datetime.now()
Antoine Millet's avatar
Antoine Millet committed

        # The date of the last action:
        self._last_action = datetime.now()

        # The connection proxy:
        self.proxy = ConnectionProxy(connection)

Antoine Millet's avatar
Antoine Millet committed
        # Jobs lock for this client:
Antoine Millet's avatar
Antoine Millet committed
    def get_uptime(self):
Antoine Millet's avatar
Antoine Millet committed
        '''
Antoine Millet's avatar
Antoine Millet committed
        Get the uptime of the client connection in seconds.
Antoine Millet's avatar
Antoine Millet committed

Antoine Millet's avatar
Antoine Millet committed
        :return: uptime of the client
        '''
Antoine Millet's avatar
Antoine Millet committed
        dt = datetime.now() - self._connection_date
        return dt.seconds + dt.days * 86400
Antoine Millet's avatar
Antoine Millet committed

    def get_idle(self):
        '''
        Get the idle time of the client connection in seconds.

        :return: idle of the client
        '''
        dt = datetime.now() - self._last_action
        return dt.seconds + dt.days * 86400

    def top(self):
        '''
        Reset the last_action date to now.
        '''

        self._last_action = datetime.now()

Antoine Millet's avatar
Antoine Millet committed
    def get_ip(self):
        peer = self.connection.getpeername()
        return ':'.join(peer.split(':')[:-1])
Antoine Millet's avatar
Antoine Millet committed
        '''
        Shutdown the connection to the client.
        '''

        self.server.rpc.unregister(self.connection, shutdown=True)

    def get_tags(self):
        '''
        Get all server defined tags.
        '''

        tags = {}
        tags['con'] = self.get_uptime()
        tags['ip'] = self.get_ip()
        return tags

    def get_remote_tags(self, tag):
        return self.connection.call('get_tags', (tag,))[tag]