Working example of client
This commit is contained in:
parent
a862637a77
commit
3cb9de0371
|
@ -1,80 +1,117 @@
|
||||||
use core::future::Future;
|
use core::future::Future;
|
||||||
|
|
||||||
use crate::network::network_trait::{Network, NetworkError};
|
use crate::network::network_trait::{Network, NetworkError};
|
||||||
use crate::packet::connack_packet::ConnackPacket;
|
use crate::packet::connack_packet::ConnackPacket;
|
||||||
use crate::packet::connect_packet::ConnectPacket;
|
use crate::packet::connect_packet::ConnectPacket;
|
||||||
use crate::packet::disconnect_packet::DisconnectPacket;
|
use crate::packet::disconnect_packet::DisconnectPacket;
|
||||||
use crate::packet::mqtt_packet::Packet;
|
use crate::packet::mqtt_packet::Packet;
|
||||||
use crate::packet::publish_packet::{PublishPacket, QualityOfService};
|
|
||||||
use crate::packet::publish_packet::QualityOfService::QoS1;
|
use crate::packet::publish_packet::QualityOfService::QoS1;
|
||||||
|
use crate::packet::publish_packet::{PublishPacket, QualityOfService};
|
||||||
|
use crate::packet::suback_packet::SubackPacket;
|
||||||
|
use crate::packet::subscription_packet::SubscriptionPacket;
|
||||||
use crate::utils::buffer_reader::BuffReader;
|
use crate::utils::buffer_reader::BuffReader;
|
||||||
|
|
||||||
pub struct MqttClientV5<'a, T, const MAX_PROPERTIES: usize> {
|
pub struct MqttClientV5<'a, T, const MAX_PROPERTIES: usize> {
|
||||||
network_driver: &'a mut T,
|
network_driver: &'a mut T,
|
||||||
buffer: &'a mut [u8]
|
buffer: &'a mut [u8],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T, const MAX_PROPERTIES: usize> MqttClientV5<'a, T, MAX_PROPERTIES>
|
impl<'a, T, const MAX_PROPERTIES: usize> MqttClientV5<'a, T, MAX_PROPERTIES>
|
||||||
where
|
where
|
||||||
T: Network
|
T: Network,
|
||||||
{
|
{
|
||||||
pub fn new(network_driver: &'a mut T, buffer: &'a mut [u8]) -> Self {
|
pub fn new(network_driver: &'a mut T, buffer: &'a mut [u8]) -> Self {
|
||||||
Self {
|
Self {
|
||||||
network_driver,
|
network_driver,
|
||||||
buffer
|
buffer,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// connect -> connack -> publish -> QoS ? -> disconn
|
|
||||||
pub async fn send_message(&'a mut self, topic_name: & str, message: & str, qos: QualityOfService) -> Result<(), NetworkError> {
|
|
||||||
|
|
||||||
|
pub async fn connect_to_broker<'b>(&'b mut self) -> Result<(), NetworkError> {
|
||||||
let mut len = {
|
let mut len = {
|
||||||
let mut connect = ConnectPacket::<3, 0>::clean();
|
let mut connect = ConnectPacket::<'b, 3, 0>::clean();
|
||||||
connect.encode(self.buffer)
|
connect.encode(self.buffer)
|
||||||
};
|
};
|
||||||
|
|
||||||
self.network_driver.send(self.buffer, len).await ?;
|
self.network_driver.send(self.buffer, len).await?;
|
||||||
|
|
||||||
//connack
|
//connack
|
||||||
{
|
let reason: u8 = {
|
||||||
self.receive().await ?;
|
self.network_driver.receive(self.buffer).await?;
|
||||||
let mut packet = ConnackPacket::<5>::new();
|
let mut packet = ConnackPacket::<'b, 5>::new();
|
||||||
packet.decode(&mut BuffReader::new(self.buffer));
|
packet.decode(&mut BuffReader::new(self.buffer));
|
||||||
|
packet.connect_reason_code
|
||||||
|
|
||||||
if packet.connect_reason_code != 0x00 {
|
|
||||||
todo!();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if reason != 0x00 {
|
||||||
|
Err(NetworkError::Connection)
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn disconnect<'b>(&'b mut self) -> Result<(), NetworkError> {
|
||||||
|
let mut disconnect = DisconnectPacket::<'b, 5>::new();
|
||||||
|
let mut len = disconnect.encode(self.buffer);
|
||||||
|
self.network_driver.send(self.buffer, len).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// connect -> connack -> publish -> QoS ? -> disconn
|
||||||
|
pub async fn send_message<'b>(
|
||||||
|
&'b mut self,
|
||||||
|
topic_name: &'b str,
|
||||||
|
message: &'b str,
|
||||||
|
qos: QualityOfService,
|
||||||
|
) -> Result<(), NetworkError> {
|
||||||
// publish
|
// publish
|
||||||
|
let len = {
|
||||||
len = {
|
let mut packet = PublishPacket::<'b, 5>::new();
|
||||||
let mut packet = PublishPacket::<5>::new(topic_name, message);
|
packet.add_topic_name(topic_name);
|
||||||
|
packet.add_message(message.as_bytes());
|
||||||
packet.encode(self.buffer)
|
packet.encode(self.buffer)
|
||||||
};
|
};
|
||||||
|
|
||||||
self.network_driver.send(self.buffer, len).await ?;
|
self.network_driver.send(self.buffer, len).await?;
|
||||||
|
|
||||||
|
|
||||||
//QoS1
|
//QoS1
|
||||||
if <QualityOfService as Into<u8>>::into(qos) == <QualityOfService as Into<u8>>::into(QoS1) {
|
if <QualityOfService as Into<u8>>::into(qos) == <QualityOfService as Into<u8>>::into(QoS1) {
|
||||||
todo!();
|
todo!();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Disconnect
|
|
||||||
let mut disconnect = DisconnectPacket::<5>::new();
|
|
||||||
len = disconnect.encode(self.buffer);
|
|
||||||
self.network_driver.send(self.buffer, len);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn receive(&'a mut self) -> Result<(), NetworkError> {
|
pub async fn subscribe_to_topic<'b>(&'b mut self, topic_name: &'b str) -> Result<(), NetworkError> {
|
||||||
self.network_driver.receive(self.buffer).await ?;
|
let len = {
|
||||||
Ok(())
|
let mut subs = SubscriptionPacket::<'b, 1, 1>::new();
|
||||||
|
subs.add_new_filter(topic_name);
|
||||||
|
subs.encode(self.buffer)
|
||||||
|
};
|
||||||
|
let xx: [u8; 14] = (self.buffer[0..14]).try_into().unwrap();
|
||||||
|
log::info!("{:x?}", xx);
|
||||||
|
self.network_driver.send(self.buffer, len).await?;
|
||||||
|
|
||||||
|
let reason = {
|
||||||
|
self.network_driver.receive(self.buffer).await?;
|
||||||
|
let mut packet = SubackPacket::<'b, 5, 5>::new();
|
||||||
|
packet.decode(&mut BuffReader::new(self.buffer));
|
||||||
|
*packet.reason_codes.get(0).unwrap()
|
||||||
|
};
|
||||||
|
|
||||||
|
if reason > 1 {
|
||||||
|
Err(NetworkError::Unknown)
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn receive_message(&'a mut self) -> Result<(), NetworkError> {
|
pub async fn receive_message<'b>(&'b mut self) -> Result<&'b [u8], NetworkError> {
|
||||||
return Ok(());
|
self.network_driver.receive(self.buffer).await?;
|
||||||
|
let mut packet = PublishPacket::<'b, 5>::new();
|
||||||
|
packet.decode(&mut BuffReader::new(self.buffer));
|
||||||
|
return Ok(packet.message.unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
pub mod client_v5;
|
pub mod client_v5;
|
||||||
|
|
|
@ -7,12 +7,12 @@
|
||||||
|
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
|
|
||||||
pub mod encoding;
|
|
||||||
pub mod packet;
|
|
||||||
pub mod utils;
|
|
||||||
pub mod client;
|
pub mod client;
|
||||||
|
pub mod encoding;
|
||||||
pub mod network;
|
pub mod network;
|
||||||
|
pub mod packet;
|
||||||
pub mod tokio_network;
|
pub mod tokio_network;
|
||||||
|
pub mod utils;
|
||||||
|
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
pub fn print_stack(file: &'static str, line: u32) {
|
pub fn print_stack(file: &'static str, line: u32) {
|
||||||
|
|
83
src/main.rs
83
src/main.rs
|
@ -1,10 +1,60 @@
|
||||||
use rust_mqtt::client::client_v5::MqttClientV5;
|
use rust_mqtt::client::client_v5::MqttClientV5;
|
||||||
use rust_mqtt::network::network_trait::Network;
|
use rust_mqtt::network::network_trait::{Network, NetworkError};
|
||||||
use rust_mqtt::packet::connect_packet::ConnectPacket;
|
use rust_mqtt::packet::connect_packet::ConnectPacket;
|
||||||
use rust_mqtt::packet::mqtt_packet::Packet;
|
use rust_mqtt::packet::mqtt_packet::Packet;
|
||||||
use rust_mqtt::packet::publish_packet::PublishPacket;
|
use rust_mqtt::packet::publish_packet::{PublishPacket, QualityOfService};
|
||||||
use rust_mqtt::packet::subscription_packet::SubscriptionPacket;
|
use rust_mqtt::packet::subscription_packet::SubscriptionPacket;
|
||||||
use rust_mqtt::tokio_network::TokioNetwork;
|
use rust_mqtt::tokio_network::TokioNetwork;
|
||||||
|
use std::time::Duration;
|
||||||
|
use tokio::time::sleep;
|
||||||
|
use tokio::{join, task};
|
||||||
|
|
||||||
|
async fn receive() {
|
||||||
|
let mut ip: [u8; 4] = [37, 205, 11, 180];
|
||||||
|
let mut port: u16 = 1883;
|
||||||
|
let mut tokio_network: TokioNetwork = TokioNetwork::new(ip, port);
|
||||||
|
tokio_network.create_connection().await;
|
||||||
|
let mut res2 = vec![0; 260];
|
||||||
|
let mut client = MqttClientV5::<TokioNetwork, 5>::new(&mut tokio_network, &mut res2);
|
||||||
|
|
||||||
|
let mut result = { client.connect_to_broker().await };
|
||||||
|
|
||||||
|
{
|
||||||
|
client.subscribe_to_topic("test/topic").await;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
log::info!("Waiting for new message!");
|
||||||
|
let mes = client.receive_message().await.unwrap();
|
||||||
|
let x = String::from_utf8_lossy(mes);
|
||||||
|
log::info!("Got new message: {}", x);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
client.disconnect().await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn publish(message: &str) {
|
||||||
|
let mut ip: [u8; 4] = [37, 205, 11, 180];
|
||||||
|
let mut port: u16 = 1883;
|
||||||
|
let mut tokio_network: TokioNetwork = TokioNetwork::new(ip, port);
|
||||||
|
tokio_network.create_connection().await;
|
||||||
|
let mut res2 = vec![0; 260];
|
||||||
|
let mut client = MqttClientV5::<TokioNetwork, 5>::new(&mut tokio_network, &mut res2);
|
||||||
|
|
||||||
|
let mut result = { client.connect_to_broker().await };
|
||||||
|
log::info!("Waiting until send!");
|
||||||
|
sleep(Duration::from_secs(15));
|
||||||
|
let mut result: Result<(), NetworkError> = {
|
||||||
|
log::info!("Sending new message!");
|
||||||
|
client
|
||||||
|
.send_message("test/topic", message, QualityOfService::QoS0)
|
||||||
|
.await
|
||||||
|
};
|
||||||
|
|
||||||
|
{
|
||||||
|
client.disconnect().await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
@ -13,27 +63,14 @@ async fn main() {
|
||||||
.format_timestamp_nanos()
|
.format_timestamp_nanos()
|
||||||
.init();
|
.init();
|
||||||
|
|
||||||
/*let mut pckt: SubscriptionPacket<1, 0> = SubscriptionPacket::new();
|
let recv = task::spawn(async move {
|
||||||
let mut res = vec![0; 140];
|
receive().await;
|
||||||
let lnsub = pckt.encode(&mut res);
|
});
|
||||||
println!("{:02X?}", &res[0..lnsub]);
|
|
||||||
let mut res2 = vec![0; 260];
|
|
||||||
|
|
||||||
let mut pblsh = PublishPacket::<0>::new(x);
|
let publ = task::spawn(async move {
|
||||||
let lnpblsh = pblsh.encode(&mut res2);
|
publish("hello world 123 !").await;
|
||||||
println!("{:02X?}", &res2[0..lnpblsh]);
|
});
|
||||||
log::info!("xxx");
|
|
||||||
|
|
||||||
let mut res3 = vec![0; 260];
|
join!(recv, publ);
|
||||||
let mut cntrl = ConnectPacket::<3, 0>::clean();
|
log::info!("Done");
|
||||||
let lncntrl = cntrl.encode(&mut res3);
|
|
||||||
println!("{:02X?}", &res3[0..lncntrl]);
|
|
||||||
log::info!("xxx");*/
|
|
||||||
let mut ip: [u8; 4] = [37, 205, 11, 180];
|
|
||||||
let mut port: u16 = 1883;
|
|
||||||
let mut tokio_network: TokioNetwork = TokioNetwork::new(ip, port);
|
|
||||||
tokio_network.create_connection().await;
|
|
||||||
let mut res2 = vec![0; 260];
|
|
||||||
let client = MqttClientV5::new(&mut tokio_network, &mut res2);
|
|
||||||
let mut x = b"hello world";
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
pub mod network_trait;
|
pub mod network_trait;
|
||||||
|
|
|
@ -3,17 +3,17 @@ use core::future::Future;
|
||||||
|
|
||||||
use crate::packet::mqtt_packet::Packet;
|
use crate::packet::mqtt_packet::Packet;
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum NetworkError {
|
pub enum NetworkError {
|
||||||
Connection,
|
Connection,
|
||||||
Unknown,
|
Unknown,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pub trait Network {
|
pub trait Network {
|
||||||
type ConnectionFuture<'m>: Future<Output = Result<(), NetworkError>>
|
type ConnectionFuture<'m>: Future<Output = Result<(), NetworkError>>
|
||||||
where
|
where
|
||||||
Self: 'm;
|
Self: 'm;
|
||||||
|
|
||||||
type WriteFuture<'m>: Future<Output = Result<(), NetworkError>>
|
type WriteFuture<'m>: Future<Output = Result<(), NetworkError>>
|
||||||
where
|
where
|
||||||
|
|
|
@ -33,7 +33,14 @@ impl<'a, const MAX_PROPERTIES: usize> ConnackPacket<'a, MAX_PROPERTIES> {
|
||||||
|
|
||||||
impl<'a, const MAX_PROPERTIES: usize> Packet<'a> for ConnackPacket<'a, MAX_PROPERTIES> {
|
impl<'a, const MAX_PROPERTIES: usize> Packet<'a> for ConnackPacket<'a, MAX_PROPERTIES> {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
todo!()
|
Self {
|
||||||
|
fixed_header: PacketType::Connack.into(),
|
||||||
|
remain_len: 0,
|
||||||
|
ack_flags: 0,
|
||||||
|
connect_reason_code: 0,
|
||||||
|
property_len: 0,
|
||||||
|
properties: Vec::<Property<'a>, MAX_PROPERTIES>::new(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn encode(&mut self, buffer: &mut [u8]) -> usize {
|
fn encode(&mut self, buffer: &mut [u8]) -> usize {
|
||||||
|
|
|
@ -98,6 +98,7 @@ impl<'a, const MAX_PROPERTIES: usize, const MAX_WILL_PROPERTIES: usize>
|
||||||
self.fixed_header = cur_type | flags;
|
self.fixed_header = cur_type | flags;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, const MAX_PROPERTIES: usize, const MAX_WILL_PROPERTIES: usize> Packet<'a>
|
impl<'a, const MAX_PROPERTIES: usize, const MAX_WILL_PROPERTIES: usize> Packet<'a>
|
||||||
for ConnectPacket<'a, MAX_PROPERTIES, MAX_WILL_PROPERTIES>
|
for ConnectPacket<'a, MAX_PROPERTIES, MAX_WILL_PROPERTIES>
|
||||||
{
|
{
|
||||||
|
|
|
@ -31,7 +31,7 @@ impl<'a, const MAX_PROPERTIES: usize> DisconnectPacket<'a, MAX_PROPERTIES> {
|
||||||
self.decode_properties(buff_reader);
|
self.decode_properties(buff_reader);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_reason(& mut self, reason: u8) {
|
fn add_reason(&mut self, reason: u8) {
|
||||||
self.disconnect_reason = reason;
|
self.disconnect_reason = reason;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ impl<'a, const MAX_PROPERTIES: usize> Packet<'a> for DisconnectPacket<'a, MAX_PR
|
||||||
remain_len: 5,
|
remain_len: 5,
|
||||||
disconnect_reason: 0x00,
|
disconnect_reason: 0x00,
|
||||||
property_len: 0,
|
property_len: 0,
|
||||||
properties: Vec::<Property<'a>, MAX_PROPERTIES>::new()
|
properties: Vec::<Property<'a>, MAX_PROPERTIES>::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,20 +24,22 @@ pub trait Packet<'a> {
|
||||||
self.set_property_len(buff_reader.read_variable_byte_int().unwrap());
|
self.set_property_len(buff_reader.read_variable_byte_int().unwrap());
|
||||||
let mut x: u32 = 0;
|
let mut x: u32 = 0;
|
||||||
let mut prop: Result<Property, ParseError>;
|
let mut prop: Result<Property, ParseError>;
|
||||||
loop {
|
if self.get_property_len() != 0 {
|
||||||
let mut res: Property;
|
loop {
|
||||||
prop = Property::decode(buff_reader);
|
let mut res: Property;
|
||||||
if let Ok(res) = prop {
|
prop = Property::decode(buff_reader);
|
||||||
log::info!("Parsed property {:?}", res);
|
if let Ok(res) = prop {
|
||||||
x = x + res.len() as u32 + 1;
|
log::info!("Parsed property {:?}", res);
|
||||||
self.push_to_properties(res);
|
x = x + res.len() as u32 + 1;
|
||||||
} else {
|
self.push_to_properties(res);
|
||||||
// error handler
|
} else {
|
||||||
log::error!("Problem during property decoding");
|
// error handler
|
||||||
}
|
log::error!("Problem during property decoding");
|
||||||
|
}
|
||||||
|
|
||||||
if x == self.get_property_len() {
|
if x == self.get_property_len() {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
|
use core::ptr::null;
|
||||||
use heapless::Vec;
|
use heapless::Vec;
|
||||||
|
|
||||||
use crate::encoding::variable_byte_integer::VariableByteIntegerEncoder;
|
use crate::encoding::variable_byte_integer::VariableByteIntegerEncoder;
|
||||||
use crate::packet::mqtt_packet::Packet;
|
use crate::packet::mqtt_packet::Packet;
|
||||||
use crate::packet::publish_packet::QualityOfService::{INVALID, QoS0, QoS1, QoS2};
|
use crate::packet::publish_packet::QualityOfService::{QoS0, QoS1, QoS2, INVALID};
|
||||||
use crate::utils::buffer_reader::BuffReader;
|
use crate::utils::buffer_reader::BuffReader;
|
||||||
use crate::utils::buffer_reader::EncodedString;
|
use crate::utils::buffer_reader::EncodedString;
|
||||||
use crate::utils::buffer_writer::BuffWriter;
|
use crate::utils::buffer_writer::BuffWriter;
|
||||||
|
@ -14,7 +15,7 @@ pub enum QualityOfService {
|
||||||
QoS0,
|
QoS0,
|
||||||
QoS1,
|
QoS1,
|
||||||
QoS2,
|
QoS2,
|
||||||
INVALID
|
INVALID,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<u8> for QualityOfService {
|
impl From<u8> for QualityOfService {
|
||||||
|
@ -23,8 +24,8 @@ impl From<u8> for QualityOfService {
|
||||||
0 => QoS0,
|
0 => QoS0,
|
||||||
1 => QoS1,
|
1 => QoS1,
|
||||||
2 => QoS2,
|
2 => QoS2,
|
||||||
_ => INVALID
|
_ => INVALID,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,7 +36,7 @@ impl Into<u8> for QualityOfService {
|
||||||
QoS1 => 1,
|
QoS1 => 1,
|
||||||
QoS2 => 2,
|
QoS2 => 2,
|
||||||
INVALID => 3,
|
INVALID => 3,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,29 +54,19 @@ pub struct PublishPacket<'a, const MAX_PROPERTIES: usize> {
|
||||||
// properties
|
// properties
|
||||||
pub properties: Vec<Property<'a>, MAX_PROPERTIES>,
|
pub properties: Vec<Property<'a>, MAX_PROPERTIES>,
|
||||||
|
|
||||||
pub message: &'a [u8],
|
pub message: Option<&'a [u8]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, const MAX_PROPERTIES: usize> PublishPacket<'a, MAX_PROPERTIES> {
|
impl<'a, const MAX_PROPERTIES: usize> PublishPacket<'a, MAX_PROPERTIES> {
|
||||||
pub fn new(topic_name: &'a str, message: &'a str) -> Self {
|
|
||||||
let mut x = Self {
|
|
||||||
fixed_header: PacketType::Publish.into(),
|
|
||||||
remain_len: 0,
|
|
||||||
topic_name: EncodedString::new(),
|
|
||||||
packet_identifier: 1,
|
|
||||||
property_len: 0,
|
|
||||||
properties: Vec::<Property<'a>, MAX_PROPERTIES>::new(),
|
|
||||||
message: message.as_bytes(),
|
|
||||||
};
|
|
||||||
x.add_topic_name(topic_name);
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add_topic_name(&mut self, topic_name: &'a str) {
|
pub fn add_topic_name(&mut self, topic_name: &'a str) {
|
||||||
self.topic_name.string = topic_name;
|
self.topic_name.string = topic_name;
|
||||||
self.topic_name.len = topic_name.len() as u16;
|
self.topic_name.len = topic_name.len() as u16;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn add_message(& mut self, message: &'a [u8]) {
|
||||||
|
self.message = Some(message);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn decode_publish_packet(&mut self, buff_reader: &mut BuffReader<'a>) {
|
pub fn decode_publish_packet(&mut self, buff_reader: &mut BuffReader<'a>) {
|
||||||
if self.decode_fixed_header(buff_reader) != (PacketType::Publish).into() {
|
if self.decode_fixed_header(buff_reader) != (PacketType::Publish).into() {
|
||||||
log::error!("Packet you are trying to decode is not PUBLISH packet!");
|
log::error!("Packet you are trying to decode is not PUBLISH packet!");
|
||||||
|
@ -88,13 +79,24 @@ impl<'a, const MAX_PROPERTIES: usize> PublishPacket<'a, MAX_PROPERTIES> {
|
||||||
self.packet_identifier = buff_reader.read_u16().unwrap();
|
self.packet_identifier = buff_reader.read_u16().unwrap();
|
||||||
}
|
}
|
||||||
self.decode_properties(buff_reader);
|
self.decode_properties(buff_reader);
|
||||||
self.message = buff_reader.read_message();
|
let mut total_len = VariableByteIntegerEncoder::len(
|
||||||
|
VariableByteIntegerEncoder::encode(self.remain_len).unwrap());
|
||||||
|
total_len = total_len + 1 + self.remain_len as usize;
|
||||||
|
self.message = Some(buff_reader.read_message(total_len));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, const MAX_PROPERTIES: usize> Packet<'a> for PublishPacket<'a, MAX_PROPERTIES> {
|
impl<'a, const MAX_PROPERTIES: usize> Packet<'a> for PublishPacket<'a, MAX_PROPERTIES> {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
todo!()
|
Self {
|
||||||
|
fixed_header: PacketType::Publish.into(),
|
||||||
|
remain_len: 0,
|
||||||
|
topic_name: EncodedString::new(),
|
||||||
|
packet_identifier: 1,
|
||||||
|
property_len: 0,
|
||||||
|
properties: Vec::<Property<'a>, MAX_PROPERTIES>::new(),
|
||||||
|
message: None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn encode(&mut self, buffer: &mut [u8]) -> usize {
|
fn encode(&mut self, buffer: &mut [u8]) -> usize {
|
||||||
|
@ -104,7 +106,7 @@ impl<'a, const MAX_PROPERTIES: usize> Packet<'a> for PublishPacket<'a, MAX_PROPE
|
||||||
let property_len_enc: [u8; 4] =
|
let property_len_enc: [u8; 4] =
|
||||||
VariableByteIntegerEncoder::encode(self.property_len).unwrap();
|
VariableByteIntegerEncoder::encode(self.property_len).unwrap();
|
||||||
let property_len_len = VariableByteIntegerEncoder::len(property_len_enc);
|
let property_len_len = VariableByteIntegerEncoder::len(property_len_enc);
|
||||||
let mut msg_len = self.message.len() as u32;
|
let mut msg_len = self.message.unwrap().len() as u32;
|
||||||
rm_ln = rm_ln + property_len_len as u32 + msg_len + self.topic_name.len as u32 + 2;
|
rm_ln = rm_ln + property_len_len as u32 + msg_len + self.topic_name.len as u32 + 2;
|
||||||
|
|
||||||
buff_writer.write_u8(self.fixed_header);
|
buff_writer.write_u8(self.fixed_header);
|
||||||
|
@ -122,7 +124,7 @@ impl<'a, const MAX_PROPERTIES: usize> Packet<'a> for PublishPacket<'a, MAX_PROPE
|
||||||
|
|
||||||
buff_writer.write_variable_byte_int(self.property_len);
|
buff_writer.write_variable_byte_int(self.property_len);
|
||||||
buff_writer.encode_properties::<MAX_PROPERTIES>(&self.properties);
|
buff_writer.encode_properties::<MAX_PROPERTIES>(&self.properties);
|
||||||
buff_writer.insert_ref(msg_len as usize, self.message);
|
buff_writer.insert_ref(msg_len as usize, self.message.unwrap());
|
||||||
return buff_writer.position;
|
return buff_writer.position;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -53,7 +53,14 @@ impl<'a, const MAX_REASONS: usize, const MAX_PROPERTIES: usize> Packet<'a>
|
||||||
for SubackPacket<'a, MAX_REASONS, MAX_PROPERTIES>
|
for SubackPacket<'a, MAX_REASONS, MAX_PROPERTIES>
|
||||||
{
|
{
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
todo!()
|
Self {
|
||||||
|
fixed_header: PacketType::Suback.into(),
|
||||||
|
remain_len: 0,
|
||||||
|
packet_identifier: 0,
|
||||||
|
property_len: 0,
|
||||||
|
properties: Vec::<Property<'a>, MAX_PROPERTIES>::new(),
|
||||||
|
reason_codes: Vec::<u8, MAX_REASONS>::new()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn encode(&mut self, buffer: &mut [u8]) -> usize {
|
fn encode(&mut self, buffer: &mut [u8]) -> usize {
|
||||||
|
|
|
@ -32,14 +32,27 @@ pub struct SubscriptionPacket<'a, const MAX_FILTERS: usize, const MAX_PROPERTIES
|
||||||
impl<'a, const MAX_FILTERS: usize, const MAX_PROPERTIES: usize>
|
impl<'a, const MAX_FILTERS: usize, const MAX_PROPERTIES: usize>
|
||||||
SubscriptionPacket<'a, MAX_FILTERS, MAX_PROPERTIES>
|
SubscriptionPacket<'a, MAX_FILTERS, MAX_PROPERTIES>
|
||||||
{
|
{
|
||||||
pub fn new() -> Self {
|
pub fn add_new_filter(& mut self, topic_name: &'a str) {
|
||||||
|
let len = topic_name.len();
|
||||||
|
let mut new_filter = TopicFilter::new();
|
||||||
|
new_filter.filter.string = topic_name;
|
||||||
|
new_filter.filter.len = len as u16;
|
||||||
|
self.topic_filters.push(new_filter);
|
||||||
|
self.topic_filter_len = self.topic_filter_len + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, const MAX_FILTERS: usize, const MAX_PROPERTIES: usize> Packet<'a>
|
||||||
|
for SubscriptionPacket<'a, MAX_FILTERS, MAX_PROPERTIES>
|
||||||
|
{
|
||||||
|
fn new() -> Self {
|
||||||
let mut x = Self {
|
let mut x = Self {
|
||||||
fixed_header: PacketType::Subscribe.into(),
|
fixed_header: PacketType::Subscribe.into(),
|
||||||
remain_len: 0,
|
remain_len: 0,
|
||||||
packet_identifier: 1,
|
packet_identifier: 1,
|
||||||
property_len: 0,
|
property_len: 0,
|
||||||
properties: Vec::<Property<'a>, MAX_PROPERTIES>::new(),
|
properties: Vec::<Property<'a>, MAX_PROPERTIES>::new(),
|
||||||
topic_filter_len: 1,
|
topic_filter_len: 0,
|
||||||
topic_filters: Vec::<TopicFilter<'a>, MAX_FILTERS>::new(),
|
topic_filters: Vec::<TopicFilter<'a>, MAX_FILTERS>::new(),
|
||||||
};
|
};
|
||||||
let mut p = TopicFilter::new();
|
let mut p = TopicFilter::new();
|
||||||
|
@ -48,14 +61,6 @@ impl<'a, const MAX_FILTERS: usize, const MAX_PROPERTIES: usize>
|
||||||
x.topic_filters.push(p);
|
x.topic_filters.push(p);
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, const MAX_FILTERS: usize, const MAX_PROPERTIES: usize> Packet<'a>
|
|
||||||
for SubscriptionPacket<'a, MAX_FILTERS, MAX_PROPERTIES>
|
|
||||||
{
|
|
||||||
fn new() -> Self {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn encode(&mut self, buffer: &mut [u8]) -> usize {
|
fn encode(&mut self, buffer: &mut [u8]) -> usize {
|
||||||
let mut buff_writer = BuffWriter::new(buffer);
|
let mut buff_writer = BuffWriter::new(buffer);
|
||||||
|
@ -82,7 +87,7 @@ impl<'a, const MAX_FILTERS: usize, const MAX_PROPERTIES: usize> Packet<'a>
|
||||||
buff_writer.write_variable_byte_int(self.property_len);
|
buff_writer.write_variable_byte_int(self.property_len);
|
||||||
buff_writer.encode_properties::<MAX_PROPERTIES>(&self.properties);
|
buff_writer.encode_properties::<MAX_PROPERTIES>(&self.properties);
|
||||||
buff_writer.encode_topic_filters_ref(
|
buff_writer.encode_topic_filters_ref(
|
||||||
false,
|
true,
|
||||||
self.topic_filter_len as usize,
|
self.topic_filter_len as usize,
|
||||||
&self.topic_filters,
|
&self.topic_filters,
|
||||||
);
|
);
|
||||||
|
|
|
@ -32,7 +32,6 @@ pub struct UnsubscriptionPacket<'a, const MAX_FILTERS: usize, const MAX_PROPERTI
|
||||||
impl<'a, const MAX_FILTERS: usize, const MAX_PROPERTIES: usize>
|
impl<'a, const MAX_FILTERS: usize, const MAX_PROPERTIES: usize>
|
||||||
UnsubscriptionPacket<'a, MAX_FILTERS, MAX_PROPERTIES>
|
UnsubscriptionPacket<'a, MAX_FILTERS, MAX_PROPERTIES>
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, const MAX_FILTERS: usize, const MAX_PROPERTIES: usize> Packet<'a>
|
impl<'a, const MAX_FILTERS: usize, const MAX_PROPERTIES: usize> Packet<'a>
|
||||||
|
|
|
@ -18,26 +18,36 @@ pub struct TokioNetwork {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TokioNetwork {
|
impl TokioNetwork {
|
||||||
fn convert_ip(& mut self) -> String {
|
fn convert_ip(&mut self) -> String {
|
||||||
String::from(format!("{}.{}.{}.{}:{}", self.ip[0], self.ip[1], self.ip[2], self.ip[3], self.port))
|
String::from(format!(
|
||||||
|
"{}.{}.{}.{}:{}",
|
||||||
|
self.ip[0], self.ip[1], self.ip[2], self.ip[3], self.port
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TokioNetwork {
|
impl TokioNetwork {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Network for TokioNetwork {
|
impl Network for TokioNetwork {
|
||||||
type ConnectionFuture<'m> where Self: 'm = impl Future<Output = Result<(), NetworkError>> + 'm;
|
type ConnectionFuture<'m>
|
||||||
type WriteFuture<'m> where Self: 'm = impl Future<Output = Result<(), NetworkError>> + 'm;
|
where
|
||||||
type ReadFuture<'m> where Self: 'm = impl Future<Output = Result<usize, NetworkError>> + 'm;
|
Self: 'm,
|
||||||
|
= impl Future<Output = Result<(), NetworkError>> + 'm;
|
||||||
|
type WriteFuture<'m>
|
||||||
|
where
|
||||||
|
Self: 'm,
|
||||||
|
= impl Future<Output = Result<(), NetworkError>> + 'm;
|
||||||
|
type ReadFuture<'m>
|
||||||
|
where
|
||||||
|
Self: 'm,
|
||||||
|
= impl Future<Output = Result<usize, NetworkError>> + 'm;
|
||||||
|
|
||||||
fn new(ip: [u8; 4], port: u16) -> Self {
|
fn new(ip: [u8; 4], port: u16) -> Self {
|
||||||
return Self {
|
return Self {
|
||||||
ip,
|
ip,
|
||||||
port,
|
port,
|
||||||
socket: Option::None,
|
socket: Option::None,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_connection<'m>(&'m mut self) -> Self::ConnectionFuture<'m> {
|
fn create_connection<'m>(&'m mut self) -> Self::ConnectionFuture<'m> {
|
||||||
|
@ -48,32 +58,31 @@ impl Network for TokioNetwork {
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
.map_err(|_| NetworkError::Connection)
|
.map_err(|_| NetworkError::Connection)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send<'m>(&'m mut self, buffer: &'m mut [u8], len: usize) -> Self::WriteFuture<'m> {
|
fn send<'m>(&'m mut self, buffer: &'m mut [u8], len: usize) -> Self::WriteFuture<'m> {
|
||||||
async move {
|
async move {
|
||||||
return if let Some(ref mut stream) = self.socket {
|
return if let Some(ref mut stream) = self.socket {
|
||||||
stream.write_all(&buffer[0..len])
|
stream
|
||||||
|
.write_all(&buffer[0..len])
|
||||||
.await
|
.await
|
||||||
.map_err(|_| NetworkError::Unknown)
|
.map_err(|_| NetworkError::Unknown)
|
||||||
} else {
|
} else {
|
||||||
Err(NetworkError::Unknown)
|
Err(NetworkError::Unknown)
|
||||||
}
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn receive<'m>(&'m mut self, buffer: &'m mut [u8]) -> Self::ReadFuture<'m> {
|
fn receive<'m>(&'m mut self, buffer: &'m mut [u8]) -> Self::ReadFuture<'m> {
|
||||||
async move {
|
async move {
|
||||||
return if let Some(ref mut stream) = self.socket {
|
return if let Some(ref mut stream) = self.socket {
|
||||||
stream.read(buffer)
|
stream
|
||||||
|
.read(buffer)
|
||||||
.await
|
.await
|
||||||
.map_err(|_| NetworkError::Connection)
|
.map_err(|_| NetworkError::Connection)
|
||||||
} else {
|
} else {
|
||||||
Err(NetworkError::Unknown)
|
Err(NetworkError::Unknown)
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,4 +95,4 @@ impl Network for TokioNetwork {
|
||||||
let len = self.socket.read(buffer).await ?;
|
let len = self.socket.read(buffer).await ?;
|
||||||
Ok(len)
|
Ok(len)
|
||||||
}*/
|
}*/
|
||||||
}
|
}
|
||||||
|
|
|
@ -150,6 +150,7 @@ impl<'a> BuffReader<'a> {
|
||||||
log::error!("Could not parse utf-8 string");
|
log::error!("Could not parse utf-8 string");
|
||||||
return Err(ParseError::Utf8Error);
|
return Err(ParseError::Utf8Error);
|
||||||
}
|
}
|
||||||
|
self.increment_position(len_res as usize);
|
||||||
return Ok(EncodedString {
|
return Ok(EncodedString {
|
||||||
string: res_str.unwrap(),
|
string: res_str.unwrap(),
|
||||||
len: len_res,
|
len: len_res,
|
||||||
|
@ -188,7 +189,7 @@ impl<'a> BuffReader<'a> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_message(&mut self) -> &'a [u8] {
|
pub fn read_message(&mut self, total_len: usize) -> &'a [u8] {
|
||||||
return &self.buffer[self.position..];
|
return &self.buffer[self.position..total_len];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,7 +49,7 @@ impl<'a> BuffWriter<'a> {
|
||||||
|
|
||||||
pub fn write_u32(&mut self, four_bytes: u32) {
|
pub fn write_u32(&mut self, four_bytes: u32) {
|
||||||
let bytes: [u8; 4] = four_bytes.to_be_bytes();
|
let bytes: [u8; 4] = four_bytes.to_be_bytes();
|
||||||
self.insert_ref(4,&bytes);
|
self.insert_ref(4, &bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_string_ref(&mut self, str: &EncodedString<'a>) {
|
pub fn write_string_ref(&mut self, str: &EncodedString<'a>) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user