yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
network_collaboration_coordinator.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EDITOR_AGENT_NETWORK_COLLABORATION_COORDINATOR_H_
2#define YAZE_APP_EDITOR_AGENT_NETWORK_COLLABORATION_COORDINATOR_H_
3
4#ifdef YAZE_WITH_GRPC // Reuse gRPC build flag for network features
5
6#include <atomic>
7#include <functional>
8#include <memory>
9#include <string>
10#include <thread>
11#include <vector>
12
13#include "absl/status/status.h"
14#include "absl/status/statusor.h"
15#include "absl/synchronization/mutex.h"
16
17namespace yaze {
18namespace editor {
19
20// Forward declarations to avoid including httplib in header
21namespace detail {
22class WebSocketClient;
23}
24
25// Coordinates network-based collaboration via WebSocket connections
26class NetworkCollaborationCoordinator {
27 public:
28 struct SessionInfo {
29 std::string session_id;
30 std::string session_code;
31 std::string session_name;
32 std::vector<std::string> participants;
33 };
34
35 struct ChatMessage {
36 std::string sender;
37 std::string message;
38 int64_t timestamp;
39 std::string message_type; // "chat", "system", "ai"
40 std::string metadata; // JSON metadata
41 };
42
43 struct RomSync {
44 std::string sync_id;
45 std::string sender;
46 std::string diff_data; // Base64 encoded
47 std::string rom_hash;
48 int64_t timestamp;
49 };
50
51 struct Snapshot {
52 std::string snapshot_id;
53 std::string sender;
54 std::string snapshot_data; // Base64 encoded
55 std::string snapshot_type;
56 int64_t timestamp;
57 };
58
59 struct Proposal {
60 std::string proposal_id;
61 std::string sender;
62 std::string proposal_data; // JSON data
63 std::string status; // "pending", "accepted", "rejected"
64 int64_t timestamp;
65 };
66
67 struct AIResponse {
68 std::string query_id;
69 std::string username;
70 std::string query;
71 std::string response;
72 int64_t timestamp;
73 };
74
75 // Callbacks for handling incoming events
76 using MessageCallback = std::function<void(const ChatMessage&)>;
77 using ParticipantCallback =
78 std::function<void(const std::vector<std::string>&)>;
79 using ErrorCallback = std::function<void(const std::string&)>;
80 using RomSyncCallback = std::function<void(const RomSync&)>;
81 using SnapshotCallback = std::function<void(const Snapshot&)>;
82 using ProposalCallback = std::function<void(const Proposal&)>;
83 using ProposalUpdateCallback =
84 std::function<void(const std::string&, const std::string&)>;
85 using AIResponseCallback = std::function<void(const AIResponse&)>;
86
87 explicit NetworkCollaborationCoordinator(const std::string& server_url);
88 ~NetworkCollaborationCoordinator();
89
90 // Session management
91 absl::StatusOr<SessionInfo> HostSession(const std::string& session_name,
92 const std::string& username,
93 const std::string& rom_hash = "",
94 bool ai_enabled = true);
95 absl::StatusOr<SessionInfo> JoinSession(const std::string& session_code,
96 const std::string& username);
97 absl::Status LeaveSession();
98
99 // Communication methods
100 absl::Status SendChatMessage(const std::string& sender,
101 const std::string& message,
102 const std::string& message_type = "chat",
103 const std::string& metadata = "");
104
105 // Advanced features
106 absl::Status SendRomSync(const std::string& sender,
107 const std::string& diff_data,
108 const std::string& rom_hash);
109
110 absl::Status SendSnapshot(const std::string& sender,
111 const std::string& snapshot_data,
112 const std::string& snapshot_type);
113
114 absl::Status SendProposal(const std::string& sender,
115 const std::string& proposal_data_json);
116
117 absl::Status UpdateProposal(const std::string& proposal_id,
118 const std::string& status);
119
120 absl::Status SendAIQuery(const std::string& username,
121 const std::string& query);
122
123 // Connection status
124 bool IsConnected() const;
125 bool InSession() const { return in_session_; }
126 const std::string& session_code() const { return session_code_; }
127 const std::string& session_name() const { return session_name_; }
128
129 // Event callbacks
130 void SetMessageCallback(MessageCallback callback);
131 void SetParticipantCallback(ParticipantCallback callback);
132 void SetErrorCallback(ErrorCallback callback);
133 void SetRomSyncCallback(RomSyncCallback callback);
134 void SetSnapshotCallback(SnapshotCallback callback);
135 void SetProposalCallback(ProposalCallback callback);
136 void SetProposalUpdateCallback(ProposalUpdateCallback callback);
137 void SetAIResponseCallback(AIResponseCallback callback);
138
139 private:
140 void ConnectWebSocket();
141 void DisconnectWebSocket();
142 void SendWebSocketMessage(const std::string& type,
143 const std::string& payload_json);
144 void HandleWebSocketMessage(const std::string& message);
145 void WebSocketReceiveLoop();
146
147 std::string server_url_;
148 std::string username_;
149 std::string session_id_;
150 std::string session_code_;
151 std::string session_name_;
152 bool in_session_ = false;
153
154 std::unique_ptr<detail::WebSocketClient> ws_client_;
155 std::atomic<bool> connected_{false};
156 std::atomic<bool> should_stop_{false};
157 std::unique_ptr<std::thread> receive_thread_;
158
159 mutable absl::Mutex mutex_;
160 MessageCallback message_callback_ ABSL_GUARDED_BY(mutex_);
161 ParticipantCallback participant_callback_ ABSL_GUARDED_BY(mutex_);
162 ErrorCallback error_callback_ ABSL_GUARDED_BY(mutex_);
163 RomSyncCallback rom_sync_callback_ ABSL_GUARDED_BY(mutex_);
164 SnapshotCallback snapshot_callback_ ABSL_GUARDED_BY(mutex_);
165 ProposalCallback proposal_callback_ ABSL_GUARDED_BY(mutex_);
166 ProposalUpdateCallback proposal_update_callback_ ABSL_GUARDED_BY(mutex_);
167 AIResponseCallback ai_response_callback_ ABSL_GUARDED_BY(mutex_);
168};
169
170} // namespace editor
171} // namespace yaze
172
173#endif // YAZE_WITH_GRPC
174#endif // YAZE_APP_EDITOR_AGENT_NETWORK_COLLABORATION_COORDINATOR_H_