1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_INTERNAL_SUBSCRIPTION_MESSAGE_SOURCE_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_INTERNAL_SUBSCRIPTION_MESSAGE_SOURCE_H
17 
18 #include "google/cloud/pubsub/version.h"
19 #include "google/cloud/future.h"
20 #include "google/cloud/status.h"
21 #include <google/pubsub/v1/pubsub.pb.h>
22 #include <functional>
23 
24 namespace google {
25 namespace cloud {
26 namespace pubsub_internal {
27 inline namespace GOOGLE_CLOUD_CPP_PUBSUB_NS {
28 
29 using MessageCallback =
30     std::function<void(google::pubsub::v1::ReceivedMessage)>;
31 
32 /**
33  * Defines the interface for one-message-at-a-time sources.
34  *
35  * A message source generates messages via `MessageCallback` callbacks.
36  * Typically the source is some sort of queue that receives `AsyncPull()`
37  * responses and breaks them into smaller messages.
38  */
39 class SubscriptionMessageSource {
40  public:
41   virtual ~SubscriptionMessageSource() = default;
42 
43   /// Start the source, set up the callback. Calling multiple times should have
44   /// no effect, only the first callback is used.
45   virtual void Start(MessageCallback) = 0;
46 
47   /// Shutdown the source, cancel any outstanding requests and or timers. No
48   /// callbacks should be generated after this call.
49   virtual void Shutdown() = 0;
50 
51   /// Request more messages from the source.
52   virtual void Read(std::size_t max_callbacks) = 0;
53 
54   /**
55    * Positive acknowledgement the message associated with @p ack_id.
56    *
57    * The application has successfully handled this message and no new deliveries
58    * are necessary. The @p size parameter should be the original message size
59    * estimate. The @p size parameter may be used by the message source to flow
60    * control large messages.
61    */
62   virtual future<Status> AckMessage(std::string const& ack_id,
63                                     std::size_t size) = 0;
64 
65   /**
66    * Negative acknowledgement for message associated with @p ack_id.
67    *
68    * The application was not able to handle this message. Nacking a message
69    * allows the service to re-deliver it, subject to the topic and subscription
70    * configuration. The @p size parameter should be the original message size
71    * estimate. The @p size parameter may be used by the message source to flow
72    * control large messages.
73    */
74   virtual future<Status> NackMessage(std::string const& ack_id,
75                                      std::size_t size) = 0;
76 };
77 
78 }  // namespace GOOGLE_CLOUD_CPP_PUBSUB_NS
79 }  // namespace pubsub_internal
80 }  // namespace cloud
81 }  // namespace google
82 
83 #endif  // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUB_INTERNAL_SUBSCRIPTION_MESSAGE_SOURCE_H
84