cpluff main

来源:互联网 发布:驾驶证考试软件 编辑:程序博客网 时间:2024/04/28 19:58
1、初始化cpluff框架 Initializing the plug-in framework
    -调用 cp_init()进行初始化
    -调用cp_set_fatal_error_handler()注册错误处理函数
    -例如:
#include <locale.h>
  #include <cpluff.h>


  void handle_fatal_error(const char *msg) {


    // ... log error, flush logs, send bug report, etc. ...


    fprintf(stderr, "A fatal error occurred: %s\n", msg);
    abort();
      }


  void initialize(void) {
    cp_status_t status;


    setlocale(LC_ALL, "");
    cp_set_fatal_error_handler(handle_fatal_error);
    status = cp_init();
    if (status != CP_OK) {
      // ... handle initialization failure ...
      }
    }


2、创建插件上下文用于插件之间的交互 Creating a plug-in context
#include <cpluff.h>


  cp_context_t *ctx;


  void create_context(void) {
    cp_status_t status;


    ctx = cp_create_context(&status);
    if (ctx == NULL) {
      // ... handle initialization failure ...
       }
     }


3、加载插件 Loading plug-ins
   从PluginListFile中加载
-调用 cp_load_plugin_descriptor 、cp_install_plugin加载
#include <stdio.h>
  #include <cpluff.h>


  extern cp_context_t *ctx;
  static const char pluginListFile[] = "/etc/example/plugins.list";


  void load_plugins(void) {
    char plugindir[128];
    FILE *lf;


    // Open plug-in list file
    lf = fopen(pluginListFile, "r");
    if (lf == NULL) {
      // ... handle loading failure ...
    }


    // Load each listed plug-in
    while (fgets(plugindir, 128, lf) != NULL) {
      cp_plugin_info_t *plugininfo;
      cp_status_t status;
      int i;


      // Remove possible trailing newline from plug-in location
      for (i = 0; plugindir[i + 1] != '\0'; i++);
      if (plugindir[i] == '\n') {
        plugindir[i] = '\0';
      }


      // Load plug-in descriptor
      plugininfo = cp_load_plugin_descriptor(ctx, plugindir, &status);
      if (pinfo == NULL) {
        // ... handle loading failure ...
      }


      // Install plug-in descriptor
      status = cp_install_plugin(ctx, plugininfo);
      if (status != CP_OK) {
        // ... handle loading failure ...
      }


      // Release plug-in descriptor information
      cp_release_info(ctx, plugininfo);
    }


    // Close plug-in list file
    fclose(lf);
  }


    集合加载,单独的文件夹下有不同的插件,每个插件有独立的文件夹 
-调用cp_register_pcollection,cp_scan_plugins
#include <cpluff.h>


  extern cp_context_t *ctx;
  static const char pluginCollectionDir[] = "/etc/example/plugins";


  void load_plugins(void) {
    cp_status_t status;


    status = cp_register_pcollection(ctx, pluginCollectionDir);
    if (status != CP_OK) {
      // ... handle loading failure ...
    }
    status = cp_scan_plugins(ctx, 0);
    if (status != CP_OK) {
      // ... handle loading failure ...
      // (notice that some plug-ins might have been loaded)
    }
      }


4、控制插件执行 Controlling plug-in execution
    -调用cp_set_context_args、cp_start_plugin、cp_run_plugins.运行插件
    #include <cpluff.h>


  extern cp_context_t *ctx;
  static const char corePluginId[] = "org.example.core";


  void run_plugins(char *argv[]) {
    cp_status_t status;


    // Set plug-in startup arguments
    cp_set_context_args(ctx, argv);


    // Start the core plug-in, possibly activating other plug-ins as well
    status = cp_start_plugin(ctx, corePluginId);
    if (status != CP_OK) {
      // ... handle startup failure ...
    }


    // Execute plug-ins until there is no more work to be done
    cp_run_plugins(ctx);
  }


  int main(int argc, char *argv[]) {
    // ... do initialization and load plug-ins ...


    run_plugins(argv);


    // ... do destruction ...
  }
    
    -如果主循环中还要做别的事情则调用cp_run_plugins_step
void mainloop(void) {
    int finished = 0;


    while (!finished) {
      // ... do main program specific operations ...


      finished = !cp_run_plugins_step(ctx);
  }
  }


5、更改插件配置 Changing plug-in configuration(opt)
6、销毁插件框架 Destroying the plug-in framework



0 0