Beispiel #1
0
    def start(self, host, port):
        self.connection.connect((host, port))

        server_thread = ReceiveMessageWorker(client, self.connection)
        server_thread.daemon = True
        server_thread.start()

        print "Client receive thread:", server_thread.name
Beispiel #2
0
    def __init__(self):
        # Initialiser en tilkobling
        self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        self.messageWorker = ReceiveMessageWorker(self, self.connection)

        self.loggedIn = False
        self.listener = None
Beispiel #3
0
    def start(self, host, port):
        self.connection.connect((host, port))

        server_thread = ReceiveMessageWorker(client, self.connection)
        server_thread.daemon = True
        server_thread.start()

        print "Client receive thread:", server_thread.name
Beispiel #4
0
	def __init__(self):
		# Initialiser en tilkobling
		self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		
		self.messageWorker = ReceiveMessageWorker(self, self.connection)
		
		self.loggedIn = False
		self.listener = None
Beispiel #5
0
 def start(self, host, port):
     self.connection.connect((host, port))
     messageWorker = ReceiveMessageWorker(self, self.connection)
     messageWorker.start()
     while True:
         message = ''
         message = raw_input()
         if message.startswith('/'):
             if message.startswith('/login') and message.count(' ') < 2 and len(message) > 7:
                 self.send(json.dumps({'request': 'login', 'username': message.split(' ', 1)[1]}))
             elif message == '/logout':
                 self.send(json.dumps({'request': 'logout'}))
             else:
                 print 'Invalid command'
                 continue
         else:
             self.send(json.dumps({'request':'message', 'message': message}))
     self.connection.close()
Beispiel #6
0
    def start(self, host, port):
        self.connection.connect((host, port))
        messageWorker = ReceiveMessageWorker(self, self.connection)
        messageWorker.start()
        self.login()

        while True:
            message = raw_input()
            if (self.logged_in == False):
                continue
            if (message == '/logout'):
                request = {
                            'request':'logout'
                            }
            else:
                request = {
                            'request':'message',
                            'message':message
                            }
            json_request = json.dumps(request)
            self.send(json_request)
Beispiel #7
0
            self.send(msgJson)
        except:
            print "No contact when sending msg to server"
            self.connection_closed()

def terminate():
    print "Closing program.."
    sys.exit()                
            
if __name__ == "__main__":
    print "Welcome to The KTN-chat!"
    client = Client()
    client.start(SERVER, PORT)
    client.loginRequest()

    print "Type in a message followed by enter to send,"
    print "please type \'logout\' to log out"
    
    msgWorkerThread = ReceiveMessageWorker(client,client.connection)
    msgWorkerThread.start()
    while 1:
        msg = raw_input()
        if msg == 'logout':
            client.logoutRequest()    
        client.sendMessage(msg)
    msgWorkerThread.join()
    client.force_disconnect()
    print "Client disconnected"


Beispiel #8
0
class Client(object):
    def __init__(self):
        # Initialiser en tilkobling
        self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        self.messageWorker = ReceiveMessageWorker(self, self.connection)

        self.loggedIn = False
        self.listener = None

    def setListener(self, listener):
        self.listener = listener

    def start(self, host, port):
        # Start tilkoblingen
        self.connection.connect((host, port))

        # Start MessageWorker
        self.messageWorker.start()

    def isLoggedIn(self):
        return self.loggedIn

    def checkMessage(self, message):
        if message == 'logout':
            data = {'request': 'logout'}
            data = json.dumps(data)
            self.send(data)
        else:
            self.sendMessage(message)

    def sendMessage(self, message):
        # Konstruer et JSON objekt som som skal
        # sendes til serveren
        data = {'request': 'message', 'message': message}

        # Lag en streng av JSON-objektet
        data = json.dumps(data)

        self.send(data)

    def printDebugMessage(self, message):
        self.listener.addMessage(message)

    # Lag en metode for å sende en melding til serveren
    def send(self, data):
        self.connection.send(data)

    def dataReceived(self, data):
        if not data: return
        data = json.loads(data)

        if (data.get('error')):
            error = data['error']
            self.listener.addMessage(error)

        elif not self.loggedIn:
            if (data.get('response')):
                if (data['response'] == 'login'):
                    self.listener.addMessage('Logged in as ' +
                                             data['username'])
                    self.loggedIn = True
                    self.showBackLog(data['messages'])
                    #backlog = data['messages']
                    #self.printBacklog(backlog)

        elif self.loggedIn:
            if (data.get('response')):
                if (data['response'] == 'message'):
                    self.listener.addMessage(data['message'])

                elif (data['response'] == 'logout'):

                    #Stopper alt
                    self.loggedIn = False
                    self.listener.shutDown()
                    self.messageWorker.shutdown()
                    self.connection.close()
                    self.listener.addMessage('You logged out')
                    sys.exit()

    def showBackLog(self, messages):
        for message in messages:
            self.listener.addMessage(message)

    def login(self, nick):
        data = {'request': 'login', 'username': nick}
        data = json.dumps(data)
        self.send(data)
class Client(object):

    def __init__(self, listener):
        self.listener = listener
        self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.logged_in = False
        self.messages = []

    def start(self, host, port):
        self.connection.connect((host, port))
        self.rec_worker = ReceiveMessageWorker(self, self.connection)
        self.rec_worker.start()

    def message_received(self, message):
        with self.listener.lock:
            self.messages.append(message['message'])
        self.listener.display_messages()

    def login_received(self, response):
        with self.listener.lock:
            if 'messages' in response.keys():
                self.logged_in = True
                for message in response['messages']:
                    self.messages.append(message)
            elif 'error' in response.keys():
                self.messages.append(response['error'])
        self.listener.display_messages()

    def logout_received(self, response):
        with self.listener.lock:
            self.logged_in = False
            self.connection_closed()
        self.listener.display_messages()

    def connection_closed(self):
        self.connection.close()
        self.rec_worker.running = False
        self.listener.running = False

    def send(self, data):
        self.connection.sendall(data)

    def send_message(self, message):
        self.send(json.dumps({'request': 'message', 'message': message}))

    def force_disconnect(self):
        self.connection_closed() 

    def login(self, username):
        self.send(json.dumps({'request': 'login', 'username': username}))
    
    def logout(self):
        self.send(json.dumps({'request': 'logout'}))

    def data_received(self, data):
        parsed = json.loads(data)
        if parsed['response'] == 'login':
            self.login_received(parsed)
        elif parsed['response'] == 'logout':
            self.logout_received(parsed)
        elif parsed['response'] == 'message':
            self.message_received(parsed)
 def start(self, host, port):
     self.connection.connect((host, port))
     self.rec_worker = ReceiveMessageWorker(self, self.connection)
     self.rec_worker.start()
Beispiel #11
0
class Client(object):

	def __init__(self):
		# Initialiser en tilkobling
		self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		
		self.messageWorker = ReceiveMessageWorker(self, self.connection)
		
		self.loggedIn = False
		self.listener = None
		
		
	
	def setListener(self, listener):
		self.listener = listener
	
	
	def start(self, host, port):
		# Start tilkoblingen
		self.connection.connect((host, port))
		
		# Start MessageWorker
		self.messageWorker.start()



		
	def isLoggedIn(self):
		return self.loggedIn
	
	def checkMessage(self,message):
	        if message == 'logout':
	                data = {'request': 'logout'}
        		data = json.dumps(data)
        		self.send(data)
        	else:
        	        self.sendMessage(message)
	
	def sendMessage(self, message):
		# Konstruer et JSON objekt som som skal
		# sendes til serveren
		data = {'request': 'message', 'message': message}

		# Lag en streng av JSON-objektet
		data = json.dumps(data)

		self.send(data)
	
	def printDebugMessage(self,message):
	        self.listener.addMessage(message)
	
	# Lag en metode for å sende en melding til serveren
	def send(self, data):
		self.connection.send(data)

	def dataReceived(self, data):
	        if not data: return
		data = json.loads(data)
		
		if(data.get('error')):
		        error = data['error']
			self.listener.addMessage(error)
		
		elif not self.loggedIn:	
			if(data.get('response')):
				if(data['response'] == 'login'):
					self.listener.addMessage('Logged in as ' + data['username'])
					self.loggedIn = True
					self.showBackLog(data['messages'])
					#backlog = data['messages']
					#self.printBacklog(backlog)
					
		elif self.loggedIn:
			if(data.get('response')):
				if(data['response'] == 'message'):
					self.listener.addMessage(data['message'])
		
                		elif(data['response'] == 'logout'):
                                        
                                        #Stopper alt
                			self.loggedIn = False;
                			self.listener.shutDown()
                			self.messageWorker.shutdown()
                			self.connection.close()
                			self.listener.addMessage('You logged out')
                			sys.exit()
                			
        def showBackLog(self,messages):
                for message in messages:
                        self.listener.addMessage(message)
			
	def login(self, nick):
		data = {'request': 'login', 'username': nick}
		data = json.dumps(data)
		self.send(data)