COID - An example program



download source code

The service header file

That's the core of our example program. This header file contains the exported class.

    #include "coid/coidsvc/coidsvc.h"

The header file always has to include coidsvc.h.

    #include "string.h"
    class TestClass
    {
    int status;

    public:
    TestClass()
    {
    }

The exported class may contain a constructor, but it must not have arguments. If you want to use arguments when you create your class, use accept_connect() for it.
There is also the accept_connect_shared() and the accept_connect_auto() method. For more information about this read Special methods

    net_fnc_s opcd accept_connect(int status_init)

The accept_connect() method is called as soon as the client creates the exported class. It has to have the keyword net_fnc_s and opcd in front of it. net_fnc_s means that this is a special, exported class. opcd is a custom data type to store error information. The value for status_init can be initialized from the remote client.

    status = status_init;
    return 0;


As usual 0 means no error. If you want to know more about error codes and the opcd type, go to http://coid.sourceforge.net/doc/opcd_type.html
It's not mandatory to use opcd, but recommended to cause no problems with error handling in case of simultaneous calls.

// returns 'length' of 'str'
net_fnc opcd get_length( net_out int & length, const char * str )

This is now a standard exported method. The net_fnc keyword tells coidgen that this method is meant to be exported. opcd is as before the error return value.
The arguments can have different decorations. net_out means that this argument is a return value (server --> client). Arguments without decoration are automatically net_in, that's a equivalent to a standard call by value (client --> server).

length = strlen(str);
return 0;

// multiplies 'a' and 'b' and returns 'result'
net_fnc opcd multiply( double a, double b, net_out double & result )
{
    result = a * b;
    return 0;
}
// multiplies 'a' and 'b' and returns 'result'
net_fnc opcd add_one( net_inout int & number )

This method has a new decoration: net_inout. That's the equivalent to a call by reference (client <--> server).
    number++;
    return 0;

// sets 'status' variable
net_fnc opcd set_status( net_out int & old_status, int new_status )
{
    old_status = status;
    status = new_status;
    return 0;
}

So much about the service header file. Basically it's just a normal class with a few methods, that are decorated with the keywords net_fnc, net_out and net_inout.

The client

The second file that is written manually and not generated is the main client file. This client does actually not much more than to create the remote class and call some methods.

#include "example_client.h"
#include <iostream>

The client has to include the generated _client.h file.

int main()
{
    std::cout << "Connecting to coid server ... ";
    TestClass_client testcl;
    opcd e = testcl.connect( 3, "localhost:55099" );

This creates the exported class by connecting to coid server (at localhost:55099) and submit '3' as constructor (accept_connect) parameter for the status variable.

The next lines are doing the error handling. If 'e' is true, an error has occurred. In this case the command opcd_formatter(e) returns a string explaining the error.
    if(e)
    {
        std::cout << "failed with error \"" << opcd_formatter(e) << "\".\n";
        return false;
    }
    else
        std::cout <<"successful. \n";

That's an example for a call of the method get_length of the exported class.

    int str_length;
    char teststring[5] = "test";
    std::cout << "Call get_length ... ";
    e = testcl.get_length( str_length, "test" );

Error handling works in the same way as above.

    if(e) {
        std::cout << "failed with error \"" << opcd_formatter(e) << "\".\n";
        return false;
    }
    else
        std::cout << "The string \"" << teststring << "\" contains " << str_length << " characters.\n";

There are more example method calls in the original client.cpp.

download source code