How to Use ICE Service

Source: Internet
Author: User

I recently studied how to use the network communication middleware ICE. According to the official manual, I wrote a "Hello World" program that programmers liked, both the server and client are developed in C ++, and the communication protocol uses the default TCP. I feel that ICE has the following advantages:

1. platform independence. Both the client and server can be developed using the popular development language (C ++/JAVA/C #/php), and the language difference is shielded. Currently, the popular method is to develop the client in C # and communicate directly with the server developed in C ++.

2. Diversity of communication protocols. Now you can select TCP, UDP, and HTTP for communication. If you have high security requirements, you can select SSL to encrypt the transmitted data.

In addition, ICE also provides some expansion components to achieve Load Balancing (ICEGrid) for network communication, unified management of communication nodes (ICEBOX), automatic program update (ICEPatch), and so on, facilitating application expansion.

Anyone who has used ICE knows that when implementing the server or client, they usually need to write some "formula" code to initialize the Ice communication device and capture exceptions, and destruction after the application is terminated. As follows:

1int status = 0;
2
3 Ice: CommunicatorPtr ic;
4
5 try
6
7 {
8
9 ic = Ice: initialize (argc, argv );
10
11 Ice: ObjectAdapterPtr adapter =
12
13 ic-> createobjectadapterwithenderson points ("SayHelloAdapter", "tcp-h 127.0.0.1-p 10000 ");
14
15 Ice: ObjectPtr object = new HelloICEI;
16
17 adapter-> add (object, ic-> stringToIdentity ("SimpleHello "));
18
19 adapter-> activate ();
20
21 ic-> waitForShutdown ();
22
23}
24
25 catch (const Ice: Exception & e)
26
27 {
28
29 cerr <e <endl;
30
31 status = 1;
32
33}
34
35 catch (const char * msg)
36
37 {
38
39 cerr <msg <endl;
40
41 status = 1;
42
43}
44
45 if (ic)
46
47 {
48
49 try
50
51 {
52
53 ic-> destroy (); // close ICE
54
55}
56
57 catch (const Ice: Exception & e)
58
59 {
60
61 cerr <e <endl;
62
63 status = 1;
64
65}
66
67}
68
69

 


If you have to write so much at a time, the first is a waste of time and energy, and the second is not to concentrate on the business logic. The other point is that the Code is not good enough in terms of cleanliness and readability (I am slightly "clean ").

Read the official manual and find that ICE provides two tool classes that encapsulate these "formula" Logics: "Application" and "Service ".

Ice: the Application itself is an abstract class, and its run () function is a pure virtual function, so it must be inherited before use.

Ice: Application is a singleton class that creates a single communicator. If you want to use multiple communication devices, you cannot use Ice: Application to define multiple apps. At most, an App instance is defined.

Other communication devices must be manually generated using Ice: initialize.

Generally, the Ice: Application class is very convenient for Ice customers and servers, but in some cases, the Application may need to be used as a Unix daemon) or the Win32 service runs at the system level. In this case, Ice provides Ice: Service. A single class that can be compared with Ice: Application, but it also encapsulates Low-Level Initialization and shutdown steps for specific platforms-which is often used by system services.

The following describes how to use the Service. This document describes only the development steps and describes how to download an example.

1. You need to reference the Ice/Service. h header file on the server. I spent two hours here.

2. Create a new class that inherits Ice: Service and implements three virtual functions. The Code is as follows:

 

1 class MyService: public Ice: Service
2
3 {
4
5 protected:
6
7 virtual bool start (int, char * [], int &);
8
9 virtual bool stop ();
10
11 virtual void interrupt ();
12
13 private:
14
15Ice: ObjectAdapterPtr m_adapter;
16
17 };
18
19 void MyService: interrupt ()
20
21 {
22
23std: cout <"Receive signal" <std: endl;
24
25Ice: Service: interrupt ();
26
27}
28
29 bool MyService: stop ()
30
31 {
32
33std: cout <"Stop running" <std: endl;
34
35 return true;
36
37}
38
39 bool MyService: start (int argc, char * argv [], int & status)
40
41 {
42
43std: string endpoint = "tcp-h localhost-p 10000 ";
44
45m_adapter = communicator ()-> createObjectAdapterWi thEndpoints ("SimpleHelloAdapter", endpoint );
46
47Ice: ObjectPtr object = new HelloICEI;
48
49m_adapter-> add (object, communicator ()-> stringToIdentity ("SimpleHello "));
50
51m_adapter-> activate ();
52
53 return true;
54
55}
56
57

3. Start the service in the main function. Note that the following operations are most common for a service: Install, uninstall, start, and stop. These four operations are controlled by the startup parameters respectively. Take the Win32 platform as an example:

-- Service NAME

Start a Windows service named "NAME. In the parameter vector passed to the start member function, this option will be removed.

However, before running an application as a Windows Service, it must be installed first. Therefore, the Ice: Service class also supports other command line options for executing management activities:

-- Install NAME [-- display DISP] [-- executable EXEC] [ARG...]

Install the NAME service. If the -- display option is specified, use DISP as the service display NAME. Otherwise, use NAME. If the -- executable option is specified, EXEC is used as the executable path name of the service. Otherwise, the path name of the executable file is used to call -- install. Any other parameters will be passed to the Service: start member function without any change. Note that the command line parameter -- service NAME is automatically added to the parameter set sent to the service at startup. Therefore, you do not need to explicitly specify these options.

-- Uninstall NAME

Remove the NAME service. If the service is currently active, stop it before anti-installation.

-- Start NAME [ARG...]

Start the NAME service. Any other parameters will be passed to the Service: start member function without any change.

-- Stop NAME

Stop the NAME service. If more than one management command is specified or -- service is used, an error occurs. After the management command is executed, the program will be terminated immediately. Ice: the Service class supports the Windows Service Control Code SERVICE_CONTROL_INTERROGATE and SERVICE_CONTROL_STOP. When SERVICE_CONTROL_STOP is received, Ice: Service calls the shutdown function.

Set the startup parameter-install MyService in Visual Studio to register a service named "MyService" in the system service.

 

 


Start the server program and you can see in the System Service list that MyService has been registered as a service.

 

 


4. Now, the ICE server can communicate with the client as a system service.


 

Related Article

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.