Question

I'm trying to write a instant messaging program, the basic ui is almost finished and i'm looking into the receiving part of messages. I have an UI class and a threaded Receive_Socket class. Each time the socket of the Received_Socket class receive a message, it does a gobject.idle_add() to call an UI method in order to display the message into a chat window. After the gobject.idle.add() line, i have a while loop which loops until the message is in fact displayed in the chat window ( I want the message to be displayed before receiving another message because i read that gobject.idle_add() does not guarantee execution order, and of course i want messages to be displayed in order :) )

I tried to sum up my code :

UI Class :

Class UI:
##### somewhere in the init #####
    self.message_status = 0
def chat_window(self, contact, message=0):
    Check if a chat window is opened for the contact, if not it opens one.
    => In reality this check if a gtk.Window containing a gtk.Notebook is opened, 
    => if not it opens one
    => Then it creates a page for the contact in the notebook, containing a 
    => gtk.Textview which displays messages
    if message:
        self.message_display(contact, message)
def message_display(self,a,b):
    => Display the message in the message in the contact's textview
    self.message_status = 1

Threaded Receive_Socket class:

Class Receive_Socket(threading.thread):
    message = sock.recv(1024)
    => the sender name is written in the message 
    if message:
        gobject.idle_add(myui.chat_window,sender, message)
        while not myui.message_status:
            time.sleep(0.1)
        myui.message_status = 0

Main Code:

if __name__ == "__main__":
    myui = UI()
    reception = Receive_Socket()
    reception.start()
    gtk.main()

My questions :

1 ) Does this kind of code seem efficient ? Is it ( Having a threaded receiving class along with my UI class ) the best way to proceed ?

2) By the time the message is displayed, the socket may have received 2 or more messages, so when it does message = sock.recv(1024) again, multiple messages will be concatenated in the variable message. I thought of including the message length in each message so if there is more than 1 message in the 1024 bytes it will take the message and put the rest in a message_buffer variable and before doing a sock.recv(1024) again it would check if the message_buffer variable contains anything and if so, put the message_buffer in the message variable instead of sock.recv(1024). Is there a easier/better solution to do this ?

Thanks in advance,

Nolhian

Was it helpful?

Solution

  1. No. Don't use threads. Instead, use glib.io_add_watch to make gtk/glib itself call your function when the socket is ready to read. That way you won't freeze your gui and won't need threads. You also won't need idle_add.

  2. If you do 1, you won't have this problem, since messages will arrive in order and there will be no concurrent threads to confuse the data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top