int main(void) {
    CommHandle *plugin = loadCommPlugin("/usr/lib/libzmqpubsub-client.so");
    if (plugin) {
        puts("Success: Plugin Loaded");
        exit(EXIT_SUCCESS);
    } else {
        puts("Failed: Plugin Load");
        exit(EXIT_FAILURE);
    }
}
Ejemplo n.º 2
0
/** Initializes Service object. Creates the service object and calls its init method for further initialization.
* @param[in] specification service specification
* @return returns service handle upon successful and NULL otherwise
*/
CommHandle *createService(ServiceSpec *specification) {
    CommHandle *commHandle = NULL;
    char cwd_temp[1024];

    // Parse configuration file
    strcpy(cwd_temp, LIB_CONFIG_DIRECTORY);
    strcat(cwd_temp, LIB_CONFIG_FILENAME);
    if (!parseConfigFile(cwd_temp)) {
        freeGlobals();
    }

    // load the plugin
    // Considers the last path and loads the plugin interface
    char *substrStart = g_configData.pluginDir;
    char *substrEnd, *folderPath = NULL;
    do {
        if (*substrStart == ':') {
            substrStart++;
        }
        if (folderPath != NULL) { // Incase of iteration, freeing the previously allocated dynamic memory
            free(folderPath);
            folderPath = NULL;
        }
        substrEnd = strstr(substrStart, ":");
        if (substrEnd == NULL) {
            folderPath = strdup(substrStart);
        } else {
            folderPath = strndup(substrStart, substrEnd - substrStart);
        }

        strcpy(cwd_temp, ""); // set empty string

        // Parse plugin file
        if (folderPath != NULL && *folderPath != '/') { // if path is not absolute path; then consider plugins directory
            strcpy(cwd_temp, LIB_PLUGINS_DIRECTORY);
        }

        strcat(cwd_temp, "lib");
        strcat(cwd_temp, specification->type.name);
        strcat(cwd_temp, "-service.so");

        if (fileExists(cwd_temp)) {
            commHandle = loadCommPlugin(cwd_temp);
            break;
        }
    }while((substrStart = strstr(substrStart, ":")) != NULL);

    if (!commHandle) {
        fprintf(stderr, "Plugin library \"lib%s-service.so\" not found\n", specification->type.name);
        cleanUp(commHandle);
        free(folderPath);
        return NULL;
    }

    // Considers the last path and loads the plugin interface
    substrStart = g_configData.pluginInterfaceDir;
    do {
        if (*substrStart == ':') {
            substrStart++;
        }
        if (folderPath != NULL) { // Incase of iteration, freeing the previously allocated dynamic memory
            free(folderPath);
            folderPath = NULL;
        }
        substrEnd = strstr(substrStart, ":");
        if (substrEnd == NULL) {
            folderPath = strdup(substrStart);
        } else {
            folderPath = strndup(substrStart, substrEnd - substrStart);
        }

        strcpy(cwd_temp, ""); // set empty string

        // Parse plugin interface file
        if (folderPath != NULL && *folderPath != '/') { // if path is not absolute path; then consider plugins directory
            strcpy(cwd_temp, LIB_CONFIG_DIRECTORY);
        }
        if (folderPath != NULL) {
            strcat(cwd_temp, folderPath);
        }
        strcat(cwd_temp, "/");
        strcat(cwd_temp, *(commHandle->interface));
        strcat(cwd_temp, ".json");

        if (fileExists(cwd_temp)) {
            if (!parsePluginInterfaces(cwd_temp)) {
                freeGlobals();
            }
            break;
        }
    }while((substrStart = strstr(substrStart, ":")) != NULL);

    free(folderPath); // free the dynamic memory
    if (loadCommInterfaces(commHandle) == false) {
        cleanUp(commHandle);
        return NULL;
    }

    if (commHandle->init) {
        commHandle->init(specification);
    }

    return commHandle;
}