const String CompatIdoComponent::DefaultSocketAddress = "127.0.0.1";
const String CompatIdoComponent::DefaultSocketPort = "5668";
const String CompatIdoComponent::DefaultInstanceName = "i2-default";
+const int CompatIdoComponent::DefaultReconnectInterval = 15;
+
/**
* Reads the socket address from the config
return instance;
}
+/**
+ * Reads the reconnect interval from the config
+ * @returns reconnect_interval The config option, or static default
+ */
+int CompatIdoComponent::GetReconnectInterval(void) const
+{
+ Value interval = GetConfig()->Get("reconnect_interval");
+ if(interval.IsEmpty())
+ return DefaultReconnectInterval;
+ else
+ return interval;
+}
+
/**
* Starts the component.
*/
const int StatusTimerInterval = 60;
const int ConfigTimerInterval = 3600;
const int ProgramStatusTimerInterval = 15;
+ const int ReconnectTimerInterval = GetReconnectInterval();
/* HINTS - XXX
* - only tcp sockets
m_ProgramStatusTimer->Start();
m_ProgramStatusTimer->Reschedule(0);
+ /*
+ * scheck for reconnect once in a while
+ */
+ m_ReconnectTimer = boost::make_shared<Timer>();
+ m_ReconnectTimer->SetInterval(ReconnectTimerInterval);
+ m_ReconnectTimer->OnTimerExpired.connect(boost::bind(&CompatIdoComponent::ReconnectTimerHandler, this));
+ m_ReconnectTimer->Start();
/*
* open ido socket once
*/
- OpenSink(GetSocketAddress(), GetSocketPort());
- SendHello(GetInstanceName());
+ OpenIdoSocket();
}
+
/**
* Stops the component.
*/
void CompatIdoComponent::Stop(void)
+{
+ CloseIdoSocket();
+}
+
+
+/**
+ * Opens the ido socket, and sends hello to ido2db
+ */
+void CompatIdoComponent::OpenIdoSocket(void)
+{
+ OpenSink(GetSocketAddress(), GetSocketPort());
+
+ /*
+ * if we're connected, do not reconnecte
+ */
+ if(m_IdoSocket->IsConnected()) {
+ m_IdoSocket->SetReconnect(false);
+
+ /* connected means we can greet ido2db */
+ SendHello(GetInstanceName());
+ } else {
+ m_IdoSocket->SetReconnect(true);
+ }
+}
+
+/*
+ * Sends goodbye to ido2db, and closes ido socket
+ */
+void CompatIdoComponent::CloseIdoSocket(void)
{
GoodByeSink();
CloseSink();
}
+
/* TODO
* subscribe to all status updates and checkresults and dump them
* should remove the periodic statusdata dump
DumpProgramStatusData();
}
+/**
+ * Periodically check if idosocket requires a reconnect
+ */
+void CompatIdoComponent::ReconnectTimerHandler(void)
+{
+ Logger::Write(LogInformation, "compatido", "Checking if ido socket requires reconnect");
+
+ if(m_IdoSocket->GetReconnect()) {
+
+ /* check if we aren't already connected */
+ if(m_IdoSocket->IsConnected()) {
+ Logger::Write(LogInformation, "compatido", "Already connected to ido socket ... no reconnect necessary.");
+ return;
+ }
+
+ /* socket was disconnected, recconnect */
+ OpenIdoSocket();
+
+ if(m_IdoSocket->IsConnected()) {
+ Logger::Write(LogInformation, "compatido", "Successfully reconnected to ido socket");
+ } else {
+ stringstream message;
+ message << "Unable to reconnect to ido socket. Trying again in " << GetReconnectInterval() << " sec.";
+ Logger::Write(LogWarning, "compatido", message.str());
+ }
+ }
+}
+
/**
* opens a tcp connection to the ido socket
Timer::Ptr m_StatusTimer;
Timer::Ptr m_ConfigTimer;
Timer::Ptr m_ProgramStatusTimer;
+ Timer::Ptr m_ReconnectTimer;
+
IdoSocket::Ptr m_IdoSocket;
String GetSocketAddress(void) const;
String GetSocketPort(void) const;
String GetInstanceName(void) const;
+ int GetReconnectInterval(void) const;
void ConfigTimerHandler(void);
void StatusTimerHandler(void);
void ProgramStatusTimerHandler(void);
+ void ReconnectTimerHandler(void);
+
+ void OpenIdoSocket(void);
+ void CloseIdoSocket(void);
- void OpenSink(String node, String service );
+ void OpenSink(String node, String service);
void SendHello(String instancename);
void GoodByeSink(void);
void CloseSink(void);
static const String DefaultSocketAddress;
static const String DefaultSocketPort;
static const String DefaultInstanceName;
+ static const int DefaultReconnectInterval;
+
};
}
* signal telling about new data
*/
OnDataAvailable.connect(boost::bind(&IdoSocket::DataAvailableHandler, this));
+
+ /*
+ * what to do on disconnect
+ */
+ OnClosed.connect(boost::bind(&IdoSocket::ClientClosedHandler, this));
+
}
/**
Write(message.CStr(), message.GetLength());
}
+
+/**
+ * Handles closed client connect
+ */
+void IdoSocket::ClientClosedHandler(void)
+{
+ try {
+ CheckException();
+ } catch (const exception& ex) {
+ stringstream message;
+ message << "Error occured for ido socket: " << ex.what();
+
+ Logger::Write(LogWarning, "compatido", message.str());
+ }
+
+ Logger::Write(LogWarning, "compatido", "Lost connection to ido socket");
+
+ SetReconnect(true);
+
+ OnDisconnected(GetSelf());
+}
+
+
+/**
+ * Set reconnect vstate
+ *
+ * @aparam enable Enables the reconnect.
+ */
+void IdoSocket::SetReconnect(bool reconnect)
+{
+ m_Reconnect = reconnect;
+}
+
+/**
+ * Get reconnect state
+ *
+ * @returns reconnect The reconnect variable
+ */
+bool IdoSocket::GetReconnect(void)
+{
+ return m_Reconnect;
+}
+
/**
* Processes inbound data.
* Currently not used, as we do not receive data from ido sockets