這些程式碼範例提供了許多範本,以示範如何為用戶端應用程式建立處理常式。
下列範例檔適用於「股票報價」範例。 範例檔有三個:
這些檔案可以在 <inst_dir>/samples/handlers 中找到。
/*********************************************************************/ /* */ /* IBM Web Services Client for C/C++ */ /* */ /* 檔名: myClientHandler.hpp */ /* */ /* 說明: 「股票報價」範例的範例用戶端處理常式 */ /* 標頭檔 */ /* */ /*********************************************************************/ /* <START_COPYRIGHT> */ /* */ /* Licensed Materials - Property of IBM */ /* */ /* 6205-001 */ /* */ /* (c) Copyright IBM Corp. 2004, 2005 */ /* All Rights Reserved */ /* */ /* U.S. Government Users Restricted Rights - use, */ /* duplication or disclosure restricted by GSA */ /* ADP Schedule Contract with IBM Corp. */ /* */ /* 狀態:1.0 版 */ /* <END_COPYRIGHT> */ /* */ /*********************************************************************/ #if !defined( _HANDLER_HPP__INCLUDED_) #define _HANDLER_HPP__INCLUDED_ #include <axis/Handler.hpp> AXIS_CPP_NAMESPACE_USE class myClientHandler : public Handler { public: myClientHandler(); virtual ~myClientHandler(); // 載入 Handler 時呼叫 init。 int AXISCALL init(); // 當 AxisClient 即將傳送要求 SOAP 訊息, // 或者在剛收到回應訊息時,呼叫 invoke。 int AXISCALL invoke( void * pvIMsg); // 如果訊息處理有錯誤,則呼叫 onFault。 void AXISCALL onFault( void * pvIMsg); // 即將卸載 Handler 時,呼叫 fini。 int AXISCALL fini(); }; #endif // !defined(_HANDLER_HPP__INCLUDED_)
/*********************************************************************/ /* */ /* IBM Web Services Client for C/C++ */ /* */ /* 檔名: myClientHandler.cpp */ /* */ /* 說明: 「股票報價」範例的範例用戶端 */ /* 處理常式 */ /* */ /*********************************************************************/ /* <START_COPYRIGHT> */ /* */ /* Licensed Materials - Property of IBM */ /* */ /* 6205-001 */ /* */ /* (c) Copyright IBM Corp. 2004, 2005 */ /* All Rights Reserved */ /* */ /* U.S. Government Users Restricted Rights - use, */ /* duplication or disclosure restricted by GSA */ /* ADP Schedule Contract with IBM Corp. */ /* */ /* 狀態:1.0 版 */ /* <END_COPYRIGHT> */ /* */ /*********************************************************************/ // 併入 myClientHandler 標頭檔以取得類別定義等。 #include "myClientHandler.hpp" // 併入標頭檔以取得 BasicHandler 物件等。 #include <axis/GDefine.hpp> #include <axis/IHandlerSoapSerializer.hpp> #include <axis/IHandlerSoapDeSerializer.hpp> #include <iostream> // 在建立物件時,呼叫 myHandler。 myClientHandler::myClientHandler() { } // 在損毀物件時,呼叫 ~myClientHandler。 myClientHandler::~myClientHandler() { } int myClientHandler::invoke( void * pvHandlerMessage) { // 將現行訊息強制轉型為 IMessageData 類型。這樣可以容許 // 使用者適當地變更 SOAP 訊息。 IMessageData * pIMsgData = (IMessageData *) pvHandlerMessage; // 檢查是否即將傳輸 SOAP 訊息,或者才剛剛 // 收到 SOAP 訊息。 if( pIMsgData->isPastPivot()) { // 是 - 可用的 SOAP 訊息是回應 cout << "通過樞軸點 – 處理常式可以看見回應訊息。" << endl; } else { // 否 - 可用的 SOAP 訊息是要求 cout << "在樞軸點之前 – 處理常式可以看見要求訊息\n" << endl; } return AXIS_SUCCESS; } void myClientHandler::onFault( void * pvFaultMessage) { // 請保持空白。 } int myClientHandler::init() { return AXIS_SUCCESS; } int myClientHandler::fini() { return AXIS_SUCCESS; }
/*********************************************************************/
/* */
/* IBM Web Services Client for C/C++ */
/* */
/* 檔名: myClientHandlerFactory.cpp */
/* */
/* 說明: 「股票報價」範例的範例用戶端處理常式 */
/* Factory */
/* */
/*********************************************************************/
/* <START_COPYRIGHT> */
/* */
/* Licensed Materials - Property of IBM */
/* */
/* 6205-001 */
/* */
/* (c) Copyright IBM Corp. 2004, 2005 */
/* All Rights Reserved */
/* */
/* U.S. Government Users Restricted Rights - use, */
/* duplication or disclosure restricted by GSA */
/* ADP Schedule Contract with IBM Corp. */
/* */
/* 狀態:1.0 版 */
/* <END_COPYRIGHT> */
/* */
/*********************************************************************/
// 併入 myClientHandler 標頭檔以取得類別定義等。
#include "myClientHandler.hpp"
// 併入標頭檔以取得 BasicHandler 物件等。
#include <axis/GDefine.hpp>
// 此處理常式程式庫的載入器可用的外部方法。
extern "C"
{
// 傳送指標給 GetClassInstance,該指標指向將包含
// 將由此 Factory 建立的處理常式物件。在傳回處理常式物件
// 之前,會將它包裝在 BasicHandler 物件中,並且呼叫處理常式的
// 起始設定方法。
STORAGE_CLASS_INFO int GetClassInstance( BasicHandler ** ppClassInstance)
{
*ppClassInstance = new BasicHandler();
myClientHandler * pmyClientHandler = new myClientHandler();
// 將函數設為 0 時,表示處理常式的類型是 C++
(*ppClassInstance)->_functions = 0;
// 如果已順利載入處理常式,請儲存處理常式物件並
// 起始設定它。
if( pmyClientHandler)
{
(*ppClassInstance)->_object = pmyClientHandler;
return pmyClientHandler->init();
}
// 如果未順利載入處理常式,則傳回錯誤。
return AXIS_FAIL;
}
// 傳送指標給 DestroyInstance,該指標指向
// 包含這類處理常式物件實例通用的 BasicHandler 物件。處理常式
// 會從包覆它的 BasicHandler 物件解開,
// 在刪除處理常式以及 BasicHandler 包覆物件之前,
// 會呼叫處理常式的 finish 方法。
STORAGE_CLASS_INFO int DestroyInstance( BasicHandler * pClassInstance)
{
if( pClassInstance)
{
//將通用處理常式物件強制轉型為特定類別。
myClientHandler * pmyClientHandler = static_cast<myClientHandler*>
(pClassInstance->_object);
// 對處理常式呼叫 finish 方法。這樣可以容許處理常式
// 在被刪除之前保持「對齊」。
pmyClientHandler->fini();
// 刪除處理常式物件。
delete pmyClientHandler;
delete pClassInstance;
// 傳回順利完成。
return AXIS_SUCCESS;
}
// 如果沒有處理常式可以關閉和刪除,則傳回錯誤。
return AXIS_FAIL;
}
}