feat: client settings and defaults
This commit is contained in:
@@ -18,19 +18,15 @@ soulseek-rs-lib = "0.1.0"
|
||||
|
||||
## Example
|
||||
|
||||
### Simple Usage
|
||||
|
||||
```rust
|
||||
use soulseek_rs::{Client, PeerAddress};
|
||||
use soulseek_rs::Client;
|
||||
use std::time::Duration;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Create and connect to Soulseek server
|
||||
let mut client = Client::new(
|
||||
PeerAddress::new("server.slsknet.org".to_string(), 2242),
|
||||
"username".to_string(),
|
||||
"password".to_string(),
|
||||
false,
|
||||
None,
|
||||
);
|
||||
let mut client = Client::new("username", "password");
|
||||
|
||||
client.connect();
|
||||
client.login()?;
|
||||
@@ -53,3 +49,25 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
Ok(())
|
||||
}
|
||||
```
|
||||
|
||||
### Advanced Configuration
|
||||
|
||||
```rust
|
||||
use soulseek_rs::{Client, ClientSettings, PeerAddress};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Create client with custom settings
|
||||
let settings = ClientSettings {
|
||||
server_address: PeerAddress::new("server.slsknet.org".to_string(), 2242),
|
||||
enable_listen: true,
|
||||
listen_port: 3000,
|
||||
..ClientSettings::new("username", "password")
|
||||
};
|
||||
|
||||
let mut client = Client::with_settings(settings);
|
||||
client.connect();
|
||||
client.login()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use crate::actor::server_actor::{PeerAddress, ServerActor, ServerMessage};
|
||||
use crate::actor::ActorHandle;
|
||||
use crate::types::{DownloadResult, DownloadStatus};
|
||||
use crate::utils::logger;
|
||||
use crate::{
|
||||
actor::{peer_registry::PeerRegistry, ActorSystem},
|
||||
error::{Result, SoulseekRs},
|
||||
@@ -26,6 +27,43 @@ use std::{
|
||||
use crate::{debug, error, info, trace};
|
||||
const DEFALT_LISTEN_PORT: u32 = 2234;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ClientSettings {
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
pub server_address: PeerAddress,
|
||||
pub enable_listen: bool,
|
||||
pub listen_port: u32,
|
||||
}
|
||||
|
||||
impl ClientSettings {
|
||||
pub fn new(
|
||||
username: impl Into<String>,
|
||||
password: impl Into<String>,
|
||||
) -> Self {
|
||||
Self {
|
||||
username: username.into(),
|
||||
password: password.into(),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ClientSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
username: String::new(),
|
||||
password: String::new(),
|
||||
server_address: PeerAddress::new(
|
||||
"server.slsknet.org".to_string(),
|
||||
2416,
|
||||
),
|
||||
enable_listen: false,
|
||||
listen_port: DEFALT_LISTEN_PORT,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
||||
pub enum ClientOperation {
|
||||
@@ -92,30 +130,24 @@ pub struct Client {
|
||||
|
||||
impl Client {
|
||||
pub fn new(
|
||||
address: PeerAddress,
|
||||
username: String,
|
||||
password: String,
|
||||
enable_listen: bool,
|
||||
listen_port: Option<u32>,
|
||||
username: impl Into<String>,
|
||||
password: impl Into<String>,
|
||||
) -> Self {
|
||||
crate::utils::logger::init();
|
||||
Self::with_settings(ClientSettings::new(username, password))
|
||||
}
|
||||
|
||||
pub fn with_settings(settings: ClientSettings) -> Self {
|
||||
logger::init();
|
||||
Self {
|
||||
enable_listen,
|
||||
listen_port: listen_port.unwrap_or(DEFALT_LISTEN_PORT),
|
||||
address,
|
||||
username,
|
||||
password,
|
||||
enable_listen: settings.enable_listen,
|
||||
listen_port: settings.listen_port,
|
||||
address: settings.server_address,
|
||||
username: settings.username,
|
||||
password: settings.password,
|
||||
context: Arc::new(RwLock::new(ClientContext::new())),
|
||||
server_handle: None,
|
||||
}
|
||||
}
|
||||
pub fn with_defaults(
|
||||
address: PeerAddress,
|
||||
username: String,
|
||||
password: String,
|
||||
) -> Self {
|
||||
Self::new(address, username, password, false, None)
|
||||
}
|
||||
|
||||
pub fn connect(&mut self) {
|
||||
let (sender, message_reader): (
|
||||
|
||||
@@ -20,7 +20,7 @@ pub mod prelude {
|
||||
|
||||
// Re-export commonly used types
|
||||
pub use actor::server_actor::PeerAddress;
|
||||
pub use client::Client;
|
||||
pub use client::{Client, ClientSettings};
|
||||
pub use error::{Result, SoulseekRs};
|
||||
pub use types::{
|
||||
DownloadResult, DownloadStatus, File, FileSearchResult, Transfer,
|
||||
|
||||
Reference in New Issue
Block a user