#[cfg(feature = "std")]
pub fn main() {
use mqtt_tiny::packets::{ToWriter, TryFromReader};
use mqtt_tiny::{Connack, Connect, Disconnect, Puback, Publish};
use std::net::TcpStream;
use std::time::UNIX_EPOCH;
let mut connection = TcpStream::connect("127.0.0.1:1883").expect("failed to connect to server");
Connect::new(30, true, b"mqtttinyexamplesconnect").expect("failed to create CONNECT packet")
.write(&mut connection).expect("failed to send CONNECT packet");
let connack = Connack::try_read(&mut connection).expect("failed to read CONNACK packet");
assert_eq!(connack.return_code(), 0, "connection was refused");
let unix_time = UNIX_EPOCH.elapsed().expect("failed to get unix timestamp");
let packet_id = unix_time.as_nanos() as u16;
let timestamp = format!("{}-unixtime", unix_time.as_secs());
Publish::new(b"mqtttinyexamplespublish/date", timestamp.as_bytes(), false)
.expect("failed to create PUBLISH packet")
.with_qos(1, packet_id, false)
.write(&mut connection).expect("failed to write PUBLISH packet");
let puback = Puback::try_read(&mut connection).expect("failed to read PUBACK packet");
assert_eq!(puback.packet_id(), packet_id, "invalid packed ID for PUBACK packet");
Disconnect::new().write(&mut connection).expect("failed to write DISCONNECT packet");
}
#[cfg(not(feature = "std"))]
pub fn main() {
panic!("Example requires the `std`-feature");
}