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();
if (getcwd(Cwd, sizeof(Cwd)) == NULL)
throw exception(/*"getcwd() failed"*/);
throw PosixException("getcwd failed", errno);
if (argv0[0] != '/')
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, ":")) {
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) {
strncpy(FullExePath, PathTest, sizeof(FullExePath));
@ -285,12 +285,12 @@ string Application::GetExeDirectory(void) const
free(PathEnv);
if (!FoundPath)
throw exception(/*"Could not determine executable path."*/);
throw Exception("Could not determine executable path.");
}
}
if ((Buf = realpath(FullExePath, NULL)) == NULL)
throw exception(/*"realpath() failed"*/);
throw PosixException("realpath failed", errno);
// remove filename
char *LastSlash = strrchr(Buf, '/');

View File

@ -43,4 +43,4 @@ string Win32Exception::FormatErrorCode(int code)
string PosixException::FormatErrorCode(int code)
{
return strerror(code);
}
}

View File

@ -20,16 +20,12 @@ void Socket::Start(void)
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)
{
Socket::Ptr self = static_pointer_cast<Socket>(shared_from_this());
Socket::CollectionType::iterator i = Sockets.find(self);
if (i != Sockets.end())
Sockets.erase(i);
Sockets.remove_if(weak_ptr_eq_raw<Socket>(this));
}
void Socket::SetFD(SOCKET fd)

View File

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

View File

@ -94,18 +94,14 @@ EventArgs Timer::GetUserArgs(void) const
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);
}
void Timer::Stop(void)
{
Timer::Ptr self = static_pointer_cast<Timer>(shared_from_this());
Timer::CollectionType::iterator i = Timers.find(self);
if (i != Timers.end())
Timers.erase(i);
Timers.remove_if(weak_ptr_eq_raw<Timer>(this));
}
void Timer::Reschedule(time_t next)

View File

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

View File

@ -50,4 +50,4 @@ public:
}
#endif /* VARIANT_H */
#endif /* VARIANT_H */

View File

@ -34,7 +34,7 @@ void ConfigRpcComponent::Start(void)
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::ObjectRemoved", bind_weak(&ConfigRpcComponent::RemoteObjectRemovedHandler, shared_from_this()));
@ -55,7 +55,6 @@ int ConfigRpcComponent::NewEndpointHandler(const NewEndpointEventArgs& ea)
{
if (ea.Endpoint->HasIdentity()) {
JsonRpcRequest request;
request.SetVersion("2.0");
request.SetMethod("config::FetchObjects");
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 msg;
msg.SetVersion("2.0");
msg.SetMethod(method);
Message params;

View File

@ -15,7 +15,7 @@ string AuthenticationComponent::GetName(void) const
void AuthenticationComponent::Start(void)
{
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();
mgr->OnNewEndpoint += bind_weak(&AuthenticationComponent::NewEndpointHandler, shared_from_this());
@ -39,7 +39,6 @@ int AuthenticationComponent::NewEndpointHandler(const NewEndpointEventArgs& neea
return 0;
JsonRpcRequest request;
request.SetVersion("2.0");
request.SetMethod("message::SetIdentity");
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 */
JsonRpcRequest request;
request.SetVersion("2.0");
request.SetMethod("message::Welcome");
request.SetMethod("auth::Welcome");
nrea.Sender->ProcessRequest(m_AuthenticationEndpoint, request);
return 0;

View File

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

View File

@ -87,7 +87,6 @@ int IcingaApplication::TestTimerHandler(const TimerEventArgs& tea)
cout << "Problem?" << endl;
JsonRpcRequest request;
request.SetVersion("2.0");
request.SetMethod("test");
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->RegisterMethodSource("message::Subscribe");
m_SubscriptionEndpoint->RegisterMethodSource("message::Provide");
m_SubscriptionEndpoint->RegisterMethodSource("message::Welcome");
EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager();
mgr->OnNewEndpoint += bind_weak(&SubscriptionComponent::NewEndpointHandler, shared_from_this());

View File

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

View File

@ -7,7 +7,10 @@ namespace icinga
class I2_JSONRPC_API JsonRpcResponse : public Message
{
public:
JsonRpcResponse(void) : Message() { }
JsonRpcResponse(void) : Message() {
SetVersion("2.0");
}
JsonRpcResponse(const Message& message) : Message(message) { }
inline bool GetVersion(string *value) const
@ -15,7 +18,7 @@ public:
return GetPropertyString("jsonrpc", value);
}
inline void SetJsonRpc(const string& value)
inline void SetVersion(const string& 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++) {
/* length specifier must have at most 9 characters */
if (i >= 9)
return false;
throw InvalidArgumentException("Length specifier must not exceed 9 characters");
len = len * 10 + (buffer[i] - '0');
}