}
/* create the primary JSON-RPC listener */
- AddListener(GetBindPort());
+ if (!AddListener(GetBindPort())) {
+ Log(LogCritical, "ApiListener", "Cannot add listener for port '" + Convert::ToString(GetBindPort()) + "'.");
+ Application::Exit(EXIT_FAILURE);
+ }
m_Timer = make_shared<Timer>();
m_Timer->OnTimerExpired.connect(boost::bind(&ApiListener::ApiTimerHandler, this));
Endpoint::Ptr ApiListener::GetMaster(void) const
{
Zone::Ptr zone = Zone::GetLocalZone();
+
+ if (!zone)
+ return Endpoint::Ptr();
+
std::vector<String> names;
BOOST_FOREACH(const Endpoint::Ptr& endpoint, zone->GetEndpoints())
bool ApiListener::IsMaster(void) const
{
- return GetMaster()->GetName() == GetIdentity();
+ Endpoint::Ptr master = GetMaster();
+
+ if (!master)
+ return false;
+
+ return master->GetName() == GetIdentity();
}
/**
*
* @param service The port to listen on.
*/
-void ApiListener::AddListener(const String& service)
+bool ApiListener::AddListener(const String& service)
{
ObjectLock olock(this);
if (!sslContext) {
Log(LogCritical, "ApiListener", "SSL context is required for AddListener()");
- return;
+ return false;
}
std::ostringstream s;
server->Bind(service, AF_UNSPEC);
} catch(std::exception&) {
Log(LogCritical, "ApiListener", "Cannot bind tcp socket on '" + service + "'.");
- return;
+ return false;
}
boost::thread thread(boost::bind(&ApiListener::ListenerThreadProc, this, server));
thread.detach();
m_Servers.insert(server);
+
+ return true;
}
void ApiListener::ListenerThreadProc(const Socket::Ptr& server)
shared_ptr<SSL_CTX> sslContext = m_SSLContext;
if (!sslContext) {
- Log(LogCritical, "ApiListener", "SSL context is required for AddListener()");
+ Log(LogCritical, "ApiListener", "SSL context is required for AddConnection()");
return;
}
}
Utility::FormatDateTime("%Y/%m/%d %H:%M:%S", ts));
}
- Log(LogNotice, "ApiListener", "Current zone master: " + GetMaster()->GetName());
+ Endpoint::Ptr master = GetMaster();
+
+ if (master)
+ Log(LogNotice, "ApiListener", "Current zone master: " + master->GetName());
std::vector<String> names;
BOOST_FOREACH(const Endpoint::Ptr& endpoint, DynamicType::GetObjects<Endpoint>())