您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 公司方案 > Linux插件框架实验报告
电子科技大学实验报告学生姓名:学号:指导教师:实验地点:A2-412实验时间:2012.01.04一、实验室名称:Linux环境高级编程实验室二、实验项目名称:插件框架实验三、实验学时:4学时四、实验目的:学习和实践插件框架的开发。理解插件的工作原理,为进一步开发高可用,高复杂度的插件打下基础。五、实验内容:1、使用动态链接库实现打印功能:开发一个程序,向屏幕打印“HelloWorld”;在不重新编译链接原程序的前提下,将打印的文字改为“HelloChina”。2、使用动态链接库实现自定义打印功能:同时要打印“HelloWorld”,打印“HelloChina”,甚至同时打印未来才会增加的其他打印信息,打印信息的链接库放在一个固定目录中,遍历这个目录,获取所有动态链接库。打印未来的这些信息,也不能重新编译链接原程序。3、1)通过命令行方式:./a.outhelp,输出所有插件实现的功能ID,以及该功能ID对应的功能描述。2)通过命令行方式:./a.outFuncID,调用具体打印功能(每个插件导出GetID接口)。4、将插件导出的Print、GetID、Help三个函数放在一个类中,主程序需要使用多个容器分别保存这些函数地址,让插件外部获取该类的对象。综合练习:实现一个软件系统,该系统可对文件进行统计分析。究竟对文件进行什么样的统计分析,最终用户自己也不是很清楚,目前只知道需要统计文件的行数。也就是说,本软件系统将会随时面临,增加文件统计新功能的难题。请结合本实验内容,设计并实现这样一个面向文件统计功能的插件系统。(需要实现的插件包括:统计某个文件的行数,统计某个文件的字节数)六、实验步骤:程序1:A.h:externcvoidf();a2.cpp:#includeiostream#includea.husingnamespacestd;voidf(){couthello,China!endl;}A1.cpp:#includeiostream#includea.husingnamespacestd;voidf(){coutHello,Wordendl;}Main.cpp:#includea.h#includedlfcn.h#includeiostreamusingnamespacestd;intmain(){void*handle=dlopen(./libtest.so,RTLD_LAZY);if(0==handle){coutdlopenerrorendl;return0;}typedefvoid(*Fun)();Funf1=(Fun)dlsym(handle,f);if(0==f1){coutf1errorendl;char*str=dlerror();coutstrendl;}(*f1)();dlclose(handle);return0;}程序运行结果如图1所示:图1:字符的变换程序2:A1.cpp:#includeiostreamusingnamespacestd;externCvoidf(){coutHelloworldendl;}A2.cpp:#includeiostreamusingnamespacestd;externCvoidf(){coutHello,china!endl;}A3.cpp:#includeiostreamusingnamespacestd;externCvoidf(){coutHello333endl;}A4.cpp:#includeiostreamusingnamespacestd;externCvoidf(){coutHello4444endl;}Test2.cpp:#includedlfcn.h#includeiostream#includedirent.h#includestdio.h#includestring.husingnamespacestd;intmain(){charpath[260];DIR*dir;structdirent*ptr;dir=opendir(/root/test/test4/plug2/plugin/);while((ptr=readdir(dir))!=NULL){if((strcmp(ptr-d_name,..)==0)||(strcmp(ptr-d_name,.)==0)){continue;}sprintf(path,/root/test/test4/plug2/plugin/%s,ptr-d_name);void*handle=dlopen(path,RTLD_LAZY);if(0==handle){coutdlopenerrorendl;return0;}typedefvoid(*Fun)();Funpf=(Fun)dlsym(handle,f);if(0==pf){coutpferrorendl;char*str=dlerror();coutstrendl;return0;}(*pf)();dlclose(handle);}closedir(dir);}程序运行结果如图2所示:图2:插件的遍历程序3:a1.cpp:#includeiostreamusingnamespacestd;constintFUNC_ID=1;externCvoidf(){coutHelloWorld!endl;}externCvoidHelp(){coutFunctionIDFUNC_ID:ThisfunctionprintsHelloWorld.endl;}a2.cpp:#includeiostreamusingnamespacestd;constintFUNC_ID=2;externCvoidf(){coutHelloChina!endl;}externCvoidHelp(){coutFunctionIDFUNC_IDThisfunctionprintshellochina.endl;}CPluginEnumerator.cpp#includeCPluginEnumerator.h#includedirent.h#includestring.h#includeiostream#includestdio.hCPluginEnumerator::CPluginEnumerator(){}CPluginEnumerator::~CPluginEnumerator(){}boolCPluginEnumerator::GetPluginNames(vectorstring&vstrPluginNames){DIR*dir=opendir(/root/test/test4/plug3/plugin);if(dir==0)returnfalse;for(;;){structdirent*ptr=readdir(dir);if(ptr==0)break;if((strcmp(ptr-d_name,.)==0)||(strcmp(ptr-d_name,..)==0))continue;charpath[260];sprintf(path,/root/test/test4/plug3/plugin/%s,ptr-d_name);vstrPluginNames.push_back(path);}closedir(dir);returntrue;}Test.cpp:#includedlfcn.h#includeiostream#includeCPluginEnumerator.h#includestring.h#includestdio.husingnamespacestd;intmain(intargc,char**argv){charpath[260];if(argc!=2)return0;if(strcmp(argv[1],help)==0){vectorstringvstrPluginNames;CPluginEnumeratorenumerator;if(!enumerator.GetPluginNames(vstrPluginNames)){coutGetPluginNameserrorendl;return0;}for(inti=0;ivstrPluginNames.size();i++){void*handle=dlopen(vstrPluginNames[i].c_str(),RTLD_LAZY);if(handle==0){coutdlopenerrorendl;return0;}typedefvoid(*FUNC_HELP)();FUNC_HELPdl_help=(FUNC_HELP)dlsym(handle,Help);if(dl_help==0){coutdlsymerrorendl;return0;}(dl_help)();dlclose(handle);}}elseif(strcmp(argv[1],1)==0){sprintf(path,/root/test/test4/plug3/plugin/%s,a1.so);void*handle=dlopen(path,RTLD_LAZY);if(handle==0){coutdlopenerrorendl;return0;}typedefvoid(*FUNC_PRINT)();FUNC_PRINTdl_print=(FUNC_PRINT)dlsym(handle,f);if(dl_print==0){coutdlsymerrorendl;return0;}(dl_print)();dlclose(handle);}elseif(strcmp(argv[1],2)==0)//得到第二个func的参数{sprintf(path,/root/test/test4/plug3/plugin/%s,a2.so);void*handle=dlopen(path,RTLD_LAZY);if(handle==0){coutdlopenerrorendl;return0;}typedefvoid(*FUNC_PRINT)();FUNC_PRINTdl_print=(FUNC_PRINT)dlsym(handle,f);if(dl_print==0){coutdlsymerrorendl;return0;}(dl_print)();dlclose(handle);}return0;}程序运行结果如图3所示:图3:插件输出程序4:CPluginEnumerator.h:#ifndefCPLUGINENUMERATOR_H#defineCPLUGINENUMERATOR_H#includevector#includestringusingnamespacestd;classCPluginEnumerator{public:CPluginEnumerator();virtual~CPluginEnumerator();boolGetPluginNames(vectorstring&vstrPluginNames);};#endifCPluginEnumerator.cpp:#includeCPluginEnumerator.h#includedirent.h#includestring.hCPluginEnumerator::CPluginEnumerator(){}CPluginEnumerator::~CPluginEnumerator(){}boolCPluginEnumerator::GetPluginNames(vectorstring&vstrPluginNames){D
本文标题:Linux插件框架实验报告
链接地址:https://www.777doc.com/doc-4903671 .html