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 = std::function<void(const std::vector<std::string>&)>;
78 using ErrorCallback = std::function<void(const std::string&)>;
79 using RomSyncCallback = std::function<void(const RomSync&)>;
80 using SnapshotCallback = std::function<void(const Snapshot&)>;
81 using ProposalCallback = std::function<void(const Proposal&)>;
82 using ProposalUpdateCallback = std::function<void(const std::string&, const std::string&)>;
83 using AIResponseCallback = std::function<void(const AIResponse&)>;
84
85 explicit NetworkCollaborationCoordinator(const std::string& server_url);
86 ~NetworkCollaborationCoordinator();
87
88 // Session management
89 absl::StatusOr<SessionInfo> HostSession(const std::string& session_name,
90 const std::string& username,
91 const std::string& rom_hash = "",
92 bool ai_enabled = true);
93 absl::StatusOr<SessionInfo> JoinSession(const std::string& session_code,
94 const std::string& username);
95 absl::Status LeaveSession();
96
97 // Communication methods
98 absl::Status SendChatMessage(const std::string& sender,
99 const std::string& message,
100 const std::string& message_type = "chat",
101 const std::string& metadata = "");
102
103 // Advanced features
104 absl::Status SendRomSync(const std::string& sender,
105 const std::string& diff_data,
106 const std::string& rom_hash);
107
108 absl::Status SendSnapshot(const std::string& sender,
109 const std::string& snapshot_data,
110 const std::string& snapshot_type);
111
112 absl::Status SendProposal(const std::string& sender,
113 const std::string& proposal_data_json);
114
115 absl::Status UpdateProposal(const std::string& proposal_id,
116 const std::string& status);
117
118 absl::Status SendAIQuery(const std::string& username,
119 const std::string& query);
120
121 // Connection status
122 bool IsConnected() const;
123 bool InSession() const { return in_session_; }
124 const std::string& session_code() const { return session_code_; }
125 const std::string& session_name() const { return session_name_; }
126
127 // Event callbacks
128 void SetMessageCallback(MessageCallback callback);
129 void SetParticipantCallback(ParticipantCallback callback);
130 void SetErrorCallback(ErrorCallback callback);
131 void SetRomSyncCallback(RomSyncCallback callback);
132 void SetSnapshotCallback(SnapshotCallback callback);
133 void SetProposalCallback(ProposalCallback callback);
134 void SetProposalUpdateCallback(ProposalUpdateCallback callback);
135 void SetAIResponseCallback(AIResponseCallback callback);
136
137 private:
138 void ConnectWebSocket();
139 void DisconnectWebSocket();
140 void SendWebSocketMessage(const std::string& type, const std::string& payload_json);
141 void HandleWebSocketMessage(const std::string& message);
142 void WebSocketReceiveLoop();
143
144 std::string server_url_;
145 std::string username_;
146 std::string session_id_;
147 std::string session_code_;
148 std::string session_name_;
149 bool in_session_ = false;
150
151 std::unique_ptr<detail::WebSocketClient> ws_client_;
152 std::atomic<bool> connected_{false};
153 std::atomic<bool> should_stop_{false};
154 std::unique_ptr<std::thread> receive_thread_;
155
156 mutable absl::Mutex mutex_;
157 MessageCallback message_callback_ ABSL_GUARDED_BY(mutex_);
158 ParticipantCallback participant_callback_ ABSL_GUARDED_BY(mutex_);
159 ErrorCallback error_callback_ ABSL_GUARDED_BY(mutex_);
160 RomSyncCallback rom_sync_callback_ ABSL_GUARDED_BY(mutex_);
161 SnapshotCallback snapshot_callback_ ABSL_GUARDED_BY(mutex_);
162 ProposalCallback proposal_callback_ ABSL_GUARDED_BY(mutex_);
163 ProposalUpdateCallback proposal_update_callback_ ABSL_GUARDED_BY(mutex_);
164 AIResponseCallback ai_response_callback_ ABSL_GUARDED_BY(mutex_);
165};
166
167} // namespace editor
168} // namespace yaze
169
170#endif // YAZE_WITH_GRPC
171#endif // YAZE_APP_EDITOR_AGENT_NETWORK_COLLABORATION_COORDINATOR_H_
Main namespace for the application.
Definition controller.cc:20