sample/simple_system

sample/simple_system

sample/simple_system にスタンドアロンで接続設定を行うサンプルがある。 このサンプルコードは二種類のプログラムを生成する。

main

二種類のサンプルプログラムで、main.cpp は共通である。ここでは、この main の動作を把握しておく。

コードの main 関数を単純化すると次のようになっている。

   Server sv;
   switch (n){
      case 1: config1(sv); break;
      case 2: config2(sv); break;
   }
   while (true) {
      sv.turn();
      nine::msleep(100);
   }

n はコマンドライン引数で定まる。switch 文では、コマンドライン引数によって呼ぶ関数を変えている。

Server クラスは main() の手前で定義されている

class Server: public nine::system::SystemServer
{
   public:
      virtual void onEstablish(int commId, const char* name) {
         printf("establish: %d, '%s'\n", commId, name); }
      virtual void onClose(int commId, const char* name) {
         printf("close: %d, '%s'\n", commId, name); }
      virtual void onComplete() {
         printf("complete\n"); }
};

SystemServer のイベントハンドラを実装しただけのクラスである。

結局のところ、main() は次のことを行っている:


  1. Server インスタンスを生成
  2. Server インスタンスで config1() または config2() を呼ぶ
  3. 無限ループで SystemServer::turn() を繰り返し呼ぶ

config1(), config2() の実装はサンプルプログラムで異なっている。ここで Server インスタンスに対して異なった方法で接続情報の登録を行っている。