Chat

BBands <[email protected]>
Newsgroups gmane.comp.python.pythoncard
Message-ID <[email protected]>
I am expanding the Chat.py sample ap a bit and have a question.

If the ap is minimized or under another window when a message comes in
I'd like to restore it or pop it to the fore to get the receiver's
attention. How can I do this? For convenience I've attached chat.py
below.

     jab
-- 
John Bollinger, CFA, CMT
www.BollingerBands.com

If you advance far enough, you arrive at the beginning.

----

#!/usr/bin/python

"""
__version__ = "$Revision: 1.6 $"
__date__ = "$Date: 2004/05/05 16:53:25 $"
"""

from PythonCard import model
import threading
import Queue
import wx

# EchoServer derived
# from echo server example in Programming Python by Mark Lutz

# get socket constructor and constants
import socket
# server machine, '' means local host
myHost = ''
# listen on a non-reserved port number
myPort = 50007

class EchoServer:
    def __init__(self, parent):
        self._parent = parent
        self.keepRunning = 1

    def server(self):
        # make a TCP socket object
        sockobj = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        # bind it to server port number
        sockobj.bind((myHost, myPort))
        # listen, allow 5 pending connects
        sockobj.listen(5)

        # listen until process killed
        while self.keepRunning:
            #print 'outer loop'
            connection, address = sockobj.accept()   # wait for next
client connect
            #print 'Server connected by', address     # connection is
a new socket
            while self.keepRunning:
                #print 'inner loop'
                # read next line on client socket
                data = connection.recv(1024)
                if not data: break
                self._parent.msgQueue.put(data)
                wx.WakeUpIdle()
            connection.close()

# The same port as used by the server
PORT = myPort

def echoSend(host, port, txt):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host, port))
    s.send(txt)
    s.close()

class Chat(model.Background):

    def on_initialize(self, event):
        self.msgQueue = Queue.Queue()
        self.components.fldYourIPAddress.text =
socket.gethostbyname(socket.gethostname())
        self.echoServer = EchoServer(self)
        self.thread = threading.Thread(target = self.echoServer.server)
        # I think this allows Python to kill the thread when we quit wxPython
        # setDaemon must be called before start
        self.thread.setDaemon(1)
        self.thread.start()

    def on_idle(self, event):
        if not self.msgQueue.empty():
            msg = self.msgQueue.get()
            self.doDisplayMsgReceived(msg)
            event.RequestMore()

    def doDisplayMsgReceived(self, data):
        if data is not None:
            self.components.fldTranscript.appendText(data + '\n')
        else:
            pass

    def on_btnSend_mouseClick(self, event):
        #print "btnSend", self.components.fldSendAddresses.text, PORT,
self.components.fldInput.text
        txt = self.components.fldNickname.text + \
            " (" + self.components.fldYourIPAddress.text + "): " + \
            self.components.fldInput.text
        addresses = self.components.fldSendAddresses.text.split(',')
        #print addresses
        for ip in addresses:
            echoSend(ip.strip(), PORT, txt)
        self.components.fldTranscript.appendText(txt + '\n')
        self.components.fldInput.text = ""
        #print "after send"

    def on_close(self, event):
        self.echoServer.keepRunning = 0
        event.skip()


if __name__ == '__main__':
    app = model.Application(Chat)
    app.MainLoop()

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.