1 /*
2 * Copyright 2019 Google LLC.
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 * https://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
16 #include <iostream>
17 #include <memory>
18 #include <ostream>
19 #include <string>
20 #include <thread> // NOLINT
21 #include <utility>
22
23 #include "absl/flags/flag.h"
24 #include "absl/flags/parse.h"
25 #include "include/grpc/grpc_security_constants.h"
26 #include "include/grpcpp/grpcpp.h"
27 #include "include/grpcpp/security/server_credentials.h"
28 #include "include/grpcpp/server_builder.h"
29 #include "include/grpcpp/server_context.h"
30 #include "include/grpcpp/support/status.h"
31 #include "private_join_and_compute/data_util.h"
32 #include "private_join_and_compute/private_join_and_compute.grpc.pb.h"
33 #include "private_join_and_compute/private_join_and_compute_rpc_impl.h"
34 #include "private_join_and_compute/protocol_server.h"
35 #include "private_join_and_compute/server_impl.h"
36
37 ABSL_FLAG(std::string, port, "0.0.0.0:10501", "Port on which to listen");
38 ABSL_FLAG(std::string, server_data_file, "",
39 "The file from which to read the server database.");
40
RunServer()41 int RunServer() {
42 std::cout << "Server: loading data... " << std::endl;
43 auto maybe_server_identifiers =
44 ::private_join_and_compute::ReadServerDatasetFromFile(
45 absl::GetFlag(FLAGS_server_data_file));
46 if (!maybe_server_identifiers.ok()) {
47 std::cerr << "RunServer: failed " << maybe_server_identifiers.status()
48 << std::endl;
49 return 1;
50 }
51
52 ::private_join_and_compute::Context context;
53 std::unique_ptr<::private_join_and_compute::ProtocolServer> server =
54 std::make_unique<
55 ::private_join_and_compute::PrivateIntersectionSumProtocolServerImpl>(
56 &context, std::move(maybe_server_identifiers.value()));
57 ::private_join_and_compute::PrivateJoinAndComputeRpcImpl service(
58 std::move(server));
59
60 ::grpc::ServerBuilder builder;
61 // Consider grpc::SslServerCredentials if not running locally.
62 builder.AddListeningPort(absl::GetFlag(FLAGS_port),
63 ::grpc::experimental::LocalServerCredentials(
64 grpc_local_connect_type::LOCAL_TCP));
65 builder.RegisterService(&service);
66 std::unique_ptr<::grpc::Server> grpc_server(builder.BuildAndStart());
67
68 // Run the server on a background thread.
69 std::thread grpc_server_thread(
70 [](::grpc::Server* grpc_server_ptr) {
71 std::cout << "Server: listening on " << absl::GetFlag(FLAGS_port)
72 << std::endl;
73 grpc_server_ptr->Wait();
74 },
75 grpc_server.get());
76
77 while (!service.protocol_finished()) {
78 // Wait for the server to be done, and then shut the server down.
79 }
80
81 // Shut down server.
82 grpc_server->Shutdown();
83 grpc_server_thread.join();
84 std::cout << "Server completed protocol and shut down." << std::endl;
85
86 return 0;
87 }
88
main(int argc,char ** argv)89 int main(int argc, char** argv) {
90 absl::ParseCommandLine(argc, argv);
91
92 return RunServer();
93 }
94