next up previous contents index
Next: sclient2 Up: シンプルクライアント Previous: シンプルクライアント   Contents   Index


sclient1

Action IntervalParallelism を実現するために, sclient1select()関数のタイムアウト機能を使用する.

プログラムの重要な部分は MyPlayer::run() にある.以下にソースコー ドの一部を示す:

   //----------------------------------------
   // enter main loop

   SocketReadSelector selector ;

   TimeVal nexttic ;  // indicate the timestamp for next command send
   nexttic.update() ; // set nexttic to the current time.

   while(True) {

      //-------------------------------------------------
      // setup selector

      selector.clear() ;
      selector.set(socket) ;

      //-------------------------------------------------
      // wait socket input or timeout (100ms) ;

      Int r = selector.selectUntil(nexttic) ;

      if(r == 0) { // in the of timeout. (no sensor input)
         doAction() ;  // enter action part
         nexttic += TimeVal(0,100,0) ; // increase nexttimetic 100ms
      } else {  // got some input
         doSensing() ; // enter sensor part
      }
   }
class SocketReadSelectorselect() の機能を抽象化するクラス であり,"itk/Socket.h" で定義されている. ``Int r = selector.selectUntil(nexttic) ;'' の行では,プログラムは ソケットからの入力か nexttic で示されるタイムアウトを待つ. nexttic は次のticのタイムスタンプ(シミュレーションステップ)を保 持する. 関数は,タイムアウトが発生すれば0を返し,ソケットに受信データがあれば その数を返す. タイムアウトの場合,プログラムは doAction() を呼び出し,コマンドが 生成されてサーバへと送信される. それ以外の場合(受信データがある場合),doSensing() が呼び出され, センサ情報が処理される.



Hidehisa Akiyama 2004-11-21