Python/asynchatPong.py
author morrowa@betelgeuse.local
Tue Jun 23 11:44:30 2009 -0700 (2009-06-23)
changeset 51 de59ce19f42e
permissions -rw-r--r--
BROKEN COMMIT. Majority of code to handle closing has been added. Listeners do not close correctly.
morrowa@51
     1
# asynchatPong
morrowa@51
     2
# Listener using asynchat
morrowa@51
     3
# Not related to BLIP - just to aid in my understanding of what's going on
morrowa@51
     4
# Sends "Pong" when it gets "Ping"
morrowa@51
     5
morrowa@51
     6
import sys
morrowa@51
     7
import traceback
morrowa@51
     8
import socket
morrowa@51
     9
import asyncore
morrowa@51
    10
import asynchat
morrowa@51
    11
morrowa@51
    12
class asynchatPongListener(asyncore.dispatcher):
morrowa@51
    13
    def __init__(self, port):
morrowa@51
    14
        asyncore.dispatcher.__init__(self)
morrowa@51
    15
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
morrowa@51
    16
        self.bind( ('', port) )
morrowa@51
    17
        self.listen(2)
morrowa@51
    18
        self.shouldAccept = True
morrowa@51
    19
    
morrowa@51
    20
    def handle_accept(self):
morrowa@51
    21
        if self.shouldAccept:
morrowa@51
    22
            sock, addr = self.accept()
morrowa@51
    23
            self.conn = asynchatPong(sock, self)
morrowa@51
    24
            self.shouldAccept = False
morrowa@51
    25
    
morrowa@51
    26
    def handle_error(self):
morrowa@51
    27
        (typ,val,trace) = sys.exc_info()
morrowa@51
    28
        print "Listener caught: %s %s\n%s" % (typ,val,traceback.format_exc())
morrowa@51
    29
        self.close()
morrowa@51
    30
    
morrowa@51
    31
    def handle_close(self):
morrowa@51
    32
        print "Listener got close"
morrowa@51
    33
        asyncore.dispatcher.handle_close(self)
morrowa@51
    34
morrowa@51
    35
class asynchatPong(asynchat.async_chat):
morrowa@51
    36
    def __init__(self, socket, listener):
morrowa@51
    37
        asynchat.async_chat.__init__(self, socket)
morrowa@51
    38
        self._listener = listener
morrowa@51
    39
        self.set_terminator("Ping")
morrowa@51
    40
    
morrowa@51
    41
    def collect_incoming_data(self, data):
morrowa@51
    42
        """called when arbitrary amount of data arrives. we just eat it"""
morrowa@51
    43
        pass
morrowa@51
    44
    
morrowa@51
    45
    def found_terminator(self):
morrowa@51
    46
        """called when the terminator we set is found"""
morrowa@51
    47
        print "Found 'Ping'"
morrowa@51
    48
        self.push("Pong")
morrowa@51
    49
        print "Sent 'Pong'"
morrowa@51
    50
    
morrowa@51
    51
    def handle_close(self):
morrowa@51
    52
        print "Closed; closing listener"
morrowa@51
    53
        self._listener.close()
morrowa@51
    54
        asynchat.async_chat.handle_close(self)
morrowa@51
    55
    
morrowa@51
    56
morrowa@51
    57
if __name__ == '__main__':
morrowa@51
    58
    pong = asynchatPongListener(1337)
morrowa@51
    59
    asyncore.loop()