#coding=utf-8
from asyncore import dispatcher
from asynchat import async_chat
import socket,asyncore

PORT=5005
NAME='TestChart'

class ChartSession(async_chat):
    """
    处理单个用户与服务器之间链接
    """
    def __init__(self,server,sock):
        async_chat.__init__(self,sock)
        self.server = server
        self.set_terminator('\r\n')
        self.data = []
        self.push('Welcome to %s \r\n' %self.server.name)

    def collect_incoming_data(self, data):
        self.data.append(data)

    def found_terminator(self):
        line = ''.join(self.data)
        self.data = []
        self.server.broadcast(line)
    def handle_close(self):
        async_chat.handle_close(self)
        self.server.disconnect(self)

class ChatServer(dispatcher):
    def __init__(self,port,name):
        dispatcher.__init__(self)
        self.create_socket(socket.AF_INET,socket.SOCK_STREAM)
        self.set_reuse_addr()
        self.bind(('',port))
        self.name = name
        self.listen(5)
        self.session = []

    def disconnect(self,session):
        self.session.remove(session)

    def broadcast(self,line):
        for session in self.session:
            session.push(line+'\r\n')

    def handle_accept(self):
        conn,addr = self.accept()
        self.session.append(ChartSession(self,conn))

if __name__=='__main__':
    s = ChatServer(PORT,NAME)
    try:
        asyncore.loop()
    except KeyboardInterrupt:
        print '停止了'