1#[cfg(feature = "p2p-libp2p")]
2pub mod mio;
3#[cfg(feature = "p2p-webrtc")]
4pub mod webrtc;
5pub mod webrtc_with_libp2p;
6
7use std::future::Future;
8
9pub trait TaskSpawner: Send + Clone {
10 fn spawn_main<F>(&self, name: &str, fut: F)
11 where
12 F: 'static + Send + Future<Output = ()>;
13}
14
15#[cfg(not(feature = "p2p-webrtc"))]
16pub mod webrtc {
17 use std::collections::BTreeMap;
18
19 use openmina_core::channels::mpsc;
20
21 use crate::{
22 channels::{ChannelId, ChannelMsg, MsgId},
23 connection::outgoing::P2pConnectionOutgoingInitOpts,
24 identity::{EncryptableType, PublicKey, SecretKey},
25 webrtc, P2pEvent, PeerId,
26 };
27
28 use super::TaskSpawner;
29
30 pub struct P2pServiceCtx {
31 pub cmd_sender: mpsc::UnboundedSender<Cmd>,
32 pub peers: BTreeMap<PeerId, PeerState>,
33 }
34
35 pub type PeerState = ();
36
37 pub enum Cmd {}
38
39 #[allow(unused_variables)]
40 pub trait P2pServiceWebrtc: redux::Service {
41 type Event: From<P2pEvent> + Send + Sync + 'static;
42
43 fn random_pick(
44 &mut self,
45 list: &[P2pConnectionOutgoingInitOpts],
46 ) -> Option<P2pConnectionOutgoingInitOpts>;
47
48 fn event_sender(&self) -> &mpsc::UnboundedSender<Self::Event>;
49
50 fn cmd_sender(&self) -> &mpsc::TrackedUnboundedSender<Cmd>;
51
52 fn peers(&mut self) -> &mut BTreeMap<PeerId, PeerState>;
53
54 fn init<S: TaskSpawner>(
55 _secret_key: SecretKey,
56 _spawner: S,
57 _rng_seed: [u8; 32],
58 ) -> P2pServiceCtx {
59 let (cmd_sender, _) = mpsc::unbounded_channel();
60 P2pServiceCtx {
61 cmd_sender,
62 peers: Default::default(),
63 }
64 }
65
66 fn outgoing_init(&mut self, peer_id: PeerId) {}
67
68 fn incoming_init(&mut self, peer_id: PeerId, offer: webrtc::Offer) {}
69
70 fn set_answer(&mut self, peer_id: PeerId, answer: webrtc::Answer) {}
71
72 fn http_signaling_request(&mut self, url: String, offer: webrtc::Offer) {}
73
74 fn disconnect(&mut self, peer_id: PeerId) -> bool {
75 false
76 }
77
78 fn channel_open(&mut self, peer_id: PeerId, id: ChannelId) {}
79
80 fn channel_send(&mut self, peer_id: PeerId, msg_id: MsgId, msg: ChannelMsg) {}
81
82 fn encrypt<T: EncryptableType>(
83 &mut self,
84 other_pk: &PublicKey,
85 message: &T,
86 ) -> Result<T::Encrypted, Box<dyn std::error::Error>>;
87
88 fn decrypt<T: EncryptableType>(
89 &mut self,
90 other_pub_key: &PublicKey,
91 encrypted: &T::Encrypted,
92 ) -> Result<T, Box<dyn std::error::Error>>;
93
94 fn auth_send(
95 &mut self,
96 peer_id: PeerId,
97 other_pub_key: &PublicKey,
98 auth: Option<webrtc::ConnectionAuthEncrypted>,
99 ) {
100 }
101
102 fn auth_encrypt_and_send(
103 &mut self,
104 peer_id: PeerId,
105 other_pub_key: &PublicKey,
106 auth: webrtc::ConnectionAuth,
107 );
108
109 fn auth_decrypt(
110 &mut self,
111 other_pub_key: &PublicKey,
112 auth: webrtc::ConnectionAuthEncrypted,
113 ) -> Option<webrtc::ConnectionAuth>;
114 }
115}