Client first
This commit is contained in:
20
src/client/client_v5.rs
Normal file
20
src/client/client_v5.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use crate::packet::publish_packet::PublishPacket;
|
||||
use crate::network::network_trait::Network;
|
||||
|
||||
struct MqttClientV5<T: Network> {
|
||||
network_driver: T,
|
||||
}
|
||||
|
||||
impl<T> MqttClientV5<T>
|
||||
where
|
||||
T: Network,
|
||||
{
|
||||
fn send_message(& mut self, topic_name: & str, message: & str, buffer: & mut [u8]) {
|
||||
let packet = PublishPacket::new(topic_name, message);
|
||||
self.network_driver.send()
|
||||
}
|
||||
|
||||
fn receive_message(& mut self) {
|
||||
|
||||
}
|
||||
}
|
||||
1
src/client/mod.rs
Normal file
1
src/client/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod client_v5;
|
||||
@@ -6,6 +6,8 @@
|
||||
pub mod encoding;
|
||||
pub mod packet;
|
||||
pub mod utils;
|
||||
pub mod client;
|
||||
mod network;
|
||||
|
||||
#[allow(unused_variables)]
|
||||
pub fn print_stack(file: &'static str, line: u32) {
|
||||
|
||||
25
src/main.rs
25
src/main.rs
@@ -1,9 +1,3 @@
|
||||
/*use rust_mqtt::packet::mqtt_packet::*;
|
||||
use rust_mqtt::packet::property::*;*/
|
||||
/*use heapless::Vec;
|
||||
use std::fs::File;
|
||||
use std::io::Read;*/
|
||||
|
||||
use rust_mqtt::packet::connect_packet::ConnectPacket;
|
||||
use rust_mqtt::packet::mqtt_packet::Packet;
|
||||
use rust_mqtt::packet::publish_packet::PublishPacket;
|
||||
@@ -31,23 +25,4 @@ fn main() {
|
||||
let lncntrl = cntrl.encode(&mut res3);
|
||||
println!("{:02X?}", &res3[0..lncntrl]);
|
||||
log::info!("xxx");
|
||||
|
||||
/*let fl = File::open("/Users/obabec/development/school/rust-mqtt/mqtt_control_example.bin");
|
||||
|
||||
let mut f = File::open("/Users/obabec/development/school/rust-mqtt/mqtt_control_example.bin").expect("no file found");
|
||||
let mut buffer: [u8; 500] = [0; 500];
|
||||
f.read(&mut buffer).expect("buffer overflow");
|
||||
|
||||
|
||||
//
|
||||
let mut payld = *b"xxxxx";*/
|
||||
//let packet = Packet::clean(txt, &mut payld);
|
||||
/*let mut buffer_reader = BuffReader::new(&buffer);
|
||||
packet_builder.decode_packet(& mut buffer_reader);
|
||||
|
||||
|
||||
let bytes: [u8; 4] = packet_builder.currentPacket.protocol_name.to_be_bytes();
|
||||
|
||||
let prot = std::str::from_utf8(&bytes).unwrap();
|
||||
log::info!("Protocol name: {}", prot)*/
|
||||
}
|
||||
|
||||
1
src/network/mod.rs
Normal file
1
src/network/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod network_trait;
|
||||
5
src/network/network_trait.rs
Normal file
5
src/network/network_trait.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
pub trait Network {
|
||||
fn send(buffer: & mut [u8]);
|
||||
fn receive(buffer: & mut [u8]);
|
||||
}
|
||||
@@ -27,21 +27,25 @@ pub struct PublishPacket<'a, const MAX_PROPERTIES: usize> {
|
||||
}
|
||||
|
||||
impl<'a, const MAX_PROPERTIES: usize> PublishPacket<'a, MAX_PROPERTIES> {
|
||||
pub fn new(message: &'a [u8]) -> Self {
|
||||
pub fn new(topic_name: & str, message: &'a str) -> Self {
|
||||
let mut x = Self {
|
||||
fixed_header: PacketType::Publish.into(),
|
||||
remain_len: 0,
|
||||
topic_name: EncodedString::new(),
|
||||
packet_identifier: 0,
|
||||
packet_identifier: 1,
|
||||
property_len: 0,
|
||||
properties: Vec::<Property<'a>, MAX_PROPERTIES>::new(),
|
||||
message,
|
||||
message: message.as_bytes(),
|
||||
};
|
||||
x.topic_name.string = "test/topic";
|
||||
x.topic_name.len = 10;
|
||||
x.add_topic_name(topic_name);
|
||||
return x;
|
||||
}
|
||||
|
||||
pub fn add_topic_name(&mut self, topic_name: & str) {
|
||||
self.topic_name.string = topic_name;
|
||||
self.topic_name.len = topic_name.len() as u16;
|
||||
}
|
||||
|
||||
pub fn decode_publish_packet(&mut self, buff_reader: &mut BuffReader<'a>) {
|
||||
if self.decode_fixed_header(buff_reader) != (PacketType::Publish).into() {
|
||||
log::error!("Packet you are trying to decode is not PUBLISH packet!");
|
||||
|
||||
Reference in New Issue
Block a user