版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、電 子 科 技 大 學實 驗 報 告學生姓名: 學 號: 指導教師:實驗地點:A2-412 一、實驗室名稱:Linux環(huán)境高級編程實驗室二、實驗項目名稱:插件框架實驗三、實驗學時:4學時四、實驗目的:學習和實踐插件框架的開發(fā)。理解插件的工作原理,為進一步開發(fā)高可用,高復雜度的插件打下基礎。五、實驗內(nèi)容:1、使用動態(tài)鏈接庫實現(xiàn)打印功能:開發(fā)一個程序,向屏幕打印“Hello World”;在不重新編譯鏈接原程序的前提下,將打印的文字改為“Hello China”。2、使用動態(tài)鏈接庫實現(xiàn)自定義打印功能:同時要打印“Hello World”,打印“Hello China”,甚至同時打印未來才會增加的其
2、他打印信息,打印信息的鏈接庫放在一個固定目錄中,遍歷這個目錄,獲取所有動態(tài)鏈接庫。打印未來的這些信息,也不能重新編譯鏈接原程序。3、1)通過命令行方式:./a.out help,輸出所有插件實現(xiàn)的功能ID,以及該功能ID對應的功能描述。2)通過命令行方式:./a.out FuncID,調(diào)用具體打印功能(每個插件導出GetID接口)。4、將插件導出的Print、GetID、Help三個函數(shù)放在一個類中,主程序需要使用多個容器分別保存這些函數(shù)地址,讓插件外部獲取該類的對象。綜合練習:實現(xiàn)一個軟件系統(tǒng),該系統(tǒng)可對文件進行統(tǒng)計分析。究竟對文件進行什么樣的統(tǒng)計分析,最終用戶自己也不是很清楚,目前只知道需
3、要統(tǒng)計文件的行數(shù)。也就是說,本軟件系統(tǒng)將會隨時面臨,增加文件統(tǒng)計新功能的難題。請結(jié)合本實驗內(nèi)容,設計并實現(xiàn)這樣一個面向文件統(tǒng)計功能的插件系統(tǒng)。(需要實現(xiàn)的插件包括:統(tǒng)計某個文件的行數(shù),統(tǒng)計某個文件的字節(jié)數(shù))六、實驗步驟:程序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;程序運行結(jié)果如圖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);程序運行結(jié)果如圖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的參數(shù)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;程序運行結(jié)果如圖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;程序運行結(jié)果如圖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)/執(zhí)行插件功能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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 廣東外語外貿(mào)大學《植物生理學實驗》2023-2024學年第一學期期末試卷
- 廣東體育職業(yè)技術學院《工程流體力學》2023-2024學年第一學期期末試卷
- 廣東南華工商職業(yè)學院《色彩造型3(頭像)》2023-2024學年第一學期期末試卷
- 廣東梅州職業(yè)技術學院《家具與陳設設計》2023-2024學年第一學期期末試卷
- 【原創(chuàng)】江蘇省建陵高級中學2013-2020學年高一寒假作業(yè)政治試題
- 2025年七年級統(tǒng)編版語文寒假預習 第03講 賣油翁
- 【志鴻優(yōu)化設計】2020高考地理(人教版)一輪教學案:第1章-第2講宇宙中的地球和太陽對地球的影響
- 【優(yōu)化方案】2022屆高三物理大一輪復習-第2章-第2節(jié)-力的合成與分解-教學講義-
- 遼寧省鞍山市海城市西部集團2024-2025學年九年級上學期1月期末質(zhì)量檢測化學試題(含答案)
- 2021高三政治易錯點點睛與突破:專題08-三大產(chǎn)業(yè)
- 四年級上冊豎式計算300題及答案
- 個人住房質(zhì)押擔保借款合同書范本(3篇)
- 亞馬遜品牌授權書(英文模板)
- DB52∕T 046-2018 貴州省建筑巖土工程技術規(guī)范
- 醫(yī)療電子票據(jù)管理系統(tǒng)建設方案
- 火箭發(fā)動機課件-
- 人教版小學六年級數(shù)學上冊教學反思(46篇)
- atv61變頻器中文手冊
- 農(nóng)業(yè)機械維修業(yè)開業(yè)技術條件
- 主要零部件的設計和強度校核參考
- 銅錢壩樞紐及溢洪道設計設計
評論
0/150
提交評論