1 // Copyright 2022 The Android Open Source Project 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 #include "core/server.h" 16 17 #include <chrono> 18 #include <memory> 19 #include <optional> 20 #include <string> 21 #include <thread> 22 23 #ifdef NETSIM_ANDROID_EMULATOR 24 #include "backend/grpc_server.h" 25 #endif 26 #include "controller/controller.h" 27 #include "frontend/frontend_server.h" 28 #include "grpcpp/security/server_credentials.h" 29 #include "grpcpp/server.h" 30 #include "grpcpp/server_builder.h" 31 #include "netsim-cxx/src/lib.rs.h" 32 #include "util/filesystem.h" 33 #include "util/ini_file.h" 34 #include "util/log.h" 35 #include "util/os_utils.h" 36 #ifdef _WIN32 37 #include <Windows.h> 38 #else 39 #include <unistd.h> 40 #endif 41 42 namespace netsim::server { 43 44 namespace { 45 constexpr std::chrono::seconds InactivityCheckInterval(5); 46 RunGrpcServer(int netsim_grpc_port)47 std::unique_ptr<grpc::Server> RunGrpcServer(int netsim_grpc_port) { 48 grpc::ServerBuilder builder; 49 int selected_port; 50 builder.AddListeningPort("0.0.0.0:" + std::to_string(netsim_grpc_port), 51 grpc::InsecureServerCredentials(), &selected_port); 52 static auto frontend_service = GetFrontendService(); 53 builder.RegisterService(frontend_service.get()); 54 #ifdef NETSIM_ANDROID_EMULATOR 55 static auto backend_service = GetBackendService(); 56 builder.RegisterService(backend_service.get()); 57 #endif 58 std::unique_ptr<grpc::Server> server(builder.BuildAndStart()); 59 60 BtsLog("Grpc server listening on localhost: %s", 61 std::to_string(selected_port).c_str()); 62 63 // Writes grpc port to ini file. 64 auto filepath = osutils::GetNetsimIniFilepath(); 65 IniFile iniFile(filepath); 66 iniFile.Read(); 67 iniFile.Set("grpc.port", std::to_string(selected_port)); 68 iniFile.Write(); 69 70 return std::move(server); 71 } 72 } // namespace 73 Run()74 void Run() { 75 // Clear all pcap files in temp directory 76 if (netsim::pcap::ClearPcapFiles()) { 77 BtsLog("netsim generated pcap files in temp directory has been removed."); 78 } 79 80 // Environment variable "NETSIM_GRPC_PORT" is set in google3 forge. If set: 81 // 1. Use the fixed port for grpc server. 82 // 2. Don't start http server. 83 auto netsim_grpc_port = std::stoi(osutils::GetEnv("NETSIM_GRPC_PORT", "0")); 84 // Run frontend and backend grpc servers. 85 auto grpc_server = RunGrpcServer(netsim_grpc_port); 86 if (netsim_grpc_port == 0) { 87 // Run frontend http server. 88 std::thread(RunHttpServer).detach(); 89 } 90 91 while (true) { 92 std::this_thread::sleep_for(InactivityCheckInterval); 93 if (auto seconds_to_shutdown = netsim::scene_controller::GetShutdownTime(); 94 seconds_to_shutdown.has_value() && 95 seconds_to_shutdown.value() < std::chrono::seconds(0)) { 96 grpc_server->Shutdown(); 97 BtsLog("Netsim has been shutdown due to inactivity."); 98 break; 99 } 100 } 101 } 102 103 } // namespace netsim::server 104