ivankelly commented on a change in pull request #1861: Cpp client: add 
getLastMessageId and hasMessageAvailable in consmer and reader
URL: https://github.com/apache/incubator-pulsar/pull/1861#discussion_r194107442
 
 

 ##########
 File path: pulsar-client-cpp/lib/ConsumerImpl.cc
 ##########
 @@ -919,4 +919,65 @@ void ConsumerImpl::seekAsync(const MessageId& msgId, 
ResultCallback callback) {
     callback(ResultNotConnected);
 }
 
+void ConsumerImpl::hasMessageAvailableAsync(HasMessageAvailableCallback 
callback) {
+    if (this->lastMessageIdAvailable()) {
+        callback(ResultOk, true);
+        return;
+    }
+
+    BrokerGetLastMessageIdCallback callback1 = [this, callback](Result result, 
MessageId messageId) {
+        if (this->lastMessageIdAvailable()) {
+            callback(ResultOk, true);
+        } else {
+            callback(ResultOk, false);
+        }
+    };
+
+    getLastMessageIdAsync(callback1);
+}
+
+void ConsumerImpl::brokerGetLastMessageIdListener(Result res, MessageId 
messageId,
+                                                  
BrokerGetLastMessageIdCallback callback) {
+    if (!callback.empty()) {
+        lastMessageInBroker_ = Optional<MessageId>::of(messageId);
+        callback(res, messageId);
+    }
+}
+
+void ConsumerImpl::getLastMessageIdAsync(BrokerGetLastMessageIdCallback 
callback) {
+    Lock lock(mutex_);
+    if (state_ == Closed || state_ == Closing) {
+        lock.unlock();
+        LOG_ERROR(getName() << "Client connection already closed.");
+        if (!callback.empty()) {
+            callback(ResultAlreadyClosed, MessageId());
+        }
+        return;
+    }
+    lock.unlock();
+
+    ClientConnectionPtr cnx = getCnx().lock();
+    if (cnx) {
+        if (cnx->getServerProtocolVersion() >= proto::v12) {
+            ClientImplPtr client = client_.lock();
+            uint64_t requestId = client->newRequestId();
+            LOG_DEBUG(getName() << " Sending getLastMessageId Command for 
Consumer - " << getConsumerId()
+                                << ", requestId - " << requestId);
+
+            cnx->newGetLastMessageId(consumerId_, requestId)
+                
.addListener(boost::bind(&ConsumerImpl::brokerGetLastMessageIdListener, 
shared_from_this(),
+                                         _1, _2, callback));
+            return;
+        } else {
+            LOG_ERROR(getName() << " Operation not supported since server 
protobuf version "
+                                << cnx->getServerProtocolVersion() << " is 
older than proto::v12");
+            callback(ResultUnsupportedVersionError, MessageId());
+            return;
+        }
+    }
+
+    LOG_ERROR(getName() << " Client Connection not ready for Consumer");
+    callback(ResultNotConnected, MessageId());
 
 Review comment:
   Put in an else block, and you can avoid the other returns

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to