Commit 95015c1a authored by legoc's avatar legoc
Browse files

Create a waiting in Instance only in waitFor()

parent f286f33f
......@@ -309,7 +309,6 @@ private:
bool m_hasResult;
std::string m_resultData;
int m_exitCode;
std::unique_ptr<Waiting> m_waiting;
};
///////////////////////////////////////////////////////////////////////////
......
......@@ -453,8 +453,6 @@ Instance::Instance(Server * server) :
m_lastState(UNKNOWN),
m_hasResult(false),
m_exitCode(-1) {
m_waiting.reset(new Waiting(std::bind(&Instance::cancelWaitFor, this)));
}
Instance::~Instance() {
......@@ -556,12 +554,15 @@ bool Instance::kill() {
State Instance::waitFor(int states, const std::string& eventName, KeyValue& keyValue, StateHandlerType handler, bool blocking) {
// Create a scoped waiting so that it is removed at the exit of the function.
Waiting scopedWaiting(std::bind(&Instance::cancelWaitFor, this));
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
......@@ -570,14 +571,14 @@ State Instance::waitFor(int states, const std::string& eventName, KeyValue& keyV
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
// Waits for a new incoming status.
std::unique_ptr<Event> event = popEvent(blocking);
// The non-blocking call returns a null message.
......@@ -603,7 +604,7 @@ State Instance::waitFor(int states, const std::string& eventName, KeyValue& keyV
handler(state);
}
// test the terminal state
// Test the terminal state.
if (state == SUCCESS
|| state == STOPPED
|| state == KILLED
......@@ -611,7 +612,7 @@ State Instance::waitFor(int states, const std::string& eventName, KeyValue& keyV
break;
}
// test the requested states
// Test the requested states.
if ((states & m_pastStates) != 0) {
return m_lastState;
}
......@@ -641,7 +642,6 @@ State Instance::waitFor(int states, const std::string& eventName, KeyValue& keyV
else {
keyValue.setStatus(KeyValue::Status::REMOVED);
}
keyValue.setValue(keyEvent->getValue());
break;
}
}
......
......@@ -72,9 +72,6 @@ public class Instance extends EventListener {
Instance(Server server) {
this.server = server;
// Register the waiting
waiting.add();
}
void setId(int id) {
......@@ -231,107 +228,122 @@ public class Instance extends EventListener {
*/
private int waitFor(int states, String eventName, KeyValue keyValue, boolean blocking) {
if (!exists()) {
return lastState;
}
// test the terminal state
if (lastState == Application.State.SUCCESS
|| lastState == Application.State.STOPPED
|| lastState == Application.State.KILLED
|| lastState == Application.State.ERROR) {
// the application is already terminated
return lastState;
}
try {
// Register the waiting.
waiting.add();
// test the requested states
if ((states & pastStates) != 0) {
// the state is already received
return lastState;
}
while (true) {
// waits for a new incoming status
Event event = popEvent(blocking);
// If the event is null, then it is the result of non-blocking call.
if (event == null) {
break;
// Exit if the app does not exist.
if (!exists()) {
return lastState;
}
if (event.getId() == id) {
// Test the terminal state.
if (lastState == Application.State.SUCCESS
|| lastState == Application.State.STOPPED
|| lastState == Application.State.KILLED
|| lastState == Application.State.ERROR) {
// The application is already terminated.
return lastState;
}
if (event instanceof StatusEvent) {
// Test the requested states.
if ((states & pastStates) != 0) {
// The state is already received.
return lastState;
}
while (true) {
// Wait for a new incoming status.
Event event = popEvent(blocking);
StatusEvent status = (StatusEvent)event;
int state = status.getState();
pastStates = status.getPastStates();
lastState = state;
// Assign the exit code.
if (status.getExitCode() != null) {
exitCode = status.getExitCode();
}
// test the terminal state
if (state == Application.State.SUCCESS
|| state == Application.State.STOPPED
|| state == Application.State.KILLED
|| state == Application.State.ERROR) {
break;
}
// If the event is null, then it is the result of non-blocking call.
if (event == null) {
break;
}
if (event.getId() == id) {
if (event instanceof StatusEvent) {
// test the requested states
if ((states & pastStates) != 0) {
return lastState;
StatusEvent status = (StatusEvent)event;
int state = status.getState();
pastStates = status.getPastStates();
lastState = state;
// Assign the exit code.
if (status.getExitCode() != null) {
exitCode = status.getExitCode();
}
// Test the terminal state.
if (state == Application.State.SUCCESS
|| state == Application.State.STOPPED
|| state == Application.State.KILLED
|| state == Application.State.ERROR) {
break;
}
// Test the requested states.
if ((states & pastStates) != 0) {
return lastState;
}
}
} else if (event instanceof ResultEvent) {
ResultEvent result = (ResultEvent)event;
resultData = result.getData();
} else if (event instanceof PublisherEvent) {
PublisherEvent publisher = (PublisherEvent)event;
if (publisher.getPublisherName().equals(eventName)) {
break;
else if (event instanceof ResultEvent) {
ResultEvent result = (ResultEvent)event;
resultData = result.getData();
}
} else if (event instanceof PortEvent) {
PortEvent port = (PortEvent)event;
if (port.getPortName().equals(eventName)) {
break;
else if (event instanceof PublisherEvent) {
PublisherEvent publisher = (PublisherEvent)event;
if (publisher.getPublisherName().equals(eventName)) {
break;
}
}
} else if (event instanceof KeyEvent) {
KeyEvent keyEvent = (KeyEvent)event;
// Check if it is the event that is waited for.
if (keyValue != null && keyEvent.getKey().equals(keyValue.getKey())) {
else if (event instanceof PortEvent) {
// Set the status and value.
if (keyEvent.getStatus() == KeyEvent.Status.STORED) {
keyValue.setStatus(KeyValue.Status.STORED);
PortEvent port = (PortEvent)event;
if (port.getPortName().equals(eventName)) {
break;
}
else {
keyValue.setStatus(KeyValue.Status.REMOVED);
}
else if (event instanceof KeyEvent) {
KeyEvent keyEvent = (KeyEvent)event;
// Check if it is the event that is waited for.
if (keyValue != null && keyEvent.getKey().equals(keyValue.getKey())) {
// Set the status and value.
if (keyEvent.getStatus() == KeyEvent.Status.STORED) {
keyValue.setStatus(KeyValue.Status.STORED);
}
else {
keyValue.setStatus(KeyValue.Status.REMOVED);
}
keyValue.setValue(keyEvent.getValue());
break;
}
keyValue.setValue(keyEvent.getValue());
}
else if (event instanceof CancelEvent) {
break;
}
} else if (event instanceof CancelEvent) {
break;
}
}
}
}
return lastState;
}
finally {
// Unregister the waiting.
waiting.remove();
}
return lastState;
}
public int waitFor(String eventName) {
......@@ -372,9 +384,6 @@ public class Instance extends EventListener {
public void terminate() {
// Unregister the status.
server.unregisterEventListener(this);
// Unregister the waiting.
waiting.remove();
}
/**
......
......@@ -226,7 +226,7 @@ public class This {
static public void cancelWaitings() {
if (instance == null) {
return;
return;
}
instance.waitingSet.cancelAll();
}
......
......@@ -43,7 +43,8 @@ public class WaitingSet {
// The iteration must be in the synchronized block.
Iterator<Waiting> i = waitingSet.iterator();
while (i.hasNext()) {
i.next().cancel();
Waiting waiting = i.next();
waiting.cancel();
}
}
}
......
......@@ -142,7 +142,6 @@ public class Subscriber {
}
public void terminate() {
waiting.remove();
impl.terminate();
}
......
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