1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use crate::{
	connection::{BaseConnection, Buffer},
	utils::Error,
};

use async_std::{io::BufReader, net::TcpStream, prelude::*, task};
use async_trait::async_trait;

use futures::{
	channel::{
		mpsc::{unbounded, UnboundedReceiver, UnboundedSender},
		oneshot::{channel, Sender},
	},
	future::{self, Either},
};
use futures_util::SinkExt;

pub struct InetSocketConnection {
	connection_setup: bool,
	read_data_receiver: Option<UnboundedReceiver<Vec<u8>>>,
	write_data_sender: Option<UnboundedSender<Vec<u8>>>,
	close_sender: Option<UnboundedSender<()>>,
	socket_path: String,
}

impl InetSocketConnection {
	pub fn new(socket_path: String) -> Self {
		InetSocketConnection {
			connection_setup: false,
			read_data_receiver: None,
			write_data_sender: None,
			close_sender: None,
			socket_path,
		}
	}
}

async fn read_data_from_socket(
	port: String,
	init_sender: Sender<Result<(), Error>>,
	mut read_sender: UnboundedSender<Vec<u8>>,
	mut write_receiver: UnboundedReceiver<Vec<u8>>,
	mut close_receiver: UnboundedReceiver<()>,
) {
	let result = TcpStream::connect(port).await;
	if let Err(err) = result {
		init_sender
			.send(Err(Error::Internal(format!("{}", err))))
			.unwrap_or(());
		return;
	}
	let client = result.unwrap();
	init_sender.send(Ok(())).unwrap_or(());
	let reader = BufReader::new(&client);
	let mut lines = reader.lines();
	let mut read_future = lines.next();
	let mut write_future = write_receiver.next();
	let mut close_future = close_receiver.next();
	let mut read_or_write_future = future::select(read_future, write_future);
	while let Either::Left((read_write_future, next_close_future)) =
		future::select(read_or_write_future, close_future).await
	{
		// Either a read or a write event has happened
		close_future = next_close_future;
		match read_write_future {
			Either::Left((read_future_result, next_write_future)) => {
				// Read event has happened
				read_future = lines.next();
				write_future = next_write_future;
				read_or_write_future = future::select(read_future, write_future);
				// Send the read data to the MPSC sender
				if let Some(Ok(line)) = read_future_result {
					let result = read_sender.send(line.as_bytes().to_vec()).await;
					if let Err(err) = result {
						println!("Error queing data from the socket to the module: {}", err);
					}
				}
			}
			Either::Right((write_future_result, next_read_future)) => {
				// Write event has happened
				read_future = next_read_future;
				write_future = write_receiver.next();
				read_or_write_future = future::select(read_future, write_future);
				// Write the recieved bytes to the socket
				if let Some(bytes) = write_future_result {
					let mut socket = &client;
					if let Err(err) = socket.write_all(&bytes).await {
						println!("Error while sending data to socket: {}", err);
					}
				}
			}
		}
	}
	// Either a read, nor a write event has happened.
	// This means the socket close event happened. Shutdown the socket and close any mpsc channels
	drop(lines);
	let result = read_sender.close().await;
	if let Err(err) = result {
		println!("Error closing the MPSC sender to queue data: {}", err);
	}
	write_receiver.close();
	close_receiver.close();
}

#[async_trait]
impl BaseConnection for InetSocketConnection {
	async fn setup_connection(&mut self) -> Result<(), Error> {
		if self.connection_setup {
			panic!("Cannot call setup_connection() more than once!");
		}
		let (read_data_sender, read_data_receiver) = unbounded::<Vec<u8>>();
		let (write_data_sender, write_data_receiver) = unbounded::<Vec<u8>>();
		let (close_sender, close_receiver) = unbounded::<()>();
		let (init_sender, init_receiver) = channel::<Result<(), Error>>();

		self.read_data_receiver = Some(read_data_receiver);
		self.write_data_sender = Some(write_data_sender);
		self.close_sender = Some(close_sender);
		let socket_path = self.socket_path.clone();

		task::spawn(async {
			read_data_from_socket(
				socket_path,
				init_sender,
				read_data_sender,
				write_data_receiver,
				close_receiver,
			)
			.await;
		});

		self.connection_setup = true;
		init_receiver.await.unwrap()
	}

	async fn close_connection(&mut self) {
		if !self.connection_setup || self.close_sender.is_none() {
			panic!("Cannot close a connection that hasn't been established yet. Did you forget to call setup_connection()?");
		}
		let mut sender = &self.close_sender.as_ref().unwrap().clone();
		if let Err(err) = sender.send(()).await {
			println!("Error attempting to close connection: {}", err);
		}
	}

	async fn send(&mut self, buffer: Buffer) {
		if !self.connection_setup || self.write_data_sender.is_none() {
			panic!("Cannot send data to a connection that hasn't been established yet. Did you forget to await the call to setup_connection()?");
		}
		let mut sender = &self.write_data_sender.as_ref().unwrap().clone();
		if let Err(err) = sender.send(buffer).await {
			println!("Error attempting to send data to connection: {}", err);
		}
	}

	fn get_data_receiver(&mut self) -> UnboundedReceiver<Buffer> {
		if !self.connection_setup || self.read_data_receiver.is_none() {
			panic!("Cannot get read sender to a connection that hasn't been established yet. Did you forget to await the call to setup_connection()?");
		}
		self.read_data_receiver.take().unwrap()
	}

	fn clone_write_sender(&self) -> UnboundedSender<Buffer> {
		if !self.connection_setup || self.write_data_sender.is_none() {
			panic!("Cannot get write sender of a connection that hasn't been established yet. Did you forget to await the call to setup_connection()?");
		}
		self.write_data_sender.as_ref().unwrap().clone()
	}
}