Typo
This commit is contained in:
parent
1dd240861d
commit
775282caf4
7 changed files with 0 additions and 0 deletions
2
example-usage/.cargo/config.toml
Normal file
2
example-usage/.cargo/config.toml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
[env]
|
||||
MTP_TYPE_MAPS = { value = "type-maps.yaml", relative = true }
|
||||
6
example-usage/Cargo.toml
Normal file
6
example-usage/Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[workspace]
|
||||
members = [
|
||||
"server",
|
||||
"client",
|
||||
]
|
||||
resolver = "3"
|
||||
13
example-usage/client/Cargo.toml
Normal file
13
example-usage/client/Cargo.toml
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
[package]
|
||||
name = "mtp-example-client"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[[bin]]
|
||||
name = "mtp-example-client"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
mtp-client = { path = "../../client" }
|
||||
mtp-codec = { path = "../../codec" }
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
48
example-usage/client/src/main.rs
Normal file
48
example-usage/client/src/main.rs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
use std::fs;
|
||||
|
||||
use mtp_client::{ClientConfig, MTPClient};
|
||||
use mtp_codec::{CommunicationType, CommunicationValue, DataType, DataValue};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let cert_pem = fs::read("server.pem")
|
||||
.expect("Missing server.pem server generates it");
|
||||
|
||||
println!("Connecting to 127.0.0.1:8080 ...");
|
||||
|
||||
let config = ClientConfig {
|
||||
url: "https://127.0.0.1:8080".into(),
|
||||
server_cert: Some(cert_pem),
|
||||
};
|
||||
|
||||
let conn = MTPClient::connect(config, 1001).await?;
|
||||
println!("Connected (version {})", conn.version);
|
||||
|
||||
let timestamp = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs();
|
||||
|
||||
let greeting = CommunicationValue::new(CommunicationType::Ping)
|
||||
.add_typed_default(DataType::Description, DataValue::Str("Hello MTP!".into()))
|
||||
.add_typed_default(DataType::Timestamp, DataValue::UnsignedNumber(timestamp as u128))
|
||||
.add_typed_default(DataType::Data, DataValue::UnsignedNumber(42))
|
||||
.with_sender(1001);
|
||||
|
||||
println!("Sending: {greeting}");
|
||||
conn.sender.send(&greeting).await?;
|
||||
|
||||
match conn.receiver.receive().await {
|
||||
Ok(msg) => {
|
||||
println!("Received: {msg}");
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Receive error: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
conn.sender.close();
|
||||
println!("\nDone");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
14
example-usage/server/Cargo.toml
Normal file
14
example-usage/server/Cargo.toml
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
[package]
|
||||
name = "mtp-example-server"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[[bin]]
|
||||
name = "mtp-example-server"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
mtp-host = { path = "../../host" }
|
||||
mtp-codec = { path = "../../codec" }
|
||||
rcgen = "0.14"
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
90
example-usage/server/src/main.rs
Normal file
90
example-usage/server/src/main.rs
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
use std::fs;
|
||||
|
||||
use mtp_codec::{CommunicationType, CommunicationValue, DataType, DataValue, TypeMap};
|
||||
use mtp_host::{HostConfig, MTPHost};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let cert_path = "server.pem";
|
||||
let key_path = "server.key";
|
||||
|
||||
let (cert_pem, key_pem) = if let (Ok(c), Ok(k)) = (fs::read(cert_path), fs::read(key_path)) {
|
||||
println!("Using existing TLS cert from {cert_path}");
|
||||
(c, k)
|
||||
} else {
|
||||
println!("Generating self-signed TLS certificate ...");
|
||||
|
||||
let key_pair = rcgen::KeyPair::generate()?;
|
||||
let params = rcgen::CertificateParams::new(vec![
|
||||
"localhost".into(),
|
||||
"127.0.0.1".into(),
|
||||
])?;
|
||||
let cert = params.self_signed(&key_pair)?;
|
||||
|
||||
let cert_str = cert.pem();
|
||||
let key_str = key_pair.serialize_pem();
|
||||
|
||||
fs::write(cert_path, cert_str.as_bytes())?;
|
||||
fs::write(key_path, key_str.as_bytes())?;
|
||||
println!("Wrote {cert_path} and {key_path}");
|
||||
|
||||
(cert_str.into_bytes(), key_str.into_bytes())
|
||||
};
|
||||
|
||||
println!("Starting MTP server on port 8080 ...");
|
||||
|
||||
let config = HostConfig {
|
||||
ip: "::".into(),
|
||||
port: 8080,
|
||||
tls_fullchain: cert_pem,
|
||||
tls_key: key_pem,
|
||||
};
|
||||
|
||||
let mut host = MTPHost::new(config).await?;
|
||||
println!("Server listening on {}", host.local_addr());
|
||||
|
||||
while let Some(conn) = host.accept().await {
|
||||
println!("\n--- New connection accepted (version {}) ---", conn.version);
|
||||
|
||||
let tm: &TypeMap = conn.codec.registry().get(&conn.version).unwrap();
|
||||
|
||||
match conn.receiver.receive().await {
|
||||
Ok(msg) => {
|
||||
println!("Received: {msg}");
|
||||
|
||||
let timestamp = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs();
|
||||
|
||||
let response = CommunicationValue::from_comm(
|
||||
CommunicationType::Pong,
|
||||
tm,
|
||||
)
|
||||
.add_data(
|
||||
mtp_codec::DataTypeId(tm.data_id_enum(DataType::Description).unwrap()),
|
||||
DataValue::Str("Hello from server!".into()),
|
||||
)
|
||||
.add_data(
|
||||
mtp_codec::DataTypeId(tm.data_id_enum(DataType::Timestamp).unwrap()),
|
||||
DataValue::UnsignedNumber(timestamp as u128),
|
||||
)
|
||||
.add_data(
|
||||
mtp_codec::DataTypeId(tm.data_id_enum(DataType::Data).unwrap()),
|
||||
DataValue::Str("custom payload".into()),
|
||||
);
|
||||
|
||||
println!("Sending: {response}");
|
||||
conn.sender.send(&response).await?;
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Receive error: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
conn.sender.close();
|
||||
println!("Connection closed\n");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
14
example-usage/type-maps.yaml
Normal file
14
example-usage/type-maps.yaml
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
protocol_version: "1.0"
|
||||
|
||||
type_maps:
|
||||
"0.0":
|
||||
CommunicationTypes:
|
||||
DataTypes:
|
||||
"1.0":
|
||||
CommunicationTypes:
|
||||
DataTypes:
|
||||
Data: 32
|
||||
"2.0":
|
||||
CommunicationTypes:
|
||||
DataTypes:
|
||||
Data: 34
|
||||
Loading…
Reference in a new issue