-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #258 from TheNeikos/feature/add_client
Add rudimentary client
- Loading branch information
Showing
36 changed files
with
1,398 additions
and
47 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "cloudmqtt-bin" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
clap = { version = "4.5.4", features = ["derive"] } | ||
cloudmqtt = { version = "0.5.0", path = ".." } | ||
tokio = { version = "1.36.0", features = ["full"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
// | ||
|
||
use clap::Parser; | ||
use cloudmqtt::client::MqttClientConnector; | ||
use cloudmqtt::transport::MqttConnectTransport; | ||
use tokio::net::TcpStream; | ||
|
||
#[derive(Debug, Parser)] | ||
#[command(version, about, long_about = None)] | ||
struct Args { | ||
#[arg(long)] | ||
hostname: String, | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let args = Args::parse(); | ||
|
||
let socket = TcpStream::connect(args.hostname).await.unwrap(); | ||
|
||
let connection = MqttConnectTransport::TokioTcp(socket); | ||
let client_id = cloudmqtt::client_identifier::ClientIdentifier::PotentiallyServerProvided; | ||
|
||
let connector = MqttClientConnector::new( | ||
connection, | ||
client_id, | ||
cloudmqtt::client::CleanStart::Yes, | ||
cloudmqtt::keep_alive::KeepAlive::Disabled, | ||
); | ||
|
||
let _client = connector.connect().await.unwrap(); | ||
|
||
println!("Yay, we connected! That's all for now"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
// | ||
|
||
#[derive(Clone, Eq, PartialEq, Hash, Ord, PartialOrd)] | ||
pub struct MqttBytes(Vec<u8>); | ||
|
||
impl MqttBytes { | ||
pub const MAX_LEN: usize = u16::MAX as usize; | ||
} | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum MqttBytesError { | ||
#[error("A vector/slice of length {} is too long, max length is {}", .0, MqttBytes::MAX_LEN)] | ||
TooLong(usize), | ||
} | ||
|
||
impl AsRef<[u8]> for MqttBytes { | ||
fn as_ref(&self) -> &[u8] { | ||
self.0.as_ref() | ||
} | ||
} | ||
|
||
impl TryFrom<Vec<u8>> for MqttBytes { | ||
type Error = MqttBytesError; | ||
|
||
fn try_from(s: Vec<u8>) -> Result<Self, Self::Error> { | ||
if s.len() > Self::MAX_LEN { | ||
Err(MqttBytesError::TooLong(s.len())) | ||
} else { | ||
Ok(Self(s)) | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<&[u8]> for MqttBytes { | ||
type Error = MqttBytesError; | ||
|
||
fn try_from(s: &[u8]) -> Result<Self, Self::Error> { | ||
if s.len() > Self::MAX_LEN { | ||
Err(MqttBytesError::TooLong(s.len())) | ||
} else { | ||
Ok(Self(s.to_vec())) | ||
} | ||
} | ||
} |
Oops, something went wrong.