Ice notes-asynchronous programming demo

Source: Internet
Author: User
Tags class manager

I recently learned about asynchronous communication. asynchronous communication includes the asynchronous dispatch (AMD) of the server and the asynchronous call (AMI) of the client ).

The following describes how to implement simple asynchronous communication (which is mainly implemented by the client, which AMD has not yet reflected ):

 

1. First write the ICE file: Demo. Ice

module Demo{interface Employee{      ["amd"]void add(int a, int b, out int sum);};};

["Amd"] indicates metadata, indicating that the server will provide method functions for asynchronous dispatch. The add function has two in parameters. One out parameter is used as the return parameter.

 

2. Map to C ++ code: Use the command slice2cpp demo. Ice

After ing, the program will generate the demo. h and demo. cpp files, as shown in demo. h In the following code.

// ....More code beforenamespace Demo{class Employee : virtual public ::Ice::Object{public:    typedef EmployeePrx ProxyType;    typedef EmployeePtr PointerType;        virtual ::Ice::ObjectPtr ice_clone() const;    virtual bool ice_isA(const ::std::string&, const ::Ice::Current& = ::Ice::Current()) const;    virtual ::std::vector< ::std::string> ice_ids(const ::Ice::Current& = ::Ice::Current()) const;    virtual const ::std::string& ice_id(const ::Ice::Current& = ::Ice::Current()) const;    static const ::std::string& ice_staticId();    virtual void add_async(const ::Demo::AMD_Employee_addPtr&, ::Ice::Int, ::Ice::Int, const ::Ice::Current& = ::Ice::Current()) = 0;    // ... More code here    };// ...

 

3. Implement the server program

Create a server. cpp file and implement the virtual method add_async () in demo. h. Create a new servant class to inherit the ice: Application class, initialize the ice run time, and register the servant to the Object Adapter.

# Include <demo. h> # include <ice/application. h> # include <iceutil/monitor. h ># include <iostream> using namespace STD; using namespace demo; Class MANAGER: virtual public employee, public iceutil: Monitor <iceutil: mutex> {public: Manager (): _ sum (0 ){}~ Manager () {} virtual void add_async (const: Demo: amd_employee_addptr & add_ptr, int A, int B, const: ice: Current & =:: ice :: current () {_ sum = a + B; sleep (1); cout <"the sum is" <_ sum <". "<Endl; add_ptr-> ice_response (_ sum); // after data is processed, call the callback function to call the method implemented by the client.} PRIVATE: int _ sum;}; Class myapplication: virtual public ice: Application {public: Virtual int run (INT argc, char * argv []) {ice: objectadapterptr adapter = communicator ()-> createobjectadapterwithenderson points ("gameserver ", "default-P 10000"); ice: objectptr manptr = new manager; adapter-> Add (manptr, communicator ()-> stringtoidentity ("manserver ")); // register the servant class Object Pointer manptr to the adapter-> activate (); // activate the adapter so that the client can access the added servant activity table communicator () in the adapter () -> waitforshutdown () ;}}; int main (INT argc, char * argv []) {myapplication app; app. main (argc, argv); Return 0 ;}

4. implement client programs

Create a client. cpp file, implement the callback function, and create a proxy to access the specified servant.

# Include <ice/ice. h> # include <demo. h> # include <iostream> # include <iceutil/iceutil. h> using namespace STD; using namespace demo; Class client_add: Public amd_employee_add {public: Virtual void ice_response (INT sum) {cout <"receive from server -- the sum is" <sum <". "<Endl;} virtual void ice_exception () {} virtual void ice_exception (const STD: exception & E) {} void failed_operation (const ice: exception & E ){ }; Typedef iceutil: handle <client_add> clientaddptr; Class clientapp: virtual public ice: Application {public: Virtual int run (INT argc, char * argv []) {ice: objectprx base = communicator ()-> stringtoproxy ("manserver: default-P 10000"); employeeprx EMP = employeeprx: checkedcast (base); If (! EMP) throw "invalid proxy! "; Clientaddptr amd_add = new client_add; callback_employee_addptr addptr = newcallback_employee_add (amd_add, & client_add: ice_response, & client_add: failed_operation ); // implement the callback Object Pointer emp-> begin_add (10, 20, addptr); // The third parameter is the intelligent pointer cout of the callback class <"I'm waiting for the data from, but not blocked. "<Endl ;}}; int main (INT argc, char * argv []) {clientapp; app. main (argc, argv); Return 0 ;}

 

5. compile server. CPP: G ++ demo. CPP server. CPP-O Server-lice-liceutil-I. /-I/opt/ice-3.4.2/include-L/opt/ice-3.4.2/lib

Compile client. CPP: G ++ demo. CPP client. CPP-O client-lice-liceutil-I. /-I/opt/ice-3.4.2/include-L/opt/ice-3.4.2/lib

Run the server:./Server Run the client:./client

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.