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 20:34

>>3
Yeah you are going a bit more advanced than is required. Of course if i had told you guys what the program was you would have known that.

It's just an ircbot lol.

Yes i just emit signal at a specified chain of execution in the main program. And since it's just a simple ircbot, all it needs is to be passed all the information about the line sent from the server. This information is parsed out by a function in the shared irc library i wrote, returned to the main program which uses it as an argument to all module functions.

Basically i don't feel the modules need to interact with the program state, i guess it is kinda limited but i'm just a hobby programmer writing for fun and no profit. Though if i wanted to do this, the only way i can think of is to pass the pointers with a function in the module, i assume this is what you meant and the best way in this situation.

That's what i use the shared irc library for though, so the modules can send data to the server using the same functions as the main program. Because all they need is the socket descriptor. Since it's such a one horse program i think it serves it's purpose this way, of course if i wanted to make something more elaborate i'll keep your advice in mind.

This is the first program of this type i've ever made, with modules that load at run-time like this. So i was very happy with your reply, i don't know any programmers to ask about these things.

Thanks anonymous, you came through like always.

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