我正在从wustl CORBA版本中修改股票引用示例。赋值是为StockFactory类实现一个处理get_stock()
调用的回复处理程序这是我的FactoryHandler实现:
FactoryHandler_i.h:
#ifndef TAO_TUTORIALS_QUOTER_AMI_CLIENT_FACTORYHANDLER_I_H
#define TAO_TUTORIALS_QUOTER_AMI_CLIENT_FACTORYHANDLER_I_H
#include "QuoterS.h"
class Stock_Factory_Handler_i : public POA_Quoter::AMI_Stock_FactoryHandler
{
public:
Stock_Factory_Handler_i (int *response_count, ::Quoter::Stock_var& result);
void get_stock (::Quoter::Stock_ptr ami_return_val);
void get_stock_excep (::Messaging::ExceptionHolder * excep_holder);
private:
int *response_count_;
::Quoter::Stock_var& result_;
};
#endif /* TAO_TUTORIALS_QUOTER_AMI_CLIENT_HANDLER_I_H */
FactoryHandler_i.cpp:
#include "FactoryHandler_i.h"
#include "ace/streams.h"
Stock_Factory_Handler_i::
Stock_Factory_Handler_i (int *response_count, ::Quoter::Stock_var& result)
: response_count_ (response_count), result_ (result)
{
}
void
Stock_Factory_Handler_i::get_stock (::Quoter::Stock_ptr ami_return_val)
{
cout << "storing result" << endl;
result_ = ami_return_val;
(*this->response_count_)++;
}
void
Stock_Factory_Handler_i::get_stock_excep (::Messaging::ExceptionHolder * excep_holder)
{
// We ignore the exception, but this counts as a response, otherwise
// the application would not finish.
cerr << "Exception raised while getting stock"
<< endl;
(*this->response_count_)++;
}
来自client.cpp,就在更改部分之前:
// ...
// Create and activate the handler...
int response_count = 0;
Single_Query_Stock_Handler_i handler_i (&response_count);
Quoter::AMI_Single_Query_StockHandler_var handler =
handler_i._this ();
// Create and activate the factory handler...
Quoter::Stock_var result;
Stock_Factory_Handler_i factory_handler_i (&response_count, result);
Quoter::AMI_Stock_FactoryHandler_var factory_handler =
factory_handler_i._this();
// Send all the requests, careful with error handling
int request_count = 0;
for (int i = 2; i != argc+1; ++i) {
try {
// Get the stock object
cout << "looking up stock symbol " << argv[i] << endl;
factory->sendc_get_stock (factory_handler.in (), argv[i]);
sleep(3); // wait for a response
cout << "converting result" << endl;
Quoter::Single_Query_Stock_var stock =
Quoter::Single_Query_Stock::_narrow (result.in ());
cout << "checking result" << endl;
CORBA::Any any;
any <<= stock;
CORBA::TypeCode_var tc = any.type();
cout << tc->kind() << endl;
if (CORBA::is_nil (stock.in ())) {
cerr << "Cannot get single query interface for <"
<< argv[i] << ">" << endl;
continue;
}
cout << "reading result" << endl;
stock->sendc_get_price_and_names (handler.in ());
request_count++;
}
catch (Quoter::Invalid_Stock_Symbol &) {
cerr << "Invalid stock symbol <"
<< argv[i] << ">" << endl;
}
}
while (response_count < 2 * request_count // multiply by 2 because both handlers increment response_count
&& orb->work_pending ()) {
orb->perform_work ();
}
// ...
运行客户端时,输出为:
looking up stock symbol MSFT
converting result
checking result
14
Cannot get single query interface for <MSFT>
(14是Stock的typecode,仅用于调试)
值得注意的是,上面缺少的是“存储结果”消息,该消息应该在FactoryHandler的get_stock()
回调方法中打印出来。我不知道为什么,因为sendc_get_stock()
方法不会产生任何(立即)错误,并且基本上只是StockHandler代码的副本,并且从那里它是AMI / ORB接口的责任进行回调。但原始示例(仅限StockHandler)工作正常。
我做错了什么(以及如何解决)?
编辑:另一部分信息:在服务器端,StockFactory的get_stock()
方法确实被调用。
答案 0 :(得分:0)
抱歉,我没有回答你。但是提示,请在TIA的邮件列表http://www.cs.wustl.edu/~schmidt/ACE-mail.html
上提问HTH
答案 1 :(得分:0)
我认为你的问题是work_pending
只有在ORB有即时工作要做的情况下才返回true,所以它在客户端发送请求之后和服务器发送回复之前的时间内返回false。 p>
要验证这一点,只需从循环中删除&& orb->work_pending()
条件,并使用带有超时参数的perform_work
版本。