I have requirement where the component is responsible for receiving a message and
process the message. There should be a generic message interface such as uint8 array (it is a binary message) and a generic framework, reads from an external configuration file, a list of handlers. The handlers should implement a generic interface such as :-
uint8[] process(uint8[] message);
Each handler should process the message and return the processed message to the framework, so that framework can hand the processed message to the next handler and so on.
The beauty of this approach is that it would allow adding new capabilities, hopefully at runtime, by adding a new handler in the configuration file. The generic framework should initialize the new handler, put it in its respective position of handlers and pass the message through all the handlers.
I have implemented this approach many times in Java, but I am struggling to implement it in C and C++. For example in C, I was thinking perhaps register each handler’s process function via a function pointer to the framework, so that framework can pass message to each of the handler. But I dont know how to inititialize the handlers based on config file. The config file will contain a “String” name of the handler. How does main method map that string name to the handler file?
Any pointers to code where such a pattern exists would be greatly helpful.
Well if your config file says this:
and you’re on a Linux platform, you can do this:
for each name in the config file. This dynamically loads a shared library each of which contains a handler function (named “handler”).
For other OS, similar facilities exist (LoadLibrary on Windows for a *.dll for example).