Commit b1d5f885 authored by legoc's avatar legoc
Browse files

Implemented the function Instance::now that provides the instant state

of the application.
parent 6ab0916d
......@@ -489,6 +489,9 @@ void Instance::setPastStates(State pastStates) {
void Instance::setInitialState(State state) {
m_initialState = state;
// It is important to set the last state, because in case of a call to the function now without any incoming state.
m_lastState = state;
}
const std::string& Instance::getName() const {
......@@ -526,10 +529,6 @@ const std::string& Instance::getErrorMessage() const {
return m_errorMessage;
}
State Instance::getInitialState() const {
return m_initialState;
}
bool Instance::stop() {
try {
Response response = m_server->stopApplicationAsynchronously(m_id, false);
......@@ -554,33 +553,33 @@ bool Instance::kill() {
return true;
}
State Instance::waitFor(int states, const std::string& eventName, StateHandlerType handler) {
State Instance::waitFor(int states, const std::string& eventName, StateHandlerType handler, bool blocking) {
if (!exists()) {
// the application was not launched
// The application was not launched
return m_lastState;
}
// test the terminal state
// Test the terminal state
if (m_lastState == SUCCESS
|| m_lastState == STOPPED
|| m_lastState == KILLED
|| m_lastState == FAILURE) {
// the application is already terminated
// The application is already terminated
return m_lastState;
}
// test the requested states
// Test the requested states
if ((states & m_pastStates) != 0) {
// the state is already received
// The state is already received
return m_lastState;
}
while (true) {
// waits for a new incoming status
auto_ptr<Event> event = m_eventSocket->receive();
// Waits for a new incoming status
auto_ptr<Event> event = m_eventSocket->receive(blocking);
// The socket is canceled.
// The socket is canceled or the non-blocking call returns a null message.
if (event.get() == 0) {
return m_lastState;
}
......@@ -593,7 +592,7 @@ State Instance::waitFor(int states, const std::string& eventName, StateHandlerTy
m_pastStates = status->getPastStates();
m_lastState = state;
// call the state handler.
// Call the state handler.
if (!handler.empty()) {
handler(state);
}
......@@ -635,6 +634,10 @@ State Instance::waitFor(int states, const std::string& eventName, StateHandlerTy
return m_lastState;
}
State Instance::waitFor(int states, const std::string& eventName, StateHandlerType handler) {
return waitFor(states, eventName, handler, true);
}
State Instance::waitFor(int states, StateHandlerType handler) {
return waitFor(states, "", handler);
}
......@@ -647,6 +650,10 @@ void Instance::cancelWaitFor() {
m_waiting->cancel();
}
State Instance::now() {
return waitFor(0, "", 0, false);
}
bool Instance::getBinaryResult(std::string& result) {
waitFor();
......
......@@ -200,7 +200,6 @@ public:
bool hasResult() const;
bool exists() const;
const std::string& getErrorMessage() const;
State getInitialState() const;
bool stop();
bool kill();
......@@ -210,6 +209,8 @@ public:
void cancelWaitFor();
State now();
bool getBinaryResult(std::string& result);
bool getResult(std::string& result);
bool getResult(std::vector<int32_t>& result);
......@@ -225,6 +226,7 @@ private:
void setErrorMessage(const std::string& message);
void setPastStates(State pastStates);
void setInitialState(State state);
State waitFor(int states, const std::string& eventName, StateHandlerType handler, bool blocking);
const Server * m_server;
std::auto_ptr<EventStreamSocket> m_eventSocket;
......
......@@ -34,9 +34,14 @@ EventStreamSocket::EventStreamSocket(SocketImpl * impl) : m_impl(impl) {
EventStreamSocket::~EventStreamSocket() {
}
std::auto_ptr<Event> EventStreamSocket::receive() {
std::auto_ptr<Event> EventStreamSocket::receive(bool blocking) {
zmq::message_t * message = m_impl->receive();
zmq::message_t * message = m_impl->receive(blocking);
// In case of non-blocking call, the message can be null.
if (message == 0) {
return auto_ptr<Event>(0);
}
string response(static_cast<char*>(message->data()), message->size());
delete message;
......
......@@ -39,7 +39,7 @@ class EventStreamSocket {
public:
~EventStreamSocket();
std::auto_ptr<Event> receive();
std::auto_ptr<Event> receive(bool blocking = true);
void cancel();
private:
......@@ -52,4 +52,4 @@ private:
}
#endif
\ No newline at end of file
#endif
......@@ -39,12 +39,19 @@ void SocketImpl::send(const std::string& data) {
m_socket->send(messageData);
}
zmq::message_t * SocketImpl::receive() {
// use the message interface
zmq::message_t * SocketImpl::receive(bool blocking) {
// Use the message interface.
zmq::message_t * message = new zmq::message_t();
m_socket->recv(message);
if (m_socket->recv(message, (blocking ? 0 : ZMQ_DONTWAIT))) {
// The message exists.
return message;
}
// No message.
delete message;
return message;
return 0;
}
void SocketImpl::cancel() {
......
......@@ -30,7 +30,7 @@ public:
virtual ~SocketImpl();
void send(const std::string& data);
zmq::message_t * receive();
zmq::message_t * receive(bool blocking = true);
void cancel();
void close();
......@@ -42,4 +42,4 @@ public:
}
#endif
\ No newline at end of file
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment