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
5#include <grpcpp/grpcpp.h>
6
7#include <iostream>
8#include <thread>
9
10#include "absl/strings/str_format.h"
12#include "rom/rom.h"
15#include "protos/canvas_automation.grpc.pb.h"
16
17namespace yaze {
18
19YazeGRPCServer::YazeGRPCServer() : is_running_(false) {}
20
21// Destructor defined here so CanvasAutomationServiceGrpc is a complete type
22YazeGRPCServer::~YazeGRPCServer() {
23 Shutdown();
24}
25
26absl::Status YazeGRPCServer::Initialize(
27 int port, test::TestManager* test_manager, Rom* rom,
28 net::RomVersionManager* version_mgr,
29 net::ProposalApprovalManager* approval_mgr,
30 CanvasAutomationServiceImpl* canvas_service) {
31 if (is_running_) {
32 return absl::FailedPreconditionError("Server is already running");
33 }
34
35 config_.port = port;
36
37 // Create ImGuiTestHarness service if test_manager provided
38 if (config_.enable_test_harness && test_manager) {
39 test_harness_service_ =
40 std::make_unique<test::ImGuiTestHarnessServiceImpl>(test_manager);
41 std::cout << "✓ ImGuiTestHarness service initialized\n";
42 } else if (config_.enable_test_harness) {
43 std::cout << "⚠ ImGuiTestHarness requested but no TestManager provided\n";
44 }
45
46 // Create ROM service if rom provided
47 if (config_.enable_rom_service && rom) {
48 rom_service_ =
49 std::make_unique<net::RomServiceImpl>(rom, version_mgr, approval_mgr);
50
51 // Configure ROM service
52 net::RomServiceImpl::Config rom_config;
53 rom_config.require_approval_for_writes =
54 config_.require_approval_for_rom_writes;
55 rom_service_->SetConfig(rom_config);
56
57 std::cout << "✓ ROM service initialized\n";
58 } else if (config_.enable_rom_service) {
59 std::cout << "⚠ ROM service requested but no ROM provided\n";
60 }
61
62 // Create Canvas Automation service if canvas_service provided
63 if (config_.enable_canvas_automation && canvas_service) {
64 // Store the provided service (not owned by us)
65 canvas_service_ = canvas_service;
66 std::cout << "✓ Canvas Automation service initialized\n";
67 } else if (config_.enable_canvas_automation) {
68 std::cout << "⚠ Canvas Automation requested but no service provided\n";
69 }
70
71 if (!test_harness_service_ && !rom_service_ && !canvas_service_) {
72 return absl::InvalidArgumentError(
73 "At least one service must be enabled and initialized");
74 }
75
76 return absl::OkStatus();
77}
78
79absl::Status YazeGRPCServer::Start() {
80 auto status = BuildServer();
81 if (!status.ok()) {
82 return status;
83 }
84
85 std::cout << "✓ YAZE gRPC automation server listening on 0.0.0.0:"
86 << config_.port << "\n";
87
88 if (test_harness_service_) {
89 std::cout << " ✓ ImGuiTestHarness available\n";
90 }
91 if (rom_service_) {
92 std::cout << " ✓ ROM service available\n";
93 }
94 if (canvas_service_) {
95 std::cout << " ✓ Canvas Automation available\n";
96 }
97
98 std::cout << "\nServer is ready to accept requests...\n";
99
100 // Block until server is shut down
101 server_->Wait();
102
103 return absl::OkStatus();
104}
105
106absl::Status YazeGRPCServer::StartAsync() {
107 auto status = BuildServer();
108 if (!status.ok()) {
109 return status;
110 }
111
112 std::cout << "✓ Unified gRPC server started on port " << config_.port << "\n";
113
114 // Server runs in background, doesn't block
115 return absl::OkStatus();
116}
117
118void YazeGRPCServer::Shutdown() {
119 if (server_ && is_running_) {
120 std::cout << "⏹ Shutting down unified gRPC server...\n";
121 server_->Shutdown();
122 server_.reset();
123 is_running_ = false;
124 std::cout << "✓ Server stopped\n";
125 }
126}
127
128bool YazeGRPCServer::IsRunning() const {
129 return is_running_;
130}
131
132absl::Status YazeGRPCServer::BuildServer() {
133 if (is_running_) {
134 return absl::FailedPreconditionError("Server already running");
135 }
136
137 std::string server_address = absl::StrFormat("0.0.0.0:%d", config_.port);
138
139 grpc::ServerBuilder builder;
140
141 // Listen on all interfaces
142 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
143
144 // Register services
145 if (test_harness_service_) {
146 // Note: The actual registration requires the gRPC service wrapper
147 // This is a simplified version - full implementation would need
148 // the wrapper from imgui_test_harness_service.cc
149 std::cout << " Registering ImGuiTestHarness service...\n";
150 // builder.RegisterService(test_harness_grpc_wrapper_.get());
151 }
152
153 if (rom_service_) {
154 std::cout << " Registering ROM service...\n";
155 builder.RegisterService(rom_service_.get());
156 }
157
158 if (canvas_service_) {
159 std::cout << " Registering Canvas Automation service...\n";
160 // Create gRPC wrapper using factory function
161 canvas_grpc_service_ =
162 CreateCanvasAutomationServiceGrpc(canvas_service_);
163 builder.RegisterService(canvas_grpc_service_.get());
164 }
165
166 // Build and start
167 server_ = builder.BuildAndStart();
168
169 if (!server_) {
170 return absl::InternalError(
171 absl::StrFormat("Failed to start server on %s", server_address));
172 }
173
174 is_running_ = true;
175
176 return absl::OkStatus();
177}
178
179} // namespace yaze
180
181#endif // YAZE_WITH_GRPC