3 // Copyright 2020 gRPC authors.
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 #ifndef GRPCPP_XDS_SERVER_BUILDER_H
20 #define GRPCPP_XDS_SERVER_BUILDER_H
22 #include <grpc/impl/codegen/port_platform.h>
24 #include <grpcpp/server_builder.h>
28 class XdsServerServingStatusNotifierInterface {
30 struct ServingStatusUpdate {
31 ::grpc::Status status;
34 virtual ~XdsServerServingStatusNotifierInterface() = default;
36 // \a uri contains the listening target associated with the notification. Note
37 // that a single target provided to XdsServerBuilder can get resolved to
38 // multiple listening addresses.
39 // The callback is invoked each time there is an update to the serving status.
40 // The API does not provide any guarantees around duplicate updates.
41 // Status::OK signifies that the server is serving, while a non-OK status
42 // signifies that the server is not serving.
43 virtual void OnServingStatusUpdate(std::string uri,
44 ServingStatusUpdate update) = 0;
47 class XdsServerBuilder : public ::grpc::ServerBuilder {
49 // It is the responsibility of the application to make sure that \a notifier
50 // outlasts the life of the server. Notifications will start being made
51 // asynchronously once `BuildAndStart()` has been called. Note that it is
52 // possible for notifications to be made before `BuildAndStart()` returns.
53 void set_status_notifier(XdsServerServingStatusNotifierInterface* notifier) {
58 // Called at the beginning of BuildAndStart().
59 ChannelArguments BuildChannelArgs() override {
60 ChannelArguments args = ServerBuilder::BuildChannelArgs();
61 grpc_channel_args c_channel_args = args.c_channel_args();
62 grpc_server_config_fetcher* fetcher = grpc_server_config_fetcher_xds_create(
63 {OnServingStatusUpdate, notifier_}, &c_channel_args);
64 if (fetcher != nullptr) set_fetcher(fetcher);
68 static void OnServingStatusUpdate(void* user_data, const char* uri,
69 grpc_serving_status_update update) {
70 if (user_data == nullptr) return;
71 XdsServerServingStatusNotifierInterface* notifier =
72 static_cast<XdsServerServingStatusNotifierInterface*>(user_data);
73 notifier->OnServingStatusUpdate(
74 uri, {grpc::Status(static_cast<StatusCode>(update.code),
75 update.error_message)});
78 XdsServerServingStatusNotifierInterface* notifier_ = nullptr;
81 namespace experimental {
82 // TODO(yashykt): Delete this after the 1.42 release.
84 "Use grpc::XdsServerServingStatusNotifierInterface instead. The "
85 "experimental version will be deleted after the 1.42 release.")
86 typedef grpc::XdsServerServingStatusNotifierInterface
87 XdsServerServingStatusNotifierInterface;
89 "Use grpc::XdsServerBuilder instead. The experimental version will be "
90 "deleted after the 1.42 release.")
91 typedef grpc::XdsServerBuilder XdsServerBuilder;
92 } // namespace experimental
95 #endif /* GRPCPP_XDS_SERVER_BUILDER_H */