# compilation will be performed. Macro expansion can be done in a controlled
# way by setting EXPAND_ONLY_PREDEF to YES.
-MACRO_EXPANSION = NO
+MACRO_EXPANSION = YES
# If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES
# then the macro expansion is limited to the macros specified with the
delegate.h \
dictionary.cpp \
dictionary.h \
- event.h \
+ observable.h \
exception.cpp \
exception.h \
fifo.cpp \
} else {
try {
result = Main(m_Arguments);
- } catch (const Exception& ex) {
+ } catch (const exception& ex) {
Application::Instance.reset();
Application::Log("---");
Application::Log("Exception: " + Utility::GetTypeName(ex));
- Application::Log("Message: " + ex.GetMessage());
+ Application::Log("Message: " + string(ex.what()));
return EXIT_FAILURE;
}
<ClInclude Include="cxx11-compat.h" />
<ClInclude Include="delegate.h" />
<ClInclude Include="dictionary.h" />
- <ClInclude Include="event.h" />
+ <ClInclude Include="observable.h" />
<ClInclude Include="exception.h" />
<ClInclude Include="fifo.h" />
<ClInclude Include="i2-base.h" />
void ForEachObject(function<int (const EventArgs&)> callback);
- Event<EventArgs> OnObjectCommitted;
- Event<EventArgs> OnObjectRemoved;
+ Observable<EventArgs> OnObjectCommitted;
+ Observable<EventArgs> OnObjectRemoved;
};
}
void ForEachObject(const string& type,
function<int (const EventArgs&)> callback);
- Event<EventArgs> OnObjectCommitted;
- Event<EventArgs> OnObjectRemoved;
+ Observable<EventArgs> OnObjectCommitted;
+ Observable<EventArgs> OnObjectRemoved;
};
}
using namespace icinga;
-/**
- * Retrieves a value from the dictionary.
- *
- * @param key The key.
- * @param value Pointer to the value.
- * @returns true if the value was retrieved, false otherwise.
- */
-bool Dictionary::GetProperty(string key, Variant *value) const
-{
- ConstDictionaryIterator i = m_Data.find(key);
-
- if (i == m_Data.end())
- return false;
-
- *value = i->second;
- return true;
-}
-
-/**
- * Sets a value in the dictionary.
- *
- * @param key The key.
- * @param value The value.
- */
-void Dictionary::SetProperty(string key, const Variant& value)
-{
- DictionaryIterator i = m_Data.find(key);
-
- Variant oldValue;
- if (i != m_Data.end()) {
- oldValue = i->second;
- m_Data.erase(i);
- }
-
- m_Data[key] = value;
-}
-
-/**
- * Retrieves a value from the dictionary.
- *
- * @param key The key.
- * @param value Pointer to the value.
- * @returns true if the value was retrieved, false otherwise.
- */
-bool Dictionary::GetProperty(string key, Dictionary::Ptr *value) const
-{
- Object::Ptr object;
-
- if (!GetProperty(key, &object))
- return false;
-
- Dictionary::Ptr dictionary = dynamic_pointer_cast<Dictionary>(object);
- if (!dictionary)
- throw InvalidArgumentException();
-
- *value = dictionary;
- return true;
-}
-
/**
* Returns an iterator to the beginning of the dictionary.
*
{
return m_Data.size();
}
-
-/**
- * Adds an unnamed value to the dictionary.
- *
- * @param value The value.
- */
-void Dictionary::AddUnnamedProperty(const Variant& value)
-{
- map<string, Variant>::const_iterator it;
- string key;
- long index = GetLength();
- do {
- stringstream s;
- s << "_" << index;
- index++;
-
- key = s.str();
- it = m_Data.find(key);
- } while (it != m_Data.end());
-
- m_Data[key] = value;
-}
typedef shared_ptr<Dictionary> Ptr;
typedef weak_ptr<Dictionary> WeakPtr;
- bool GetProperty(string key, Variant *value) const;
- void SetProperty(string key, const Variant& value);
-
+ /**
+ * Retrieves a value from the dictionary.
+ *
+ * @param key The key.
+ * @param value Pointer to the value.
+ * @returns true if the value was retrieved, false otherwise.
+ */
template<typename T>
bool GetProperty(string key, T *value) const
{
- Variant data;
+ ConstDictionaryIterator i = m_Data.find(key);
- if (!GetProperty(key, &data))
+ if (i == m_Data.end())
return false;
- *value = data;
+ *value = i->second;
return true;
}
- bool GetProperty(string key, Dictionary::Ptr *value) const;
+ /**
+ * Sets a value in the dictionary.
+ *
+ * @param key The key.
+ * @param value The value.
+ */
+ template<typename T>
+ void SetProperty(string key, const T& value)
+ {
+ m_Data[key] = value;
+ }
+
+ /**
+ * Adds an unnamed value to the dictionary.
+ *
+ * @param value The value.
+ */
+ template<typename T>
+ void AddUnnamedProperty(const T& value)
+ {
+ DictionaryIterator it;
+ string key;
+ long index = GetLength();
+ do {
+ stringstream s;
+ s << "_" << index;
+ index++;
+
+ key = s.str();
+ it = m_Data.find(key);
+ } while (it != m_Data.end());
+
+ SetProperty(key, value);
+ }
DictionaryIterator Begin(void);
DictionaryIterator End(void);
- void AddUnnamedProperty(const Variant& value);
-
long GetLength(void) const;
};
+++ /dev/null
-/******************************************************************************
- * Icinga 2 *
- * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
- * *
- * This program is free software; you can redistribute it and/or *
- * modify it under the terms of the GNU General Public License *
- * as published by the Free Software Foundation; either version 2 *
- * of the License, or (at your option) any later version. *
- * *
- * This program is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
- * GNU General Public License for more details. *
- * *
- * You should have received a copy of the GNU General Public License *
- * along with this program; if not, write to the Free Software Foundation *
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
- ******************************************************************************/
-
-#ifndef EVENT_H
-#define EVENT_H
-
-namespace icinga
-{
-
-/**
- * Base class for event arguments.
- */
-struct I2_BASE_API EventArgs
-{
- Object::Ptr Source; /**< The source of the event. */
-};
-
-/**
- * An observable event.
- */
-template<class TArgs>
-class Event
-{
-public:
- typedef function<int (const TArgs&)> ObserverType;
-
-private:
- vector<ObserverType> m_Observers;
-
-public:
- /**
- * Adds an observer to this event.
- *
- * @param rhs The delegate.
- */
- Event<TArgs>& operator +=(const ObserverType& rhs)
- {
- m_Observers.push_back(rhs);
- return *this;
- }
-
- /**
- * Removes an observer from this event.
- *
- * @param rhs The delegate.
- */
- Event<TArgs>& operator -=(const ObserverType& rhs)
- {
- m_Observers.erase(rhs);
- return *this;
- }
-
- /**
- * Invokes each observer function that is registered for this event. Any
- * observer function which returns -1 is removed.
- *
- * @param args Event arguments.
- */
- void operator()(const TArgs& args)
- {
- typename vector<ObserverType>::iterator i;
-
- for (i = m_Observers.begin(); i != m_Observers.end(); ) {
- int result = (*i)(args);
-
- if (result == -1)
- i = m_Observers.erase(i);
- else
- i++;
- }
- }
-};
-
-}
-
-#endif /* EVENT_H */
*
* @param message A message describing the exception.
*/
-Exception::Exception(const string& message)
+Exception::Exception(const char *message)
{
SetMessage(message);
}
*
* @returns The description.
*/
-string Exception::GetMessage(void) const
+const char *Exception::GetMessage(void) const
{
return m_Message;
}
+/**
+ * Retrieves the description for the exception.
+ *
+ * @returns The description.
+ */
+const char *Exception::what(void) const throw()
+{
+ return GetMessage();
+}
+
/**
* Sets the description for the exception.
*
* @param message The description.
*/
-void Exception::SetMessage(string message)
+void Exception::SetMessage(const char *message)
{
- m_Message = message;
+ if (m_Message)
+ delete m_Message;
+
+ m_Message = Memory::StrDup(message);
}
#ifdef _WIN32
/**
* Base class for all exceptions.
*/
-class I2_BASE_API Exception
+class I2_BASE_API Exception : exception
{
private:
- string m_Message;
+ const char *m_Message;
protected:
- void SetMessage(string message);
+ void SetMessage(const char *message);
public:
Exception(void);
- Exception(const string& message);
+ Exception(const char *message);
/**
* Destructor for the Exception class. Must be virtual for RTTI to work.
*/
virtual ~Exception(void)
{
+ delete m_Message;
}
- string GetMessage(void) const;
+ const char *GetMessage(void) const;
+
+ virtual const char *what(void) const throw();
};
#define DEFINE_EXCEPTION_CLASS(klass) \
{ \
} \
\
- inline klass(const string& message) \
+ inline klass(const char *message) \
: Exception(message) \
{ \
} \
DEFINE_EXCEPTION_CLASS(NotImplementedException);
DEFINE_EXCEPTION_CLASS(InvalidArgumentException);
+DEFINE_EXCEPTION_CLASS(InvalidCastException);
#ifdef _WIN32
/**
*/
inline Win32Exception(const string& message, int errorCode)
{
- SetMessage(message + ": " + FormatErrorCode(errorCode));
+ string msg = message + ": " + FormatErrorCode(errorCode);
+ SetMessage(msg.c_str());
}
/**
*/
inline PosixException(const string& message, int errorCode)
{
- SetMessage(message + ": " + FormatErrorCode(errorCode));
+ string msg = message + ": " + FormatErrorCode(errorCode);
+ SetMessage(msg.c_str());
}
/**
*/
inline OpenSSLException(const string& message, int errorCode)
{
- SetMessage(message + ": " + FormatErrorCode(errorCode));
+ string msg = message + ": " + FormatErrorCode(errorCode);
+ SetMessage(msg.c_str());
}
/**
#include "exception.h"
#include "memory.h"
#include "delegate.h"
-#include "event.h"
+#include "observable.h"
#include "variant.h"
#include "dictionary.h"
#include "timer.h"
void SetFD(SOCKET fd);
SOCKET GetFD(void) const;
- Event<EventArgs> OnReadable;
- Event<EventArgs> OnWritable;
- Event<EventArgs> OnException;
+ Observable<EventArgs> OnReadable;
+ Observable<EventArgs> OnWritable;
+ Observable<EventArgs> OnException;
- Event<SocketErrorEventArgs> OnError;
- Event<EventArgs> OnClosed;
+ Observable<SocketErrorEventArgs> OnError;
+ Observable<EventArgs> OnClosed;
virtual bool WantsToRead(void) const;
virtual bool WantsToWrite(void) const;
virtual bool WantsToRead(void) const;
virtual bool WantsToWrite(void) const;
- Event<EventArgs> OnDataAvailable;
+ Observable<EventArgs> OnDataAvailable;
};
/**
void Listen(void);
- Event<NewClientEventArgs> OnNewClient;
+ Observable<NewClientEventArgs> OnNewClient;
virtual bool WantsToRead(void) const;
};
using namespace icinga;
-typedef struct threadparam_s
-{
- ThreadProc callback;
- void *param;
-} threadparam_t;
-
/**
* Helper function that deals with OS-specific differences in the thread
* proc's function signature.
#ifdef _WIN32
static DWORD WINAPI ThreadStartProc(LPVOID param)
{
- threadparam_t *tparam = (threadparam_t *)param;
- tparam->callback(tparam->param);
+ ThreadParameters *tparam = (ThreadParameters *)param;
+ tparam->Callback(tparam->UserParams);
delete tparam;
return 0;
}
#else /* _WIN32 */
static void *ThreadStartProc(void *param)
{
- threadparam_t *tparam = (threadparam_t *)param;
- tparam->callback(tparam->param);
+ ThreadParameters *tparam = (ThreadParameters *)param;
+ tparam->Callback(tparam->UserParams);
delete tparam;
return NULL;
}
*/
Thread::Thread(ThreadProc callback, void *param)
{
- threadparam_t *tparam = new threadparam_t();
+ ThreadParameters *tparam = new ThreadParameters();
if (tparam == NULL)
throw OutOfMemoryException("Out of memory");
- tparam->callback = callback;
- tparam->param = param;
+ tparam->Callback = callback;
+ tparam->UserParams = param;
#ifdef _WIN32
m_Thread = CreateThread(NULL, 0, ThreadStartProc, tparam, CREATE_SUSPENDED, NULL);
if (m_Thread == NULL)
throw Win32Exception("CreateThread failed.", GetLastError());
#else /* _WIN32 */
- pthread_create(&m_Thread, NULL, ThreadStartProc, &tparam);
+ if (pthread_create(&m_Thread, NULL, ThreadStartProc, &tparam) < 0)
+ throw PosixException("pthread_create failed.", errno);
#endif /* _WIN32 */
}
typedef void (*ThreadProc)(void *);
+struct ThreadParameters
+{
+ ThreadProc Callback;
+ void *UserParams;
+};
+
/**
* A wrapper around OS-specific thread functionality.
*/
void Reschedule(time_t next);
- Event<TimerEventArgs> OnTimerExpired;
+ Observable<TimerEventArgs> OnTimerExpired;
};
}
virtual bool WantsToRead(void) const;
virtual bool WantsToWrite(void) const;
- Event<VerifyCertificateEventArgs> OnVerifyCertificate;
+ Observable<VerifyCertificateEventArgs> OnVerifyCertificate;
};
TCPClient::Ptr TLSClientFactory(TCPClientRole role, shared_ptr<SSL_CTX> sslContext);
if (!role)
continue;
- Dictionary::Ptr permissions;
- if (!role->GetProperty(messageType, &permissions))
+ Object::Ptr object;
+ if (!role->GetProperty(messageType, &object))
continue;
+ Dictionary::Ptr permissions = dynamic_pointer_cast<Dictionary>(object);
+ if (!permissions)
+ throw InvalidCastException();
+
for (DictionaryIterator is = permissions->Begin(); is != permissions->End(); is++) {
if (Utility::Match(is->second.GetString(), message))
return true;
ConfigObject::Ptr endpointConfig = endpointCollection->GetObject(identity);
Dictionary::Ptr roles;
- if (endpointConfig)
- endpointConfig->GetProperty("roles", &roles);
+ if (endpointConfig) {
+ Object::Ptr object;
+ if (endpointConfig->GetProperty("roles", &object)) {
+ roles = dynamic_pointer_cast<Dictionary>(object);
+
+ if (!roles)
+ throw InvalidCastException();
+ }
+ }
Endpoint::Ptr endpoint = GetEndpointManager()->GetEndpointByIdentity(identity);
ConstTopicIterator BeginPublications(void) const;
ConstTopicIterator EndPublications(void) const;
- Event<EventArgs> OnIdentityChanged;
- Event<EventArgs> OnSessionEstablished;
+ Observable<EventArgs> OnIdentityChanged;
+ Observable<EventArgs> OnSessionEstablished;
};
}
Endpoint::Ptr GetEndpointByIdentity(string identity) const;
- Event<NewEndpointEventArgs> OnNewEndpoint;
+ Observable<NewEndpointEventArgs> OnNewEndpoint;
};
}
if (!request.GetMethod(&method))
return;
- map<string, Event<NewRequestEventArgs> >::iterator i = m_TopicHandlers.find(method);
+ map<string, Observable<NewRequestEventArgs> >::iterator i = m_TopicHandlers.find(method);
if (i == m_TopicHandlers.end())
return;
class I2_ICINGA_API VirtualEndpoint : public Endpoint
{
private:
- map< string, Event<NewRequestEventArgs> > m_TopicHandlers;
+ map< string, Observable<NewRequestEventArgs> > m_TopicHandlers;
public:
typedef shared_ptr<VirtualEndpoint> Ptr;
nea.Message = message;
OnNewMessage(nea);
} catch (const Exception& ex) {
- Application::Log("Exception while processing message from JSON-RPC client: " + ex.GetMessage());
+ Application::Log("Exception while processing message from JSON-RPC client: " + string(ex.GetMessage()));
Close();
return 1;
virtual void Start(void);
- Event<NewMessageEventArgs> OnNewMessage;
+ Observable<NewMessageEventArgs> OnNewMessage;
};
TCPClient::Ptr JsonRpcClientFactory(TCPClientRole role, shared_ptr<SSL_CTX> sslContext);
bool MessagePart::GetProperty(string key, MessagePart *value) const
{
- Dictionary::Ptr dictionary;
- if (!GetDictionary()->GetProperty(key, &dictionary))
+ Object::Ptr object;
+ if (GetDictionary()->GetProperty(key, &object))
return false;
+ Dictionary::Ptr dictionary = dynamic_pointer_cast<Dictionary>(object);
+ if (!dictionary)
+ throw InvalidCastException();
+
*value = MessagePart(dictionary);
return true;
}