channel.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. use async_std::sync::{Arc, Mutex};
  2. use futures::{
  3. io::{ReadHalf, WriteHalf},
  4. AsyncReadExt,
  5. };
  6. use log::{debug, error, info};
  7. use rand::Rng;
  8. use serde_json::json;
  9. use smol::Executor;
  10. use url::Url;
  11. use crate::{
  12. system::{StoppableTask, StoppableTaskPtr, Subscriber, SubscriberPtr, Subscription},
  13. util::Timestamp,
  14. Error, Result,
  15. };
  16. use super::{
  17. message,
  18. message_subscriber::{MessageSubscription, MessageSubsystem},
  19. TransportStream,
  20. };
  21. /// Atomic pointer to async channel.
  22. pub type ChannelPtr = Arc<Channel>;
  23. struct ChannelInfo {
  24. random_id: u32,
  25. last_msg: String,
  26. last_status: String,
  27. // Message log which is cleared on querying get_info
  28. log: Mutex<Vec<(Timestamp, String, String)>>,
  29. }
  30. impl ChannelInfo {
  31. fn new() -> Self {
  32. Self {
  33. random_id: rand::thread_rng().gen(),
  34. last_msg: String::new(),
  35. last_status: String::new(),
  36. log: Mutex::new(Vec::new()),
  37. }
  38. }
  39. async fn get_info(&self) -> serde_json::Value {
  40. let result = json!({
  41. "random_id": self.random_id,
  42. "last_msg": self.last_msg,
  43. "last_status": self.last_status,
  44. "log": self.log.lock().await.clone(),
  45. });
  46. self.log.lock().await.clear();
  47. result
  48. }
  49. }
  50. /// Async channel for communication between nodes.
  51. pub struct Channel {
  52. reader: Mutex<ReadHalf<Box<dyn TransportStream>>>,
  53. writer: Mutex<WriteHalf<Box<dyn TransportStream>>>,
  54. address: Url,
  55. message_subsystem: MessageSubsystem,
  56. stop_subscriber: SubscriberPtr<Error>,
  57. receive_task: StoppableTaskPtr,
  58. stopped: Mutex<bool>,
  59. info: Mutex<ChannelInfo>,
  60. }
  61. impl Channel {
  62. /// Sets up a new channel. Creates a reader and writer TCP stream and
  63. /// summons the message subscriber subsystem. Performs a network
  64. /// handshake on the subsystem dispatchers.
  65. pub async fn new(stream: Box<dyn TransportStream>, address: Url) -> Arc<Self> {
  66. let (reader, writer) = stream.split();
  67. let reader = Mutex::new(reader);
  68. let writer = Mutex::new(writer);
  69. let message_subsystem = MessageSubsystem::new();
  70. Self::setup_dispatchers(&message_subsystem).await;
  71. Arc::new(Self {
  72. reader,
  73. writer,
  74. address,
  75. message_subsystem,
  76. stop_subscriber: Subscriber::new(),
  77. receive_task: StoppableTask::new(),
  78. stopped: Mutex::new(false),
  79. info: Mutex::new(ChannelInfo::new()),
  80. })
  81. }
  82. pub async fn get_info(&self) -> serde_json::Value {
  83. self.info.lock().await.get_info().await
  84. }
  85. /// Starts the channel. Runs a receive loop to start receiving messages or
  86. /// handles a network failure.
  87. pub fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) {
  88. debug!(target: "net", "Channel::start() [START, address={}]", self.address());
  89. let self2 = self.clone();
  90. self.receive_task.clone().start(
  91. self.clone().main_receive_loop(),
  92. // Ignore stop handler
  93. |result| self2.handle_stop(result),
  94. Error::ServiceStopped,
  95. executor,
  96. );
  97. debug!(target: "net", "Channel::start() [END, address={}]", self.address());
  98. }
  99. /// Stops the channel. Steps through each component of the channel
  100. /// connection and sends a stop signal. Notifies all subscribers that
  101. /// the channel has been closed.
  102. pub async fn stop(&self) {
  103. debug!(target: "net", "Channel::stop() [START, address={}]", self.address());
  104. let mut stopped = self.stopped.lock().await;
  105. if !*stopped {
  106. *stopped = true;
  107. self.stop_subscriber.notify(Error::ChannelStopped).await;
  108. self.receive_task.stop().await;
  109. self.message_subsystem.trigger_error(Error::ChannelStopped).await;
  110. debug!(target: "net", "Channel::stop() [END, address={}]", self.address());
  111. }
  112. drop(stopped);
  113. }
  114. /// Creates a subscription to a stopped signal.
  115. pub async fn subscribe_stop(&self) -> Subscription<Error> {
  116. debug!(target: "net",
  117. "Channel::subscribe_stop() [START, address={}]",
  118. self.address()
  119. );
  120. // TODO: this should check the stopped status
  121. // Call to receive should return ChannelStopped on newly created sub
  122. let sub = self.stop_subscriber.clone().subscribe().await;
  123. debug!(target: "net",
  124. "Channel::subscribe_stop() [END, address={}]",
  125. self.address()
  126. );
  127. sub
  128. }
  129. /// Sends a message across a channel. Calls function 'send_message' that
  130. /// creates a new payload and sends it over the TCP connection as a
  131. /// packet. Returns an error if something goes wrong.
  132. pub async fn send<M: message::Message>(&self, message: M) -> Result<()> {
  133. debug!(target: "net",
  134. "Channel::send() [START, command={:?}, address={}]",
  135. M::name(),
  136. self.address()
  137. );
  138. // TODO can we use RwLock here instead of Mutex
  139. {
  140. let stopped = *self.stopped.lock().await;
  141. if stopped {
  142. return Err(Error::ChannelStopped)
  143. }
  144. }
  145. // Catch failure and stop channel, return a net error
  146. let result = match self.send_message(message).await {
  147. Ok(()) => Ok(()),
  148. Err(err) => {
  149. error!("Channel send error for [{}]: {}", self.address(), err);
  150. self.stop().await;
  151. Err(Error::ChannelStopped)
  152. }
  153. };
  154. debug!(target: "net",
  155. "Channel::send() [END, command={:?}, address={}]",
  156. M::name(),
  157. self.address()
  158. );
  159. {
  160. let info = &mut *self.info.lock().await;
  161. info.last_msg = M::name().to_string();
  162. info.last_status = "sent".to_string();
  163. }
  164. result
  165. }
  166. /// Implements send message functionality. Creates a new payload and encodes
  167. /// it. Then creates a message packet- the base type of the network- and
  168. /// copies the payload into it. Then we send the packet over the TCP
  169. /// stream.
  170. async fn send_message<M: message::Message>(&self, message: M) -> Result<()> {
  171. let mut payload = Vec::new();
  172. message.encode(&mut payload)?;
  173. let packet = message::Packet { command: String::from(M::name()), payload };
  174. let time = Timestamp::current_time();
  175. //let time = time::unix_timestamp()?;
  176. {
  177. let info = &mut *self.info.lock().await;
  178. info.log.lock().await.push((time, "send".to_string(), packet.command.clone()));
  179. }
  180. let stream = &mut *self.writer.lock().await;
  181. message::send_packet(stream, packet).await
  182. }
  183. /// Subscribe to a messages on the message subsystem.
  184. pub async fn subscribe_msg<M: message::Message>(&self) -> Result<MessageSubscription<M>> {
  185. debug!(target: "net",
  186. "Channel::subscribe_msg() [START, command={:?}, address={}]",
  187. M::name(),
  188. self.address()
  189. );
  190. let sub = self.message_subsystem.subscribe::<M>().await;
  191. debug!(target: "net",
  192. "Channel::subscribe_msg() [END, command={:?}, address={}]",
  193. M::name(),
  194. self.address()
  195. );
  196. sub
  197. }
  198. /// Return the local socket address.
  199. pub fn address(&self) -> Url {
  200. self.address.clone()
  201. }
  202. /// End of file error. Triggered when unexpected end of file occurs.
  203. fn is_eof_error(err: Error) -> bool {
  204. match err {
  205. Error::Io(io_err) => io_err == std::io::ErrorKind::UnexpectedEof,
  206. _ => false,
  207. }
  208. }
  209. /// Perform network handshake for message subsystem dispatchers.
  210. async fn setup_dispatchers(message_subsystem: &MessageSubsystem) {
  211. message_subsystem.add_dispatch::<message::VersionMessage>().await;
  212. message_subsystem.add_dispatch::<message::VerackMessage>().await;
  213. message_subsystem.add_dispatch::<message::PingMessage>().await;
  214. message_subsystem.add_dispatch::<message::PongMessage>().await;
  215. message_subsystem.add_dispatch::<message::GetAddrsMessage>().await;
  216. message_subsystem.add_dispatch::<message::AddrsMessage>().await;
  217. }
  218. /// Convenience function that returns the Message Subsystem.
  219. pub fn get_message_subsystem(&self) -> &MessageSubsystem {
  220. &self.message_subsystem
  221. }
  222. /// Run the receive loop. Start receiving messages or handle network
  223. /// failure.
  224. async fn main_receive_loop(self: Arc<Self>) -> Result<()> {
  225. debug!(target: "net",
  226. "Channel::receive_loop() [START, address={}]",
  227. self.address()
  228. );
  229. let reader = &mut *self.reader.lock().await;
  230. loop {
  231. let packet = match message::read_packet(reader).await {
  232. Ok(packet) => packet,
  233. Err(err) => {
  234. if Self::is_eof_error(err.clone()) {
  235. info!("Channel {:?} disconnected", self.address());
  236. } else {
  237. error!("Read error on channel: {}", err);
  238. }
  239. debug!(target: "net",
  240. "Channel::receive_loop() stopping channel {:?}",
  241. self.address()
  242. );
  243. self.stop().await;
  244. return Err(Error::ChannelStopped)
  245. }
  246. };
  247. {
  248. let info = &mut *self.info.lock().await;
  249. info.last_msg = packet.command.clone();
  250. info.last_status = "recv".to_string();
  251. let time = Timestamp::current_time();
  252. //let time = time::unix_timestamp()?;
  253. info.log.lock().await.push((time, "recv".to_string(), packet.command.clone()));
  254. }
  255. // Send result to our subscribers
  256. self.message_subsystem.notify(&packet.command, packet.payload).await;
  257. }
  258. }
  259. /// Handle network errors. Panic if error passes silently, otherwise
  260. /// broadcast the error.
  261. async fn handle_stop(self: Arc<Self>, result: Result<()>) {
  262. debug!(target: "net", "Channel::handle_stop() [START, address={}]", self.address());
  263. match result {
  264. Ok(()) => panic!("Channel task should never complete without error status"),
  265. Err(err) => {
  266. // Send this error to all channel subscribers
  267. self.message_subsystem.trigger_error(err).await;
  268. }
  269. }
  270. debug!(target: "net", "Channel::handle_stop() [END, address={}]", self.address());
  271. }
  272. }