def handle(self): print(self.client_address) try: while True: try: self.recvData = json.loads(self.rfile.readline()[0:-1]) #print self.recvData except ValueError: break command = self.recvData['command'] if command == Pm.CREATEACCOUNT: print command result = DB.CreateAccount(self.recvData) if result: DB.SaveUserData() self.wfile.write(SuccessMessage(command)) else: self.wfile.write(FailMessage(command)) elif command == Pm.USERLOGIN: print command account = self.recvData['account'] result = DB.UserLogin(self.recvData) #print SocketLst if result: print self.recvData SocketLst[self.recvData['account']] = self.request AddrLst[self.recvData['account']] = [] AddrLst[self.recvData['account']].append(self) AddrLst[self.recvData['account']].append( self.recvData['port']) AddrLst[self.recvData['account']].append( self.recvData['localip']) DB.SaveUserData() self.wfile.write(SuccessMessage(command)) else: self.wfile.write(FailMessage(command)) elif command == Pm.USERLOGOUT: print command result = DB.UserLogout(self.recvData) if result: DB.SaveUserData() #SocketLst.pop(self.recvData['account'], None) #print SocketLst break #self.wfile.write(SuccessMessage(command)) else: pass #self.wfile.write(FailMessage(command)) elif command == Pm.MODIFYACCOUNT: print command result = DB.ModifyAccount(self.recvData) if result: DB.SaveUserData() self.wfile.write(SuccessMessage(command)) else: self.wfile.write(FailMessage(command)) print DB.UserData elif command == Pm.GETPROFILE: print command sendData = DB.GetProfile(self.recvData) self.wfile.write(sendData) elif command == Pm.GETFRIENDPROFILE: print command sendData = DB.GetFriendProfile(self.recvData) self.wfile.write(sendData) elif command == Pm.DELETEACCOUNT: print command result = DB.DeleteAccount(self.recvData) if result: DB.SaveUserData() #self.wfile.write(SuccessMessage(command)) else: pass #self.wfile.write(FailMessage(command)) elif command == Pm.SEARCHACCOUNT: print command result = DB.SearchUser(self.recvData) if result: self.wfile.write(SuccessMessage(command)) else: self.wfile.write(FailMessage(command)) elif command == Pm.FRIENDREQUEST: print command print self.recvData result = DB.FriendRequest(self.recvData) print result if result: DB.SaveUserData() if self.recvData['to'] in SocketLst: sendData = DB.FriendRequestPacket(self.recvData) print sendData SocketLst[self.recvData['to']].sendall(sendData) #self.wfile.write(SuccessMessage(command)) else: pass #self.wfile.write(FailMessage(command)) elif command == Pm.SHOWFRIENDREQUEST: print command data = DB.GetAllFriendRequest(self.recvData) print data self.wfile.write(data) elif command == Pm.CHANGESTATE: print command result = DB.ChangeState(self.recvData) if result: self.wfile.write(SuccessMessage(command)) print DB.OnlineLst print DB.BusyLst print DB.OfflineLst else: self.wfile.write(FailMessage(command)) elif command == Pm.ASKINGINFO: print command data = CR.AskingUpdate(self.recvData) #print data self.wfile.write(data) elif command == Pm.ACCEPTINVITE: print command result = DB.AcceptFriendRequest(self.recvData) if not result: print 'accept gg' elif command == Pm.REJECTINVITE: print command result = DB.RejectFriendRequest(self.recvData) if not result: print 'reject gg' elif command == Pm.CHATTOONE: print command sendData = CR.CreateMessage(self.recvData) if self.recvData['to'] in SocketLst: SocketLst[self.recvData['to']].sendall(sendData) elif command == Pm.SOUNDREQUEST: print command sendData = CR.SoundRequest(self.recvData) if self.recvData['to'] in SocketLst: SocketLst[self.recvData['to']].sendall(sendData) elif command == Pm.SENDSOUND: print command sendData = CR.CreateSoundMessage(self.recvData) if self.recvData['to'] in SocketLst: SocketLst[self.recvData['to']].sendall(sendData) elif command == Pm.CREATEGROUP: print command result = CR.CreateGroup(self.recvData) CR.SaveGroupData() elif command == Pm.ADDMEMBER: print command result = CR.AddMemberToGroup(self.recvData) #print result #print self.recvData if result: CR.SaveGroupData() #print CR.GroupTable self.wfile.write(SuccessMessage(command)) else: self.wfile.write(FailMessage(command)) elif command == Pm.REMOVEMEMBER: print command result = CR.RemoveMemberFromGroup(self.recvData) if result: self.wfile.write(SuccessMessage(command)) else: self.wfile.write(FailMessage(command)) elif command == Pm.CHATTOGROUP: print command sendData = CR.CreateGroupMessageDict(self.recvData) sendStr = json.dumps(sendData) for member in sendData['member']: if member in SocketLst and not member == self.recvData[ 'account']: SocketLst[member].sendall(sendStr) elif command == Pm.REQUESTIP: print command sendData = RequestIP(self.recvData) self.wfile.write(sendData) elif command == Pm.PIKAREQUEST: print command sendData = PikaRequest(self.recvData) SocketLst[self.recvData['to']].sendall(sendData) elif command == Pm.ACCEPTPIKA: print command sendData = PikaAccept(self.recvData) SocketLst[self.recvData['to']].sendall(sendData) elif command == Pm.REJECTPIKA: print command sendData = PikaReject(self.recvData) SocketLst[self.recvData['to']].sendall(sendData) elif command == Pm.REMOVEFRIEND: print command result = DB.RemoveFriend(self.recvData) if result: self.wfile.write(SuccessMessage(command)) else: self.wfile.write(FailMessage(command)) #print DB.UserData #self.data = json.loads(self.data) #self.wfile.write(self.data) except socket.error: try: a = account print 'account leave:' % account if a in DB.OnlineLst: DB.OnlineLst.remove(a) if a in DB.BusyLst: DB.BusyLst.remove(a) if a in DB.OfflineLst: DB.OfflineLst.remove(a) if a in DB.LoginLst: DB.LoginLst.remove(a) except: pass try: for one in SocketLst: if SocketLst[one] is self.request: key = one a = key break SocketLst.pop(key, None) if a in DB.OnlineLst: DB.OnlineLst.remove(a) if a in DB.BusyLst: DB.BusyLst.remove(a) if a in DB.OfflineLst: DB.OfflineLst.remove(a) if a in DB.LoginLst: DB.LoginLst.remove(a) except KeyError: pass print SocketLst print DB.LoginLst