235 bool publish(
const std::string& topic,
const std::string& message);
243 bool publish(
const std::string& topic,
const nlohmann::json& message);
312 std::atomic<bool> running_;
315 std::thread heartbeat_thread_;
316 mutable std::mutex heartbeat_mutex_;
317 std::condition_variable heartbeat_cv_;
320 mutable std::mutex topics_mutex_;
321 std::unordered_map<std::string, std::unique_ptr<TopicSubscription>> topics_;
322 std::unordered_set<std::string> subscribed_topics_;
325 mutable std::mutex scores_mutex_;
326 std::unordered_map<std::string, std::unique_ptr<PeerScore>> peer_scores_;
329 mutable std::mutex message_cache_mutex_;
330 std::unordered_map<std::string, std::unique_ptr<MessageMetadata>> message_cache_;
331 std::unordered_map<std::string, std::chrono::steady_clock::time_point> message_ids_seen_;
334 mutable std::mutex handlers_mutex_;
335 std::unordered_map<std::string, MessageValidator> message_validators_;
336 std::unordered_map<std::string, MessageHandler> message_handlers_;
337 std::unordered_map<std::string, PeerJoinedHandler> peer_joined_handlers_;
338 std::unordered_map<std::string, PeerLeftHandler> peer_left_handlers_;
342 mutable std::mutex control_queue_mutex_;
343 std::vector<nlohmann::json> pending_grafts_;
344 std::vector<nlohmann::json> pending_prunes_;
345 std::vector<nlohmann::json> pending_ihaves_;
346 std::vector<nlohmann::json> pending_iwants_;
349 mutable std::mutex rng_mutex_;
353 void heartbeat_loop();
354 void process_heartbeat();
355 void handle_gossipsub_message(
const std::string& peer_id,
const nlohmann::json& message);
358 void handle_subscribe(
const std::string& peer_id,
const nlohmann::json& payload);
359 void handle_unsubscribe(
const std::string& peer_id,
const nlohmann::json& payload);
360 void handle_publish(
const std::string& peer_id,
const nlohmann::json& payload);
361 void handle_gossip(
const std::string& peer_id,
const nlohmann::json& payload);
362 void handle_graft(
const std::string& peer_id,
const nlohmann::json& payload);
363 void handle_prune(
const std::string& peer_id,
const nlohmann::json& payload);
364 void handle_ihave(
const std::string& peer_id,
const nlohmann::json& payload);
365 void handle_iwant(
const std::string& peer_id,
const nlohmann::json& payload);
366 void handle_heartbeat(
const std::string& peer_id,
const nlohmann::json& payload);
369 void maintain_mesh(
const std::string& topic);
370 void add_peer_to_mesh(
const std::string& topic,
const std::string& peer_id);
371 void remove_peer_from_mesh(
const std::string& topic,
const std::string& peer_id);
372 std::vector<std::string> select_peers_for_mesh(
const std::string& topic,
int count);
373 std::vector<std::string> select_peers_for_gossip(
const std::string& topic,
int count,
const std::unordered_set<std::string>& exclude = {});
376 std::string generate_message_id(
const std::string& topic,
const std::string& message,
const std::string& sender_peer_id);
377 bool is_message_seen(
const std::string& message_id);
378 void cache_message(
const std::string& message_id,
const std::string& topic,
const std::string& message,
const std::string& sender_peer_id);
379 void cleanup_message_cache();
382 void update_peer_score(
const std::string& peer_id);
383 void handle_peer_connected(
const std::string& peer_id);
384 void handle_peer_disconnected(
const std::string& peer_id);
387 bool send_gossipsub_message(
const std::string& peer_id,
GossipSubMessageType type,
const nlohmann::json& payload);
388 bool broadcast_gossipsub_message(
GossipSubMessageType type,
const nlohmann::json& payload,
const std::unordered_set<std::string>& exclude = {});
391 ValidationResult validate_message(
const std::string& topic,
const std::string& message,
const std::string& sender_peer_id);
392 bool is_peer_score_acceptable(
const std::string& peer_id,
double threshold);
395 TopicSubscription* get_or_create_topic(
const std::string& topic);
396 void cleanup_topic(
const std::string& topic);
399 std::vector<std::string> random_sample(
const std::vector<std::string>& peers,
int count);
400 std::vector<std::string> random_sample(
const std::unordered_set<std::string>& peers,
int count);