04.08.2014 Views

o_18ufhmfmq19t513t3lgmn5l1qa8a.pdf

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 24 ■ PROJECT 5: A VIRTUAL TEA PARTY 471<br />

def enter(self, room):<br />

# Remove self from current room and add self to<br />

# next room...<br />

try: cur = self.room<br />

except AttributeError: pass<br />

else: cur.remove(self)<br />

self.room = room<br />

room.add(self)<br />

def collect_incoming_data(self, data):<br />

self.data.append(data)<br />

def found_terminator(self):<br />

line = ''.join(self.data)<br />

self.data = []<br />

try: self.room.handle(self, line)<br />

except EndSession:<br />

self.handle_close()<br />

def handle_close(self):<br />

async_chat.handle_close(self)<br />

self.enter(LogoutRoom(self.server))<br />

class ChatServer(dispatcher):<br />

"""<br />

A chat server with a single room.<br />

"""<br />

def __init__(self, port, name):<br />

dispatcher.__init__(self)<br />

self.create_socket(socket.AF_INET, socket.SOCK_STREAM)<br />

self.set_reuse_addr()<br />

self.bind(('', port))<br />

self.listen(5)<br />

self.name = name<br />

self.users = {}<br />

self.main_room = ChatRoom(self)<br />

def handle_accept(self):<br />

conn, addr = self.accept()<br />

ChatSession(self, conn)<br />

if __name__ == '__main__':<br />

s = ChatServer(PORT, NAME)<br />

try: asyncore.loop()<br />

except KeyboardInterrupt: print

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!