[ C++で開発 ]

MICOのインストール

C++用オープンソースCORBAの一つMICOをインストールします。

Windowsでのインストール

VC++

MICOはVC++7.0以上が必要です。持っていないので省略。

Cygwin

Cygwinでビルドします。

work$ tar xzf mico-2.3.11.tar.gz
work$ cd mico
work$ ./configure
   :
work$ make
   :
work$ make install

IDLコンパイラの使い方

time$ idl --c++-suffix cpp time.idl
time$ ls
time.cpp  time.h  time.idl
time$

コンパイル・リンク

サーバ
time$ g++ -c server.cpp
time$ g++ -c time.cpp
time$ g++ server.o time.o -L/usr/local/lib -lmico2.3.11 -o server
time$
クライアント
time$ g++ -c client.cpp
time$ g++ -c time.cpp
time$ g++ client.o time.o -L/usr/local/lib -lmico2.3.11 -o client
time$

実行

サーバ
time$ ./server > ior
クライアント
time$ ./client `cat ./ior`
Time in Greenwich is 13:00:46
time$

動作確認プログラム

単純なtime

time.idl
// time.idl
struct TimeOfDay {
  short hour;
  short minute;
  short second;
};

interface Time {
  TimeOfDay get_gmt();
};
server.h
//server.h
#include "time.h"

class Time_impl : public virtual POA_Time
{
public:
    virtual TimeOfDay get_gmt() throw (CORBA::SystemException);
};
server.cpp
// server.cpp
#include "server.h"
#include <iostream>

TimeOfDay Time_impl::get_gmt() throw (CORBA::SystemException)
{
    time_t time_now = time(0);
    struct tm* time_p = gmtime(&time_now);

    TimeOfDay tod;
    tod.hour = time_p->tm_hour;
    tod.minute = time_p->tm_min;
    tod.second = time_p->tm_sec;

    return tod;
}

int main(int argc, char* argv[])
{
    try {
        CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);

        CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
        PortableServer::POA_var poa = PortableServer::POA::_narrow(obj.in());

        PortableServer::POAManager_var mgr = poa->the_POAManager();
        mgr->activate();

        Time_impl time_servant;

        Time_var tm = time_servant._this();
        CORBA::String_var str = orb->object_to_string(tm.in());
        std::cout << str.in() << std::endl;

        orb->run();
    } catch (const CORBA::Exception& e) {
        std::cerr << "Uncaught CORBA exception" << std::endl;
        return 1;
    }
    return 0;
}
client.cpp
// client.cpp
#include <iostream>
#include <iomanip>
#include "time.h"

int main(int argc, char* argv[])
{
    try {
        if (argc != 2) {
            std::cerr << "Usage: client IOR_string" << std::endl;
            throw 0;
        }
        CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);

        CORBA::Object_var obj = orb->string_to_object(argv[1]);
        if (CORBA::is_nil(obj.in())) {
            std::cerr << "Nil Time reference" << std::endl;
            throw 0;
        }

        Time_var tm = Time::_narrow(obj.in());

        if (CORBA::is_nil(tm.in())) {
            std::cerr << "Argument is not a Time reference" << std::endl;
            throw 0;
        }

        TimeOfDay tod = tm->get_gmt();
        std::cout << "Time in Greenwich is "
                  << std::setw(2) << std::setfill('0') << tod.hour << ":"
                  << std::setw(2) << std::setfill('0') << tod.minute << ":"
                  << std::setw(2) << std::setfill('0') << tod.second << std::endl;

    } catch (const CORBA::Exception& e) {
        std::cerr << "Uncaught CORBA exception" << std::endl;
        return 1;
    }
    return 0;
}

EJBクライアント

まず、リモートインタフェースおよびホームインタフェースからIDLファイルを生成します。

生成されたIDLファイルを使用するにあたって、以下の注意点が必要となります。

time$ idl -I. -I/usr/local/include/mico --c++-suffix cpp -DprimaryKey=primKey time.idl
time$ ls
time.cpp  time.h  time.idl
time$