♻️ Remove some unused code
							parent
							
								
									95a038f7af
								
							
						
					
					
						commit
						6b772ad512
					
				| 
						 | 
					@ -27,11 +27,5 @@ async fn main () -> Result <(), Box <dyn Error>> {
 | 
				
			||||||
		toml::from_str (&config_s).expect (&format! ("Can't parse {:?} as TOML", config_file_path))
 | 
							toml::from_str (&config_s).expect (&format! ("Can't parse {:?} as TOML", config_file_path))
 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	let opt = Opt::from_args ();
 | 
						ptth::server::main (config_file).await
 | 
				
			||||||
	
 | 
					 | 
				
			||||||
	let opt = ptth::server::Opt {
 | 
					 | 
				
			||||||
		
 | 
					 | 
				
			||||||
	};
 | 
					 | 
				
			||||||
	
 | 
					 | 
				
			||||||
	ptth::server::main (config_file, opt).await
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -114,11 +114,7 @@ mod tests {
 | 
				
			||||||
				file_server_root: None,
 | 
									file_server_root: None,
 | 
				
			||||||
			};
 | 
								};
 | 
				
			||||||
			spawn (async move {
 | 
								spawn (async move {
 | 
				
			||||||
				let opt = server::Opt {
 | 
									server::main (config_file).await.unwrap ();
 | 
				
			||||||
					
 | 
					 | 
				
			||||||
				};
 | 
					 | 
				
			||||||
				
 | 
					 | 
				
			||||||
				server::main (config_file, opt).await.unwrap ();
 | 
					 | 
				
			||||||
			});
 | 
								});
 | 
				
			||||||
			
 | 
								
 | 
				
			||||||
			tokio::time::delay_for (std::time::Duration::from_millis (500)).await;
 | 
								tokio::time::delay_for (std::time::Duration::from_millis (500)).await;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,3 @@
 | 
				
			||||||
pub mod watcher;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
use std::{
 | 
					use std::{
 | 
				
			||||||
	error::Error,
 | 
						error::Error,
 | 
				
			||||||
	collections::*,
 | 
						collections::*,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,79 +0,0 @@
 | 
				
			||||||
// Watchers on the wall was the last good episode of Game of Thrones
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
use std::{
 | 
					 | 
				
			||||||
	collections::*,
 | 
					 | 
				
			||||||
	sync::Arc,
 | 
					 | 
				
			||||||
	time::Duration,
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
use futures::channel::oneshot;
 | 
					 | 
				
			||||||
use tokio::{
 | 
					 | 
				
			||||||
	sync::Mutex,
 | 
					 | 
				
			||||||
	time::delay_for,
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
pub struct Watchers <T> {
 | 
					 | 
				
			||||||
	pub senders: HashMap <String, oneshot::Sender <T>>,
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
impl <T> Default for Watchers <T> {
 | 
					 | 
				
			||||||
	fn default () -> Self {
 | 
					 | 
				
			||||||
		Self {
 | 
					 | 
				
			||||||
			senders: Default::default (),
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
impl <T: 'static + Send + Sync> Watchers <T> {
 | 
					 | 
				
			||||||
	pub fn add_watcher_with_id (&mut self, s: oneshot::Sender <T>, id: String) {
 | 
					 | 
				
			||||||
		self.senders.insert (id, s);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	
 | 
					 | 
				
			||||||
	pub fn remove_watcher (&mut self, id: &str) {
 | 
					 | 
				
			||||||
		self.senders.remove (id);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	
 | 
					 | 
				
			||||||
	pub fn wake_one (&mut self, msg: T, id: &str) -> bool {
 | 
					 | 
				
			||||||
		//println! ("wake_one {}", id);
 | 
					 | 
				
			||||||
		
 | 
					 | 
				
			||||||
		if let Some (waiter) = self.senders.remove (id) {
 | 
					 | 
				
			||||||
			waiter.send (msg).ok ();
 | 
					 | 
				
			||||||
			true
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		else {
 | 
					 | 
				
			||||||
			false
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	
 | 
					 | 
				
			||||||
	pub fn num_watchers (&self) -> usize {
 | 
					 | 
				
			||||||
		self.senders.len ()
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	
 | 
					 | 
				
			||||||
	pub async fn long_poll (that: Arc <Mutex <Self>>, id: String) -> Option <T> {
 | 
					 | 
				
			||||||
		//println! ("long_poll {}", id);
 | 
					 | 
				
			||||||
		
 | 
					 | 
				
			||||||
		let (s, r) = oneshot::channel ();
 | 
					 | 
				
			||||||
		let timeout = Duration::from_secs (5);
 | 
					 | 
				
			||||||
		
 | 
					 | 
				
			||||||
		let id_2 = id.clone ();
 | 
					 | 
				
			||||||
		{
 | 
					 | 
				
			||||||
			let mut that = that.lock ().await;
 | 
					 | 
				
			||||||
			that.add_watcher_with_id (s, id_2);
 | 
					 | 
				
			||||||
			//println! ("Added server {}", id);
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		
 | 
					 | 
				
			||||||
		tokio::spawn (async move {
 | 
					 | 
				
			||||||
			delay_for (timeout).await;
 | 
					 | 
				
			||||||
			let mut that = that.lock ().await;
 | 
					 | 
				
			||||||
			that.remove_watcher (&id);
 | 
					 | 
				
			||||||
			//println! ("Removed server {}", id);
 | 
					 | 
				
			||||||
		});
 | 
					 | 
				
			||||||
		
 | 
					 | 
				
			||||||
		if let Ok (message) = r.await {
 | 
					 | 
				
			||||||
			Some (message)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		else {
 | 
					 | 
				
			||||||
			None
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
| 
						 | 
					@ -24,7 +24,6 @@ pub mod file_server;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct ServerState {
 | 
					struct ServerState {
 | 
				
			||||||
	config: Config,
 | 
						config: Config,
 | 
				
			||||||
	opt: Opt,
 | 
					 | 
				
			||||||
	handlebars: Handlebars <'static>,
 | 
						handlebars: Handlebars <'static>,
 | 
				
			||||||
	client: Client,
 | 
						client: Client,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -118,12 +117,7 @@ pub struct Config {
 | 
				
			||||||
	pub file_server_root: Option <PathBuf>,
 | 
						pub file_server_root: Option <PathBuf>,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive (Clone)]
 | 
					pub async fn main (config_file: ConfigFile) 
 | 
				
			||||||
pub struct Opt {
 | 
					 | 
				
			||||||
	
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
pub async fn main (config_file: ConfigFile, opt: Opt) 
 | 
					 | 
				
			||||||
-> Result <(), Box <dyn Error>> 
 | 
					-> Result <(), Box <dyn Error>> 
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	use std::convert::TryInto;
 | 
						use std::convert::TryInto;
 | 
				
			||||||
| 
						 | 
					@ -149,7 +143,6 @@ pub async fn main (config_file: ConfigFile, opt: Opt)
 | 
				
			||||||
			relay_url: config_file.relay_url,
 | 
								relay_url: config_file.relay_url,
 | 
				
			||||||
			file_server_root: config_file.file_server_root,
 | 
								file_server_root: config_file.file_server_root,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		opt,
 | 
					 | 
				
			||||||
		handlebars,
 | 
							handlebars,
 | 
				
			||||||
		client,
 | 
							client,
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue