Example #1
0
    def getSecret(self, username, message):
        """ Start chatting with the bot server. GLADOS replies back to the client socket
            so we have to keep the connection open and wait to receive replies from bot. """

        try:
            if len(self.__userList) == 0:
                raise Exception, 'Use the list command to see the online users'

            if (username in self.__userList):

                uIp, uP = self.__userList[username]
                c = ConnectionManager(uIp, uP)
                c.connect()
                c.send(p.T_MESSAGE, [self.__username], message)

                # We wait for four replies from GLADOS to receive the final token
                # The application is going to block until GLADOS replies.
                # Quick and dirty solution as GLaDOS speaks "differently" from
                # the way this client is implemented. So, the message is printed
                # in raw format just to get the final token.
                for i in range(0, 3):
                    r = c.receive()
                    self.__agent.printMessage(str(r), 'Bot user')
                    if (r.type == p.T_PING):
                        c.send(p.T_PONG, [self.__username])
                c.disconnect()
            else:
                raise Exception, 'User %s can\'t be reached.' % username

        except Exception, e:
            self.__handleError('Chat', e)
Example #2
0
    def getSecret(self, username, message):
        """ Start chatting with the bot server. GLADOS replies back to the client socket
            so we have to keep the connection open and wait to receive replies from bot. """

        try:
            if len(self.__userList) == 0:
                raise Exception, 'Use the list command to see the online users'

            if (username in self.__userList):
   
                uIp, uP = self.__userList[username]
                c = ConnectionManager(uIp, uP)
                c.connect()
                c.send(p.T_MESSAGE, [self.__username], message)
                
                # We wait for four replies from GLADOS to receive the final token
                # The application is going to block until GLADOS replies.
                # Quick and dirty solution as GLaDOS speaks "differently" from
                # the way this client is implemented. So, the message is printed
                # in raw format just to get the final token. 
                for i in range(0,3):
                    r = c.receive()
                    self.__agent.printMessage(str(r),'Bot user')
                    if(r.type == p.T_PING):
                        c.send(p.T_PONG, [self.__username])
                c.disconnect()
            else:
                raise Exception, 'User %s can\'t be reached.' % username

        except Exception,e:
            self.__handleError('Chat', e)
Example #3
0
    def chat(self, username, message, getSecret=False):
        """ Start chatting with a specific user """

        try:
            if len(self.__userList) == 0:
                raise Exception, 'Use the list command to see the online users'

            if (username in self.__userList):

                # Setup a new socket connection with the other user and send data.
                # The user can reply at the binded port from the directory server
                uIp, uP = self.__userList[username]
                c = ConnectionManager(uIp, uP)
                c.connect()
                c.send(p.T_MESSAGE, [self.__username], message)
                c.disconnect()

            else:
                raise Exception, 'User %s can\'t be reached.' % username

        except Exception, e:
            self.__handleError('Chat', e)
Example #4
0
    def chat(self, username, message, getSecret = False):
        """ Start chatting with a specific user """

        try:
            if len(self.__userList) == 0:
                raise Exception, 'Use the list command to see the online users'

            if (username in self.__userList):
   
                # Setup a new socket connection with the other user and send data.
                # The user can reply at the binded port from the directory server
                uIp, uP = self.__userList[username]
                c = ConnectionManager(uIp, uP)
                c.connect()
                c.send(p.T_MESSAGE, [self.__username], message)
                c.disconnect()

            else:
                raise Exception, 'User %s can\'t be reached.' % username

        except Exception,e:
            self.__handleError('Chat', e)
Example #5
0
    def ping(self, username):
        """ Ping user """

        try:
            if len(self.__userList) == 0:
                raise Exception, 'Use the list command to see the online users'

            if (username in self.__userList):

                uIp, uP = self.__userList[username]
                c = ConnectionManager(uIp, uP)

                c.connect()
                c.send(p.T_PING, [self.__username])

                pong = c.receive()
                self.__agent.printMessage(pong.type, pong.args.pop())

                c.disconnect()
            else:
                raise Exception, 'User %s can\'t be pinged.' % username

        except Exception, e:
            self.__handleError('Ping', e)
Example #6
0
    def ping(self, username):
        """ Ping user """

        try:       
            if len(self.__userList) == 0:
                raise Exception, 'Use the list command to see the online users'

            if (username in self.__userList):
   
                uIp, uP = self.__userList[username]
                c = ConnectionManager(uIp, uP)

                c.connect()
                c.send(p.T_PING, [self.__username])

                pong = c.receive()
                self.__agent.printMessage(pong.type, pong.args.pop())

                c.disconnect()
            else:
                raise Exception, 'User %s can\'t be pinged.' % username

        except Exception,e:
            self.__handleError('Ping', e)
Example #7
0
class ChatClient:
    """ Chat client handles all outcoming requests like (authentication, user searching and of course chatting. The general idea of the client is to
    open a connection for sending messages and receive all the incoming messages
    at the server port that is known through the directory server."""
    def __init__(self, host, port, username, password, agent):
        self.__host = host
        self.__port = port
        self.__username = username
        self.__password = password
        self.__agent = agent
        self.__cm = None
        self.__userList = {}

    def __connect(self):
        self.__cm = ConnectionManager(self.__host, self.__port)
        self.__cm.connect()

    def __disconnect(self):
        self.__cm.disconnect()

    def __trigger(self, toBeExecuted, args=[]):
        """ Trigger function uses the power of functinal programing to execute the functions 
            that are passed as parameters. In details, its a wrapper for every command that
            requires re-authentication with the directory server. """

        self.__connect()
        [f(args) for f in toBeExecuted]
        self.__disconnect()

    def authenticate(self):
        self.__trigger([self.__login, self.__bind])

    def search(self, username):
        self.__trigger([self.__login, self.__searchUser], [username])

    def listAll(self):
        self.__trigger([self.__login, self.__searchUser])

    def leave(self):
        self.__trigger([self.__login, self.__unregister])

    def chat(self, username, message, getSecret=False):
        """ Start chatting with a specific user """

        try:
            if len(self.__userList) == 0:
                raise Exception, 'Use the list command to see the online users'

            if (username in self.__userList):

                # Setup a new socket connection with the other user and send data.
                # The user can reply at the binded port from the directory server
                uIp, uP = self.__userList[username]
                c = ConnectionManager(uIp, uP)
                c.connect()
                c.send(p.T_MESSAGE, [self.__username], message)
                c.disconnect()

            else:
                raise Exception, 'User %s can\'t be reached.' % username

        except Exception, e:
            self.__handleError('Chat', e)
Example #8
0
class ChatClient:
    """ Chat client handles all outcoming requests like (authentication, user searching and of course chatting. The general idea of the client is to
    open a connection for sending messages and receive all the incoming messages
    at the server port that is known through the directory server."""

    def __init__(self, host, port, username, password, agent):
        self.__host = host
        self.__port = port
        self.__username = username
        self.__password = password
        self.__agent = agent
        self.__cm = None
        self.__userList = {}

    def __connect(self):
        self.__cm = ConnectionManager(self.__host, self.__port)
        self.__cm.connect()
        
    def __disconnect(self):
        self.__cm.disconnect()

    def __trigger(self, toBeExecuted, args = []):
        """ Trigger function uses the power of functinal programing to execute the functions 
            that are passed as parameters. In details, its a wrapper for every command that
            requires re-authentication with the directory server. """

        self.__connect()
        [ f(args) for f in toBeExecuted ]
        self.__disconnect()

    def authenticate(self):
        self.__trigger([self.__login, self.__bind])

    def search(self, username):
        self.__trigger([self.__login, self.__searchUser], [username])

    def listAll(self):
        self.__trigger([self.__login, self.__searchUser])

    def leave(self):
        self.__trigger([self.__login, self.__unregister])

    def chat(self, username, message, getSecret = False):
        """ Start chatting with a specific user """

        try:
            if len(self.__userList) == 0:
                raise Exception, 'Use the list command to see the online users'

            if (username in self.__userList):
   
                # Setup a new socket connection with the other user and send data.
                # The user can reply at the binded port from the directory server
                uIp, uP = self.__userList[username]
                c = ConnectionManager(uIp, uP)
                c.connect()
                c.send(p.T_MESSAGE, [self.__username], message)
                c.disconnect()

            else:
                raise Exception, 'User %s can\'t be reached.' % username

        except Exception,e:
            self.__handleError('Chat', e)