跟我從頭學TAO編程系列 (2) -- 編寫最簡單的TAO應用程序

跟我從頭學TAO編程系列

編寫最簡單的TAO應用程序

Stone Jiang [email protected]

http://www.ace-tao.org

 

如果您對TAO有一定了解,卻不知如何駕馭它,那請跟我來從頭學學TAO編譯。

如果您對TAO還不是非常瞭解的話,請跟我來,一起對TAO有一個初步的認識。

 

上一篇我們學會了如何下載、編譯和安裝TAO,這一節我們來編寫最簡單的TAO應用程序--Hello TAO!

1. 編寫TAO應用程序的一般步驟

1) 定義接口,編寫idl文件
2) 編寫mpc文件,生成工程文件
3) 編寫server端代碼
4) 編寫client端代碼
5) 編譯,調試運行,查看結果

 

2.定義接口

接口(interface)在TAO(corba)編程在非重要,它定義了客戶端(主動發起請求一方)向服務端(被動等待請求)之間通訊消息格式。在本例在, 我們定義了一個名爲"Hello"的接口。Hello接口中具有兩個方法:

     get_string() 和

     shutdown()

TAO用接口定義語言(idl)來表示具體的接口定義。見Hello.idl文件

//@file:   Test.idl
//@author: StoneJiang<[email protected]>
//@ref :  http://www.ace-tao.org

/// Put the interfaces in a module, to avoid global namespace pollution
module Test
{
  /// A very simple interface
  interface Hello
  {
    /// Return a simple string
    string get_string ();

    /// A method to shutdown the ORB
    /**
     * This method is used to simplify the test shutdown process
     */
    oneway void shutdown ();
  };
};

 

我們將接口Hello放在 module Test中,避免全局名字污染。

3.編寫mpc文件

上一篇我們簡單的講解了mpc在編譯和ACE,TAO中的應用,在本節我們進一步使用mpc工具,爲我們生成項目工程文件。

見Hello.mpc

//@file:   Hello.mpc 
//@author: StoneJiang<[email protected]>
// @ref :  http://www.ace-tao.org

project(*idl): taoidldefaults {
  idlflags += -Sp
  IDL_Files {
    Test.idl
  }
  custom_only = 1
}

project(*Server): taoserver {
  after += *idl
  Source_Files {
    Hello.cpp
    server.cpp
  }
  Source_Files {
    TestC.cpp
    TestS.cpp
  }
  IDL_Files {
  }
}

project(*Client): taoclient {
  after += *idl
  Source_Files {
    client.cpp
  }
  Source_Files {
    TestC.cpp
  }
  IDL_Files {
  }
}

 

 

Hello.mpc定義了三個項目(project),其中*作爲佔位符,替換文件名Hello,所以這三個項目分別爲

  1) Hello_idl

  2) Hello_Server

  3) Hello_Client

 

4. 服務端代碼的編寫

   服務端由 Server.cpp, Hello.cpp, Hello.h組成

// $Id: server.cpp 82798 2008-09-21 10:07:12Z johnnyw $

//@file:   server.cpp 
//@author: StoneJiang<[email protected]>
//@ref :  http://www.ace-tao.org

#include "Hello.h"
#include "ace/Get_Opt.h"
#include "ace/OS_NS_stdio.h"

const ACE_TCHAR *ior_output_file = ACE_TEXT ("test.ior");

int
parse_args (int argc, ACE_TCHAR *argv[])
{
  ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'o':
        ior_output_file = get_opts.opt_arg ();
        break;

      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s "
                           "-o <iorfile> "
                           "-e shutdown server"
                           "/n",
                           argv [0]),
                          -1);
      }
  // Indicates sucessful parsing of the command line
  return 0;
}

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

      CORBA::Object_var poa_object =
        orb->resolve_initial_references("RootPOA");

      PortableServer::POA_var root_poa =
        PortableServer::POA::_narrow (poa_object.in ());

      if (CORBA::is_nil (root_poa.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           " (%P|%t) Panic: nil RootPOA/n"),
                          1);

      PortableServer::POAManager_var poa_manager = root_poa->the_POAManager ();
      if (parse_args (argc, argv) != 0)
        return 1;
     poa_manager->activate ();

      Hello *hello_impl = 0;
      ACE_NEW_RETURN (hello_impl,
                      Hello (orb.in ()),
                      1);
      PortableServer::ServantBase_var owner_transfer(hello_impl);

      PortableServer::ObjectId_var id =
        root_poa->activate_object (hello_impl);

      CORBA::Object_var object = root_poa->id_to_reference (id.in ());

      Test::Hello_var hello = Test::Hello::_narrow (object.in ());

      CORBA::String_var ior = orb->object_to_string (hello.in ());

      // Output the IOR to the <ior_output_file>
      FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
      if (output_file == 0)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Cannot open output file for writing IOR: %s/n",
                           ior_output_file),
                           1);
      ACE_OS::fprintf (output_file, "%s", ior.in ());
      ACE_OS::fclose (output_file);

      ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - wait for requsting from client./n"));

      orb->run ();

      ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - event loop finished/n"));

      root_poa->destroy (1, 1);

      orb->destroy ();
    }
  catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("Exception caught:");
      return 1;
    }

  return 0;
}

 

 

 

 

//@file:   Hello.cpp 
//@author: StoneJiang<[email protected]>
// @ref :  http://www.ace-tao.org

#include "Hello.h"

#include "ace/Log_Msg.h"

Hello::Hello (CORBA::ORB_ptr orb)
  : orb_ (CORBA::ORB::_duplicate (orb))
{
}

char *
Hello::get_string (void)
{
   ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - get_string()./n"));
  return CORBA::string_dup ("Hello TAO!");
}

void
Hello::shutdown (void)
{
     ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - shutdown()./n"));
  this->orb_->shutdown (0);
}

 

 

//@file:   Hello.h 
//@author: StoneJiang<[email protected]>
//@ref :  http://www.ace-tao.org

#ifndef HELLO_H
#define HELLO_H
#include /**/ "ace/pre.h"

#include "TestS.h"

/// Implement the Test::Hello interface
class Hello
  : public virtual POA_Test::Hello
{
public:
  /// Constructor
  Hello (CORBA::ORB_ptr orb);

  // = The skeleton methods
  virtual char * get_string (void);

  virtual void shutdown (void);

private:
  /// Use an ORB reference to convert strings to objects and shutdown
  /// the application.
  CORBA::ORB_var orb_;
};

#include /**/ "ace/post.h"
#endif /* HELLO_H */

 

 

5. 客戶端代碼的編寫

   服務端由 client.cpp組件

// @file: client.cpp  StoneJiang<[email protected]>
// @ref :  http://www.ace-tao.org

#include "TestC.h"
#include "ace/Get_Opt.h"

const ACE_TCHAR *ior = ACE_TEXT ("file://test.ior");
int  end = 0;

int
parse_args (int argc, ACE_TCHAR *argv[])
{
    ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k: e"));
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'k':
        ior = get_opts.opt_arg ();
        break;
      case 'e':
          end = 1;
          break;

      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s "
                           "-k <ior> "
                           "/n",
                           argv [0]),
                          -1);
      }
  // Indicates sucessful parsing of the command line
  return 0;
}

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

      if (parse_args (argc, argv) != 0)
        return 1;

      CORBA::Object_var tmp = orb->string_to_object(ior);

      Test::Hello_var hello = Test::Hello::_narrow(tmp.in ());

      if (CORBA::is_nil (hello.in ()))
        {
          ACE_ERROR_RETURN ((LM_DEBUG,
                             "Nil Test::Hello reference <%s>/n",
                             ior),
                            1);
        }

      CORBA::String_var the_string = hello->get_string ();

      ACE_DEBUG ((LM_DEBUG, "(%P|%t) - string returned <%C>/n",
                  the_string.in ()));

      if (end)
      {
          hello->shutdown ();
      }

      orb->destroy ();
    }
  catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("Exception caught:");
      return 1;
    }

  return 0;
}

 

6. 編譯、調試和運行

6.1 生成工程文件

    在Dos Shell中輸入

  mwc.pl -type vc9

如下圖:

image

6.2 打開工程文件

  用Visual Studio 2008打開工程文檔

image

編譯後得到 server.exe和client.exe

image

6.3 運行服務端

image

6.4 運行客戶端

image

6.5 運行客戶端,並讓服務端退出

image

 

7. 結束

到此,我們最簡單的已經運行成功。

完整的源代碼將上傳到http://www.ace-tao.org/home/link.php?url=d3d3LmFjZS10YW8ub3JnL2Jicw%3D%3D

源代碼中已爲你生成了vc9,vc8,vc71以及linux GNUmakefile文件。

我們將接下來對本例在的代碼作解釋,敬請關注。

有任何問題,請來郵件 [email protected]或在bbs (http://www.ace-tao.org/bbs)上留言。

http://www.ace-tao.org/home/link.php?url=d3d3LmFjZS10YW8ub3JnL2Jicw%3D%3D

發佈了14 篇原創文章 · 獲贊 2 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章