版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、电 子 科 技 大 学实 验 报 告学生姓名: 学 号: 指导教师:实验地点:A2-412 一、实验室名称:Linux环境高级编程实验室二、实验项目名称:插件框架实验三、实验学时:4学时四、实验目的:学习和实践插件框架的开发。理解插件的工作原理,为进一步开发高可用,高复杂度的插件打下基础。五、实验内容:1、使用动态链接库实现打印功能:开发一个程序,向屏幕打印“Hello World”;在不重新编译链接原程序的前提下,将打印的文字改为“Hello China”。2、使用动态链接库实现自定义打印功能:同时要打印“Hello World”,打印“Hello China”,甚至同时打印未来才会增加的其
2、他打印信息,打印信息的链接库放在一个固定目录中,遍历这个目录,获取所有动态链接库。打印未来的这些信息,也不能重新编译链接原程序。3、1)通过命令行方式:./a.out help,输出所有插件实现的功能ID,以及该功能ID对应的功能描述。2)通过命令行方式:./a.out FuncID,调用具体打印功能(每个插件导出GetID接口)。4、将插件导出的Print、GetID、Help三个函数放在一个类中,主程序需要使用多个容器分别保存这些函数地址,让插件外部获取该类的对象。综合练习:实现一个软件系统,该系统可对文件进行统计分析。究竟对文件进行什么样的统计分析,最终用户自己也不是很清楚,目前只知道需
3、要统计文件的行数。也就是说,本软件系统将会随时面临,增加文件统计新功能的难题。请结合本实验内容,设计并实现这样一个面向文件统计功能的插件系统。(需要实现的插件包括:统计某个文件的行数,统计某个文件的字节数)六、实验步骤:程序1:A. h:extern "c" void f();a2.cpp:#include<iostream>#include"a.h"using namespace std;void f() cout << "hello,China!" << endl;A1.cpp:#include
4、<iostream>#include"a.h"using namespace std;void f() cout << "Hello,Word" << endl;Main.cpp:#include"a.h"#include<dlfcn.h>#include<iostream>using namespace std;int main() void *handle = dlopen("./libtest.so", RTLD_LAZY); if(0 = hand
5、le) cout << "dlopen error" << endl;return 0; typedef void (*Fun)(); Fun f1 = (Fun)dlsym(handle, "f"); if(0 = f1) cout << "f1 error" << endl; char *str = dlerror();cout << str << endl; (*f1)(); dlclose(handle); return 0;程序运行结果如图1所示:图1:
6、字符的变换程序2:A1.cpp:#include<iostream>using namespace std;extern"C" void f()cout << "Hello world" << endl;A2.cpp:#include<iostream>using namespace std;extern "C" void f() cout << "Hello,china!" << endl;A3.cpp:#include<iostrea
7、m>using namespace std;extern "C" void f()cout << "Hello 333" << endl;A4.cpp:#include<iostream>using namespace std;extern "C" void f()cout << "Hello 4444" << endl;Test2.cpp:#include<dlfcn.h>#include<iostream>#include
8、<dirent.h>#include<stdio.h>#include<string.h>using namespace std;int main()char path260;DIR *dir;struct dirent *ptr; dir=opendir("/root/test/test4/plug2/plugin/");while(ptr=readdir(dir)!=NULL)if (strcmp(ptr->d_name,".")=0)|(strcmp(ptr->d_name,"."
9、)=0)continue;sprintf(path,"/root/test/test4/plug2/plugin/%s", ptr->d_name); void *handle = dlopen(path, RTLD_LAZY); if(0 = handle) cout << "dlopen error" << endl;return 0; typedef void (*Fun)(); Fun pf = (Fun)dlsym(handle, "f"); if(0 = pf) cout << &
10、quot;pf error" << endl; char *str = dlerror();cout << str << endl;return 0; (*pf)();dlclose(handle); closedir(dir);程序运行结果如图2所示:图2:插件的遍历程序3:a1.cpp:#include <iostream>using namespace std;const int FUNC_ID = 1;extern "C" void f() cout << "Hello World!&q
11、uot; << endl;extern "C" void Help() cout << "Function ID " << FUNC_ID << " : This function prints Hello World." << endl;a2.cpp:#include <iostream>using namespace std;const int FUNC_ID = 2;extern "C" void f() cout << &q
12、uot;Hello China!" << endl;extern "C" void Help() cout << "Function ID " << FUNC_ID << " This function prints hello china." << endl;CPluginEnumerator.cpp#include "CPluginEnumerator.h"#include <dirent.h>#include <strin
13、g.h>#include <iostream>#include <stdio.h>CPluginEnumerator:CPluginEnumerator()CPluginEnumerator:CPluginEnumerator()bool CPluginEnumerator:GetPluginNames(vector<string>& vstrPluginNames) DIR *dir = opendir("/root/test/test4/plug3/plugin"); if(dir = 0)return false; f
14、or(;) struct dirent *ptr = readdir(dir);if(ptr = 0)break;if(strcmp(ptr->d_name, ".") = 0)|(strcmp(ptr->d_name, ".") = 0)continue;char path260;sprintf(path, "/root/test/test4/plug3/plugin/%s", ptr->d_name);vstrPluginNames.push_back(path); closedir(dir);return tr
15、ue;Test.cpp:#include <dlfcn.h>#include <iostream>#include "CPluginEnumerator.h"#include <string.h>#include <stdio.h>using namespace std;int main(int argc, char *argv)char path 260; if(argc != 2)return 0; if(strcmp(argv1, "help") = 0) vector<string> v
16、strPluginNames; CPluginEnumerator enumerator; if(!enumerator.GetPluginNames(vstrPluginNames) cout << "GetPluginNames error" << endl;return 0; for(int i = 0; i< vstrPluginNames.size(); i+) void *handle = dlopen(vstrPluginNamesi.c_str(), RTLD_LAZY);if(handle = 0)cout <<
17、"dlopen error" << endl; return 0;typedef void (*FUNC_HELP)();FUNC_HELP dl_help = (FUNC_HELP)dlsym(handle, "Help");if(dl_help = 0)cout << "dlsym error" << endl; return 0;(dl_help)();dlclose(handle);else if(strcmp(argv1, "1") = 0)sprintf(path,
18、"/root/test/test4/plug3/plugin/%s", "a1.so");void *handle = dlopen(path, RTLD_LAZY);if(handle = 0)cout << "dlopen error" << endl;return 0;typedef void (*FUNC_PRINT)();FUNC_PRINT dl_print = (FUNC_PRINT)dlsym(handle, "f");if(dl_print = 0)cout <<
19、; "dlsym error" << endl;return 0;(dl_print)();dlclose(handle);else if(strcmp(argv1, "2") = 0)/ 得到第二个func的参数sprintf(path, "/root/test/test4/plug3/plugin/%s", "a2.so");void *handle = dlopen(path, RTLD_LAZY);if(handle = 0)cout << "dlopen error&qu
20、ot; << endl;return 0;typedef void (*FUNC_PRINT)();FUNC_PRINT dl_print = (FUNC_PRINT)dlsym(handle, "f");if(dl_print = 0)cout << "dlsym error" << endl;return 0;(dl_print)();dlclose(handle);return 0;程序运行结果如图3所示:图3:插件输出程序4:CPluginEnumerator.h:#ifndef CPLUGINENUMERAT
21、OR_H#define CPLUGINENUMERATOR_H#include <vector>#include <string>using namespace std;class CPluginEnumeratorpublic: CPluginEnumerator(); virtual CPluginEnumerator(); bool GetPluginNames(vector<string>& vstrPluginNames);#endifCPluginEnumerator.cpp:#include "CPluginEnumerato
22、r.h"#include <dirent.h>#include <string.h>CPluginEnumerator:CPluginEnumerator()CPluginEnumerator:CPluginEnumerator()bool CPluginEnumerator:GetPluginNames(vector<string>& vstrPluginNames) DIR *dir = opendir("./plugin"); if(dir = 0)return false; for(;) struct diren
23、t *pentry = readdir(dir);if(pentry = 0) break;if(strcmp(pentry->d_name, ".") = 0) continue;if(strcmp(pentry->d_name, ".") = 0) continue;string str = "./plugin/"str += pentry->d_name;vstrPluginNames.push_back(str); closedir(dir); return true;CPluginController.h#
24、ifndef CPLUGINCONTROLLER_H#define CPLUGINCONTROLLER_H#include <vector>class IPrintPlugin;class CPluginControllerpublic:CPluginController(void);virtual CPluginController(void);bool InitializeController(void);bool UninitializeController(void);bool ProcessHelp(void);bool ProcessRequest(int Functi
25、onID);private:std:vector<void *> m_vhForPlugin;std:vector<IPrintPlugin*> m_vpPlugin;#endifCPluginController.cpp#include "CPluginController.h"#include "CPluginEnumerator.h"#include "IPrintPlugin.h"#include "dlfcn.h"CPluginController:CPluginControlle
26、r(void)CPluginController:CPluginController(void)bool CPluginController:InitializeController(void)std:vector<std:string> vstrPluginNames;CPluginEnumerator enumerator;if(!enumerator.GetPluginNames(vstrPluginNames)return false;for(unsigned int i=0 ; i<vstrPluginNames.size(); i+)typedef int (*P
27、LUGIN_CREATE)(IPrintPlugin*);PLUGIN_CREATE CreateProc; IPrintPlugin *pPlugin = NULL;void* hinstLib = dlopen(vstrPluginNamesi.c_str(), RTLD_LAZY); if(hinstLib != NULL) m_vhForPlugin.push_back(hinstLib);CreateProc = (PLUGIN_CREATE)dlsym(hinstLib, "CreateObj"); if(NULL != CreateProc) (CreateP
28、roc)(&pPlugin);if(pPlugin != NULL)m_vpPlugin.push_back(pPlugin);return true;bool CPluginController:ProcessRequest(int FunctionID)for(unsigned int i = 0; i < m_vpPlugin.size(); i+)if(m_vpPlugini->GetID() = FunctionID)m_vpPlugini->Print();break;return true;bool CPluginController:ProcessHe
29、lp(void)std:vector<std:string> vstrPluginNames;CPluginEnumerator enumerator;if(!enumerator.GetPluginNames(vstrPluginNames)return false;for(unsigned int i=0 ; i<vstrPluginNames.size(); i+)typedef int (*PLUGIN_CREATE)(IPrintPlugin*);PLUGIN_CREATE CreateProc; IPrintPlugin *pPlugin = NULL;void*
30、 hinstLib = dlopen(vstrPluginNamesi.c_str(), RTLD_LAZY); if(hinstLib != NULL) CreateProc = (PLUGIN_CREATE)dlsym(hinstLib, "CreateObj"); if(NULL != CreateProc) (CreateProc)(&pPlugin);if(pPlugin != NULL)pPlugin->Help();dlclose(hinstLib);return true;bool CPluginController:UninitializeC
31、ontroller()for(unsigned int i = 0; i < m_vhForPlugin.size(); i+)dlclose(m_vhForPlugini);return true;IPrintPlugin.h#pragma onceclass IPrintPluginpublic:IPrintPlugin();virtual IPrintPlugin();virtual void Help() = 0;virtual void Print() = 0;virtual int GetID() = 0;IPrintPlugin.cpp#include "IPri
32、ntPlugin.h"IPrintPlugin:IPrintPlugin()IPrintPlugin:IPrintPlugin()Function.cpp#include <iostream>#include "IPrintPlugin.h"using namespace std;const int FUNC_ID = 1;class CPrintPlugin : public IPrintPluginpublic:CPrintPlugin()virtual CPrintPlugin()virtual void Print()cout <<
33、 "Hello World!" << endl;virtual void Help()cout << "Function ID " << FUNC_ID << " : This function will print hello world." << endl;virtual int GetID(void)return FUNC_ID;extern "C" void CreateObj(IPrintPlugin *ppPlugin)static CPrin
34、tPlugin plugin;*ppPlugin = &plugin;function1.cpp#include <iostream>#include "IPrintPlugin.h"using namespace std;const int FUNC_ID = 2;class CPrintPlugin : public IPrintPluginpublic:CPrintPlugin()virtual CPrintPlugin()virtual void Print()cout << "Hello China!" <
35、< endl;virtual void Help()cout << "Function ID " << FUNC_ID << " : This function will print hello china." << endl;virtual int GetID(void)return FUNC_ID;extern "C" void CreateObj(IPrintPlugin *ppPlugin)static CPrintPlugin plugin;*ppPlugin = &am
36、p;plugin;Main.cpp#include <iostream>#include "CPluginController.h"#include <string.h>#include <stdlib.h>using namespace std;int main(int argc, char *argv)if(argc != 2)cout << "Parameters error" << endl;return 0;if(strcmp(argv1, "help") = 0)
37、CPluginController pc;pc.ProcessHelp();return 0;int FunctionID = atoi(argv1);CPluginController pc;pc.InitializeController();pc.ProcessRequest(FunctionID);pc.UninitializeController();return 0;程序运行结果如图4所示:图4:插件获取类对象综合练习:CPluginController.cpp#include "CPluginController.h"#include "CPlugin
38、Enumerator.h"#include "IPrintPlugin.h"#include "dlfcn.h"#include <string.h>CPluginController:CPluginController(void)CPluginController:CPluginController(void)bool CPluginController:InitializeController(void)std:vector<std:string> vstrPluginNames;CPluginEnumerator
39、enumerator;if(!enumerator.GetPluginNames(vstrPluginNames)return false;for(unsigned int i=0 ; i<vstrPluginNames.size(); i+)typedef int (*PLUGIN_CREATE)(IPrintPlugin*);PLUGIN_CREATE CreateProc; IPrintPlugin *pPlugin = NULL;void* hinstLib = dlopen(vstrPluginNamesi.c_str(), RTLD_LAZY); if(hinstLib !=
40、 NULL) m_vhForPlugin.push_back(hinstLib);CreateProc = (PLUGIN_CREATE)dlsym(hinstLib, "CreateObj"); if(NULL != CreateProc) (CreateProc)(&pPlugin);if(pPlugin != NULL)m_vpPlugin.push_back(pPlugin);return true;bool CPluginController:ProcessRequest(int FunctionID)for(unsigned int i = 0; i &
41、lt; m_vpPlugin.size(); i+)if(m_vpPlugini->GetID() = FunctionID)m_vpPlugini->Print();break;return true;bool CPluginController:ProcessHelp(void)std:vector<std:string> vstrPluginNames;CPluginEnumerator enumerator;if(!enumerator.GetPluginNames(vstrPluginNames)return false;for(unsigned int i=
42、0 ; i<vstrPluginNames.size(); i+)typedef int (*PLUGIN_CREATE)(IPrintPlugin*);PLUGIN_CREATE CreateProc; IPrintPlugin *pPlugin = NULL;void* hinstLib = dlopen(vstrPluginNamesi.c_str(), RTLD_LAZY); if(hinstLib != NULL) CreateProc = (PLUGIN_CREATE)dlsym(hinstLib, "CreateObj"); if(NULL != Cre
43、ateProc) (CreateProc)(&pPlugin);if(pPlugin != NULL)pPlugin->Help();dlclose(hinstLib);return true;bool CPluginController:IfProcess(char *Function)/判断插件是否存在unsigned int i;for(i = 0; i < m_vpPlugin.size(); i+)if(strcmp(Function, m_vpPlugini->GetName() = 0)break;if(i < m_vpPlugin.size()/
44、插件存在return true;elsereturn false;bool CPluginController:ProcessFunction(char *Function,char*Document)/执行插件功能for(unsigned int i = 0; i < m_vpPlugin.size(); i+)if(strcmp(Function, m_vpPlugini->GetName() = 0)m_vpPlugini->Fun(Document);/插件功能break;return true;bool CPluginController:UninitializeC
45、ontroller()for(unsigned int i = 0; i < m_vhForPlugin.size(); i+)dlclose(m_vhForPlugini);return true;CPluginController.h#ifndef CPLUGINCONTROLLER_H#define CPLUGINCONTROLLER_H#include <vector>class IPrintPlugin;class CPluginControllerpublic:CPluginController(void);virtual CPluginController(vo
46、id);bool InitializeController(void);bool UninitializeController(void);bool ProcessHelp(void);bool ProcessRequest(int FunctionID);bool IfProcess(char *Function);bool ProcessFunction(char *Function,char *Document);private:std:vector<void *> m_vhForPlugin;std:vector<IPrintPlugin*> m_vpPlugi
47、n;#endifCPluginEnumerator.h#ifndef CPLUGINENUMERATOR_H#define CPLUGINENUMERATOR_H#include <vector>#include <string>using namespace std;class CPluginEnumeratorpublic: CPluginEnumerator(); virtual CPluginEnumerator(); bool GetPluginNames(vector<string>& vstrPluginNames);#endifCPl
48、uginEnumerator.cpp#include "CPluginEnumerator.h"#include <dirent.h>#include <string.h>CPluginEnumerator:CPluginEnumerator()CPluginEnumerator:CPluginEnumerator()bool CPluginEnumerator:GetPluginNames(vector<string>& vstrPluginNames) DIR *dir = opendir("./plugin&quo
49、t;); if(dir = 0)return false; for(;) struct dirent *pentry = readdir(dir);if(pentry = 0) break;if(strcmp(pentry->d_name, ".") = 0) continue;if(strcmp(pentry->d_name, ".") = 0) continue;string str = "./plugin/"str += pentry->d_name;vstrPluginNames.push_back(str)
50、; closedir(dir); return true;Main.cpp#include <iostream>#include "CPluginController.h"#include <string.h>#include <stdlib.h>using namespace std;int main(int argc, char *argv)if(argc = 2)if(strcmp(argv1, "help") = 0)/帮助功能CPluginController pc;pc.ProcessHelp();retu
51、rn 0;elseint FunctionID = atoi(argv1); CPluginController pc; pc.InitializeController(); pc.ProcessRequest(FunctionID); pc.UninitializeController();return 0;else if(argc = 3)CPluginController pc;char *Function = argv1;/Function+=".so"char *Document = argv2;/操作的文件名pc.InitializeController();i
52、f(pc.IfProcess(Function)=false)/判断插件是否存在cout << "No this plugin!" << endl;else/ pc.ProcessFunction(Function,Document);pc.UninitializeController();return 0;elsecout << "Parameters error" << endl;return 0;插件:IPrintPlugin.cpp#include "IPrintPlugin.h"
53、;IPrintPlugin:IPrintPlugin()IPrintPlugin:IPrintPlugin()IPrintPlugin.h#pragma onceclass IPrintPluginpublic:IPrintPlugin();virtual IPrintPlugin();virtual void Help() = 0;virtual void Print() = 0;virtual int GetID() = 0;virtual char * GetName() = 0;/添加部分virtual void Fun(char*) = 0;Line.cpp#include <
54、iostream>#include "IPrintPlugin.h"#include <unistd.h>/read(file)#include <fcntl.h>#include <string.h>using namespace std;const int FUNC_ID = 3;char FUNC_NAME="sl"/statistics line!class CPrintPlugin : public IPrintPluginpublic:CPrintPlugin()virtual CPrintPlugin()virtual void Print()cout << "Statistics the document line!" << endl;virtual void Help()cout << "Function ID " << FUNC_ID << " : This function will statistics the document line.&q
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 电厂安全工作计划
- 关于父爱母爱的
- 关于大学生恋爱问题
- 六年级科学 《建高塔》
- 《GE战略实施》课件
- 大班下学期音乐教学计划3篇
- 健康安全小常识
- 教育机构返聘退休讲师劳务合同
- 安全生产园区管理办法
- 古建筑修复灰工施工合同
- 2024年木屑贸易协议3篇
- “希腊罗马古典文化”历史教学课件
- 【MOOC】农产品贸易-福建农林大学 中国大学慕课MOOC答案
- 连云港市农商控股集团限公司2024年专业技术人员招聘【重点基础提升】模拟试题(共500题)附带答案详解
- MOOC 3D工程图学-华中科技大学 中国大学慕课答案
- 劳务外包服务方案(技术方案)
- 亚马逊品牌授权书(英文模板)
- 精益生产现场管理和改善课件(共132页).ppt
- 钢轨闪光焊 技术 焊缝 伤损_图文
- 风管风机吊架重量表
- 房屋漏水赔偿协议书甲方
评论
0/150
提交评论