jeudi 29 octobre 2020

Modular event list and ensuring ID uniqueness

I am looking for a design pattern/documentations/leads to solve a complex problem my team and I are encountering. The question is flagged as C++ because the solution would be implemented in C++ but the question is less about the language and more about the design.

The goal is to build some sort of a modular event list. Each module will add events to a list that get consumed by a process. The problem is that in order to avoid an unmaintainable table of all possible events (due to modularity, adding or removing a module would change this table), each event should be flag by an unique ID. But we don't seem to find a way to ensure that the ID we generate is unique without such a table. Another constraint is that this is targeted to embedded system with limited amount of Flash and RAM, so it would be ideal if the final "event table" would not be included in the binary.

The general idea would be the following :

generic_header_file:

uint32_t someHashFunction(someParameter); // Ideally this would be a constexpr

// Adds an event to the list
void add_event_to_list(uint32_t value);

// Returns an event and consumes it
uint32_t get_next_event(void);

module_1_file

# include "generic_header_file.h"
void module_1_function(somePArameter)
{
    //...
    add_event_to_list(someHashFunction("Name_of_event_to_be_hashed1"))
    //...
}

module_2_file

#include "generic_header_file.h"
void module_2_function(someParameter)
{
    //...
    add_event_to_list(someHashFunction("Name_of_event_to_be_hashed2"))
    //...
}

process_file

#include "generic_header_file.h"
void myProcess(void)
{
    uint32_t event;

    event = get_next_event();

    switch(event)
    {
        case someHashFunction("Name_of_event_to_be_hashed1"):
        {
            //...
        } break;
        case someHashFunction("Name_of_event_to_be_hashed2"):
        {
            //...
        } break;
        case someHashFunction("SomeOtherEvent"):
        {
            // Should never be received because the associated module was not added
            //...
        } break;
    }
}

The interesting way this would work is that even if we remove a module, we don't need to modify to heavily an application. For example, if we remove the module responsible for generating the "SomeOtherEvent" event, since there is no access to a global array containing all the hashes, no out of array access can happen, and on simple and generic application this should also drastically improve development time, allowing us to heavily reuse already existing software.

The problem with the hash method, is that without a list of all the generated hash, we cannot guarantee no collisions occur. And since module shall be independent and of course in different files, I don't see a way to even build such a hash table.

I am open to any comments or leads on this matter (even a paper demonstrating such a solution is impossible, at least we would know).

Aucun commentaire:

Enregistrer un commentaire