publisher and subscriber, c, embedded

Thread Starter

bug13

Joined Feb 13, 2012
2,002
Hi team

I am want to learn to implement something like publisher and subscriber pattern in C in embedded environment. Something like MQTT, but just some basic publisher -> broker -> subscriber functions. All these will be running within the same application/MCU.

Has anyone come across this information? Or source code?

Thanks team!
 

bogosort

Joined Sep 24, 2011
696
Maybe I'm not understanding your request, but if you want to learn a design pattern, I think that you're better off writing the code that implements the pattern yourself, rather than reading what other people have done.
 

Thread Starter

bug13

Joined Feb 13, 2012
2,002
Maybe I'm not understanding your request, but if you want to learn a design pattern, I think that you're better off writing the code that implements the pattern yourself, rather than reading what other people have done.
Yes I agree, what I was hoping to find are what is the basic structure. Eg if I want to learn a circular buffer, I will need these:
C:
buffer_put();
buffer_get();
buffer_isFull();
buffer_isEmpty();
/* etc... */
I think it will be great help if I know these if I want to write my own circular buffer, that's all.
 

Thread Starter

bug13

Joined Feb 13, 2012
2,002
I don't even have a clue what you are talking about. In what context would such a paradigm be relevant?
I was thinking in a bigger and complex project. I can separate the producer (publisher) and the consumer (subscriber).

Say there is an user button, it can publish a press event and release event. In a small application, I can just do this:
button -> application.
But in a more complex project, there may be multiple part of the code want to have access to these events, I can do this:
button -> application_1
-> application_2
-> application_3
-> etc...
My button event and application_1/2/3 need to be tightly couple, as the complexity increase, the code is harder to maintain.

The better way, in my opinion, the button can publish the events to a broker, and whoever want access to those event can just subscribe to it. My code can be loosely couple and a lot easier to write and maintain.
button (publisher) -> broker -> application_1
-> application_2
-> application_3
The benefit will be obvious if we have multiple publishers and multiple subscribers, and the publishers don't need to worry about who will be using the data. And the subscribers can subscribe to whatever event they need.
 

Papabravo

Joined Feb 24, 2006
21,159
You're using a certain syntax without defining the associated semantics. I still don't get what you are aiming at.
I can associate the terms publisher and subscriber, which I am not familiar with, and the terms producer and consumer that I am familiar with. Is there any meaningful distinction between the two pairs of terms?
 

Thread Starter

bug13

Joined Feb 13, 2012
2,002
You're using a certain syntax without defining the associated semantics.
Is there any meaningful distinction between the two pairs of terms?
It could be quite possible that I am using the wrong terms, as I have no idea where/how to start.

Let me try to explain it in another way.

Say you want to maintain a mailing list, at first you only have a handful of subscribers. That's easy, because you just need to send a email to all the people on your mailing list.

And eventually, because of you hard work, your mailing list is very popular. And different people want to receive different topic of from your mailing list.
You can:
  1. A) Manually maintain a few different mailing list for different topics, and send them the email. But every time someone want to add/remove from a topic. He/she need to email you, tell you what he/she want to change, then you change it.
  2. B) You write some software, let's call it broker, it can handle all the subscription/removal for you. All you have to do is send the email to the broker with different topics. The broker can forward the correct topic to the correct email address. Whoever want to change a topic, he/she can just email the broker with the correct title (say remove:topitc, or subscribe: topic). And the broker can do all the removal, subscription and forwarding for you automatically.

Hope it make more sense now.
 

bogosort

Joined Sep 24, 2011
696
Yes I agree, what I was hoping to find are what is the basic structure. Eg if I want to learn a circular buffer, I will need these:
C:
buffer_put();
buffer_get();
buffer_isFull();
buffer_isEmpty();
/* etc... */
I think it will be great help if I know these if I want to write my own circular buffer, that's all.
I'm still not understanding what you're looking for. If your goal is to learn how to implement a FIFO/ring buffer, you shouldn't start with someone else's API. Rather, you determine how the FIFO should behave and write the interface yourself.

As for the publisher-subscriber pattern (https://en.wikipedia.org/wiki/Publish–subscribe_pattern), it seems like you already understand how it should work at a high level. The learning comes from trying to implement it and fixing the mistakes you make along the way.
 

Thread Starter

bug13

Joined Feb 13, 2012
2,002
I'm still not understanding what you're looking for. If your goal is to learn how to implement a FIFO/ring buffer, you shouldn't start with someone else's API. Rather, you determine how the FIFO should behave and write the interface yourself.
I see what you are saying. I guess I just want some commonly accepted APIs, and start from there. Just need to learn enough to be able to implement it for my own need, for my application. Not trying to re-invent the wheel here. If that makes sense.
 

bogosort

Joined Sep 24, 2011
696
I see what you are saying. I guess I just want some commonly accepted APIs, and start from there. Just need to learn enough to be able to implement it for my own need, for my application. Not trying to re-invent the wheel here. If that makes sense.
Understood. Some languages have the pub-sub pattern built in, such as Observables in Java. For other languages, you should be able to find already designed libraries to play with; try googling, e.g., "publish subscribe library C++".
 

ApacheKid

Joined Jan 12, 2015
1,533
A ring buffer is an abstract concept and can be created in any language really even C. It is not really related to pub/sub, that's a different kind of concept, a higher level one (but could easily leverage ring buffers).

I designed a ring buffer last year coded in C# (not portable to MCUs) that uses a novel mechanism that allows asynchronous handlers (which could easily map to interrupt handlers) to start async read/write operations that work directly with the buffer storage.

This eliminated the common burden of copying from some IO buffer into a ring buffer, the ring buffer's internal buffer is the IO buffer.

Here's the code.
 

upand_at_them

Joined May 15, 2010
940
I am want to learn to implement something like publisher and subscriber pattern in C in embedded environment. Something like MQTT, but just some basic publisher -> broker -> subscriber functions. All these will be running within the same application/MCU.

Has anyone come across this information? Or source code?
There are already MQTT libraries for ESP8266, Arduino, and others.
 

Thread Starter

bug13

Joined Feb 13, 2012
2,002
A ring buffer is an abstract concept and can be created in any language really even C. It is not really related to pub/sub, that's a different kind of concept, a higher level one (but could easily leverage ring buffers).

I designed a ring buffer last year coded in C# (not portable to MCUs) that uses a novel mechanism that allows asynchronous handlers (which could easily map to interrupt handlers) to start async read/write operations that work directly with the buffer storage.

This eliminated the common burden of copying from some IO buffer into a ring buffer, the ring buffer's internal buffer is the IO buffer.

Here's the code.
I didn't really understand your code. But I guess I need something like this:
C#:
    public class PublisherSubscriber : IPublisherSubscriber
    {
        // Implemented interface 1
        public void Interface_1(){
         // my implementation code
        }
        
        // Implemented interface 2
        // Implemented interface 3
        // Implemented interface 4
        // etc...
    }
On a curious note, I didn't you can use C# on a ESP8266 hardware?? How's the performance like?
 
Top