Archives for: May 2005, 21

05/21/05

GModuleLoader

After Philip was talking about his GQueueThread/ASyncWorker on #gnome-nl, I was tempted to write some Glib-extension too, and so I did.

You might remember the extra exercise in my article on GModules and vtables, about loading all modules in the current directory and use them. Well, I created a generic object to handle this, because it's a functionality needed quite frequently in plugin-based programs.

I called this "GModuleLoader" ;-)

Here's a sample client application, loading 2 modules:

#include <glib.h>
#include <gmodule.h>

#include "g-module-loader.h"
#include "g-module-loader-module.h"

gint main(gint argc, gchar *argv[]) {
        GModuleLoader *l = NULL;
        guint cnt = 0;
        GPtrArray *modules = NULL;

        g_type_init();

        /* Create a new GModuleLoader */
        l = (GModuleLoader *) g_module_loader_new();
        g_assert(l != NULL);

        /* Load all modules in ".libs", with "simple-test" as data.
         * We do not want any error reporting (yet) */
        g_module_loader_load(l, ".libs", "simple-test", NULL);

        g_print("\n\nLooping through all loaded modules, printing the data\n");
        /* Get the list of all modules */
        modules = g_module_loader_get_modules(l);
        if(modules == NULL) {
                g_object_unref(l);
                g_error("No modules in list, aborting");
                return 1;
        }
        
        /* Loop */
        for(cnt = 0; cnt < modules->len; cnt++) {
                GModuleLoaderModule *m = NULL;
                gchar *data = NULL;
                GModule *mod = NULL;

                /* Get the GModuleLoaderModule */
                m = (GModuleLoaderModule *) g_ptr_array_index(modules, cnt);
                /* Normally we don't ever need the real GModule,
                 * because we use a vtable with all tables we need as returned
                 * data from the init function */
                mod = g_module_loader_module_get_module(m);
                if(m == NULL) {
                        g_error("Module in a GModuleLoaderModule should never be null");
                        return 1;
                }
                /* This is the data returned by the module init function.
                 * Most times this will be a vtable/struct */
                data = g_module_loader_module_get_module_data(m);

                g_print("%s:\t%s\n", g_module_name(mod), data);
        }
        g_print("\n");
        
        /* Clean up */
        g_object_unref(l);
        
        return 0;
}

I hope this is understandable :-)

Here's the code for "module1.c", the source for libmodule1.so. "module2.c" is almost the same: s/M1/M2 and s/module1/module2:

#include <glib.h>
#include <gmodule.h>

/* This is a fixed name, the module init function
 * It takes a pointer (given by the client code)
 * and can return some data (most of the time a vtable/struct, we keep it 
 * simple here) */
G_MODULE_EXPORT gpointer module_get_data(gpointer data) {
        gchar *s = NULL;
        
        s = (gchar *) data;
        if(s != NULL) {
                g_print("\n[M1] Module1 loaded with data \"%s\"\n\n", s);
        }

        return g_strdup("[M1] module1data");
}

/* Same thing, fixed name.
 * This function takes a pointer to the data given by module_get_data
 * and is called when this data should be freed */
G_MODULE_EXPORT void module_free_data(gpointer data) {
        gchar *s = NULL;
        
        s = (gchar *) data;
        if(s != NULL) {
                g_print("\n[M1] Freeing module1 data \"%s\"\n\n", s);
                g_free(data);
        }

        return;
}

As you can see I'm using fixed names in the plugins, I don't think there's any other way to do this kind of things. None of these 2 functions are required though.

Here's the output of the test application:

# ./simple-test
** (process:24094): DEBUG: Loading all modules in .libs, suffix is so
** (process:24094): DEBUG: Suffix for .libs/libmodule1.so is so, loading module
** (process:24094): DEBUG: Loading data for .libs/libmodule1.so

[M1] Module1 loaded with data "simple-test"

** (process:24094): DEBUG: Module .libs/libmodule1.so is valid, adding
** (process:24094): DEBUG: Suffix for .libs/libmodule2.so is so, loading module
** (process:24094): DEBUG: Loading data for .libs/libmodule2.so

[M2] Module2 loaded with data "simple-test"

** (process:24094): DEBUG: Module .libs/libmodule2.so is valid, adding


Looping through all loaded modules, printing the data
.libs/libmodule1.so:    [M1] module1data
.libs/libmodule2.so:    [M2] module2data

** (process:24094): DEBUG: Deleting 2 modules
** (process:24094): DEBUG: Freeing data for .libs/libmodule1.so

[M1] Freeing module1 data "[M1] module1data"

** (process:24094): DEBUG: Closing module .libs/libmodule1.so
** (process:24094): DEBUG: Freeing data for .libs/libmodule2.so

[M2] Freeing module2 data "[M2] module2data"

** (process:24094): DEBUG: Closing module .libs/libmodule2.so

It should be fairly easy to follow all steps, find out the module_get_data and module_free_data calls, etc.

It's working fairly well now, I'll try to enhance it some more, test it better and document it, then maybe I'll send some email to the Glib list ;-)

Permalink . Ikke . 07:05:58 pm . 741 Words . Technology, Linux, Coding Corner . . 318 views . Leave a comment
Exams are coming, still some projectwork, todo-list

The exams are coming and I still have a lot of projectwork to do. Today, I should have had my last class this year, but the prof sent an e-mail announcing the lesson was cancelled. So, within one week, at the 30th of may, I'll have my first exam. Five to go, from may the 30th till july the first. Well spred in time, so it should be not that heavy, but it's a very long time where I'll see my girlfriend very less :-( I'll miss her :|

But a remaining problem is the amount of projectwork that still has to be done. Today, I finally finished my work for computer graphics. I still have to do some work for Information Theory (very boring statistics), Speech Processing (wich isn't that easy too) and Information Security (wich can be complete soon). So let's hope it doesn't take that much time. We'll see. I hope I can finish information theory and information security tomorrow.

I also have a kind of todo-list with things I want to do with my gentoo when I take a break, after one exam or just after the whole period:

  • Choose a new Gnome-theme
  • Check how I can make wpa_supplicant requesting a new IP when I move from one wlan to another one. Also following point should be taken into account.
  • Write some script for my VPN-tunneling: it should always be started except when I'm registered at the VTK-hotspot, but when I'm not at the students home or at a university hotspot, I only want to get traffic for news.ugent.be and smtp.ugent.be and maybe some internal UGent-websites go through the tunnel. Just a routing script thus.
  • I want to search for a system to detect when my VPN-connection is down. It should be easy if the tunneling device went down too, but that isn't. The only method I know for now, is sending pings to some server and inspect the packet loss. There has to be a better, quicker and less consuming method. I'm using vpnc, so if you know a sollution, you're very welcome to leave a message ;-)
  • Find out why my x-server crashes when I'm using my two synaptics-things at the same time. I have a touchpad and some little thing between the g and h button to move my cursor. When I'm using the two together, sometimes my X crashes.
  • I want to find out why my USB-mouse doesn't work when X is allready running while I attach the device. Now I always have to restart X. Must be some simple problem.
  • Now I have the weather-applet in my panel, giving me the current weather situation, but I also want a forecast.
  • I want to get the internal smart-card reader working. I have such a thing built into my computer (Dell D810) and want to experiment a little with the new Belgian electronic identity card aka eID. I also want to test some bank-functions like reloading my proton and so on. I just want to play with it :-)
  • Make software suspend and hibernate working. Ubuntu Linux configures this automaticly, see this installation report. That last one is also an interesting one for D810 users who want to install linux on their machine ;-) It seems more people are buying a D810 or D800 so the counter of installation reports increases.
  • Follow the Gentoo on Laptops Guide

I'm sure the above list is far from complete. Suggestions can always be made ;-)

a Belgian eID card
A Belgian eID card

Oh, before I forget. Yesterday, I registered the domain peterdedecker.net to create a personal homepage where I can give some information about myself, put my CV online, maybe move this blog to, place some documents about student representation, learn some webdesign,... Again, suggestions are very welcome. If it's a page about me, I'ld like to have this blog there. Not that I dislike this place, au contraire, but on a personal website, the minimum requirement today is a weblog. I don't know wich engine to use: I like b2evolution and its good spam-protection a lot, but as Ikke allready told, development is like stalled. It seems WordPress and Nucleus are very nice ones. It's also a little bit easier because I don't have to worry about multiple blogs hosting, but I sure want to convert this blog to the new engine (if so). If you have some experiences in moving blogs or using other engines, feel free to comment.

Goodnight!

Permalink . Peter . 01:46:29 . 744 Words . Life & Fun, My Gentoo, Studies, Internet & Blogs . Email . No views

Geplukt uit een interview met Yves Leterme in De Standaard:

Hebt u nooit een coalitiewissel overwogen, bijvoorbeeld door de VLD in te ruilen voor de groenen?

,,Ik zie niet in wat dat zou oplossen. Daarbij, Groen! is een partij met vakantie. Mieke Vogels heeft zelf gezegd dat dit een sabbatjaar is. Ik weet niet of er buiten de politiek veel mensen zijn die een sabbatjaar kunnen nemen en bijna 4.000 euro per maand opstrijken.''

Zulke uithalen zijn we van u niet gewoon.
,,Ik ben scherp omdat Jos Stassen, de fractieleider van Groen!, mij in het Vlaams parlement verweet dat ik mijn verantwoordelijkheid niet opneem. Tijdens mijn gesprek met de voorzitter, Vera Dua, bij de vorming van de Vlaamse regering, was het eerste en het laatste wat ze zei: 'Wij willen geen verantwoordelijkheid nemen.' Nu roepen ze dat Leterme zijn verantwoordelijkheid niet neemt. Dat gaat er bij mij niet in.''

Het volledige interview is hier te vinden. Mogelijks is registratie vereist.

Permalink . Peter . 00:24:38 . 159 Words . Politics . Email . No views