6. Khala Login Life Cycle Management

Source: Internet
Author: User

Khala enables lifecycle management of devices and provides lifecycle-related interfaces that allow users to customize business-related operations while Khala lifecycle management by overriding these lifecycle interfaces in a specific device type implementation class. The specific interface is explained as follows:

Onlogincheckmsg ():

Login check, in this can be queried by the DB and other means to check the login device account is legitimate, you can set the device ID for the connection device (this ID must be unique, will be treated as a device key, if the recurrence will be executed onloginfailuremsg, if not set to the temporary ID value as the ID key) , if the account check fails, set the failed reply message and return directly to false. If the check succeeds and the set ID is not duplicated, the login operation is completed and Onloginsuccessmsg is executed.

Onloginsuccessmsg ():

Login successful, you can perform the related operation after successful login. At this point the device enters the lifecycle of the login device.

Onloginfailuremsg ():

Login exception, the set ID and the logged on Device ID conflict, it is generally indicated that the account is logged in.

Onlogoutmsg ():

The logout operation indicates that the account is logged out. At the end of this operation, Releaseconnnode will be executed to release the associated resources for the device and end the device's logon life cycle and eventually disconnect.

Releaseconnnode ():

Frees the device resources. will be executed when exiting the device life cycle, typically after the device performs a logoff operation, but also when the device exits unexpectedly.

We experience the Khala device life cycle by creating a Myusrtype device type. Myusrtype Enjoy login device management by inheriting from NodeType, and rewrite the relevant lifecycle interface for customization.

We created a table called Usrinfo in MySQL to record device account information. We have created two new accounts, Moss1 and Moss2, and we have maintained different IDs for each account through DB.

We check the DB for login account information in onlogincheckmsg. The specific DB operation we can achieve through the encapsulation mysql++, specific related to Baidu mysql++ documents. If the account does not exist, we reply to the login failure message, and return false, that is, the end of the login request operation. If the account exists, we will save the ID in DB as the device ID. and returns True.

Virtual BOOLOnlogincheckmsg (khala::infonodeptr&infonodeptr, Json::value&msg, Khala::timestamp time) {    //Get account nameSTD::stringName =msg[key_login_name].asstring (); //Get PasswordSTD::stringpasswd =msg[key_passwd].asstring (); //Querying DBUsrinforesult res =usrinfodao::getinstance (). Queryusrinfo (NAME,PASSWD); if(!res.issuccess ()) {        //The account does not existInfonodeptr->send ("Login Failure,err account!"); return false; }    //Get DB Query InformationUsrinfodo Usrinfodo =Res.getusrinfodo (); //set the device ID to the ID saved in DBInfonodeptr->setId (usrinfodo.id); return true;}

After that, the system will check the logged-in device with the ID as key, if the ID exists on the logged-on device, it indicates that the account is logged in and the onloginfailuremsg operation will be performed. Here we implement the onloginfailuremsg operation is very simple, just tell the client that the ID of the corresponding device account has been logged in, this login failed.

Virtual bool onloginfailuremsg (khala::infonodeptr& infonodeptr,            json::value& msg, Khala: : Timestamp time) {    infonodeptr->send ("login failure,logined id! " );     return true ;}

If the ID is not logged in, the appropriate logon action is performed to save the connection information to the login device pool. And finally executes the onloginsuccessmsg, indicates that the device account login is successful, formally entered the login life cycle. Here we can implement the relevant operation after successful login. Our implementation here is also very simple, telling the client that the login is successful, and the device ID is how much.

Virtual bool onloginsuccessmsg (khala::infonodeptr& infonodeptr,        json::value& msg, Khala: : Timestamp time) {    std::stringstream ss;     " login success! Your ID is: " << infonodeptr->getId ();    Infonodeptr,Send (Ss.str ());     return true ;}

As for the logoff operation, still need to implement the specific and business-related message return on the line, the Khala system receives the logoff operation, will handle the previously maintained device information, release and call Releaseconnnode release the resources of the device request, Finally, end the lifetime of this login and disconnect.

Virtual bool onlogoutmsg (khala::infonodeptr& infonodeptr, json::value& msg,        khala::timestamp time) {    Std::stringstream ss;     " logout success! Your ID is: " << infonodeptr->getId ();    Infonodeptr,Send (Ss.str ());     return true ;}

Because this device does not actively request the resources, so the releaseconnnode operation does not need to do anything, but in order to demonstrate Releaseconnnode in the device logon life cycle time, we through the system log for related output.

Virtual void Releaseconnnode (khala::infonodeptr& infonodeptr,            khala::timestamp time) {    " This is Node ID:""  release function! " ;}

Finally, we'll also set the type name for our Myusrtype and register it in Main, not here.

Myusrtype's complete code is at the end.

We test (./example/testclient/hellokhalaclient3.py) through the client.

We choose the login operation, and select the login type is Usrtype, and then enter the MOSS1 account password, which shows that our login is successful, and the ID is 1, is the corresponding ID in DB moss1. We then query the IsLogin command, which shows that we have logged in. Querying Devtype is exactly the type of myusrtype we created.

If we enter an incorrect account password, the result returns the wrong account, and the query IsLogin display is not logged in.

At this time our MOSS1 account has been logged in, if we open a client, through the MOSS1 account login operation. This returns the processing in Onloginfailuremsg, where the account has been logged in elsewhere, and the login failed.

We then log off on the MOSS1 account that is already logged in. We receive the correct logoff operation back and the connection is disconnected.

At this point we query Khala server log, showing that Releaseconnnode was executed successfully.

We open the client again, and log in with the Moss1 account, and then through the Ctr+c Force client exit, at this time we check the log, the system detects the client abnormal exit, while the Releaseconnnode is still being executed. So if we apply for a resource in the login connection, regardless of whether the client ended up being properly logged out or exited abnormally, as long as we execute the correct request resource release operation in Releaseconnnode, it will not cause the server to fail because of resource leakage.

The complete Myusrtype Class code:

#include <khala/NodeType.h>#include<sstream>#include"dao/usrinfodao.h"classMyusrtype: PublicKhala::nodetype { Public: Myusrtype (); Virtual~Myusrtype (); Virtual BOOLOnlogincheckmsg (khala::infonodeptr&infonodeptr, Json::value&msg, Khala::timestamp time) {std::stringName =msg[key_login_name].asstring (); STD::stringpasswd =msg[key_passwd].asstring (); Usrinforesult Res=usrinfodao::getinstance (). Queryusrinfo (name, passwd); if(!res.issuccess ()) {Infonodeptr->send ("Login Failure,err account!"); return false; } Usrinfodo Usrinfodo=Res.getusrinfodo (); Infonodeptr-setId (usrinfodo.id); return true; }    Virtual BOOLOnloginsuccessmsg (khala::infonodeptr&infonodeptr, Json::value&msg, Khala::timestamp time)        {Std::stringstream ss; SS<<"login success! Your ID is:"<< infonodeptr->getId (); Infonodeptr-Send (Ss.str ()); return true; }    Virtual BOOLOnloginfailuremsg (khala::infonodeptr&infonodeptr, Json::value&msg, Khala::timestamp time) {Infonodeptr->send ("Login failure,logined id!"); return true; }    Virtual BOOLOnlogoutmsg (khala::infonodeptr& infonodeptr, json::value&msg, Khala::timestamp time)        {Std::stringstream ss; SS<<"logout success! Your ID is:"<< infonodeptr->getId (); Infonodeptr-Send (Ss.str ()); return true; }    Virtual voidReleaseconnnode (khala::infonodeptr&infonodeptr, Khala::timestamp time) {Log_info<<"This is Node ID:"<< infonodeptr->getId ()<<"Release function!"; }    Virtual ConstSTD::string&Getobjecttypename () {StaticSTD::stringtypestr (My_usr_type); returnTypestr; }};

6. Khala Login Life Cycle Management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.