1 /*
2 *
3 * Copyright 2018 gRPC authors.
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/tsi/alts/handshaker/alts_shared_resource.h"
22
23 #include <grpc/support/log.h>
24
25 #include "src/core/tsi/alts/handshaker/alts_handshaker_client.h"
26
27 static alts_shared_resource_dedicated g_alts_resource_dedicated;
28
grpc_alts_get_shared_resource_dedicated(void)29 alts_shared_resource_dedicated* grpc_alts_get_shared_resource_dedicated(void) {
30 return &g_alts_resource_dedicated;
31 }
32
thread_worker(void *)33 static void thread_worker(void* /*arg*/) {
34 while (true) {
35 grpc_event event =
36 grpc_completion_queue_next(g_alts_resource_dedicated.cq,
37 gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
38 GPR_ASSERT(event.type != GRPC_QUEUE_TIMEOUT);
39 if (event.type == GRPC_QUEUE_SHUTDOWN) {
40 break;
41 }
42 GPR_ASSERT(event.type == GRPC_OP_COMPLETE);
43 alts_handshaker_client* client =
44 static_cast<alts_handshaker_client*>(event.tag);
45 alts_handshaker_client_handle_response(client, event.success);
46 }
47 }
48
grpc_alts_shared_resource_dedicated_init()49 void grpc_alts_shared_resource_dedicated_init() {
50 g_alts_resource_dedicated.cq = nullptr;
51 gpr_mu_init(&g_alts_resource_dedicated.mu);
52 }
53
grpc_alts_shared_resource_dedicated_start(const char * handshaker_service_url)54 void grpc_alts_shared_resource_dedicated_start(
55 const char* handshaker_service_url) {
56 gpr_mu_lock(&g_alts_resource_dedicated.mu);
57 if (g_alts_resource_dedicated.cq == nullptr) {
58 g_alts_resource_dedicated.channel =
59 grpc_insecure_channel_create(handshaker_service_url, nullptr, nullptr);
60 g_alts_resource_dedicated.cq =
61 grpc_completion_queue_create_for_next(nullptr);
62 g_alts_resource_dedicated.thread =
63 grpc_core::Thread("alts_tsi_handshaker", &thread_worker, nullptr);
64 g_alts_resource_dedicated.interested_parties = grpc_pollset_set_create();
65 grpc_pollset_set_add_pollset(g_alts_resource_dedicated.interested_parties,
66 grpc_cq_pollset(g_alts_resource_dedicated.cq));
67 g_alts_resource_dedicated.thread.Start();
68 }
69 gpr_mu_unlock(&g_alts_resource_dedicated.mu);
70 }
71
grpc_alts_shared_resource_dedicated_shutdown()72 void grpc_alts_shared_resource_dedicated_shutdown() {
73 if (g_alts_resource_dedicated.cq != nullptr) {
74 grpc_pollset_set_del_pollset(g_alts_resource_dedicated.interested_parties,
75 grpc_cq_pollset(g_alts_resource_dedicated.cq));
76 grpc_completion_queue_shutdown(g_alts_resource_dedicated.cq);
77 g_alts_resource_dedicated.thread.Join();
78 grpc_pollset_set_destroy(g_alts_resource_dedicated.interested_parties);
79 grpc_completion_queue_destroy(g_alts_resource_dedicated.cq);
80 grpc_channel_destroy(g_alts_resource_dedicated.channel);
81 }
82 gpr_mu_destroy(&g_alts_resource_dedicated.mu);
83 }
84