Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Now i'm no fancy big city expert programmer

Name: Anonymous 2007-10-16 13:38

...but i'm making a program in my own free time that loads modules dynamically at run-time using dlopen. This is done using C on BSD UNIX systems, i have no other systems available to me right now.

Basically my program is sort of like irssi, with modules, but the modules are written in C. So i'm in the process of adding hooks or events in the main program and i would like to ask your advice.

The main program and all modules are linked to a shared library which holds some functions common to both program and modules. These are not important as they have to do with an application protocol. The module handling code is in the main program, this might be good to know.

Now here is what i want you to judge and give advice on, i let each module declare values in a globally known struct called _module_info. This is done in the global scope for each module.

It basically holds information for the module, anything from author and description to it's command and the EVENTS it should hook in to. It also holds the address of the main function in the module. Now feel free to give advice on this because of course this is a limitation but i feel that as long as each modules function is passed ALL the info it might need from the main program, it can decide for itself what to do with it.

When all available modules are loaded by the program after it has started up, it uses dlsym to get the address of the _module_info object and puts these in an array of pointers to structs which is global to the main program.

So then i define a bunch of events in the main program header file, now i've defined them as hex values for some reason, i dunno why i guess they could just as well be integers. The point is that they're unique.

Then i execute a function at select places in the main program, first argument is one of the event signals, remaining arguments is information that needs to be passed to the module. This is all the information a module might require as this is a simple program.

This function loops through our global array of structs with module information and checks each modules array of events that it has defined. Now here is another limitation because this array has a defined size, so you can't define any more events than that. But again, it's not a huge program so when i was done i only had about 8 events, so the array is 16 big just in case.

You can probably guess the rest, each module that matches the event going on has it's main function executed with two constant arguments. First is a socket descriptor because this is a network program, second is all the information a module might require, assembled by functions in the main program.

So basically i would like some opinions on this method i'm using with modules. What can be improved, am i a complete idiot and why i should become an hero?

Name: Anonymous 2007-10-16 15:14

Now here is another limitation because this array has a defined size, so you can't define any more events than that.
Use a linked list.

Further notes -

1) I don't see anything about how modules interact with the program state. Assume the module wants to change something - how does it achieve this? One way to implement this would be to feed the module a bunch of function pointers, another is to poll it and watch the return values, etc.

2) How to modules inter-communicate? Do they interact by modifying state in the program (as described above), or is there a way for them to directly interact?

3) In what order are modules notified of an event? Is there some kind of fixed priority-chain, or is it arbitrary?

I think you're off to a good start, but the part which I feel is lacking is the API exposed to the modules. From the module, you should be able to have enough functionality to actually *do* something, and be able to interact with other modules and such.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List