0

I'm using mosquitto c++ wrapper to publish message/file.

In my test I can easily send messages that contain text, but how can I send a file?

My publisher method is:

bool Publisher::publish(const char* message) {
    const int ret = mosquittopp::publish(NULL, topic_, strlen(message),
        (uint8_t*) message);
    /* custom log for mosquitto passing res and what I'm doing */
    MosquittoLog::checkResult(ret, "sending message"); 
    return (ret == MOSQ_ERR_SUCCESS);
}

I find this post where is explained how publish file with python.
Is in c++ almost the same?
If it, how can I distinguish between files and plain text on the Subscriber?

void Subscriber::on_message(const struct mosquitto_message* message) {
/* pseudode
   if message is file do A
   else if plainText do B    
*/
}
Community
  • 1
  • 1
Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146

1 Answers1

4

Broadly:

Publishing a file

  1. Read the file into a memory buffer
  2. Pass it, and it's length to publish(...)

Reading the file

http://courses.cs.vt.edu/cs2604/fall02/binio.html#read

Publishing

What i don't understand from the mosquitto documentation is whether you can destroy the buffer immediately after you publish it, or whether it needs to hang around until the MQTT message is sent.

Distinguishing between data types

Either prefix the payload with a flag which describes the data type. Or use a different MQTT topic for different data types. Failing that, and depending on how space efficient you need to be, you could wrap your payload in a protobuf, JSON or XML message.

JCx
  • 2,689
  • 22
  • 32
  • So the distinguishing is against me, checking payload, as you suggest. Ok, this is what I need to know. I was hoping that there was simplest and faster way! Btw thanks! – Luca Davanzo Jul 10 '14 at 14:45
  • in the doubt, I will destroy buffer after the respond! – Luca Davanzo Jul 10 '14 at 14:47
  • Yes. It's glossed over in the MQTT standard with "Payload: Contains the data for publishing. The content and format of the data is application specific." See section 3.3: http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html – JCx Jul 10 '14 at 14:50
  • 1
    I'm going to have a look inside mosiqutto and answer that payload question. I need to know anyway :) – JCx Jul 10 '14 at 14:50
  • memcpy(message->msg.payload, payload, payloadlen*sizeof(uint8_t)); for QoS != 0. And looks like it sends straight away for QoS == 0. So I think you'd be fine to delete your payload immediately on the return from publish. – JCx Jul 10 '14 at 14:57
  • I'll try. I've to do hard test, then I report you – Luca Davanzo Jul 10 '14 at 15:00
  • 1
    You can free your payload immediately. – ralight Jul 10 '14 at 15:15