ICE 学习进阶2-AMI 方式

来源:互联网 发布:常州一名网络女主播 编辑:程序博客网 时间:2024/06/05 23:45


1. AMI :

When a client issues an AMI request, the Ice run time hands the message off to the local transport buffer or, if the buffer is currently full, queues the request for later delivery. The application can then continue its activities and poll or wait for completion of the invocation, or receive a callback when the invocation completes.
AMI is transparent to the server: there is no way for the server to tell whether a client sent a request synchronously or asynchronously.

2. ICE 支持AMI方式,即为每个接口Func,自动增加begin_Func() 和 end_Func()两个接口,如下示例:

  Ice::AsyncResultPtr r = APrintPrx->begin_PrintString("hello,world"); // 不阻塞
  APrintPrx->end_PrintString(r); //因为PrintString没有返回值  //阻塞


Note that begin_getName returns a value of type AsyncResultPtr.The AsyncResult associated with this smart pointer contains the state that the Ice run time requires to keep track of the asynchronous invocation. You must pass the AsyncResultPtr that is returned by the begin_ method to the corresponding end_ method.
The begin_ method has one parameter for each in-parameter of the corresponding Slice operation. Similarly, the end_ method has one out-parameter for each out-parameter of the corresponding Slice operation (plus the AsyncResultPtr
parameter
).
begin_函数和end_函数要捕获 CommunicatorDestroyedException.异常; 而且end_函数还有捕获函数执行的异常

3. AsyncResult 类介绍
The AsyncResult that is returned by the begin_ method encapsulates the state of the asynchronous invocation,有如下几个接口
waitForCompleted(): This method blocks the caller until the result of an invocation becomes available.

void waitForSent() :   This method blocks the calling thread until a request has been written to the client-side transport

 CommunicatorPtr getCommunicator() const :This method returns the communicator that sent the invocation.
 virtual ConnectionPtr getConnection() const :This method returns the connection that was used for the invocation.
virtual ObjectPrx getProxy() const :This method returns the proxy that was used to call the begin_ method. 

 const string& getOperation() const :This method returns the name of the operation.
 LocalObjectPtr getCookie() const:This method returns the cookie that was passed to the begin_ method ,If you did not pass a cookie to the begin_ method, the return value is null.

bool isCompleted() const:This method returns true if, at the time it is called, the result of an invocation is available,indicating that a call to the end_ method will not block the caller.Otherwise, if the result is not yet available, the method returns false.

 

bool sentSynchronously() const:This method returns true if a request was written to the client-side transport without first being queued. If the request was initially queued, sentSynchronously returns false

 

bool isSent() const: isSent returns true if, at the time it is called, the request has been written to the local transport (whether it was initially queued or not). Otherwise, if the
request is still queued, isSent returns false.

4 一个异步调用,提高系统性能的例子:

//////////////

FileHandle file = open(...);
FileTransferPrx ft = ...;
const int chunkSize = ...;
Ice::Int offset = 0;

list<Ice::AsyncResultPtr> results;
const int numRequests = 5;

while (!file.eof()) {
ByteSeq bs;
bs = file.read(chunkSize);

// Send up to numRequests + 1 chunks asynchronously.
Ice::AsyncResultPtr r = ft->begin_send(offset, bs);
offset += bs.size();

// Wait until this request has been passed to the transport.
r->waitForSent();
results.push_back(r);

// Once there are more than numRequests, wait for the least
// recent one to complete.
while (results.size() > numRequests) {
Ice::AsyncResultPtr r = results.front();
results.pop_front();
r->waitForCompleted();
}
}

// Wait for any remaining requests to complete.
while (!results.empty()) {
Ice::AsyncResultPtr r = results.front();
results.pop_front();
r->waitForCompleted();
}

/////////////

 

 

5. 将异步请求,批量请求

Applications that send batched requests (see Section 32.16) can either flush a
batch explicitly or allow the Ice run time to flush automatically. The proxy method
ice_flushBatchRequests performs an immediate flush using the synchronous
invocation model and may block the calling thread until the entire message
can be sent. Ice also provides asynchronous versions of this method so you can
flush batch requests asynchronously.
begin_ice_flushBatchRequests and
end_ice_flushBatchRequests are proxy methods that flush any batch
requests queued by that proxy.