Code cleanups.

Proper error handling for some *NIX functions.
This commit is contained in:
Gunnar Beutner 2012-04-23 09:48:20 +02:00
parent 42749696b6
commit f7acf4ba3f
15 changed files with 25 additions and 35 deletions

View File

@ -254,7 +254,7 @@ string Application::GetExeDirectory(void) const
const char *argv0 = m_Arguments[0].c_str(); const char *argv0 = m_Arguments[0].c_str();
if (getcwd(Cwd, sizeof(Cwd)) == NULL) if (getcwd(Cwd, sizeof(Cwd)) == NULL)
throw exception(/*"getcwd() failed"*/); throw PosixException("getcwd failed", errno);
if (argv0[0] != '/') if (argv0[0] != '/')
snprintf(FullExePath, sizeof(FullExePath), "%s/%s", Cwd, argv0); snprintf(FullExePath, sizeof(FullExePath), "%s/%s", Cwd, argv0);
@ -271,7 +271,7 @@ string Application::GetExeDirectory(void) const
for (Directory = strtok(PathEnv, ":"); Directory != NULL; Directory = strtok(NULL, ":")) { for (Directory = strtok(PathEnv, ":"); Directory != NULL; Directory = strtok(NULL, ":")) {
if (snprintf(PathTest, sizeof(PathTest), "%s/%s", Directory, argv0) < 0) if (snprintf(PathTest, sizeof(PathTest), "%s/%s", Directory, argv0) < 0)
throw exception(/*"snprintf() failed"*/); throw PosixException("snprintf failed", errno);
if (access(PathTest, X_OK) == 0) { if (access(PathTest, X_OK) == 0) {
strncpy(FullExePath, PathTest, sizeof(FullExePath)); strncpy(FullExePath, PathTest, sizeof(FullExePath));
@ -285,12 +285,12 @@ string Application::GetExeDirectory(void) const
free(PathEnv); free(PathEnv);
if (!FoundPath) if (!FoundPath)
throw exception(/*"Could not determine executable path."*/); throw Exception("Could not determine executable path.");
} }
} }
if ((Buf = realpath(FullExePath, NULL)) == NULL) if ((Buf = realpath(FullExePath, NULL)) == NULL)
throw exception(/*"realpath() failed"*/); throw PosixException("realpath failed", errno);
// remove filename // remove filename
char *LastSlash = strrchr(Buf, '/'); char *LastSlash = strrchr(Buf, '/');

View File

@ -20,16 +20,12 @@ void Socket::Start(void)
OnException += bind_weak(&Socket::ExceptionEventHandler, shared_from_this()); OnException += bind_weak(&Socket::ExceptionEventHandler, shared_from_this());
Sockets.insert(static_pointer_cast<Socket>(shared_from_this())); Sockets.push_back(static_pointer_cast<Socket>(shared_from_this()));
} }
void Socket::Stop(void) void Socket::Stop(void)
{ {
Socket::Ptr self = static_pointer_cast<Socket>(shared_from_this()); Sockets.remove_if(weak_ptr_eq_raw<Socket>(this));
Socket::CollectionType::iterator i = Sockets.find(self);
if (i != Sockets.end())
Sockets.erase(i);
} }
void Socket::SetFD(SOCKET fd) void Socket::SetFD(SOCKET fd)

View File

@ -29,7 +29,7 @@ public:
typedef shared_ptr<Socket> Ptr; typedef shared_ptr<Socket> Ptr;
typedef weak_ptr<Socket> WeakPtr; typedef weak_ptr<Socket> WeakPtr;
typedef set< Socket::WeakPtr, owner_less<Socket::WeakPtr> > CollectionType; typedef list<Socket::WeakPtr> CollectionType;
static Socket::CollectionType Sockets; static Socket::CollectionType Sockets;

View File

@ -94,18 +94,14 @@ EventArgs Timer::GetUserArgs(void) const
void Timer::Start(void) void Timer::Start(void)
{ {
Timers.insert(static_pointer_cast<Timer>(shared_from_this())); Timers.push_back(static_pointer_cast<Timer>(shared_from_this()));
Reschedule(time(NULL) + m_Interval); Reschedule(time(NULL) + m_Interval);
} }
void Timer::Stop(void) void Timer::Stop(void)
{ {
Timer::Ptr self = static_pointer_cast<Timer>(shared_from_this()); Timers.remove_if(weak_ptr_eq_raw<Timer>(this));
Timer::CollectionType::iterator i = Timers.find(self);
if (i != Timers.end())
Timers.erase(i);
} }
void Timer::Reschedule(time_t next) void Timer::Reschedule(time_t next)

View File

@ -30,7 +30,7 @@ public:
typedef shared_ptr<Timer> Ptr; typedef shared_ptr<Timer> Ptr;
typedef weak_ptr<Timer> WeakPtr; typedef weak_ptr<Timer> WeakPtr;
typedef set< Timer::WeakPtr, owner_less<Timer::WeakPtr> > CollectionType; typedef list<Timer::WeakPtr> CollectionType;
static Timer::CollectionType Timers; static Timer::CollectionType Timers;

View File

@ -34,7 +34,7 @@ void ConfigRpcComponent::Start(void)
m_ConfigRpcEndpoint->RegisterMethodSource("config::PropertyChanged"); m_ConfigRpcEndpoint->RegisterMethodSource("config::PropertyChanged");
} }
m_ConfigRpcEndpoint->RegisterMethodHandler("message::Welcome", bind_weak(&ConfigRpcComponent::WelcomeMessageHandler, shared_from_this())); m_ConfigRpcEndpoint->RegisterMethodHandler("auth::Welcome", bind_weak(&ConfigRpcComponent::WelcomeMessageHandler, shared_from_this()));
m_ConfigRpcEndpoint->RegisterMethodHandler("config::ObjectCreated", bind_weak(&ConfigRpcComponent::RemoteObjectUpdatedHandler, shared_from_this())); m_ConfigRpcEndpoint->RegisterMethodHandler("config::ObjectCreated", bind_weak(&ConfigRpcComponent::RemoteObjectUpdatedHandler, shared_from_this()));
m_ConfigRpcEndpoint->RegisterMethodHandler("config::ObjectRemoved", bind_weak(&ConfigRpcComponent::RemoteObjectRemovedHandler, shared_from_this())); m_ConfigRpcEndpoint->RegisterMethodHandler("config::ObjectRemoved", bind_weak(&ConfigRpcComponent::RemoteObjectRemovedHandler, shared_from_this()));
@ -55,7 +55,6 @@ int ConfigRpcComponent::NewEndpointHandler(const NewEndpointEventArgs& ea)
{ {
if (ea.Endpoint->HasIdentity()) { if (ea.Endpoint->HasIdentity()) {
JsonRpcRequest request; JsonRpcRequest request;
request.SetVersion("2.0");
request.SetMethod("config::FetchObjects"); request.SetMethod("config::FetchObjects");
ea.Endpoint->ProcessRequest(m_ConfigRpcEndpoint, request); ea.Endpoint->ProcessRequest(m_ConfigRpcEndpoint, request);
} }
@ -76,7 +75,6 @@ int ConfigRpcComponent::WelcomeMessageHandler(const NewRequestEventArgs& ea)
JsonRpcRequest ConfigRpcComponent::MakeObjectMessage(const ConfigObject::Ptr& object, string method, bool includeProperties) JsonRpcRequest ConfigRpcComponent::MakeObjectMessage(const ConfigObject::Ptr& object, string method, bool includeProperties)
{ {
JsonRpcRequest msg; JsonRpcRequest msg;
msg.SetVersion("2.0");
msg.SetMethod(method); msg.SetMethod(method);
Message params; Message params;

View File

@ -15,7 +15,7 @@ string AuthenticationComponent::GetName(void) const
void AuthenticationComponent::Start(void) void AuthenticationComponent::Start(void)
{ {
m_AuthenticationEndpoint = make_shared<VirtualEndpoint>(); m_AuthenticationEndpoint = make_shared<VirtualEndpoint>();
m_AuthenticationEndpoint->RegisterMethodHandler("message::SetIdentity", bind_weak(&AuthenticationComponent::IdentityMessageHandler, shared_from_this())); m_AuthenticationEndpoint->RegisterMethodHandler("auth::SetIdentity", bind_weak(&AuthenticationComponent::IdentityMessageHandler, shared_from_this()));
EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager(); EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager();
mgr->OnNewEndpoint += bind_weak(&AuthenticationComponent::NewEndpointHandler, shared_from_this()); mgr->OnNewEndpoint += bind_weak(&AuthenticationComponent::NewEndpointHandler, shared_from_this());
@ -39,7 +39,6 @@ int AuthenticationComponent::NewEndpointHandler(const NewEndpointEventArgs& neea
return 0; return 0;
JsonRpcRequest request; JsonRpcRequest request;
request.SetVersion("2.0");
request.SetMethod("message::SetIdentity"); request.SetMethod("message::SetIdentity");
IdentityMessage params; IdentityMessage params;
@ -67,8 +66,7 @@ int AuthenticationComponent::IdentityMessageHandler(const NewRequestEventArgs& n
/* there's no authentication for now, just tell them it's ok to send messages */ /* there's no authentication for now, just tell them it's ok to send messages */
JsonRpcRequest request; JsonRpcRequest request;
request.SetVersion("2.0"); request.SetMethod("auth::Welcome");
request.SetMethod("message::Welcome");
nrea.Sender->ProcessRequest(m_AuthenticationEndpoint, request); nrea.Sender->ProcessRequest(m_AuthenticationEndpoint, request);
return 0; return 0;

View File

@ -114,7 +114,6 @@ int EndpointManager::NewMethodSinkHandler(const NewMethodEventArgs& ea)
return 0; return 0;
JsonRpcRequest request; JsonRpcRequest request;
request.SetVersion("2.0");
request.SetMethod("message::Subscribe"); request.SetMethod("message::Subscribe");
SubscriptionMessage subscriptionMessage; SubscriptionMessage subscriptionMessage;
@ -134,7 +133,6 @@ int EndpointManager::NewMethodSourceHandler(const NewMethodEventArgs& ea)
return 0; return 0;
JsonRpcRequest request; JsonRpcRequest request;
request.SetVersion("2.0");
request.SetMethod("message::Provide"); request.SetMethod("message::Provide");
SubscriptionMessage subscriptionMessage; SubscriptionMessage subscriptionMessage;

View File

@ -87,7 +87,6 @@ int IcingaApplication::TestTimerHandler(const TimerEventArgs& tea)
cout << "Problem?" << endl; cout << "Problem?" << endl;
JsonRpcRequest request; JsonRpcRequest request;
request.SetVersion("2.0");
request.SetMethod("test"); request.SetMethod("test");
for (int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)

View File

@ -19,7 +19,6 @@ void SubscriptionComponent::Start(void)
m_SubscriptionEndpoint->RegisterMethodHandler("message::Provide", bind_weak(&SubscriptionComponent::ProvideMessageHandler, shared_from_this())); m_SubscriptionEndpoint->RegisterMethodHandler("message::Provide", bind_weak(&SubscriptionComponent::ProvideMessageHandler, shared_from_this()));
m_SubscriptionEndpoint->RegisterMethodSource("message::Subscribe"); m_SubscriptionEndpoint->RegisterMethodSource("message::Subscribe");
m_SubscriptionEndpoint->RegisterMethodSource("message::Provide"); m_SubscriptionEndpoint->RegisterMethodSource("message::Provide");
m_SubscriptionEndpoint->RegisterMethodSource("message::Welcome");
EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager(); EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager();
mgr->OnNewEndpoint += bind_weak(&SubscriptionComponent::NewEndpointHandler, shared_from_this()); mgr->OnNewEndpoint += bind_weak(&SubscriptionComponent::NewEndpointHandler, shared_from_this());

View File

@ -8,7 +8,10 @@ class I2_JSONRPC_API JsonRpcRequest : public Message
{ {
public: public:
JsonRpcRequest(void) : Message() { } JsonRpcRequest(void) : Message() {
SetVersion("2.0");
}
JsonRpcRequest(const Message& message) : Message(message) { } JsonRpcRequest(const Message& message) : Message(message) { }
inline bool GetVersion(string *value) const inline bool GetVersion(string *value) const

View File

@ -7,7 +7,10 @@ namespace icinga
class I2_JSONRPC_API JsonRpcResponse : public Message class I2_JSONRPC_API JsonRpcResponse : public Message
{ {
public: public:
JsonRpcResponse(void) : Message() { } JsonRpcResponse(void) : Message() {
SetVersion("2.0");
}
JsonRpcResponse(const Message& message) : Message(message) { } JsonRpcResponse(const Message& message) : Message(message) { }
inline bool GetVersion(string *value) const inline bool GetVersion(string *value) const
@ -15,7 +18,7 @@ public:
return GetPropertyString("jsonrpc", value); return GetPropertyString("jsonrpc", value);
} }
inline void SetJsonRpc(const string& value) inline void SetVersion(const string& value)
{ {
SetPropertyString("jsonrpc", value); SetPropertyString("jsonrpc", value);
} }

View File

@ -77,7 +77,7 @@ bool Netstring::ReadMessageFromFIFO(FIFO::Ptr fifo, Message *message)
for (i = 0; i < buffer_length && isdigit(buffer[i]); i++) { for (i = 0; i < buffer_length && isdigit(buffer[i]); i++) {
/* length specifier must have at most 9 characters */ /* length specifier must have at most 9 characters */
if (i >= 9) if (i >= 9)
return false; throw InvalidArgumentException("Length specifier must not exceed 9 characters");
len = len * 10 + (buffer[i] - '0'); len = len * 10 + (buffer[i] - '0');
} }