7. Application and release of Khala equipment resources

Source: Internet
Author: User

In the actual business, each time we log in, we may need to maintain some device-related resource information on the server, and the different devices need to maintain the same resource information. Khala provides the maintenance and storage capabilities of equipment resources, which we present in a specific business.

A user chat device, the user can add a new friend ID, or can query all currently added friend ID, so the server should record and maintain the current friend ID for the login device. However, in the default implementation of Khala, the current friend ID, which is closely related to the business, obviously does not provide the interface directly. However, Khala provides a extracontext interface, which is implemented by the any type in the boost library to maintain different device resource information. The specific business implementation is as follows:

We are still modifying under the Myusrtype type we implemented earlier. First we implement a user device resource class Usrinfo, in which we maintain the friend ID through set and provide an interface for adding and querying the friend ID. In Usrinfo's destructor, we track the life cycle of a resource object by printing the log.

classUsrinfo { Public: Usrinfo (ConstSTD::string&name): name_ (name) {}Virtual~Usrinfo () {Log_info<<"usrinfo Name:"<< name_ <<"is destructed!"; }    //Add a friend's ID    voidAddfriends (UINTID) {Myfriends_.insert (ID); }    //get IDs for all your friendsstd::vector<UINT>getallfriends () {std::vector<UINT>friends;        Friends.assign (Myfriends_.begin (), Myfriends_.end ()); returnfriends; }    ConstSTD::string& GetName ()Const {        returnname_; }Private:    //Current user nameSTD::stringname_; //The current user added friend IDs11STD::Set<UINT>Myfriends_;};

In Myusrtype's onloginsuccessmsg, we applied for usrinfo resources and saved their pointers to Infonodeptr Extracontext. In practical use, we can manage the requested resource usrinfo by using the smart pointer shared_ptr, which extracontext the smart pointer that will be saved as Usrinfo.

BOOLMyusrtype::onloginsuccessmsg (khala::infonodeptr&infonodeptr, Json::value&msg, Khala::timestamp time) {std::stringName =msg[key_login_name].asstring (); //Save the requested Usrinfo pointer to the ExtracontextInfonodeptr->setextracontext (Newusrinfo (name));    Std::stringstream SS; SS<<"login success! Your ID is:"<< infonodeptr->getId (); Infonodeptr-Send (Ss.str ()); return true;}

We can give myusrtype to create addfriend and getfriends two message events, add methods see 5, Login Management device type + Create a new device type, in the implementation of these two message events, We obtain usrinfo resources through Extracontext and read and write the resource information.

BOOLMyusrtype::onaddfriend (khala::infonodeptr& infonodeptr, json::value&msg, Khala::timestamp time) {    UINTfriendID =Msg[key_friend_id].asuint (); //try to get usrinfo resourcesusrinfo* Usrinfo = boost::any_cast<usrinfo*>(Infonodeptr-Getextracontext ()); if(Usrinfo = =0)        return false; //Write Usrinfo ResourcesUsrinfo->addfriends (friendID);    Std::stringstream SS; SS<<"Add friend ID:"<< friendID <<"Success!"; Infonodeptr-Send (Ss.str ()); return true;}BOOLMyusrtype::ongetfriends (khala::infonodeptr& infonodeptr, json::value&msg, Khala::timestamp time) {    //try to get usrinfo resourcesusrinfo* Usrinfo = boost::any_cast<usrinfo*>(Infonodeptr-Getextracontext ()); if(Usrinfo = =0)        return false; //reading information from a usrinfo resourcestd::vector<UINT> friends = usrinfo->getallfriends ();    Std::stringstream SS; SS<<"My friends ID:"; Std::vector<UINT>::iterator it =Friends.begin ();  for(; It!=friends.end (); it++) {SS<<*it<<" "; } infonodeptr-Send (Ss.str ()); return true;}

Because the Usrinfo resource is the resource that we request during the life cycle of maintaining the Myusrtype type of device, we must release the requested Usrinfo resource when the device exits its lifecycle to end. The release of equipment resources is done in Releaseconnnode.

void Myusrtype::releaseconnnode (khala::infonodeptr& infonodeptr,        khala::timestamp time) {    UsrInfo * Usrinfo = boost::any_cast<usrinfo*>(                infonodeptr-getextracontext ());     if 0 {        // release the resources requested by the device        Delete  usrinfo        ; 0 ;    }}

We test it through the client program (./example/testclient/hellokhalaclient3.py). First select the Usrtype type for the login operation.

Then we add a friend,id of 100 and then query all friend IDs, showing only 1001 IDs.

We then add a friend,id for this user to 200, and then query through Getfriends, which shows that there are 100 and 2002 IDs.

Finally, we opt out and check the logs to see if the resources we have requested can be successfully released as the device account exits. The log shows that the Usrinfo resource requested by our account named Moss1 has been successfully released with the end of the device life cycle.

Through the above examples, we realized the application management and release of myusrtype type equipment to usrinfo resources. Khala users can request and manage the appropriate device resources for different devices based on their actual business needs.

7. Application and release of Khala equipment resources

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.