iCOM "Hello World" client

You should complete creating the hello_world component tutorial first.

Files [hello_world.idl greettest.cc]

Step 1 - Locate IDL


When creating clients that merely use simple components, you are generally not going to be creating new IDL files. Instead you will be using an IDL file or files that define the types and interface you would like to use. Likewise we will be using the IDL from our example component hello_world.idl.

Here is the IDL file from the component example:

hello_world.idl

line 1:
line 2: #pragma prefix hello_world "{c1d3ccdf-6470-4866-964f-a75e28539536}"
line 3:
line 4: module hello_world
line 5: {
line 6: interface iGreeting
line 7: {
line 8: void greet();
line 9: };
line 10: };

 

Step 2 - Compiling the IDL


As stated before, the IDL file is a description file, not much good to you when you are creating programs in C++, or Python. Now you need to take that description and create a usable set of definitions in your language of choice; in this example we are going to use C++. To create the C++ header files that define your IDL types run the iCOM IDL compiler, iidlc, against the IDL. For example:

		iidlc -bcxx hello_world.idl

Notice the option, "-bcxx", this tells the IDL compiler which language to generate for. This command will create a header file call hello_world.h in the current directory. This is what we will use to create our component.

 

Step 3 - Create the client


Here is the C++ client that will use our component:

file: greeter.cc

line 1: #include "hello_world.hh"
line 3
line 4: using namespace hello_world;
line 5: using namespace icom;
line 6:
line 7: int main(int, char* [])
line 8: {
line 9: iComponentManager::_ptr cm = icom::init();
line 10: iGreeting::_ptr g = cm->createComponent(icomMakeId(hello_world,"Greeter"),
line 11: typeIdOf(g), 0);
line 12: if(g) {
line 13: g->greet(L"World");
line 14: return 0;
line 15: }
line 16: return 1;
line 17: }

Line by line here are the major parts.

line 1: #include "hello_world.h"

This line includes the IDL generated declarations for our types. The generated header file already includes the necessary iCOM header file.

line 4,5: using namespace hello_world; using namespace icom;

The using clause is nice to make the typing easier, because you don't have to use fully qualified names; although sometimes they lead to name clashes.

line 7: int main(int, char*[])

Standard main function.

line 9: iComponentManager::_ptr cm = icom::init();

This is the iCOM initialization. Everything for iCOM is done through a component manager. The initial component manager is retrieved through this call to "init". When the component manager interface that is returned from this function is released, it uninitializes iCOM; so it's scope must be aligned with your usage of iCOM.

line 10,11: iGreeting::_ptr g = cm->createComponent(icomMakeId(hello_world,"Greeter"), typeIdOf(g), 0);

This requests the component manager to locate and create a component. The createComponent method takes three parameters, first the component-id to create. Second the type-id of the interface you would like from the component. Third is a controlling interface (this is for aggregate components, and can be ignored for now). For the component-id we use icomMakeId again to id from the hello_world_LibId constant defined in the hello_world.hh header file. The type-id of the requested interface is retrieved through using a template function, icom::typeIdOf, which returns the type-id of any IDL defined type. If all goes well, this will return an interface ptr to an instance of our "Greeter" component.

line 12: if(g) {

This tests the value of our interface pointer for validity, since the call to createComponent will return 0 when the component cannot be created.

line 13: g->greet(L"World");

Finally, we call the greet method of the hello_world::iGreeting interface, which should print something like "Hello World".

 

Step 5 - Compiling Client


Using your C++ compiler, compile the greeter.cc file into a shared library:

		g++ -o greettest -pthread -I/usr/local/include/icom -L/usr/local/lib greettest.cc -licomcxx

A couple of caveats:

 

Step 6 - Run the client!


Run the client and you should see the ever so satisfying words, "Hello World", on the command line.

 

You are done, you have built your first iCOM client.