ICE is an excellent middleware for network communication, and we will now use ICE to create point-to-point communication and publish/subscribe communication.Publish and subscribe model Point-to-point mode

1. Point-to-point communication mainly adopts RPC communication mode

1. Create an ice file and compile.h and.cpp files with slice.exe

module POne {
	interface PrinterOne {
	void printString(string s);
	int add(int num1,int num2);
	int sub(int num1,int num2);
	};
	};
Copy the code

Generate.h and.cpp files

  slice.exe PrinterOne.ice
Copy the code

Printerone. h and printerone. CPP: These two files are required by both the server and the client.

2. Write the steps of the server side:

  1. Initialize the Ice: : Communicator, using the function createObjectAdapterWithEndpoints create an adapter adapter and named SimplePrinterAdapter, used to monitor the TCP/IP port 10000 from the request;
  2. Class PrinterOneI inherits from PrinterOne and implements pure virtual functions in it. Create a PinterOneI service on this endpoint. This object provides PrinterOne’s services.
  3. Add the PrinterOneI object add to the Adapter and identify it as SimplerPrinter; If you have multiple different PrinterOne objects, give them different names to distinguish them.
  4. When a client requests a PrinterOne service, the Adapter forwards the request to PrinterOne.
  5. Finally, the main thread is blocked with waitForShutdown until the server runs shut down

3. Compile the client procedure

  1. Initialize Ice::Communicator and create a proxy stringToProxy using a string containing the proxy name and communication protocol TCP/IP and port (need to be consistent with the server).
  2. Using stringToProxy returns a Ice: : ObjectPrx object, using PrinterOnePrx: : checkedCast converts PrinterOne object. This function checkedCast sends a message to the server, “Does the server have a proxy for such a PrinterOne object?” if it does, it converts it to a PrinterOne object, and if it does not, it returns an empty object
  3. Now you can use the methods printString, add, sub of the PrinterOne object, and the invocation communicates by sending a request to the server through the proxy.

Publish/subscribe communication.

1. The IceStorm service needs to be started. The IceStorm service needs to be started using iceBox

config.icebox

  IceBox.Service.IceStormDemo=IceStormService,37:createIceStorm --Ice.Config=config.service
Copy the code

config.sevice

# The IceStorm service instance name. IceStormDemo.InstanceName=TestTopic # This property defines the endpoints on which  the IceStorm # TopicManager listens. IceStormDemo.TopicManager.Endpoints=tcp -h10.01.113. -p 9999
	IceStormDemo.Publish.Endpoints=tcp -h 10.01.113. -p 11000
	Freeze.DbEnv.IceStormDemo.DbHome=db
Copy the code

2. In the icebox. Exe directory, create an empty folder iceStormDemo

Icebox — ice.config =config.icebox To start the IceStorm service

4. Write ice files

module TOne
{
	interface TopicOne 
	{
		void printString(string s);
	};
};
Copy the code

Compile to topicone. h and topicone. CPP files

5. Write publishers

  1. Through stringToProxy (” IceStorm/TopicManager: TCP -p 9999 “) to obtain TopicManager agent, for it is a major publisher and subscriber IceStorm object;
  2. Retrieve (“TopicOne”) TopicOne Topic via topicManager-> Retrieve (“TopicOne”) if topicManager-> Create (“TopicOne”) Topic Topic is not created directly;
  3. Get the publisher object proxy for the topic TopicOne, and then get the interface for the topic through Ice::uncheckedCast
  4. Publish messages and communicate with subscribers through the interface.

6. Write subscribers

  1. Through stringToProxy (” IceStorm/TopicManager: TCP -p 9999 “) to obtain TopicManager agent;
  2. Subclass TopicOneI to inherit from TopicOne, create an Adapter with addWithUUID to host our TopicOneI service;
  3. Activate the service TopicOneI via Adapter activate();
  4. Get TopicOne and subscribe TopicOne using TopicOneI service;
  5. Process the information from the client until the technical location.

Download the source code

aaa