channel.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2022 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use async_std::sync::{Arc, Mutex};
  19. use futures::{
  20. io::{ReadHalf, WriteHalf},
  21. AsyncReadExt,
  22. };
  23. use log::{debug, error, info};
  24. use rand::Rng;
  25. use serde_json::json;
  26. use smol::Executor;
  27. use url::Url;
  28. use super::{
  29. message,
  30. message_subscriber::{MessageSubscription, MessageSubsystem},
  31. transport::TransportStream,
  32. Session, SessionBitflag, SessionWeakPtr,
  33. };
  34. use crate::{
  35. system::{StoppableTask, StoppableTaskPtr, Subscriber, SubscriberPtr, Subscription},
  36. util::time::NanoTimestamp,
  37. Error, Result,
  38. };
  39. /// Atomic pointer to async channel.
  40. pub type ChannelPtr = Arc<Channel>;
  41. struct ChannelInfo {
  42. random_id: u32,
  43. remote_node_id: String,
  44. last_msg: String,
  45. last_status: String,
  46. // Message log which is cleared on querying get_info
  47. log: Option<Mutex<Vec<(NanoTimestamp, String, String)>>>,
  48. }
  49. impl ChannelInfo {
  50. fn new(channel_log: bool) -> Self {
  51. let log = match channel_log {
  52. true => Some(Mutex::new(Vec::new())),
  53. false => None,
  54. };
  55. Self {
  56. random_id: rand::thread_rng().gen(),
  57. remote_node_id: String::new(),
  58. last_msg: String::new(),
  59. last_status: String::new(),
  60. log,
  61. }
  62. }
  63. // ANCHOR: get_info
  64. async fn get_info(&self) -> serde_json::Value {
  65. let log = match &self.log {
  66. Some(l) => {
  67. let mut lock = l.lock().await;
  68. let ret = lock.clone();
  69. *lock = Vec::new();
  70. ret
  71. }
  72. None => vec![],
  73. };
  74. json!({
  75. "random_id": self.random_id,
  76. "remote_node_id": self.remote_node_id,
  77. "last_msg": self.last_msg,
  78. "last_status": self.last_status,
  79. "log": log,
  80. })
  81. }
  82. // ANCHOR_END: get_info
  83. }
  84. /// Async channel for communication between nodes.
  85. pub struct Channel {
  86. reader: Mutex<ReadHalf<Box<dyn TransportStream>>>,
  87. writer: Mutex<WriteHalf<Box<dyn TransportStream>>>,
  88. address: Url,
  89. message_subsystem: MessageSubsystem,
  90. stop_subscriber: SubscriberPtr<Error>,
  91. receive_task: StoppableTaskPtr,
  92. stopped: Mutex<bool>,
  93. info: Mutex<ChannelInfo>,
  94. session: SessionWeakPtr,
  95. }
  96. impl Channel {
  97. /// Sets up a new channel. Creates a reader and writer TCP stream and
  98. /// summons the message subscriber subsystem. Performs a network
  99. /// handshake on the subsystem dispatchers.
  100. pub async fn new(
  101. stream: Box<dyn TransportStream>,
  102. address: Url,
  103. session: SessionWeakPtr,
  104. ) -> Arc<Self> {
  105. let (reader, writer) = stream.split();
  106. let reader = Mutex::new(reader);
  107. let writer = Mutex::new(writer);
  108. let message_subsystem = MessageSubsystem::new();
  109. Self::setup_dispatchers(&message_subsystem).await;
  110. let channel_log = session.upgrade().unwrap().p2p().settings().channel_log;
  111. Arc::new(Self {
  112. reader,
  113. writer,
  114. address,
  115. message_subsystem,
  116. stop_subscriber: Subscriber::new(),
  117. receive_task: StoppableTask::new(),
  118. stopped: Mutex::new(false),
  119. info: Mutex::new(ChannelInfo::new(channel_log)),
  120. session,
  121. })
  122. }
  123. pub async fn get_info(&self) -> serde_json::Value {
  124. self.info.lock().await.get_info().await
  125. }
  126. /// Starts the channel. Runs a receive loop to start receiving messages or
  127. /// handles a network failure.
  128. pub fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) {
  129. debug!(target: "net", "Channel::start() [START, address={}]", self.address());
  130. let self2 = self.clone();
  131. self.receive_task.clone().start(
  132. self.clone().main_receive_loop(),
  133. |result| self2.handle_stop(result),
  134. Error::NetworkServiceStopped,
  135. executor,
  136. );
  137. debug!(target: "net", "Channel::start() [END, address={}]", self.address());
  138. }
  139. /// Stops the channel. Steps through each component of the channel
  140. /// connection and sends a stop signal. Notifies all subscribers that
  141. /// the channel has been closed.
  142. pub async fn stop(&self) {
  143. debug!(target: "net", "Channel::stop() [START, address={}]", self.address());
  144. if !(*self.stopped.lock().await) {
  145. *self.stopped.lock().await = true;
  146. self.stop_subscriber.notify(Error::ChannelStopped).await;
  147. self.receive_task.stop().await;
  148. self.message_subsystem.trigger_error(Error::ChannelStopped).await;
  149. debug!(target: "net", "Channel::stop() [END, address={}]", self.address());
  150. }
  151. }
  152. /// Creates a subscription to a stopped signal.
  153. /// If the channel is stopped then this will return a ChannelStopped error.
  154. pub async fn subscribe_stop(&self) -> Result<Subscription<Error>> {
  155. debug!(target: "net",
  156. "Channel::subscribe_stop() [START, address={}]",
  157. self.address()
  158. );
  159. {
  160. let stopped = *self.stopped.lock().await;
  161. if stopped {
  162. return Err(Error::ChannelStopped)
  163. }
  164. }
  165. let sub = self.stop_subscriber.clone().subscribe().await;
  166. debug!(target: "net",
  167. "Channel::subscribe_stop() [END, address={}]",
  168. self.address()
  169. );
  170. Ok(sub)
  171. }
  172. /// Sends a message across a channel. Calls function 'send_message' that
  173. /// creates a new payload and sends it over the TCP connection as a
  174. /// packet. Returns an error if something goes wrong.
  175. pub async fn send<M: message::Message>(&self, message: M) -> Result<()> {
  176. debug!(target: "net",
  177. "Channel::send() [START, command={:?}, address={}]",
  178. M::name(),
  179. self.address()
  180. );
  181. {
  182. let stopped = *self.stopped.lock().await;
  183. if stopped {
  184. return Err(Error::ChannelStopped)
  185. }
  186. }
  187. // Catch failure and stop channel, return a net error
  188. let result = match self.send_message(message).await {
  189. Ok(()) => Ok(()),
  190. Err(err) => {
  191. error!("Channel send error for [{}]: {}", self.address(), err);
  192. self.stop().await;
  193. Err(Error::ChannelStopped)
  194. }
  195. };
  196. debug!(target: "net",
  197. "Channel::send() [END, command={:?}, address={}]",
  198. M::name(),
  199. self.address()
  200. );
  201. {
  202. let info = &mut *self.info.lock().await;
  203. info.last_msg = M::name().to_string();
  204. info.last_status = "sent".to_string();
  205. }
  206. result
  207. }
  208. /// Implements send message functionality. Creates a new payload and encodes
  209. /// it. Then creates a message packet- the base type of the network- and
  210. /// copies the payload into it. Then we send the packet over the TCP
  211. /// stream.
  212. async fn send_message<M: message::Message>(&self, message: M) -> Result<()> {
  213. let mut payload = Vec::new();
  214. message.encode(&mut payload)?;
  215. let packet = message::Packet { command: String::from(M::name()), payload };
  216. let time = NanoTimestamp::current_time();
  217. //let time = time::unix_timestamp()?;
  218. {
  219. let info = &mut *self.info.lock().await;
  220. if let Some(l) = &info.log {
  221. l.lock().await.push((time, "send".to_string(), packet.command.clone()));
  222. };
  223. }
  224. let stream = &mut *self.writer.lock().await;
  225. message::send_packet(stream, packet).await
  226. }
  227. /// Subscribe to a messages on the message subsystem.
  228. pub async fn subscribe_msg<M: message::Message>(&self) -> Result<MessageSubscription<M>> {
  229. debug!(target: "net",
  230. "Channel::subscribe_msg() [START, command={:?}, address={}]",
  231. M::name(),
  232. self.address()
  233. );
  234. let sub = self.message_subsystem.subscribe::<M>().await;
  235. debug!(target: "net",
  236. "Channel::subscribe_msg() [END, command={:?}, address={}]",
  237. M::name(),
  238. self.address()
  239. );
  240. sub
  241. }
  242. /// Return the local socket address.
  243. pub fn address(&self) -> Url {
  244. self.address.clone()
  245. }
  246. pub async fn remote_node_id(&self) -> String {
  247. self.info.lock().await.remote_node_id.clone()
  248. }
  249. pub async fn set_remote_node_id(&self, remote_node_id: String) {
  250. self.info.lock().await.remote_node_id = remote_node_id;
  251. }
  252. /// End of file error. Triggered when unexpected end of file occurs.
  253. fn is_eof_error(err: Error) -> bool {
  254. match err {
  255. Error::Io(io_err) => io_err == std::io::ErrorKind::UnexpectedEof,
  256. _ => false,
  257. }
  258. }
  259. /// Perform network handshake for message subsystem dispatchers.
  260. async fn setup_dispatchers(message_subsystem: &MessageSubsystem) {
  261. message_subsystem.add_dispatch::<message::VersionMessage>().await;
  262. message_subsystem.add_dispatch::<message::VerackMessage>().await;
  263. message_subsystem.add_dispatch::<message::PingMessage>().await;
  264. message_subsystem.add_dispatch::<message::PongMessage>().await;
  265. message_subsystem.add_dispatch::<message::GetAddrsMessage>().await;
  266. message_subsystem.add_dispatch::<message::AddrsMessage>().await;
  267. message_subsystem.add_dispatch::<message::ExtAddrsMessage>().await;
  268. }
  269. /// Convenience function that returns the Message Subsystem.
  270. pub fn get_message_subsystem(&self) -> &MessageSubsystem {
  271. &self.message_subsystem
  272. }
  273. /// Run the receive loop. Start receiving messages or handle network
  274. /// failure.
  275. async fn main_receive_loop(self: Arc<Self>) -> Result<()> {
  276. debug!(target: "net",
  277. "Channel::receive_loop() [START, address={}]",
  278. self.address()
  279. );
  280. let reader = &mut *self.reader.lock().await;
  281. loop {
  282. let packet = match message::read_packet(reader).await {
  283. Ok(packet) => packet,
  284. Err(err) => {
  285. if Self::is_eof_error(err.clone()) {
  286. info!("Inbound connection {} disconnected", self.address());
  287. } else {
  288. error!("Read error on channel {}: {}", self.address(), err);
  289. }
  290. debug!(target: "net",
  291. "Channel::receive_loop() stopping channel {}",
  292. self.address()
  293. );
  294. self.stop().await;
  295. return Err(Error::ChannelStopped)
  296. }
  297. };
  298. {
  299. let info = &mut *self.info.lock().await;
  300. info.last_msg = packet.command.clone();
  301. info.last_status = "recv".to_string();
  302. let time = NanoTimestamp::current_time();
  303. //let time = time::unix_timestamp()?;
  304. if let Some(l) = &info.log {
  305. l.lock().await.push((time, "recv".to_string(), packet.command.clone()));
  306. };
  307. }
  308. // Send result to our subscribers
  309. self.message_subsystem.notify(&packet.command, packet.payload).await;
  310. }
  311. }
  312. /// Handle network errors. Panic if error passes silently, otherwise
  313. /// broadcast the error.
  314. async fn handle_stop(self: Arc<Self>, result: Result<()>) {
  315. debug!(target: "net", "Channel::handle_stop() [START, address={}]", self.address());
  316. match result {
  317. Ok(()) => panic!("Channel task should never complete without error status"),
  318. Err(err) => {
  319. // Send this error to all channel subscribers
  320. self.message_subsystem.trigger_error(err).await;
  321. }
  322. }
  323. debug!(target: "net", "Channel::handle_stop() [END, address={}]", self.address());
  324. }
  325. fn session(&self) -> Arc<dyn Session> {
  326. self.session.upgrade().unwrap()
  327. }
  328. pub fn session_type_id(&self) -> SessionBitflag {
  329. let session = self.session();
  330. session.type_id()
  331. }
  332. }