Slice-to-C ++ ing of the Client
1. Introduction
Its ing definition: how to translate slice data types into C ++, and how customers call operations, PASS Parameters, and handle errors.
C ++ ing is completely thread-safe. For example, the class reference mechanism locks parallel access. Therefore, if many threads share a class instance, the reference count will not be destroyed.
2. identifier ing
The Slice identifier is mapped to the same C ++ identifier.
3. Module ing
The Slice module maps to the C ++ namespace. The sling maintains the nested hierarchy defined by slice.
4. Ice namespace
All the APIs of ice run time are embedded in the ice namespace.
5. Simple built-in type ing
Ice: byte is the type definition of unsigned char. Therefore, the byte value range is 0... Within 255.
6. User-Defined type ing
Slice supports user-defined types: enumeration, structure, sequence, and dictionary. The Slice dictionary maps to STD: Map of C ++.
7. Constant ing
8. Abnormal ing
9. ing of runtime exceptions
Connecttimeoutexception can be handled as any of the following exception types:
A. Ice: exception this is the root of the inheritance tree.
B. Ice: userexception is the root of all user exceptions.
C. Ice: localexception this is the root exception of all runtime exceptions.
C. Ice: timeoutexception this is not only an operation call timeout, but also a base exception for connection establishment timeout.
D. Ice: connectatimeoutexception if the connection to the server times out during the first attempt, this exception is thrown.
10. Interface ing
To call a remote operation, you need to call a member function of a local class instance. This instance represents a remote object.
10.1 proxy class and proxy handle
Client Applications will never directly manipulate the proxy class. In fact, you cannot directly instantiate the proxy class. It is always instantiated by ice run time for the customer.
10.2 proxy Handle Method
The copy constructor is responsible for ensuring that a proxy handle can be constructed based on another Proxy handle. Internally, this will add one reference count to the proxy; The Destructor will reduce the reference count by one; once the count is reduced to zero, the underlying proxy instance will be released to avoid Memory leakage.
Wide assignment and narrow assignment: You can always assign the derived type to the base type, but this is not the case.
11. Operation ing
11.1 normal operation
Because idempotent and nonmutating affect call dispatch rather than an interface, some methods are the same when mapped to C ++. For example:
11.2 PASS Parameters
A. In Parameters
If a parameter is passed through a value or a const reference, the call will certainly not change the value of the parameter.
B. out parameters
C ++ ing transmits output parameters through reference. The caller simply transmits the variables to the operation. Once the operation is completed, the server will set the values of these variables.
12. Exception Handling
Class 13 ing
The Slice class maps to the C ++ class with the same name. For each slice data member, the production class corresponds to a public data member, and each operation has a virtual function.
13.1 inherit from ice: Object
Like interfaces, classes are implicitly inherited from a common base class ice: object.
13.2 data members
For each data member in the slice definition, the generated class has a corresponding public data member.
13.3 class operations
In the generated class, operations on the class are mapped to purely virtual member functions. This means that if a class contains operations, you must generate a class from the generated class and provide the Operation implementation in this class.
13.4 class factories
13.5 smart pointers for Classes
The Slice compiler generates smart pointers for each type. For the slice class <class-Name>, the compiler generates a C ++ smart pointer called <class-Name> PTR.
Server slice-to-C ++
1. Main Function on the server side
The main entry point of the ice run time is represented by the Local interface ice: communicator. At the beginning of the program, you must first call ice: Initialize to initialize the ice run time. Ice :: communicator returns a smart pointer pointing to an ice: communicator instance.
2. Ice: Application
2.1 use ice: Application on the client
Derive a subclass from ice: Application and write the client code into the run method.
2.2 capture Signals
Member method:
Shutdownoninterrupt: used to close the application, which is the default action.
Ignoreinterrupt: This function ignores signals.
Holdinterrupt: This function temporarily blocks signal transmission.
Releaseinterrupt: This function restores the signal to the previous arrangement.
Interrupted: If the communicator is closed due to a signal, this function returns true; otherwise, false.
2. Ice: Service Class
The ice application using the ice: service class must define at least one subclass and redefine the start member function. The main function of the application must instantiate This subclass, call its main member function, and pass the parameter vector of the program as a parameter to it.
Ice: Service virtual member function
Ice: communicatorptr initializecommunicator (): Initialize the communicator
Void interrupt (): The signal processor calls it to indicate that the signal is received.
Void Shutdown (): process that causes the server to start shutting down.
Bool stop (): allows subclass to be processed before termination.
Void waitforshutdown (): waits for the service to close indefinitely.
Ice: Service non-virtual member function
Void disableinterrupt (): Disable the signal processing behavior of ice: service.
Void enableinterrupt (): enables the signal processing behavior of ice: service.
Static Service * instance (): returns the single instance of ice: service.
3. Interface ing
The server interface ing provides an up-call API for ice run time: virtual functions are implemented in the servant class, the hooks you provide can direct the control thread from the server's ice run time to your application code.
3.1 skeleton
On the server side, interfaces are mapped to the skeleton class. For each operation on the corresponding interface, the skeleton class has a pure virtual method. A skeleton class is an abstract base class because its members are pure virtual functions.
4. servant class
It is a good idea to always use virtual inheritance when defining a servant class. Using Virtual inheritance is harmless. If you add multiple inheritance to the interface layer during development, you do not need to go back and add the virtual keyword to all your servant classes.
Ordinary, idempotent and nonmutating operations: Only nonmutating operations are mapped to const member functions, while normal operations and NT operations are mapped to normal functions.
5. parameter transfer
For each slice operation parameter, the C ++ ing generates a corresponding parameter for the virtual member function in the skeleton.
The rules for passing server parameters are the same as those for the client:
A. in parameters are passed through values or const references
The B. out parameter is passed through the application.
C. The return value is passed through the value.
6. Exception
To throw an exception from an operation, you only need to instantiate the exception, initialize it, and then throw it.
7. Object embodiment
7.1 to implement the ice object, take the following steps as an example of the servant class:
A. instantiate the servant class
B. Create an identifier for the ice object embodied in this servant
C. Inform the ice run time of the existence of this servant.
D. Send the proxy of this object to the customer so that the customer can access it.
E.
7.2 taking servant as an example, smart pointer definition:
A. When a new servant is instantiated, its reference count is initialized to 0.
B. Assign the servant address to servant. A smart pointer will increase the reference count of servant to 1.
C. When you call add and pass the servant smart pointer to the Object Adapter, the Object Adapter retains a copy of the handle internally. This will increase the reference count of servant to 2.
D. When activitesservant returns, the destructor of the servant variable will reduce the reference count of servant to 1.
7.3 use UUID for identification
The ice object model assumes that the Object ID is globally unique. One way to ensure such uniqueness is to use UUID (universally unique identifiers) for identification.
8. Chapter Summary
For customers and servers, the slice data type ing is the same. Compared with clients, server-side ing only adds several additional mechanisms: a small API for initializing and ending run time, add some rules to handle how to derive the servant class from the skeleton and how to register the servant class with the server run time.