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