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