protocol_privmsg.rs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. use async_std::sync::Arc;
  2. use std::cmp::Ordering;
  3. use async_executor::Executor;
  4. use async_trait::async_trait;
  5. use chrono::Utc;
  6. use log::debug;
  7. use rand::{rngs::OsRng, RngCore};
  8. use darkfi::{
  9. net,
  10. util::serial::{SerialDecodable, SerialEncodable},
  11. Result,
  12. };
  13. use crate::{buffers::Buffers, settings, Privmsg};
  14. #[derive(SerialDecodable, SerialEncodable, Clone, Debug)]
  15. struct Inv {
  16. id: u64,
  17. invs: Vec<InvObject>,
  18. }
  19. #[derive(SerialDecodable, SerialEncodable, Clone, Debug)]
  20. pub struct LastTerm {
  21. pub term: u64,
  22. }
  23. impl Inv {
  24. fn new(invs: Vec<InvObject>) -> Self {
  25. let id = OsRng.next_u64();
  26. Self { id, invs }
  27. }
  28. }
  29. #[derive(SerialDecodable, SerialEncodable, Clone, Debug)]
  30. struct GetData {
  31. invs: Vec<InvObject>,
  32. }
  33. impl GetData {
  34. fn new(invs: Vec<InvObject>) -> Self {
  35. Self { invs }
  36. }
  37. }
  38. #[derive(SerialDecodable, SerialEncodable, Clone, Debug)]
  39. struct InvObject(String);
  40. pub struct ProtocolPrivmsg {
  41. jobsman: net::ProtocolJobsManagerPtr,
  42. notify: async_channel::Sender<Privmsg>,
  43. msg_sub: net::MessageSubscription<Privmsg>,
  44. inv_sub: net::MessageSubscription<Inv>,
  45. getdata_sub: net::MessageSubscription<GetData>,
  46. last_term_sub: net::MessageSubscription<LastTerm>,
  47. p2p: net::P2pPtr,
  48. channel: net::ChannelPtr,
  49. buffers: Buffers,
  50. }
  51. impl ProtocolPrivmsg {
  52. pub async fn init(
  53. channel: net::ChannelPtr,
  54. notify: async_channel::Sender<Privmsg>,
  55. p2p: net::P2pPtr,
  56. buffers: Buffers,
  57. ) -> net::ProtocolBasePtr {
  58. let message_subsytem = channel.get_message_subsystem();
  59. message_subsytem.add_dispatch::<Privmsg>().await;
  60. message_subsytem.add_dispatch::<Inv>().await;
  61. message_subsytem.add_dispatch::<GetData>().await;
  62. message_subsytem.add_dispatch::<LastTerm>().await;
  63. let msg_sub =
  64. channel.clone().subscribe_msg::<Privmsg>().await.expect("Missing Privmsg dispatcher!");
  65. let inv_sub = channel.subscribe_msg::<Inv>().await.expect("Missing Inv dispatcher!");
  66. let getdata_sub =
  67. channel.clone().subscribe_msg::<GetData>().await.expect("Missing GetData dispatcher!");
  68. let last_term_sub = channel
  69. .clone()
  70. .subscribe_msg::<LastTerm>()
  71. .await
  72. .expect("Missing LastTerm dispatcher!");
  73. Arc::new(Self {
  74. notify,
  75. msg_sub,
  76. inv_sub,
  77. getdata_sub,
  78. last_term_sub,
  79. jobsman: net::ProtocolJobsManager::new("ProtocolPrivmsg", channel.clone()),
  80. p2p,
  81. channel,
  82. buffers,
  83. })
  84. }
  85. async fn handle_receive_inv(self: Arc<Self>) -> Result<()> {
  86. debug!(target: "ircd", "ProtocolPrivmsg::handle_receive_inv() [START]");
  87. let exclude_list = vec![self.channel.address()];
  88. loop {
  89. let inv = self.inv_sub.receive().await?;
  90. let inv = (*inv).to_owned();
  91. if !self.buffers.seen_ids.push(inv.id).await {
  92. continue
  93. }
  94. let mut inv_requested = vec![];
  95. for inv_object in inv.invs.iter() {
  96. if !self.buffers.unread_msgs.inc_read_confirms(&inv_object.0).await {
  97. inv_requested.push(inv_object.clone());
  98. }
  99. }
  100. if !inv_requested.is_empty() {
  101. self.channel.send(GetData::new(inv_requested)).await?;
  102. }
  103. self.update_unread_msgs().await?;
  104. self.p2p.broadcast_with_exclude(inv, &exclude_list).await?;
  105. }
  106. }
  107. async fn handle_receive_msg(self: Arc<Self>) -> Result<()> {
  108. debug!(target: "ircd", "ProtocolPrivmsg::handle_receive_msg() [START]");
  109. let exclude_list = vec![self.channel.address()];
  110. loop {
  111. let msg = self.msg_sub.receive().await?;
  112. let mut msg = (*msg).to_owned();
  113. if !self.buffers.seen_ids.push(msg.id).await {
  114. continue
  115. }
  116. if msg.read_confirms >= settings::MAX_CONFIRM {
  117. self.add_to_msgs(&msg).await?;
  118. } else {
  119. msg.read_confirms += 1;
  120. let hash = self.add_to_unread_msgs(&msg).await;
  121. self.p2p.broadcast(Inv::new(vec![InvObject(hash)])).await?;
  122. }
  123. self.update_unread_msgs().await?;
  124. self.p2p.broadcast_with_exclude(msg, &exclude_list).await?;
  125. }
  126. }
  127. async fn handle_receive_last_term(self: Arc<Self>) -> Result<()> {
  128. debug!(target: "ircd", "ProtocolPrivmsg::handle_receive_last_term() [START]");
  129. loop {
  130. let last_term = self.last_term_sub.receive().await?;
  131. let last_term = last_term.term;
  132. self.update_unread_msgs().await?;
  133. match self.buffers.privmsgs.last_term().await.cmp(&last_term) {
  134. Ordering::Less => {
  135. for msg in self.buffers.privmsgs.fetch_msgs(last_term).await {
  136. self.channel.send(msg).await?;
  137. }
  138. }
  139. Ordering::Greater | Ordering::Equal => continue,
  140. }
  141. }
  142. }
  143. async fn handle_receive_getdata(self: Arc<Self>) -> Result<()> {
  144. debug!(target: "ircd", "ProtocolPrivmsg::handle_receive_getdata() [START]");
  145. loop {
  146. let getdata = self.getdata_sub.receive().await?;
  147. let getdata = (*getdata).to_owned();
  148. for inv in getdata.invs {
  149. if let Some(msg) = self.buffers.unread_msgs.get(&inv.0).await {
  150. self.channel.send(msg.clone()).await?;
  151. }
  152. }
  153. }
  154. }
  155. async fn add_to_unread_msgs(&self, msg: &Privmsg) -> String {
  156. self.buffers.unread_msgs.insert(msg).await
  157. }
  158. async fn update_unread_msgs(&self) -> Result<()> {
  159. for (hash, msg) in self.buffers.unread_msgs.load().await {
  160. if msg.timestamp + settings::UNREAD_MSG_EXPIRE_TIME < Utc::now().timestamp() {
  161. self.buffers.unread_msgs.remove(&hash).await;
  162. continue
  163. }
  164. if msg.read_confirms >= settings::MAX_CONFIRM {
  165. self.add_to_msgs(&msg).await?;
  166. self.buffers.unread_msgs.remove(&hash).await;
  167. }
  168. }
  169. Ok(())
  170. }
  171. async fn add_to_msgs(&self, msg: &Privmsg) -> Result<()> {
  172. self.buffers.privmsgs.push(msg).await;
  173. self.notify.send(msg.clone()).await?;
  174. Ok(())
  175. }
  176. }
  177. #[async_trait]
  178. impl net::ProtocolBase for ProtocolPrivmsg {
  179. /// Starts ping-pong keep-alive messages exchange. Runs ping-pong in the
  180. /// protocol task manager, then queues the reply. Sends out a ping and
  181. /// waits for pong reply. Waits for ping and replies with a pong.
  182. async fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) -> Result<()> {
  183. // once a channel get started
  184. for m in self.buffers.privmsgs.load().await {
  185. self.channel.send(m).await?;
  186. }
  187. debug!(target: "ircd", "ProtocolPrivmsg::start() [START]");
  188. self.jobsman.clone().start(executor.clone());
  189. self.jobsman.clone().spawn(self.clone().handle_receive_msg(), executor.clone()).await;
  190. self.jobsman.clone().spawn(self.clone().handle_receive_inv(), executor.clone()).await;
  191. self.jobsman.clone().spawn(self.clone().handle_receive_getdata(), executor.clone()).await;
  192. self.jobsman.clone().spawn(self.clone().handle_receive_last_term(), executor.clone()).await;
  193. debug!(target: "ircd", "ProtocolPrivmsg::start() [END]");
  194. Ok(())
  195. }
  196. fn name(&self) -> &'static str {
  197. "ProtocolPrivmsg"
  198. }
  199. }
  200. impl net::Message for Privmsg {
  201. fn name() -> &'static str {
  202. "privmsg"
  203. }
  204. }
  205. impl net::Message for Inv {
  206. fn name() -> &'static str {
  207. "inv"
  208. }
  209. }
  210. impl net::Message for GetData {
  211. fn name() -> &'static str {
  212. "getdata"
  213. }
  214. }
  215. impl net::Message for LastTerm {
  216. fn name() -> &'static str {
  217. "last_term"
  218. }
  219. }