~markwright/scalestack/zeromq

« back to all changes in this revision

Viewing changes to ScaleStack/Module.cc

  • Committer: Eric Day
  • Date: 2010-02-21 10:36:03 UTC
  • Revision ID: eday@oddments.org-20100221103603-u0agc1fsduqhl728
Initial commit with build system and basic module loading.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Scale Stack
 
3
 *
 
4
 * Copyright (C) 2010 Eric Day (eday@oddments.org)
 
5
 * All rights reserved.
 
6
 *
 
7
 * Use and distribution licensed under the BSD license. See the
 
8
 * COPYING file in the root project directory for full text.
 
9
 */
 
10
 
 
11
/**
 
12
 * @file
 
13
 * @brief Module Definitions
 
14
 */
 
15
 
 
16
#include "config.h"
 
17
 
 
18
#include <dlfcn.h>
 
19
 
 
20
#include <ScaleStack/Module.h>
 
21
#include <ScaleStack/Kernel.h>
 
22
 
 
23
namespace ScaleStack
 
24
{
 
25
 
 
26
Module::Module(const std::string& name_arg):
 
27
  name(name_arg),
 
28
  pathname(),
 
29
  handle(NULL),
 
30
  definition(NULL),
 
31
  canonical_name(),
 
32
  version(),
 
33
  author(),
 
34
  title(),
 
35
  license(),
 
36
  dependencies()
 
37
{
 
38
}
 
39
 
 
40
Module::~Module()
 
41
{
 
42
  if (handle != NULL)
 
43
    dlclose(handle);
 
44
}
 
45
 
 
46
const std::string& Module::getName(void) const
 
47
{
 
48
  return name;
 
49
}
 
50
 
 
51
const std::string& Module::getPathname(void) const
 
52
{
 
53
  return pathname;
 
54
}
 
55
 
 
56
const std::string& Module::getCanonicalName(void) const
 
57
{
 
58
  return canonical_name;
 
59
}
 
60
 
 
61
const std::string& Module::getVersion(void) const
 
62
{
 
63
  return version;
 
64
}
 
65
 
 
66
const std::string& Module::getAuthor(void) const
 
67
{
 
68
  return author;
 
69
}
 
70
 
 
71
const std::string& Module::getTitle(void) const
 
72
{
 
73
  return title;
 
74
}
 
75
 
 
76
const std::string& Module::getLicense(void) const
 
77
{
 
78
  return license;
 
79
}
 
80
 
 
81
const std::vector<std::string>& Module::getDependencies(void) const
 
82
{
 
83
  return dependencies;
 
84
}
 
85
 
 
86
bool Module::checkName(const std::string& check_name) const
 
87
{
 
88
  return name == check_name || canonical_name == check_name;
 
89
}
 
90
 
 
91
void Module::load(void)
 
92
{
 
93
  if (handle != NULL)
 
94
    return;
 
95
 
 
96
  pathname = "ScaleStack/.libs/" + name + ".so";
 
97
  handle = dlopen(pathname.c_str(), RTLD_NOW | RTLD_GLOBAL);
 
98
  if (handle == NULL)
 
99
    throw DLOpenFailed();
 
100
 
 
101
  definition = static_cast<Module::Definition*>(dlsym(handle, "_scalestack_definition"));
 
102
  if (definition == NULL)
 
103
    throw DLSymFailed();
 
104
 
 
105
  canonical_name = definition->name;
 
106
  version = definition->version;
 
107
  author = definition->author;
 
108
  title = definition->title;
 
109
  license = definition->license;
 
110
 
 
111
  for (const char** dependency = definition->dependencies;
 
112
       *dependency != NULL;
 
113
       dependency++)
 
114
  {
 
115
    dependencies.push_back(*dependency);
 
116
  }
 
117
 
 
118
  definition->options();
 
119
}
 
120
 
 
121
void Module::unload(void)
 
122
{
 
123
  if (handle == NULL)
 
124
    return;
 
125
 
 
126
  if (dlclose(handle) != 0)
 
127
    throw DLCloseFailed();
 
128
 
 
129
  definition = NULL;
 
130
  handle = NULL;
 
131
}
 
132
 
 
133
ScaleStackExceptionMessage(DLOpenFailed, dlerror())
 
134
ScaleStackExceptionMessage(DLSymFailed, dlerror())
 
135
ScaleStackExceptionMessage(DLCloseFailed, dlerror())
 
136
 
 
137
} /* namespace ScaleStack */