Let's start with the classic "Hello World!" example:
The interface is very simple, just a method to say hello! On this example, we will use Python, so there is no need to compile it...
But if you prefer another language, just select the apropiate translator:
// 1. Define module and interface name
module Example {
interface Hello {
// 2. Add needed methods
void sayIt();
};
};
$ slice2cpp interface.ice
$ slice2java interface.ice
$ slice2js interface.ice
$ slice2cs interface.ice
$ slice2php interface.ice
[...]
This server listens on a TCP port, waiting for incoming invocations. Its like any other ZeroC Ice server, you only need to also: 1) load IDM slice, and 2) using a valid IDM slice, 3) register your proxy on IDM router.
import sys
import Ice
# 1. Load IDM slice
Ice.loadSlice("/usr/share/slice/idm/idm.ice")
import IDM
Ice.loadSlice("interface.ice")
import Example
class HelloI(Example.Hello):
def sayIt(self, current):
print("Hello World!")
class Server(Ice.Application):
def run(self, args):
ic = self.communicator()
# 2. Add servant with a valid IDM address
address = ic.stringToIdentity("10AA02")
adapter = ic.createObjectAdapter("Adapter")
proxy = adapter.add(HelloI(), address)
adapter.activate()
# 3. Register proxy on IDM router
router = ic.propertyToProxy("IDM.Router.Proxy")
router = IDM.RouterPrx.checkedCast(router)
router.adv(str(proxy))
print("Ready, waiting events...")
self.shutdownOnInterrupt()
ic.waitForShutdown()
if __name__ == "__main__":
Server().main(sys.argv)
Create a regular UDP client. But wait... UDP? Yes! Thats the point with IDM, you can have different protocols on each side, like TCP, UDP, RS-485, ZigBee... No problem, IDM will make the message arrive to its correct destination.
import sys
import Ice
Ice.loadSlice("interface.ice")
import Example
class Client(Ice.Application):
def run(self, args):
ic = self.communicator()
# 1. Use remote object's IDM address
address = ic.stringToIdentity("10AA02")
# 2. Send the invocation through the IDM router
remote = ic.propertyToProxy("IDM.Router.Proxy")
remote = remote.ice_identity(address)
remote = Example.HelloPrx.uncheckedCast(remote)
remote.sayIt()
if __name__ == "__main__":
Client().main(sys.argv)
To make everything work, you need to run an IDM router
first. Its very easy, just copy (or download) the following configuration file,
router.config
. Then, launch it as follows:
Router.Table.Path = /tmp/router.table
Router.Adapter.Endpoints = tcp -h 127.0.0.1 -p 6140
Router.Ids = 10AA01
$ idm-router --Ice.Config=router.config
[...]
INFO:root: waiting events...
Then, the second step is launching your TCP server. Again, copy
or download the configuration file, server.config
,
and run the following command (you will see how it is registered
on router):
IDM.Router.Proxy = 10AA01 -t:tcp -h 127.0.0.1 -p 6140
Adapter.Endpoints = tcp
$ python3 ./server.py --Ice.Config=server.config
Ready, waiting events...
Last, run the UDP Client: copy or download the config file,
client.config
, and run as indicated. If everything
is as expected, you will see a "Hello World!" message on the
server console, indicating that the method invocation arrived
correctly. Good job!
IDM.Router.Proxy = 10AA01 -t:tcp -h 127.0.0.1 -p 6140
$ python3 ./client.py --Ice.Config=client.config