Skip to content

Commit

Permalink
synchronize access to listener lists in REST
Browse files Browse the repository at this point in the history
  • Loading branch information
slogan621 committed Apr 11, 2021
1 parent 8bb043d commit 45e17ad
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions app/src/main/java/org/thousandsmiles/tscharts_lib/RESTful.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,45 @@ public abstract class RESTful {
private int m_status;
private int m_timeout = 5000; // Milliseconds
private int m_retries = 3;
private Object lock = new Object();
private ArrayList<RESTCompletionListener> m_listener = new ArrayList<RESTCompletionListener>();

public void addListener(RESTCompletionListener o) {
m_listener.add(o);
synchronized(lock) {
m_listener.add(o);
}
}

public void removeListener(RESTCompletionListener o) {
m_listener.remove(o);
synchronized(lock) {
m_listener.remove(o);
}
}

protected void onSuccess(int code, String message, JSONArray a)
{
for (RESTCompletionListener x : m_listener) {
x.onSuccess(code, message, a);
synchronized(lock) {
for (RESTCompletionListener x : m_listener) {
x.onSuccess(code, message, a);
}
}
}

protected void onSuccess(int code, String message, JSONObject o)
{
for (RESTCompletionListener x : m_listener) {
x.onSuccess(code, message, o);
synchronized(lock) {
for (RESTCompletionListener x : m_listener) {
x.onSuccess(code, message, o);
}
}
}

protected void onSuccess(int code, String message)
{
for (RESTCompletionListener x : m_listener) {
x.onSuccess(code, message);
synchronized(lock) {
for (RESTCompletionListener x : m_listener) {
x.onSuccess(code, message);
}
}
}

Expand All @@ -74,8 +85,10 @@ protected int getRetries()

protected void onFail(int code, String message)
{
for (RESTCompletionListener x : m_listener) {
x.onFail(code, message);
synchronized(lock) {
for (RESTCompletionListener x : m_listener) {
x.onFail(code, message);
}
}
}

Expand Down

0 comments on commit 45e17ad

Please sign in to comment.