经过前面的协议处理。DB封装,用户封装以后。
下面进入业务处理单元。
在logic.py的Logic类中。
1:识别协议与分发
def handlePackage(self, connection , package):
"""
逻辑处理部分
"""
if isinstance(package, PackageRegister):
self.handleUserRegister(connection , package)
elif isinstance(package, PackageLogin):
self.handleUserLogin(connection , package)
elif isinstance(package, PackageGetNotFriendsByCodeAndDate):
self.handleGetNotFriendsWithCodeAndDate( connection , package )
elif isinstance( package , PackageAddFriendRequest ):
self.handleAddFriendRequest( connection, package )
elif isinstance( package , PackageAddFriendStatus ):
self.handleAddFriendRequestStatus( connection , package )
elif isinstance( package , PackageGetFriends ):
self.handleGetFriends( connection, package )
elif isinstance( package , PackageDeleteFriend ):
self.handleDeleteFriend( connection , package )
elif isinstance( package , PackageGetFriendDetail ):
self.handleGetFriendDetail( connection , package )
elif isinstance( package , PackageSendChatMessage ):
self.handleSendChatMessage( connection , package )
分发需要处理的内容看脑图
举例说明登录部分:
def handleUserLogin(self, connection , package):
"""
用户登录处理
"""
retPackage = SendToClientPackage('login')
#step 1,检查参数合法性
if self.findBadInput(package.username) or self.findBadInput(package.password):
retPackage.errcode = PACKAGE_ERRCODE_INPUTWRONG
else:
#step 2. 查询数据库
db_user = self.dbEngine.isUserExist(package.username, package.password)
if db_user:
#step 1. 枚举在线好友,如果在线,退掉
online_user = self.onlineUsers.getUserExistByUsername(package.username)
if online_user:
#step 1.发送异地登录消息
another = SendToClientPackage('anotherlogin')
another.status = 1
online_user.connection.send_message( json.dumps( another , cls= ComplexEncoder ) )
#step 2.关闭联接
online_user.connection.close()
#从新加入到在线用户
user = UserObject(connection, db_user)
self.onlineUsers.addNewOnlineUser(user)
retPackage.status = 1
retPackage.obj = SendToClientPackageUser( user.DBUser.uid,
user.DBUser.username,
user.DBUser.sex,
user.DBUser.description)
#加载好友列表
self.getUserFriendsWithDBAndOnLineUsers( user )
#检查离线消息,是否有人希望添加我为好友
self.getAllAddFriendRequestFromDBAndSendToClient( user )
#是否有人给我发离线消息
self.getOfflineChatMessageAndSendWithUser( user )
#广播好友列表,通知本人上线
self.broadcastOnlineStatusToAllFriend( user , 1 )
#修改在线列表,本人上线
self.setUserOnlineInOnlineUsersFriends( user )
else:
#用户不存在,提醒注册
retPackage.errcode = 10010
connection.send_message( json.dumps(retPackage, cls=ComplexEncoder) )