Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/ServiceDiscovery/Services.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,24 @@ bool Services::Init(Store &m_variables, zmq::context_t* context_in, SlowControlC
if(!m_variables.Get("service_name",m_name)) m_name="test_service";
if(!m_variables.Get("db_name",m_dbname)) m_dbname="daq";



if(!m_backend_client.Initialise(m_variables)){
std::clog<<"error initialising slowcontrol client"<<std::endl;
return false;
}

// TODO FIXME with better mechanism
// after Initilising the slow control client needs ~15 seconds for the middleman to connect
std::this_thread::sleep_for(std::chrono::seconds(15));
// hopefully the middleman has found us by now
// wait for the middleman to connect. ServiceDiscovery broadcasts are sent out intermittently,
// so we need to wait for the middleman to receive one & connect before we can communicate with it.
int pub_period=5;
m_variables.Get("service_publish_sec",pub_period);
Ready(pub_period*1000);

return true;
}

bool Services::Ready(const unsigned int timeout){
return m_backend_client.Ready(timeout);
}

// ===========================================================================
// Write Functions
// ---------------
Expand Down
1 change: 1 addition & 0 deletions src/ServiceDiscovery/Services.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace ToolFramework {
Services();
~Services();
bool Init(Store &m_variables, zmq::context_t* context_in, SlowControlCollection* sc_vars_in, bool new_service=false);
bool Ready(const unsigned int timeout=10000); // default service discovery broadcast period is 5s, middleman also checks intermittently, compound total time should be <10s...

bool SQLQuery(const std::string& database, const std::string& query, std::vector<std::string>& responses, const unsigned int timeout=SERVICES_DEFAULT_TIMEOUT);
bool SQLQuery(const std::string& database, const std::string& query, std::string& response, const unsigned int timeout=SERVICES_DEFAULT_TIMEOUT);
Expand Down
27 changes: 27 additions & 0 deletions src/ServiceDiscovery/ServicesBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -936,3 +936,30 @@ bool ServicesBackend::Receive(zmq::socket_t* sock, std::vector<zmq::message_t>&
return true;
}

bool ServicesBackend::Ready(int timeout){

// poll the output sockets for listeners
// only poll dealer socket, pub sockets always return true immediately so ignore the timeout
// polling the input socket checks for a message, so don't do that.
int ret;
try {
dlr_socket_mutex.lock();
ret = zmq::poll(&out_polls.at(1), 1, timeout);
dlr_socket_mutex.unlock();
} catch (zmq::error_t& err){
std::cerr<<"ServicesBackend::Ready caught "<<err.what()<<std::endl;
return false;
}
if(ret<0){
// error polling - is the socket closed?
std::cerr<<"ServicesBackend::Ready error: "<<zmq_strerror(errno)<<std::endl;
return false;
} else if(ret==0){
// 'resource temoprarily unavailable' - no-one connected.
} else if(out_polls.at(1).revents & ZMQ_POLLOUT){
return true;
}

return false;

}
1 change: 1 addition & 0 deletions src/ServiceDiscovery/ServicesBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class ServicesBackend {
ServicesBackend(){};
~ServicesBackend(){};
void SetUp(zmq::context_t* in_context, std::function<void(std::string msg, int msg_verb, int verbosity)> log=nullptr); // possibly move to constructor
bool Ready(int timeout); // check if zmq sockets have connections
bool Initialise(std::string configfile);
bool Initialise(Store &varaibles_in);
bool Finalise();
Expand Down