yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
unified_grpc_server.cc
Go to the documentation of this file.
2
3#ifdef YAZE_WITH_GRPC
4
6
7#include <grpcpp/grpcpp.h>
8
9#include <iostream>
10#include <thread>
11
12#include "absl/strings/str_format.h"
17#include "protos/canvas_automation.grpc.pb.h"
18#include "rom/rom.h"
19
21
22namespace yaze {
23
24YazeGRPCServer::YazeGRPCServer() : is_running_(false) {}
25
26YazeGRPCServer::~YazeGRPCServer() {
27 Shutdown();
28}
29
30absl::Status YazeGRPCServer::Initialize(
31 int port,
32 emu::IEmulator* emulator,
33 RomGetter rom_getter,
34 RomLoader rom_loader,
35 test::TestManager* test_manager,
36 net::RomVersionManager* version_mgr,
37 net::ProposalApprovalManager* approval_mgr,
38 CanvasAutomationServiceImpl* canvas_service) {
39 if (is_running_) {
40 return absl::FailedPreconditionError("Server is already running");
41 }
42
43 config_.port = port;
44
45 // Create ImGuiTestHarness service if test_manager provided
46 if (config_.enable_test_harness && test_manager) {
47 test_harness_service_ =
48 std::make_unique<test::ImGuiTestHarnessServiceImpl>(test_manager);
49 std::cout << "✓ ImGuiTestHarness service initialized\n";
50 }
51
52 // Create Emulator service if emulator provided
53 if (config_.enable_emulator_service && emulator) {
54 emulator_service_ = std::make_unique<net::EmulatorServiceImpl>(
55 emulator, rom_getter, rom_loader);
56 std::cout << "✓ Emulator service initialized\n";
57 } else if (config_.enable_emulator_service) {
58 std::cout << "⚠ Emulator service requested but no emulator provided\n";
59 }
60
61 // Create ROM service if rom_getter provided
62 if (config_.enable_rom_service && rom_getter) {
63 rom_service_ = std::make_unique<net::RomServiceImpl>(
64 rom_getter, version_mgr, approval_mgr);
65
66 // Configure ROM service
67 net::RomServiceImpl::Config rom_config;
68 rom_config.require_approval_for_writes =
69 config_.require_approval_for_rom_writes;
70 rom_service_->SetConfig(rom_config);
71
72 std::cout << "✓ ROM service initialized\n";
73 } else if (config_.enable_rom_service) {
74 std::cout << "⚠ ROM service requested but no ROM provided\n";
75 }
76
77 // Create Canvas Automation service if canvas_service provided
78 if (config_.enable_canvas_automation && canvas_service) {
79 // Store the provided service (not owned by us)
80 canvas_service_ = canvas_service;
81 std::cout << "✓ Canvas Automation service initialized\n";
82 } else if (config_.enable_canvas_automation) {
83 std::cout << "⚠ Canvas Automation requested but no service provided\n";
84 }
85
86 if (!test_harness_service_ && !rom_service_ && !canvas_service_ && !emulator_service_) {
87 return absl::InvalidArgumentError(
88 "At least one service must be enabled and initialized");
89 }
90
91 return absl::OkStatus();
92}
93
94absl::Status YazeGRPCServer::Start() {
95 auto status = BuildServer();
96 if (!status.ok()) {
97 return status;
98 }
99
100 std::cout << "✓ YAZE gRPC automation server listening on 0.0.0.0:"
101 << config_.port << "\n";
102
103 if (test_harness_service_) {
104 std::cout << " ✓ ImGuiTestHarness available\n";
105 }
106 if (emulator_service_) {
107 std::cout << " ✓ EmulatorService available\n";
108 }
109 if (rom_service_) {
110 std::cout << " ✓ ROM service available\n";
111 }
112 if (canvas_service_) {
113 std::cout << " ✓ Canvas Automation available\n";
114 }
115
116 std::cout << "\nServer is ready to accept requests...\n";
117
118 // Block until server is shut down
119 server_->Wait();
120
121 return absl::OkStatus();
122}
123
124absl::Status YazeGRPCServer::StartAsync() {
125 auto status = BuildServer();
126 if (!status.ok()) {
127 return status;
128 }
129
130 std::cout << "✓ Unified gRPC server started on port " << config_.port << "\n";
131
132 // Server runs in background, doesn't block
133 return absl::OkStatus();
134}
135
136absl::Status YazeGRPCServer::AddService(
137 std::unique_ptr<grpc::Service> service) {
138 if (!service) {
139 return absl::InvalidArgumentError("Service is null");
140 }
141 if (is_running_) {
142 return absl::FailedPreconditionError(
143 "Cannot add services after the server has started");
144 }
145 extra_services_.push_back(std::move(service));
146 return absl::OkStatus();
147}
148
149void YazeGRPCServer::Shutdown() {
150 if (server_ && is_running_) {
151 std::cout << "⏹ Shutting down unified gRPC server...\n";
152 server_->Shutdown();
153 server_.reset();
154 is_running_ = false;
155 std::cout << "✓ Server stopped\n";
156 }
157}
158
159bool YazeGRPCServer::IsRunning() const {
160 return is_running_;
161}
162
163absl::Status YazeGRPCServer::BuildServer() {
164 if (is_running_) {
165 return absl::FailedPreconditionError("Server already running");
166 }
167
168 std::string server_address = absl::StrFormat("0.0.0.0:%d", config_.port);
169
170 grpc::ServerBuilder builder;
171
172 // Listen on all interfaces
173 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
174
175 // Register services
176 if (test_harness_service_) {
177 // Create gRPC wrapper for the test harness service
178 test_harness_grpc_wrapper_ =
179 test::CreateImGuiTestHarnessServiceGrpc(test_harness_service_.get());
180 std::cout << " Registering ImGuiTestHarness service...\n";
181 builder.RegisterService(test_harness_grpc_wrapper_.get());
182 }
183
184 if (emulator_service_) {
185 std::cout << " Registering EmulatorService...\n";
186 builder.RegisterService(emulator_service_.get());
187 }
188
189 if (rom_service_) {
190 std::cout << " Registering ROM service...\n";
191 builder.RegisterService(rom_service_.get());
192 }
193
194 if (canvas_service_) {
195 std::cout << " Registering Canvas Automation service...\n";
196 // Create gRPC wrapper using factory function
197 canvas_grpc_service_ = CreateCanvasAutomationServiceGrpc(canvas_service_);
198 builder.RegisterService(canvas_grpc_service_.get());
199 }
200
201 for (auto& service : extra_services_) {
202 builder.RegisterService(service.get());
203 }
204
205 // Build and start
206 server_ = builder.BuildAndStart();
207
208 if (!server_) {
209 return absl::InternalError(
210 absl::StrFormat("Failed to start server on %s", server_address));
211 }
212
213 is_running_ = true;
214
215 return absl::OkStatus();
216}
217
218} // namespace yaze
219
220#endif // YAZE_WITH_GRPC