-
Notifications
You must be signed in to change notification settings - Fork 1
/
matrixmanager.cpp
78 lines (78 loc) · 1.58 KB
/
matrixmanager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "matrixmanager.h"
#include "log.h"
#include <dlfcn.h>
#include <cassert>
using namespace std;
int MatrixManager::loadMatrix(const char *name, Postable* receiver, void *param)
{
Matrix *matrix;
MatrixFactory *factory;
if(mMatrix)
return -1;
string path = name;
map<string, MatrixFactory*>::iterator i = mFactories.find(path);
if(i!=mFactories.end())
{
factory = i->second;
assert(factory);
}
else
{
ModuleInfo *info = new ModuleInfo;
path = mModulePath + name;
path += ".xml";
try {
info->open(path.c_str());
}
catch(runtime_error e)
{
ErrLog("Failed to parse matrix module infomation file "<<path<<" error message:"<<e.what());
delete info;
return -1;
}
if(info->type() != ModuleInfo::MatrixModule)
{
ErrLog("Module "<<path<<" is not a matrix");
delete info;
return -1;
}
InfoLog("Matrix Module Infomation:"<<endl<<*info);
path = mModulePath + name;
path += ".so";
void *handle = dlopen(path.c_str(), RTLD_LAZY | RTLD_NODELETE);
if(!handle)
{
delete info;
ErrLog("Failed to open matrix module "<<path<<" error message:"<<dlerror());
return -1;
}
factory = (MatrixFactory *)dlsym(handle, "_factory");
dlclose(handle);
if(!factory)
{
delete info;
ErrLog("Matrix ["<<path<<"] factory not found");
return -1;
}
factory->setInfo(info);
}
matrix = factory->create(receiver);
if(!matrix)
{
return -1;
}
mMatrix = matrix;
return matrix->start(param);
}
void MatrixManager::unloadMatrix()
{
if(mMatrix)
{
mMatrix->stop();
mMatrix = NULL;
}
}
Matrix* MatrixManager::getMatrix()
{
return mMatrix;
}