Skip to content

Commit

Permalink
gwt: Make sure no concurrent requests of the same type can exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
calin-iorgulescu committed Feb 20, 2015
1 parent d530387 commit fa30e21
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ public class Delegate<T> {
private RequestBuilder rb;
private boolean isGet, attachLocale;
private String url;
private HashMap<String, Request> ongoingRequests;

public Delegate(HandlerManager eventBus, String url, boolean isGet, boolean attachLocale) {
public Delegate(HandlerManager eventBus, String url, boolean isGet, boolean attachLocale,
HashMap<String, Request> ongoingRequests) {
this.eventBus = eventBus;
this.isGet = isGet;
this.attachLocale = attachLocale;
this.url = url;
this.ongoingRequests = ongoingRequests;
/**
* We reconstruct the entire RequestBuilder for GET requests when
* we call sendRequest(), so don't create a new instance for it
Expand All @@ -45,6 +48,10 @@ public Delegate(HandlerManager eventBus, String url, boolean isGet, boolean atta
rb.setHeader("Content-Type", "application/x-www-form-urlencoded");
rb.setTimeoutMillis(requestTimeoutMillis);
}

Request ongoing = ongoingRequests.get(url);
if (ongoing != null) ongoing.cancel();

}

private String packParameters(HashMap<String, String> params) {
Expand Down Expand Up @@ -109,7 +116,8 @@ public void onResponseReceived(Request request, Response response) {
* or at least so is said in the GWT documentation. Given the nature of the
* different requests this seems more appropriate
*/
rb.sendRequest(packedParameters, rqc);
Request req = rb.sendRequest(packedParameters, rqc);
ongoingRequests.put(url, req);
} catch(RequestException e) {
callback.onFailure(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.http.client.Request;

import ro.pub.cs.vmchecker.client.model.Assignment;
import ro.pub.cs.vmchecker.client.model.AuthenticationResponse;
Expand Down Expand Up @@ -39,6 +40,9 @@ public class HTTPService {
public static String UPLOAD_MD5_URL = VMCHECKER_SERVICES_URL + "uploadAssignmentMd5";
public static String BEGIN_EVALUATION_URL = VMCHECKER_SERVICES_URL + "beginEvaluation";

private HandlerManager eventBus;
private HashMap<String, Request> ongoingRequests = new HashMap<String, Request>();

/**
* Computes the base URL for services by erasing the last level
* of directories from the URL and add the SERVICES_SUFFIX to it
Expand All @@ -53,20 +57,18 @@ public static String computeServicesURL() {
return uiURL.substring(0, uiURL.substring(0, uiURL.lastIndexOf('/')).lastIndexOf('/') + 1) + SERVICES_SUFFIX + '/';
}

private HandlerManager eventBus;

public HTTPService(HandlerManager eventBus) {
this.eventBus = eventBus;
}

public void getCourses(final AsyncCallback<Course[]> callback) {
Delegate<Course[]> delegate = new Delegate<Course[]>(eventBus, GET_COURSES_URL, true, false);
Delegate<Course[]> delegate = new Delegate<Course[]>(eventBus, GET_COURSES_URL, true, false, ongoingRequests);
delegate.sendRequest(callback, new CoursesListDecoder(), null);
}

public void getAssignments(String courseId, final AsyncCallback<Assignment[]> callback) {
Delegate<Assignment[]> delegate =
new Delegate<Assignment[]>(eventBus, GET_ASSIGNMENTS_URL, true, false);
new Delegate<Assignment[]>(eventBus, GET_ASSIGNMENTS_URL, true, false, ongoingRequests);
HashMap<String, String> params = new HashMap<String, String>();
params.put("courseId", courseId);
delegate.sendRequest(callback, new AssignmentsListDecoder(), params);
Expand All @@ -75,7 +77,7 @@ public void getAssignments(String courseId, final AsyncCallback<Assignment[]> ca
public void getUploadedMd5(String courseId, String assignmentId,
final AsyncCallback<Md5Status> callback) {
Delegate<Md5Status> delegate =
new Delegate<Md5Status>(eventBus, GET_UPLOADED_MD5_URL, true, false);
new Delegate<Md5Status>(eventBus, GET_UPLOADED_MD5_URL, true, false, ongoingRequests);
HashMap<String, String> params = new HashMap<String, String>();
params.put("courseId", courseId);
params.put("assignmentId", assignmentId);
Expand All @@ -85,7 +87,7 @@ public void getUploadedMd5(String courseId, String assignmentId,
public void getStorageDirContents(String courseId, String assignmentId,
final AsyncCallback<FileList> callback) {
Delegate<FileList> delegate =
new Delegate<FileList>(eventBus, GET_STORAGE_FILE_LIST_URL, true, false);
new Delegate<FileList>(eventBus, GET_STORAGE_FILE_LIST_URL, true, false, ongoingRequests);
HashMap<String, String> params = new HashMap<String, String>();
params.put("courseId", courseId);
params.put("assignmentId", assignmentId);
Expand All @@ -100,7 +102,7 @@ public void getStorageDirContents(String courseId, String assignmentId,
public void getResults(String courseId, String assignmentId,
final AsyncCallback<EvaluationResult[]> callback) {
Delegate<EvaluationResult[]> delegate =
new Delegate<EvaluationResult[]>(eventBus, GET_USER_RESULTS_URL, true, true);
new Delegate<EvaluationResult[]>(eventBus, GET_USER_RESULTS_URL, true, true, ongoingRequests);
HashMap<String, String> params = new HashMap<String, String>();
params.put("courseId", courseId);
params.put("assignmentId", assignmentId);
Expand All @@ -110,7 +112,7 @@ public void getResults(String courseId, String assignmentId,
public void getUserResults(String courseId, String assignmentId, String username,
final AsyncCallback<EvaluationResult[]> callback) {
Delegate<EvaluationResult[]> delegate =
new Delegate<EvaluationResult[]>(eventBus, GET_USER_RESULTS_URL, true, true);
new Delegate<EvaluationResult[]>(eventBus, GET_USER_RESULTS_URL, true, true, ongoingRequests);
HashMap<String, String> params = new HashMap<String, String>();
params.put("courseId", courseId);
params.put("assignmentId", assignmentId);
Expand All @@ -121,7 +123,7 @@ public void getUserResults(String courseId, String assignmentId, String username
public void getTeamResults(String courseId, String assignmentId, String teamname,
final AsyncCallback<EvaluationResult[]> callback) {
Delegate<EvaluationResult[]> delegate =
new Delegate<EvaluationResult[]>(eventBus, GET_TEAM_RESULTS_URL, true, true);
new Delegate<EvaluationResult[]>(eventBus, GET_TEAM_RESULTS_URL, true, true, ongoingRequests);
HashMap<String, String> params = new HashMap<String, String>();
params.put("courseId", courseId);
params.put("assignmentId", assignmentId);
Expand All @@ -132,7 +134,7 @@ public void getTeamResults(String courseId, String assignmentId, String teamname

public void getAllResults(String courseId, final AsyncCallback<ResultInfo[]> callback) {
Delegate<ResultInfo[]> delegate =
new Delegate<ResultInfo[]>(eventBus, GET_ALL_RESULTS_URL, true, false);
new Delegate<ResultInfo[]>(eventBus, GET_ALL_RESULTS_URL, true, false, ongoingRequests);
HashMap<String, String> params = new HashMap<String, String>();
params.put("courseId", courseId);
delegate.sendRequest(callback, new StatisticsDecoder(), params);
Expand All @@ -141,7 +143,7 @@ public void getAllResults(String courseId, final AsyncCallback<ResultInfo[]> cal
public void performAuthentication(String username, String password, Boolean extendSession,
final AsyncCallback<AuthenticationResponse> callback) {
Delegate<AuthenticationResponse> delegate =
new Delegate<AuthenticationResponse>(eventBus, PERFORM_AUTHENTICATION_URL, false, true);
new Delegate<AuthenticationResponse>(eventBus, PERFORM_AUTHENTICATION_URL, false, true, ongoingRequests);
HashMap<String, String> params = new HashMap<String, String>();
params.put("username", username);
params.put("password", password);
Expand All @@ -150,7 +152,7 @@ public void performAuthentication(String username, String password, Boolean exte
}

public void sendLogoutRequest(final AsyncCallback<Boolean> callback) {
Delegate<Boolean> delegate = new Delegate<Boolean>(eventBus, LOGOUT_URL, true, false);
Delegate<Boolean> delegate = new Delegate<Boolean>(eventBus, LOGOUT_URL, true, false, ongoingRequests);
delegate.sendRequest(callback, new NullDecoder(), null);
}

Expand Down

0 comments on commit fa30e21

Please sign in to comment.