def __init__(self): gtk.Window.__init__(self) self.msg=MsgFactory() self.set_default_size(400,200) self.set_position(gtk.WIN_POS_CENTER_ALWAYS) self.set_keep_above(True) self.set_decorated(False) self.connect('key-press-event',self.login) self.layout=LayOut(self) self.add(self.layout) #self.fix=gtk.Fixed() #self.fix.set_size_request(400,100) #self.layout.put(self.fix,0,50) label=gtk.Label("用户名") self.layout.put(label,100,55) self.username=gtk.Entry() self.username.set_has_frame(False) self.username.grab_focus() self.username.set_size_request(150,25) self.layout.put(self.username,150,50) label=gtk.Label("密 码") self.layout.put(label,100,105) self.password=gtk.Entry() self.password.set_has_frame(False) self.password.set_size_request(150,25) self.layout.put(self.password,150,100)
class Login(gtk.Window): def __init__(self): gtk.Window.__init__(self) self.msg=MsgFactory() self.set_default_size(400,200) self.set_position(gtk.WIN_POS_CENTER_ALWAYS) self.set_keep_above(True) self.set_decorated(False) self.connect('key-press-event',self.login) self.layout=LayOut(self) self.add(self.layout) #self.fix=gtk.Fixed() #self.fix.set_size_request(400,100) #self.layout.put(self.fix,0,50) label=gtk.Label("用户名") self.layout.put(label,100,55) self.username=gtk.Entry() self.username.set_has_frame(False) self.username.grab_focus() self.username.set_size_request(150,25) self.layout.put(self.username,150,50) label=gtk.Label("密 码") self.layout.put(label,100,105) self.password=gtk.Entry() self.password.set_has_frame(False) self.password.set_size_request(150,25) self.layout.put(self.password,150,100) def run(self): self.show_all() self.connector=reactor.connectTCP(HOST,PORT,ClientFactory(self)) self.transport=self.connector.transport reactor.run() def login(self,widget,event): if event.keyval == 65293: username=self.username.get_text() password=self.password.get_text() self.sendLoginMsg(username,password) if event.keyval == 65307: self.destroy() gtk.main_quit() def sendLoginMsg(self,username,password): Type='CUSTOM_USER_LOGIN' PassWord=md5(password).hexdigest() UserName=username SerialNumber=0 msg=self.msg.packMsg(Type,UserName,PassWord,SerialNumber) print("Sending login-msg ...") self.transport.write(msg) print("Done") for child in self.layout.get_children(): self.layout.remove(child)
class ChatFactory(Factory): def __init__(self): self.clients = {} self.loopcall=LoopingCall(self.getOnLineUser) self.loopcall.start(60,False) self.msg=MsgFactory() #self.mysql=MySql() def buildProtocol(self,addr): protocol=Chat() protocol.factory=self protocol.msg=self.msg #protocol.mysql=self.mysql return protocol def stopFactory(self): #logger.info("Shutdown database connection") for transport in self.clients.values(): transport.loseConnection() #self.mysql.close() #logger.info("Done") def addClient(self,*newclient): if newclient[0] in self.clients : self.clients.pop(newclient[0]) self.clients[newclient[0]]=newclient[1] def delClient(self,client): if self.clients.has_key(client): self.clients.pop(client) def getClient(self,guid): return self.clients.get(guid,None) def sendAll(self, message): for proto in self.clients: proto.transport.write(message + "\n") def sendNotify(self,guid,mysql,notify): ''' users is a generator object ''' users=mysql.getUsers(guid) for user in users: if user in self.clients: if user != guid: logger.debug("Sending {!s}...{!s}".format(notify,user)) msg=self.msg.packMsg(notify,guid) logger.debug("Sending {!s} msg : {!s}".format(notify,msg)) self.clients[user].transport.write(msg) def getOnLineUser(self): user=len(self.clients) logger.debug("Online user num is {}".format(user))
def __init__(self): self.clients = {} self.loopcall=LoopingCall(self.getOnLineUser) self.loopcall.start(60,False) self.msg=MsgFactory()