mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-21 12:44:58 +02:00
Code cleanup
This commit is contained in:
parent
bf1e07b686
commit
69c30c264a
@ -47,6 +47,8 @@ libbase_la_SOURCES = \
|
|||||||
timer.h \
|
timer.h \
|
||||||
unix.cpp \
|
unix.cpp \
|
||||||
unix.h \
|
unix.h \
|
||||||
|
utility.cpp \
|
||||||
|
utility.h \
|
||||||
variant.cpp \
|
variant.cpp \
|
||||||
variant.h \
|
variant.h \
|
||||||
win32.cpp \
|
win32.cpp \
|
||||||
|
@ -31,10 +31,8 @@ Application::Application(void)
|
|||||||
|
|
||||||
Application::~Application(void)
|
Application::~Application(void)
|
||||||
{
|
{
|
||||||
Timer::StopAllTimers();
|
for (map<string, Component::Ptr>::iterator i = m_Components.begin();
|
||||||
Socket::CloseAllSockets();
|
i != m_Components.end(); i++) {
|
||||||
|
|
||||||
for (map<string, Component::Ptr>::iterator i = m_Components.begin(); i != m_Components.end(); i++) {
|
|
||||||
i->second->Stop();
|
i->second->Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,7 +58,8 @@ void Application::RunEventLoop(void)
|
|||||||
FD_ZERO(&exceptfds);
|
FD_ZERO(&exceptfds);
|
||||||
|
|
||||||
Socket::CollectionType::iterator prev, i;
|
Socket::CollectionType::iterator prev, i;
|
||||||
for (i = Socket::Sockets.begin(); i != Socket::Sockets.end(); ) {
|
for (i = Socket::Sockets.begin();
|
||||||
|
i != Socket::Sockets.end(); ) {
|
||||||
Socket::Ptr socket = i->lock();
|
Socket::Ptr socket = i->lock();
|
||||||
|
|
||||||
prev = i;
|
prev = i;
|
||||||
@ -102,7 +101,8 @@ void Application::RunEventLoop(void)
|
|||||||
Sleep(tv.tv_sec * 1000 + tv.tv_usec);
|
Sleep(tv.tv_sec * 1000 + tv.tv_usec);
|
||||||
ready = 0;
|
ready = 0;
|
||||||
} else
|
} else
|
||||||
ready = select(nfds + 1, &readfds, &writefds, &exceptfds, &tv);
|
ready = select(nfds + 1, &readfds, &writefds,
|
||||||
|
&exceptfds, &tv);
|
||||||
|
|
||||||
if (ready < 0)
|
if (ready < 0)
|
||||||
break;
|
break;
|
||||||
@ -112,7 +112,8 @@ void Application::RunEventLoop(void)
|
|||||||
EventArgs ea;
|
EventArgs ea;
|
||||||
ea.Source = shared_from_this();
|
ea.Source = shared_from_this();
|
||||||
|
|
||||||
for (i = Socket::Sockets.begin(); i != Socket::Sockets.end(); ) {
|
for (i = Socket::Sockets.begin();
|
||||||
|
i != Socket::Sockets.end(); ) {
|
||||||
Socket::Ptr socket = i->lock();
|
Socket::Ptr socket = i->lock();
|
||||||
|
|
||||||
prev = i;
|
prev = i;
|
||||||
@ -123,75 +124,35 @@ void Application::RunEventLoop(void)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
int fd = socket->GetFD();
|
int fd;
|
||||||
|
|
||||||
if (FD_ISSET(fd, &writefds) && socket->GetFD() != INVALID_SOCKET)
|
fd = socket->GetFD();
|
||||||
|
if (fd != INVALID_SOCKET && FD_ISSET(fd, &writefds))
|
||||||
socket->OnWritable(ea);
|
socket->OnWritable(ea);
|
||||||
|
|
||||||
if (FD_ISSET(fd, &readfds) && socket->GetFD() != INVALID_SOCKET)
|
fd = socket->GetFD();
|
||||||
|
if (fd != INVALID_SOCKET && FD_ISSET(fd, &readfds))
|
||||||
socket->OnReadable(ea);
|
socket->OnReadable(ea);
|
||||||
|
|
||||||
if (FD_ISSET(fd, &exceptfds) && socket->GetFD() != INVALID_SOCKET)
|
fd = socket->GetFD();
|
||||||
|
if (fd != INVALID_SOCKET && FD_ISSET(fd, &exceptfds))
|
||||||
socket->OnException(ea);
|
socket->OnException(ea);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Application::Daemonize(void) {
|
|
||||||
#ifndef _WIN32
|
|
||||||
pid_t pid;
|
|
||||||
pid_t sid;
|
|
||||||
int fd;
|
|
||||||
|
|
||||||
pid = fork();
|
|
||||||
if (pid == -1) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pid) {
|
|
||||||
fprintf(stdout, "DONE\n");
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
fd = open("/dev/null", O_RDWR);
|
|
||||||
if (fd) {
|
|
||||||
if (fd != 0) {
|
|
||||||
dup2(fd, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fd != 1) {
|
|
||||||
dup2(fd, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fd != 2) {
|
|
||||||
dup2(fd, 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fd > 2) {
|
|
||||||
close(fd);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sid = setsid();
|
|
||||||
if (sid == -1) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Application::Shutdown(void)
|
void Application::Shutdown(void)
|
||||||
{
|
{
|
||||||
m_ShuttingDown = true;
|
m_ShuttingDown = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
ConfigHive::Ptr Application::GetConfigHive(void)
|
ConfigHive::Ptr Application::GetConfigHive(void) const
|
||||||
{
|
{
|
||||||
return m_ConfigHive;
|
return m_ConfigHive;
|
||||||
}
|
}
|
||||||
|
|
||||||
Component::Ptr Application::LoadComponent(const string& path, const ConfigObject::Ptr& componentConfig)
|
Component::Ptr Application::LoadComponent(const string& path,
|
||||||
|
const ConfigObject::Ptr& componentConfig)
|
||||||
{
|
{
|
||||||
Component::Ptr component;
|
Component::Ptr component;
|
||||||
Component *(*pCreateComponent)();
|
Component *(*pCreateComponent)();
|
||||||
@ -208,13 +169,16 @@ Component::Ptr Application::LoadComponent(const string& path, const ConfigObject
|
|||||||
throw ComponentLoadException("Could not load module");
|
throw ComponentLoadException("Could not load module");
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
pCreateComponent = (Component *(*)())GetProcAddress(hModule, "CreateComponent");
|
pCreateComponent = (CreateComponentFunction)GetProcAddress(hModule,
|
||||||
|
"CreateComponent");
|
||||||
#else /* _WIN32 */
|
#else /* _WIN32 */
|
||||||
pCreateComponent = (Component *(*)())lt_dlsym(hModule, "CreateComponent");
|
pCreateComponent = (CreateComponentFunction)lt_dlsym(hModule,
|
||||||
|
"CreateComponent");
|
||||||
#endif /* _WIN32 */
|
#endif /* _WIN32 */
|
||||||
|
|
||||||
if (pCreateComponent == NULL)
|
if (pCreateComponent == NULL)
|
||||||
throw ComponentLoadException("Loadable module does not contain CreateComponent function");
|
throw ComponentLoadException("Loadable module does not "
|
||||||
|
"contain CreateComponent function");
|
||||||
|
|
||||||
component = Component::Ptr(pCreateComponent());
|
component = Component::Ptr(pCreateComponent());
|
||||||
component->SetConfig(componentConfig);
|
component->SetConfig(componentConfig);
|
||||||
@ -270,12 +234,12 @@ void Application::SetArguments(const vector<string>& arguments)
|
|||||||
m_Arguments = arguments;
|
m_Arguments = arguments;
|
||||||
}
|
}
|
||||||
|
|
||||||
const vector<string>& Application::GetArguments(void)
|
const vector<string>& Application::GetArguments(void) const
|
||||||
{
|
{
|
||||||
return m_Arguments;
|
return m_Arguments;
|
||||||
}
|
}
|
||||||
|
|
||||||
const string& Application::GetExeDirectory(void)
|
string Application::GetExeDirectory(void) const
|
||||||
{
|
{
|
||||||
static string ExePath;
|
static string ExePath;
|
||||||
|
|
||||||
@ -364,24 +328,19 @@ bool Application::IsDebugging(void) const
|
|||||||
return m_Debugging;
|
return m_Debugging;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Application::SigIntHandler(int signum)
|
#ifndef _WIN32
|
||||||
|
static void ApplicationSigIntHandler(int signum)
|
||||||
{
|
{
|
||||||
Application::Instance->Shutdown();
|
Application::Instance->Shutdown();
|
||||||
|
|
||||||
#ifndef _WIN32
|
|
||||||
struct sigaction sa;
|
struct sigaction sa;
|
||||||
memset(&sa, 0, sizeof(sa));
|
memset(&sa, 0, sizeof(sa));
|
||||||
sa.sa_handler = SIG_DFL;
|
sa.sa_handler = SIG_DFL;
|
||||||
sigaction(SIGINT, &sa, NULL);
|
sigaction(SIGINT, &sa, NULL);
|
||||||
|
}
|
||||||
#endif /* _WIN32 */
|
#endif /* _WIN32 */
|
||||||
}
|
|
||||||
|
|
||||||
static void application_sigint_handler(int signum)
|
int icinga::RunApplication(int argc, char **argv, Application *instance)
|
||||||
{
|
|
||||||
Application::Instance->SigIntHandler(signum);
|
|
||||||
}
|
|
||||||
|
|
||||||
int application_main(int argc, char **argv, Application *instance)
|
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
@ -390,7 +349,7 @@ int application_main(int argc, char **argv, Application *instance)
|
|||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
struct sigaction sa;
|
struct sigaction sa;
|
||||||
memset(&sa, 0, sizeof(sa));
|
memset(&sa, 0, sizeof(sa));
|
||||||
sa.sa_handler = application_sigint_handler;
|
sa.sa_handler = ApplicationSigIntHandler;
|
||||||
sigaction(SIGINT, &sa, NULL);
|
sigaction(SIGINT, &sa, NULL);
|
||||||
#endif /* _WIN32 */
|
#endif /* _WIN32 */
|
||||||
|
|
||||||
@ -408,29 +367,12 @@ int application_main(int argc, char **argv, Application *instance)
|
|||||||
result = Application::Instance->Main(args);
|
result = Application::Instance->Main(args);
|
||||||
} catch (const Exception& ex) {
|
} catch (const Exception& ex) {
|
||||||
cerr << "---" << endl;
|
cerr << "---" << endl;
|
||||||
|
cerr << "Exception: " << Utility::GetTypeName(ex) << endl;
|
||||||
string klass = typeid(ex).name();
|
|
||||||
|
|
||||||
#ifdef HAVE_GCC_ABI_DEMANGLE
|
|
||||||
int status;
|
|
||||||
char *realname = abi::__cxa_demangle(klass.c_str(), 0, 0, &status);
|
|
||||||
|
|
||||||
if (realname != NULL) {
|
|
||||||
klass = string(realname);
|
|
||||||
free(realname);
|
|
||||||
}
|
|
||||||
#endif /* HAVE_GCC_ABI_DEMANGLE */
|
|
||||||
|
|
||||||
cerr << "Exception: " << klass << endl;
|
|
||||||
cerr << "Message: " << ex.GetMessage() << endl;
|
cerr << "Message: " << ex.GetMessage() << endl;
|
||||||
|
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Application::Instance.reset();
|
|
||||||
|
|
||||||
assert(Object::ActiveObjects == 0);
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,10 @@ private:
|
|||||||
vector<string> m_Arguments;
|
vector<string> m_Arguments;
|
||||||
bool m_Debugging;
|
bool m_Debugging;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void RunEventLoop(void);
|
||||||
|
string GetExeDirectory(void) const;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
typedef shared_ptr<Application> Ptr;
|
typedef shared_ptr<Application> Ptr;
|
||||||
typedef weak_ptr<Application> WeakPtr;
|
typedef weak_ptr<Application> WeakPtr;
|
||||||
@ -27,36 +31,32 @@ public:
|
|||||||
virtual int Main(const vector<string>& args) = 0;
|
virtual int Main(const vector<string>& args) = 0;
|
||||||
|
|
||||||
void SetArguments(const vector<string>& arguments);
|
void SetArguments(const vector<string>& arguments);
|
||||||
const vector<string>& GetArguments(void);
|
const vector<string>& GetArguments(void) const;
|
||||||
|
|
||||||
void RunEventLoop(void);
|
|
||||||
bool Daemonize(void);
|
|
||||||
void Shutdown(void);
|
void Shutdown(void);
|
||||||
|
|
||||||
void Log(const char *format, ...);
|
void Log(const char *format, ...);
|
||||||
|
|
||||||
ConfigHive::Ptr GetConfigHive(void);
|
ConfigHive::Ptr GetConfigHive(void) const;
|
||||||
|
|
||||||
shared_ptr<Component> LoadComponent(const string& path, const ConfigObject::Ptr& componentConfig);
|
shared_ptr<Component> LoadComponent(const string& path,
|
||||||
|
const ConfigObject::Ptr& componentConfig);
|
||||||
void RegisterComponent(shared_ptr<Component> component);
|
void RegisterComponent(shared_ptr<Component> component);
|
||||||
void UnregisterComponent(shared_ptr<Component> component);
|
void UnregisterComponent(shared_ptr<Component> component);
|
||||||
shared_ptr<Component> GetComponent(const string& name);
|
shared_ptr<Component> GetComponent(const string& name);
|
||||||
void AddComponentSearchDir(const string& componentDirectory);
|
void AddComponentSearchDir(const string& componentDirectory);
|
||||||
|
|
||||||
const string& GetExeDirectory(void);
|
|
||||||
|
|
||||||
bool IsDebugging(void) const;
|
bool IsDebugging(void) const;
|
||||||
void SigIntHandler(int signum);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
int I2_EXPORT RunApplication(int argc, char **argv, Application *instance);
|
||||||
|
|
||||||
int I2_EXPORT application_main(int argc, char **argv, icinga::Application *instance);
|
}
|
||||||
|
|
||||||
#define IMPLEMENT_ENTRY_POINT(klass) \
|
#define IMPLEMENT_ENTRY_POINT(klass) \
|
||||||
int main(int argc, char **argv) { \
|
int main(int argc, char **argv) { \
|
||||||
klass *instance = new klass(); \
|
klass *instance = new klass(); \
|
||||||
return application_main(argc, argv, instance); \
|
return icinga::RunApplication(argc, argv, instance); \
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* APPLICATION_H */
|
#endif /* APPLICATION_H */
|
||||||
|
@ -30,6 +30,7 @@
|
|||||||
<ClCompile Include="thread.cpp" />
|
<ClCompile Include="thread.cpp" />
|
||||||
<ClCompile Include="timer.cpp" />
|
<ClCompile Include="timer.cpp" />
|
||||||
<ClCompile Include="unix.cpp" />
|
<ClCompile Include="unix.cpp" />
|
||||||
|
<ClCompile Include="utility.cpp" />
|
||||||
<ClCompile Include="variant.cpp" />
|
<ClCompile Include="variant.cpp" />
|
||||||
<ClCompile Include="win32.cpp" />
|
<ClCompile Include="win32.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
@ -56,6 +57,7 @@
|
|||||||
<ClInclude Include="thread.h" />
|
<ClInclude Include="thread.h" />
|
||||||
<ClInclude Include="timer.h" />
|
<ClInclude Include="timer.h" />
|
||||||
<ClInclude Include="unix.h" />
|
<ClInclude Include="unix.h" />
|
||||||
|
<ClInclude Include="utility.h" />
|
||||||
<ClInclude Include="variant.h" />
|
<ClInclude Include="variant.h" />
|
||||||
<ClInclude Include="win32.h" />
|
<ClInclude Include="win32.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
@ -25,6 +25,8 @@ public:
|
|||||||
virtual void Stop(void) = 0;
|
virtual void Stop(void) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef Component *(*CreateComponentFunction)(void);
|
||||||
|
|
||||||
#define EXPORT_COMPONENT(klass) \
|
#define EXPORT_COMPONENT(klass) \
|
||||||
extern "C" I2_EXPORT icinga::Component *CreateComponent(void) \
|
extern "C" I2_EXPORT icinga::Component *CreateComponent(void) \
|
||||||
{ \
|
{ \
|
||||||
|
@ -4,6 +4,11 @@
|
|||||||
namespace icinga
|
namespace icinga
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CondVar
|
||||||
|
*
|
||||||
|
* A wrapper around OS-specific condition variable functionality.
|
||||||
|
*/
|
||||||
class I2_BASE_API CondVar
|
class I2_BASE_API CondVar
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
@ -29,7 +29,7 @@ public:
|
|||||||
|
|
||||||
Event<EventArgs> OnObjectCreated;
|
Event<EventArgs> OnObjectCreated;
|
||||||
Event<EventArgs> OnObjectRemoved;
|
Event<EventArgs> OnObjectRemoved;
|
||||||
Event<DictionaryPropertyChangedEventArgs> OnPropertyChanged;
|
Event<PropertyChangedEventArgs> OnPropertyChanged;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,8 @@ ConfigCollection::Ptr ConfigHive::GetCollection(const string& collection)
|
|||||||
return ci->second;
|
return ci->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConfigHive::ForEachObject(const string& type, function<int (const EventArgs&)> callback)
|
void ConfigHive::ForEachObject(const string& type,
|
||||||
|
function<int (const EventArgs&)> callback)
|
||||||
{
|
{
|
||||||
CollectionIterator ci = Collections.find(type);
|
CollectionIterator ci = Collections.find(type);
|
||||||
|
|
||||||
|
@ -15,14 +15,16 @@ public:
|
|||||||
|
|
||||||
void AddObject(const ConfigObject::Ptr& object);
|
void AddObject(const ConfigObject::Ptr& object);
|
||||||
void RemoveObject(const ConfigObject::Ptr& object);
|
void RemoveObject(const ConfigObject::Ptr& object);
|
||||||
ConfigObject::Ptr GetObject(const string& collection, const string& name = string());
|
ConfigObject::Ptr GetObject(const string& collection,
|
||||||
|
const string& name = string());
|
||||||
ConfigCollection::Ptr GetCollection(const string& collection);
|
ConfigCollection::Ptr GetCollection(const string& collection);
|
||||||
|
|
||||||
void ForEachObject(const string& type, function<int (const EventArgs&)> callback);
|
void ForEachObject(const string& type,
|
||||||
|
function<int (const EventArgs&)> callback);
|
||||||
|
|
||||||
Event<EventArgs> OnObjectCreated;
|
Event<EventArgs> OnObjectCreated;
|
||||||
Event<EventArgs> OnObjectRemoved;
|
Event<EventArgs> OnObjectRemoved;
|
||||||
Event<DictionaryPropertyChangedEventArgs> OnPropertyChanged;
|
Event<PropertyChangedEventArgs> OnPropertyChanged;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,7 @@ bool ConfigObject::GetReplicated(void) const
|
|||||||
return m_Replicated;
|
return m_Replicated;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ConfigObject::PropertyChangedHandler(const DictionaryPropertyChangedEventArgs dpcea)
|
int ConfigObject::PropertyChangedHandler(const PropertyChangedEventArgs& dpcea)
|
||||||
{
|
{
|
||||||
ConfigHive::Ptr hive = m_Hive.lock();
|
ConfigHive::Ptr hive = m_Hive.lock();
|
||||||
if (hive) {
|
if (hive) {
|
||||||
|
@ -17,7 +17,7 @@ private:
|
|||||||
string m_Type;
|
string m_Type;
|
||||||
bool m_Replicated;
|
bool m_Replicated;
|
||||||
|
|
||||||
int PropertyChangedHandler(const DictionaryPropertyChangedEventArgs dpcea);
|
int PropertyChangedHandler(const PropertyChangedEventArgs& dpcea);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
typedef shared_ptr<ConfigObject> Ptr;
|
typedef shared_ptr<ConfigObject> Ptr;
|
||||||
|
@ -18,7 +18,7 @@ int delegate_fwd(int (TObject::*function)(TArgs), weak_ptr<TObject> wref, const
|
|||||||
template<class TObject, class TArgs>
|
template<class TObject, class TArgs>
|
||||||
function<int (TArgs)> bind_weak(int (TObject::*function)(TArgs), const weak_ptr<TObject>& wref)
|
function<int (TArgs)> bind_weak(int (TObject::*function)(TArgs), const weak_ptr<TObject>& wref)
|
||||||
{
|
{
|
||||||
return bind<int>(delegate_fwd<TObject, TArgs>, function, wref, _1);
|
return bind(delegate_fwd<TObject, TArgs>, function, wref, _1);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class TObject, class TArgs>
|
template<class TObject, class TArgs>
|
||||||
|
@ -25,7 +25,7 @@ void Dictionary::SetProperty(string key, const Variant& value)
|
|||||||
|
|
||||||
m_Data[key] = value;
|
m_Data[key] = value;
|
||||||
|
|
||||||
DictionaryPropertyChangedEventArgs dpce;
|
PropertyChangedEventArgs dpce;
|
||||||
dpce.Source = shared_from_this();
|
dpce.Source = shared_from_this();
|
||||||
dpce.Property = key;
|
dpce.Property = key;
|
||||||
dpce.OldValue = oldValue;
|
dpce.OldValue = oldValue;
|
||||||
|
@ -7,7 +7,7 @@ namespace icinga
|
|||||||
typedef map<string, Variant>::const_iterator ConstDictionaryIterator;
|
typedef map<string, Variant>::const_iterator ConstDictionaryIterator;
|
||||||
typedef map<string, Variant>::iterator DictionaryIterator;
|
typedef map<string, Variant>::iterator DictionaryIterator;
|
||||||
|
|
||||||
struct I2_BASE_API DictionaryPropertyChangedEventArgs : public EventArgs
|
struct I2_BASE_API PropertyChangedEventArgs : public EventArgs
|
||||||
{
|
{
|
||||||
string Property;
|
string Property;
|
||||||
Variant OldValue;
|
Variant OldValue;
|
||||||
@ -41,7 +41,7 @@ public:
|
|||||||
DictionaryIterator Begin(void);
|
DictionaryIterator Begin(void);
|
||||||
DictionaryIterator End(void);
|
DictionaryIterator End(void);
|
||||||
|
|
||||||
Event<DictionaryPropertyChangedEventArgs> OnPropertyChanged;
|
Event<PropertyChangedEventArgs> OnPropertyChanged;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
18
base/event.h
18
base/event.h
@ -16,34 +16,24 @@ public:
|
|||||||
typedef function<int (const TArgs&)> DelegateType;
|
typedef function<int (const TArgs&)> DelegateType;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
list<DelegateType> m_Delegates;
|
vector<DelegateType> m_Delegates;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void Hook(const DelegateType& delegate)
|
|
||||||
{
|
|
||||||
m_Delegates.push_front(delegate);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Unhook(const DelegateType& delegate)
|
|
||||||
{
|
|
||||||
m_Delegates.remove(delegate);
|
|
||||||
}
|
|
||||||
|
|
||||||
Event<TArgs>& operator +=(const DelegateType& rhs)
|
Event<TArgs>& operator +=(const DelegateType& rhs)
|
||||||
{
|
{
|
||||||
Hook(rhs);
|
m_Delegates.push_back(rhs);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Event<TArgs>& operator -=(const DelegateType& rhs)
|
Event<TArgs>& operator -=(const DelegateType& rhs)
|
||||||
{
|
{
|
||||||
Unhook(rhs);
|
m_Delegates.erase(rhs);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void operator()(const TArgs& args)
|
void operator()(const TArgs& args)
|
||||||
{
|
{
|
||||||
typename list<DelegateType>::iterator prev, i;
|
typename vector<DelegateType>::iterator prev, i;
|
||||||
|
|
||||||
for (i = m_Delegates.begin(); i != m_Delegates.end(); ) {
|
for (i = m_Delegates.begin(); i != m_Delegates.end(); ) {
|
||||||
prev = i;
|
prev = i;
|
||||||
|
@ -8,14 +8,39 @@ Exception::Exception(void)
|
|||||||
|
|
||||||
Exception::Exception(const string& message)
|
Exception::Exception(const string& message)
|
||||||
{
|
{
|
||||||
m_Message = message;
|
SetMessage(message);
|
||||||
}
|
|
||||||
|
|
||||||
Exception::~Exception(void)
|
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|
||||||
string Exception::GetMessage(void) const
|
string Exception::GetMessage(void) const
|
||||||
{
|
{
|
||||||
return m_Message;
|
return m_Message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Exception::SetMessage(string message)
|
||||||
|
{
|
||||||
|
m_Message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
string Win32Exception::FormatErrorCode(int code)
|
||||||
|
{
|
||||||
|
char *message;
|
||||||
|
string result = "Unknown error.";
|
||||||
|
|
||||||
|
DWORD rc = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||||
|
FORMAT_MESSAGE_FROM_SYSTEM, NULL, code, 0, (char *)&message,
|
||||||
|
0, NULL);
|
||||||
|
|
||||||
|
if (rc != 0) {
|
||||||
|
result = string(message);
|
||||||
|
LocalFree(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
#endif /* _WIN32 */
|
||||||
|
|
||||||
|
string PosixException::FormatErrorCode(int code)
|
||||||
|
{
|
||||||
|
return strerror(code);
|
||||||
|
}
|
@ -4,19 +4,31 @@
|
|||||||
namespace icinga
|
namespace icinga
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exception
|
||||||
|
*
|
||||||
|
* Base class for all exceptions.
|
||||||
|
*/
|
||||||
class I2_BASE_API Exception
|
class I2_BASE_API Exception
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
string m_Message;
|
string m_Message;
|
||||||
|
|
||||||
public:
|
protected:
|
||||||
typedef shared_ptr<Exception> Ptr;
|
void SetMessage(string message);
|
||||||
typedef weak_ptr<Exception> WeakPtr;
|
|
||||||
|
|
||||||
|
public:
|
||||||
Exception(void);
|
Exception(void);
|
||||||
Exception(const string& message);
|
Exception(const string& message);
|
||||||
|
|
||||||
virtual ~Exception(void);
|
/**
|
||||||
|
* ~Exception
|
||||||
|
*
|
||||||
|
* Required for RTTI.
|
||||||
|
*/
|
||||||
|
virtual ~Exception(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
string GetMessage(void) const;
|
string GetMessage(void) const;
|
||||||
};
|
};
|
||||||
@ -25,9 +37,6 @@ public:
|
|||||||
class klass : public Exception \
|
class klass : public Exception \
|
||||||
{ \
|
{ \
|
||||||
public: \
|
public: \
|
||||||
typedef shared_ptr<klass> Ptr; \
|
|
||||||
typedef weak_ptr<klass> WeakPtr; \
|
|
||||||
\
|
|
||||||
inline klass(void) : Exception() \
|
inline klass(void) : Exception() \
|
||||||
{ \
|
{ \
|
||||||
} \
|
} \
|
||||||
@ -40,6 +49,30 @@ public:
|
|||||||
DEFINE_EXCEPTION_CLASS(NotImplementedException);
|
DEFINE_EXCEPTION_CLASS(NotImplementedException);
|
||||||
DEFINE_EXCEPTION_CLASS(InvalidArgumentException);
|
DEFINE_EXCEPTION_CLASS(InvalidArgumentException);
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
class Win32Exception : public Exception
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
inline Win32Exception(const string& message, int errorCode)
|
||||||
|
{
|
||||||
|
SetMessage(message + ": " + FormatErrorCode(errorCode));
|
||||||
|
}
|
||||||
|
|
||||||
|
static string FormatErrorCode(int code);
|
||||||
|
};
|
||||||
|
#endif /* _WIN32 */
|
||||||
|
|
||||||
|
class PosixException : public Exception
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
inline PosixException(const string& message, int errorCode)
|
||||||
|
{
|
||||||
|
SetMessage(message + ": " + FormatErrorCode(errorCode));
|
||||||
|
}
|
||||||
|
|
||||||
|
static string FormatErrorCode(int code);
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* EXCEPTION_H */
|
#endif /* EXCEPTION_H */
|
||||||
|
@ -67,6 +67,7 @@ using namespace std::tr1::placeholders;
|
|||||||
#include "mutex.h"
|
#include "mutex.h"
|
||||||
#include "condvar.h"
|
#include "condvar.h"
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
|
#include "utility.h"
|
||||||
#include "object.h"
|
#include "object.h"
|
||||||
#include "exception.h"
|
#include "exception.h"
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
|
@ -11,7 +11,7 @@ void *Memory::Allocate(size_t size)
|
|||||||
void *ptr = malloc(size);
|
void *ptr = malloc(size);
|
||||||
|
|
||||||
if (size != 0 && ptr == NULL)
|
if (size != 0 && ptr == NULL)
|
||||||
throw OutOfMemoryException();
|
throw OutOfMemoryException("malloc failed.");
|
||||||
|
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
@ -21,7 +21,7 @@ void *Memory::Reallocate(void *ptr, size_t size)
|
|||||||
void *new_ptr = realloc(ptr, size);
|
void *new_ptr = realloc(ptr, size);
|
||||||
|
|
||||||
if (size != 0 && new_ptr == NULL)
|
if (size != 0 && new_ptr == NULL)
|
||||||
throw OutOfMemoryException();
|
throw OutOfMemoryException("realloc failed.");
|
||||||
|
|
||||||
return new_ptr;
|
return new_ptr;
|
||||||
}
|
}
|
||||||
@ -31,7 +31,7 @@ char *Memory::StrDup(const char *str)
|
|||||||
char *new_str = strdup(str);
|
char *new_str = strdup(str);
|
||||||
|
|
||||||
if (str == NULL)
|
if (str == NULL)
|
||||||
throw OutOfMemoryException();
|
throw OutOfMemoryException("strdup failed.");
|
||||||
|
|
||||||
return new_str;
|
return new_str;
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,11 @@ namespace icinga
|
|||||||
|
|
||||||
DEFINE_EXCEPTION_CLASS(OutOfMemoryException);
|
DEFINE_EXCEPTION_CLASS(OutOfMemoryException);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Memory
|
||||||
|
*
|
||||||
|
* Singleton class which implements memory allocation helpers.
|
||||||
|
*/
|
||||||
class I2_BASE_API Memory
|
class I2_BASE_API Memory
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
@ -4,6 +4,11 @@
|
|||||||
namespace icinga
|
namespace icinga
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mutex
|
||||||
|
*
|
||||||
|
* A wrapper around OS-specific mutex functionality.
|
||||||
|
*/
|
||||||
class I2_BASE_API Mutex
|
class I2_BASE_API Mutex
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
@ -1,13 +1,3 @@
|
|||||||
#include "i2-base.h"
|
#include "i2-base.h"
|
||||||
|
|
||||||
using namespace icinga;
|
using namespace icinga;
|
||||||
|
|
||||||
unsigned long Object::ActiveObjects;
|
|
||||||
|
|
||||||
Object::Object(void) {
|
|
||||||
ActiveObjects++;
|
|
||||||
}
|
|
||||||
|
|
||||||
Object::~Object(void) {
|
|
||||||
ActiveObjects--;
|
|
||||||
}
|
|
||||||
|
@ -4,20 +4,29 @@
|
|||||||
namespace icinga
|
namespace icinga
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Object
|
||||||
|
*
|
||||||
|
* Base class for all heap-allocated objects. At least one of its methods
|
||||||
|
* has to be virtual for RTTI to work.
|
||||||
|
*/
|
||||||
class I2_BASE_API Object : public enable_shared_from_this<Object>
|
class I2_BASE_API Object : public enable_shared_from_this<Object>
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
Object(const Object& other);
|
Object(const Object& other);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Object(void);
|
inline Object(void)
|
||||||
virtual ~Object(void);
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
inline virtual ~Object(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
typedef shared_ptr<Object> Ptr;
|
typedef shared_ptr<Object> Ptr;
|
||||||
typedef weak_ptr<Object> WeakPtr;
|
typedef weak_ptr<Object> WeakPtr;
|
||||||
|
|
||||||
static unsigned long ActiveObjects;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
@ -37,6 +46,11 @@ public:
|
|||||||
|
|
||||||
typedef function<Object::Ptr ()> factory_function;
|
typedef function<Object::Ptr ()> factory_function;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* factory<T>
|
||||||
|
*
|
||||||
|
* Returns a new object of type T.
|
||||||
|
*/
|
||||||
template<class T>
|
template<class T>
|
||||||
Object::Ptr factory(void)
|
Object::Ptr factory(void)
|
||||||
{
|
{
|
||||||
|
@ -58,7 +58,8 @@ void Socket::Close(bool from_dtor)
|
|||||||
closesocket(m_FD);
|
closesocket(m_FD);
|
||||||
m_FD = INVALID_SOCKET;
|
m_FD = INVALID_SOCKET;
|
||||||
|
|
||||||
/* nobody can possibly have a valid event subscription when the destructor has been called */
|
/* nobody can possibly have a valid event subscription when the
|
||||||
|
destructor has been called */
|
||||||
if (!from_dtor) {
|
if (!from_dtor) {
|
||||||
EventArgs ea;
|
EventArgs ea;
|
||||||
ea.Source = shared_from_this();
|
ea.Source = shared_from_this();
|
||||||
@ -70,64 +71,35 @@ void Socket::Close(bool from_dtor)
|
|||||||
Stop();
|
Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
string Socket::FormatErrorCode(int code)
|
void Socket::HandleSocketError(void)
|
||||||
{
|
|
||||||
char *message;
|
|
||||||
string result = "Unknown socket error.";
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, code, 0, (char *)&message, 0, NULL) != 0) {
|
|
||||||
result = string(message);
|
|
||||||
LocalFree(message);
|
|
||||||
}
|
|
||||||
#else /* _WIN32 */
|
|
||||||
if (code != 0)
|
|
||||||
message = strerror(code);
|
|
||||||
|
|
||||||
result = string(message);
|
|
||||||
#endif /* _WIN32 */
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
int Socket::ExceptionEventHandler(const EventArgs& ea)
|
|
||||||
{
|
{
|
||||||
int opt;
|
int opt;
|
||||||
socklen_t optlen = sizeof(opt);
|
socklen_t optlen = sizeof(opt);
|
||||||
|
|
||||||
int rc = getsockopt(GetFD(), SOL_SOCKET, SO_ERROR, (char *)&opt, &optlen);
|
int rc = getsockopt(GetFD(), SOL_SOCKET, SO_ERROR, (char *)&opt, &optlen);
|
||||||
|
|
||||||
if (rc < 0) {
|
if (rc >= 0 && opt != 0) {
|
||||||
Close();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (opt != 0) {
|
|
||||||
SocketErrorEventArgs sea;
|
SocketErrorEventArgs sea;
|
||||||
sea.Code = opt;
|
sea.Code = opt;
|
||||||
sea.Message = FormatErrorCode(sea.Code);
|
#ifdef _WIN32
|
||||||
|
sea.Message = Win32Exception::FormatErrorCode(sea.Code);
|
||||||
|
#else /* _WIN32 */
|
||||||
|
sea.Message = PosixException::FormatErrorCode(sea.Code);
|
||||||
|
#endif /* _WIN32 */
|
||||||
OnError(sea);
|
OnError(sea);
|
||||||
|
}
|
||||||
|
|
||||||
Close();
|
Close();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Socket::ExceptionEventHandler(const EventArgs& ea)
|
||||||
|
{
|
||||||
|
HandleSocketError();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::CloseAllSockets(void)
|
|
||||||
{
|
|
||||||
for (Socket::CollectionType::iterator i = Sockets.begin(); i != Sockets.end(); ) {
|
|
||||||
Socket::Ptr socket = i->lock();
|
|
||||||
|
|
||||||
i++;
|
|
||||||
|
|
||||||
if (socket == NULL)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
socket->Close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Socket::WantsToRead(void) const
|
bool Socket::WantsToRead(void) const
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
|
@ -19,12 +19,10 @@ private:
|
|||||||
|
|
||||||
int ExceptionEventHandler(const EventArgs& ea);
|
int ExceptionEventHandler(const EventArgs& ea);
|
||||||
|
|
||||||
protected:
|
|
||||||
string FormatErrorCode(int errorCode);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Socket(void);
|
Socket(void);
|
||||||
|
|
||||||
|
void HandleSocketError(void);
|
||||||
void Close(bool from_dtor);
|
void Close(bool from_dtor);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -40,8 +38,6 @@ public:
|
|||||||
void SetFD(SOCKET fd);
|
void SetFD(SOCKET fd);
|
||||||
SOCKET GetFD(void) const;
|
SOCKET GetFD(void) const;
|
||||||
|
|
||||||
static void CloseAllSockets(void);
|
|
||||||
|
|
||||||
Event<EventArgs> OnReadable;
|
Event<EventArgs> OnReadable;
|
||||||
Event<EventArgs> OnWritable;
|
Event<EventArgs> OnWritable;
|
||||||
Event<EventArgs> OnException;
|
Event<EventArgs> OnException;
|
||||||
|
@ -41,17 +41,7 @@ void TCPClient::Connect(const string& hostname, unsigned short port)
|
|||||||
#else /* _WIN32 */
|
#else /* _WIN32 */
|
||||||
if (rc < 0 && errno != EINPROGRESS) {
|
if (rc < 0 && errno != EINPROGRESS) {
|
||||||
#endif /* _WIN32 */
|
#endif /* _WIN32 */
|
||||||
SocketErrorEventArgs sea;
|
HandleSocketError();
|
||||||
#ifdef _WIN32
|
|
||||||
sea.Code = WSAGetLastError();
|
|
||||||
#else /* _WIN32 */
|
|
||||||
sea.Code = errno;
|
|
||||||
#endif /* _WIN32 */
|
|
||||||
sea.Message = FormatErrorCode(sea.Code);
|
|
||||||
|
|
||||||
OnError(sea);
|
|
||||||
|
|
||||||
Close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m_PeerHost = hostname;
|
m_PeerHost = hostname;
|
||||||
@ -95,19 +85,7 @@ int TCPClient::ReadableEventHandler(const EventArgs& ea)
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (rc <= 0) {
|
if (rc <= 0) {
|
||||||
if (rc < 0) {
|
HandleSocketError();
|
||||||
SocketErrorEventArgs sea;
|
|
||||||
#ifdef _WIN32
|
|
||||||
sea.Code = WSAGetLastError();
|
|
||||||
#else /* _WIN32 */
|
|
||||||
sea.Code = errno;
|
|
||||||
#endif /* _WIN32 */
|
|
||||||
sea.Message = FormatErrorCode(sea.Code);
|
|
||||||
|
|
||||||
OnError(sea);
|
|
||||||
}
|
|
||||||
|
|
||||||
Close();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,19 +105,7 @@ int TCPClient::WritableEventHandler(const EventArgs& ea)
|
|||||||
rc = send(GetFD(), (const char *)m_SendQueue->GetReadBuffer(), m_SendQueue->GetSize(), 0);
|
rc = send(GetFD(), (const char *)m_SendQueue->GetReadBuffer(), m_SendQueue->GetSize(), 0);
|
||||||
|
|
||||||
if (rc <= 0) {
|
if (rc <= 0) {
|
||||||
if (rc < 0) {
|
HandleSocketError();
|
||||||
SocketErrorEventArgs sea;
|
|
||||||
#ifdef _WIN32
|
|
||||||
sea.Code = WSAGetLastError();
|
|
||||||
#else /* _WIN32 */
|
|
||||||
sea.Code = errno;
|
|
||||||
#endif /* _WIN32 */
|
|
||||||
sea.Message = FormatErrorCode(sea.Code);
|
|
||||||
|
|
||||||
OnError(sea);
|
|
||||||
}
|
|
||||||
|
|
||||||
Close();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,18 +42,9 @@ int TCPServer::ReadableEventHandler(const EventArgs& ea)
|
|||||||
|
|
||||||
fd = accept(GetFD(), (sockaddr *)&addr, &addrlen);
|
fd = accept(GetFD(), (sockaddr *)&addr, &addrlen);
|
||||||
|
|
||||||
if (fd == INVALID_SOCKET) {
|
if (fd < 0) {
|
||||||
SocketErrorEventArgs sea;
|
HandleSocketError();
|
||||||
#ifdef _WIN32
|
return 0;
|
||||||
sea.Code = WSAGetLastError();
|
|
||||||
#else /* _WIN32 */
|
|
||||||
sea.Code = errno;
|
|
||||||
#endif /* _WIN32 */
|
|
||||||
sea.Message = FormatErrorCode(sea.Code);
|
|
||||||
|
|
||||||
OnError(sea);
|
|
||||||
|
|
||||||
Close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NewClientEventArgs nea;
|
NewClientEventArgs nea;
|
||||||
|
@ -9,14 +9,9 @@ void TCPSocket::MakeSocket(void)
|
|||||||
int fd = socket(AF_INET, SOCK_STREAM, 0);
|
int fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
|
||||||
if (fd == INVALID_SOCKET) {
|
if (fd == INVALID_SOCKET) {
|
||||||
SocketErrorEventArgs sea;
|
HandleSocketError();
|
||||||
#ifdef _WIN32
|
|
||||||
sea.Code = WSAGetLastError();
|
return;
|
||||||
#else /* _WIN32 */
|
|
||||||
sea.Code = errno;
|
|
||||||
#endif /* _WIN32 */
|
|
||||||
sea.Message = FormatErrorCode(sea.Code);
|
|
||||||
OnError(sea);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SetFD(fd);
|
SetFD(fd);
|
||||||
@ -40,19 +35,6 @@ void TCPSocket::Bind(const char *hostname, unsigned short port)
|
|||||||
sin.sin_addr.s_addr = hostname ? inet_addr(hostname) : htonl(INADDR_ANY);
|
sin.sin_addr.s_addr = hostname ? inet_addr(hostname) : htonl(INADDR_ANY);
|
||||||
sin.sin_port = htons(port);
|
sin.sin_port = htons(port);
|
||||||
|
|
||||||
int rc = ::bind(GetFD(), (sockaddr *)&sin, sizeof(sin));
|
if (::bind(GetFD(), (sockaddr *)&sin, sizeof(sin)) < 0)
|
||||||
|
HandleSocketError();
|
||||||
if (rc < 0) {
|
|
||||||
SocketErrorEventArgs sea;
|
|
||||||
#ifdef _WIN32
|
|
||||||
sea.Code = WSAGetLastError();
|
|
||||||
#else /* _WIN32 */
|
|
||||||
sea.Code = errno;
|
|
||||||
#endif /* _WIN32 */
|
|
||||||
sea.Message = FormatErrorCode(sea.Code);
|
|
||||||
|
|
||||||
OnError(sea);
|
|
||||||
|
|
||||||
Close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,12 @@ typedef struct threadparam_s
|
|||||||
void *param;
|
void *param;
|
||||||
} threadparam_t;
|
} threadparam_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ThreadStartProc
|
||||||
|
*
|
||||||
|
* Helper function that deals with OS-specific differences in the thread
|
||||||
|
* proc's function signature.
|
||||||
|
*/
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
static DWORD WINAPI ThreadStartProc(LPVOID param)
|
static DWORD WINAPI ThreadStartProc(LPVOID param)
|
||||||
{
|
{
|
||||||
@ -26,21 +32,35 @@ static void *ThreadStartProc(void *param)
|
|||||||
}
|
}
|
||||||
#endif /* _WIN32 */
|
#endif /* _WIN32 */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thread
|
||||||
|
*
|
||||||
|
* Constructor for the thread class. Creates a new thread that begins
|
||||||
|
* executing immediately.
|
||||||
|
*/
|
||||||
Thread::Thread(ThreadProc callback)
|
Thread::Thread(ThreadProc callback)
|
||||||
{
|
{
|
||||||
threadparam_t *tparam = new threadparam_t();
|
threadparam_t *tparam = new threadparam_t();
|
||||||
|
|
||||||
if (tparam == NULL)
|
if (tparam == NULL)
|
||||||
throw exception(/*"Out of memory"*/);
|
throw OutOfMemoryException("Out of memory");
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
m_Thread = CreateThread(NULL, 0, ThreadStartProc, tparam, CREATE_SUSPENDED, NULL);
|
m_Thread = CreateThread(NULL, 0, ThreadStartProc, tparam, CREATE_SUSPENDED, NULL);
|
||||||
|
|
||||||
|
if (m_Thread == NULL)
|
||||||
|
throw Win32Exception("CreateThread failed.", GetLastError());
|
||||||
#else /* _WIN32 */
|
#else /* _WIN32 */
|
||||||
pthread_create(&m_Thread, NULL, ThreadStartProc, &tparam);
|
pthread_create(&m_Thread, NULL, ThreadStartProc, &tparam);
|
||||||
#endif /* _WIN32 */
|
#endif /* _WIN32 */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ~Thread
|
||||||
|
*
|
||||||
|
* Destructor for the Thread class. Cleans up the resources associated
|
||||||
|
* with the thread.
|
||||||
|
*/
|
||||||
Thread::~Thread(void)
|
Thread::~Thread(void)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@ -50,6 +70,11 @@ Thread::~Thread(void)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Join
|
||||||
|
*
|
||||||
|
* Waits until the thread has finished executing.
|
||||||
|
*/
|
||||||
void Thread::Join(void)
|
void Thread::Join(void)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
@ -6,6 +6,11 @@ namespace icinga
|
|||||||
|
|
||||||
typedef void (*ThreadProc)(void *);
|
typedef void (*ThreadProc)(void *);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thread
|
||||||
|
*
|
||||||
|
* A wrapper around OS-specific thread functionality.
|
||||||
|
*/
|
||||||
class I2_BASE_API Thread
|
class I2_BASE_API Thread
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
@ -16,10 +21,9 @@ private:
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Thread(void (*callback)(void *));
|
Thread(ThreadProc callback);
|
||||||
~Thread(void);
|
~Thread(void);
|
||||||
|
|
||||||
void Start(void);
|
|
||||||
void Join(void);
|
void Join(void);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -61,20 +61,6 @@ void Timer::CallExpiredTimers(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Timer::StopAllTimers(void)
|
|
||||||
{
|
|
||||||
for (Timer::CollectionType::iterator i = Timers.begin(); i != Timers.end(); ) {
|
|
||||||
Timer::Ptr timer = i->lock();
|
|
||||||
|
|
||||||
i++;
|
|
||||||
|
|
||||||
if (timer == NULL)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
timer->Stop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Note: the timer delegate must not call Disable() on any other timers than
|
/* Note: the timer delegate must not call Disable() on any other timers than
|
||||||
* the timer that originally invoked the delegate */
|
* the timer that originally invoked the delegate */
|
||||||
void Timer::Call(void)
|
void Timer::Call(void)
|
||||||
|
@ -44,7 +44,6 @@ public:
|
|||||||
|
|
||||||
static time_t GetNextCall(void);
|
static time_t GetNextCall(void);
|
||||||
static void CallExpiredTimers(void);
|
static void CallExpiredTimers(void);
|
||||||
static void StopAllTimers(void);
|
|
||||||
|
|
||||||
void Start(void);
|
void Start(void);
|
||||||
void Stop(void);
|
void Stop(void);
|
||||||
|
48
base/utility.cpp
Normal file
48
base/utility.cpp
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
#include "i2-base.h"
|
||||||
|
|
||||||
|
using namespace icinga;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Daemonize
|
||||||
|
*
|
||||||
|
* Detaches from the controlling terminal.
|
||||||
|
*/
|
||||||
|
void Utility::Daemonize(void) {
|
||||||
|
#ifndef _WIN32
|
||||||
|
pid_t pid;
|
||||||
|
pid_t sid;
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
pid = fork();
|
||||||
|
if (pid == -1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pid)
|
||||||
|
exit(0);
|
||||||
|
|
||||||
|
fd = open("/dev/null", O_RDWR);
|
||||||
|
if (fd) {
|
||||||
|
if (fd != 0) {
|
||||||
|
dup2(fd, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fd != 1) {
|
||||||
|
dup2(fd, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fd != 2) {
|
||||||
|
dup2(fd, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fd > 2) {
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sid = setsid();
|
||||||
|
if (sid == -1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
46
base/utility.h
Normal file
46
base/utility.h
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
#ifndef UTILITY_H
|
||||||
|
#define UTILITY_H
|
||||||
|
|
||||||
|
namespace icinga
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility
|
||||||
|
*
|
||||||
|
* Utility functions.
|
||||||
|
*/
|
||||||
|
class I2_BASE_API Utility
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
Utility(void);
|
||||||
|
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* GetTypeName
|
||||||
|
*
|
||||||
|
* Returns the type name of an object (using RTTI).
|
||||||
|
*/
|
||||||
|
template<class T>
|
||||||
|
static string GetTypeName(const T& value)
|
||||||
|
{
|
||||||
|
string klass = typeid(value).name();
|
||||||
|
|
||||||
|
#ifdef HAVE_GCC_ABI_DEMANGLE
|
||||||
|
int status;
|
||||||
|
char *realname = abi::__cxa_demangle(klass.c_str(), 0, 0, &status);
|
||||||
|
|
||||||
|
if (realname != NULL) {
|
||||||
|
klass = string(realname);
|
||||||
|
free(realname);
|
||||||
|
}
|
||||||
|
#endif /* HAVE_GCC_ABI_DEMANGLE */
|
||||||
|
|
||||||
|
return klass;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Daemonize(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* UTILITY_H */
|
@ -146,7 +146,7 @@ int ConfigRpcComponent::LocalObjectRemovedHandler(const EventArgs& ea)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ConfigRpcComponent::LocalPropertyChangedHandler(const DictionaryPropertyChangedEventArgs& ea)
|
int ConfigRpcComponent::LocalPropertyChangedHandler(const PropertyChangedEventArgs& ea)
|
||||||
{
|
{
|
||||||
ConfigObject::Ptr object = static_pointer_cast<ConfigObject>(ea.Source);
|
ConfigObject::Ptr object = static_pointer_cast<ConfigObject>(ea.Source);
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ private:
|
|||||||
|
|
||||||
int LocalObjectCreatedHandler(const EventArgs& ea);
|
int LocalObjectCreatedHandler(const EventArgs& ea);
|
||||||
int LocalObjectRemovedHandler(const EventArgs& ea);
|
int LocalObjectRemovedHandler(const EventArgs& ea);
|
||||||
int LocalPropertyChangedHandler(const DictionaryPropertyChangedEventArgs& ea);
|
int LocalPropertyChangedHandler(const PropertyChangedEventArgs& ea);
|
||||||
|
|
||||||
int FetchObjectsHandler(const NewRequestEventArgs& ea);
|
int FetchObjectsHandler(const NewRequestEventArgs& ea);
|
||||||
int RemoteObjectUpdatedHandler(const NewRequestEventArgs& ea);
|
int RemoteObjectUpdatedHandler(const NewRequestEventArgs& ea);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user