From c3ef49b17c27deabbcf1d1223da45836c15548ac Mon Sep 17 00:00:00 2001 From: Winston Li Date: Thu, 4 Dec 2014 21:27:52 +0000 Subject: [PATCH] Basic implementation of postback key. --- .../bridge/WriteLatexDataSource.java | 4 +-- .../writelatex/WLDirectoryNodeSnapshot.java | 6 +++-- .../wlgitbridge/writelatex/WriteLatexAPI.java | 12 ++++----- .../api/request/push/PostbackContents.java | 25 ++++++++++++------- .../api/request/push/PostbackManager.java | 19 ++++++++------ .../writelatex/model/WLDataModel.java | 3 ++- 6 files changed, 42 insertions(+), 27 deletions(-) diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/bridge/WriteLatexDataSource.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/bridge/WriteLatexDataSource.java index fe81647514..d6fd44d412 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/bridge/WriteLatexDataSource.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/bridge/WriteLatexDataSource.java @@ -23,7 +23,7 @@ public interface WriteLatexDataSource { public void putDirectoryContentsToProjectWithName(String projectName, RawDirectoryContents directoryContents, String hostname) throws SnapshotPostException, IOException, FailedConnectionException; /* Called by postback thread. */ - public void postbackReceivedSuccessfully(String projectName, int versionID) throws UnexpectedPostbackException; - public void postbackReceivedWithException(String projectName, SnapshotPostException exception) throws UnexpectedPostbackException; + public void postbackReceivedSuccessfully(String projectName, String postbackKey, int versionID) throws UnexpectedPostbackException; + public void postbackReceivedWithException(String projectName, String postbackKey, SnapshotPostException exception) throws UnexpectedPostbackException; } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/WLDirectoryNodeSnapshot.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/WLDirectoryNodeSnapshot.java index 450e77af7b..aff3d57d4b 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/WLDirectoryNodeSnapshot.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/WLDirectoryNodeSnapshot.java @@ -18,13 +18,15 @@ public class WLDirectoryNodeSnapshot implements CandidateSnapshot { private final String projectName; private final String projectURL; private final WLDirectoryNode directoryNode; + private final String postbackKey; private final CandidateSnapshotCallback callback; - public WLDirectoryNodeSnapshot(WLProject project, WLDirectoryNode directoryNode, String hostname, CandidateSnapshotCallback callback) { + public WLDirectoryNodeSnapshot(WLProject project, WLDirectoryNode directoryNode, String hostname, String postbackKey, CandidateSnapshotCallback callback) { previousVersionID = project.getLatestSnapshotID(); projectName = project.getName(); projectURL = "http://" + hostname + "/" + projectName; this.directoryNode = directoryNode; + this.postbackKey = postbackKey; this.callback = callback; } @@ -33,7 +35,7 @@ public class WLDirectoryNodeSnapshot implements CandidateSnapshot { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("latestVerId", previousVersionID); jsonObject.add("files", getFilesAsJson()); - jsonObject.addProperty("postbackUrl", projectURL + "/postback"); + jsonObject.addProperty("postbackUrl", projectURL + "/" + postbackKey); System.out.println(jsonObject); return jsonObject; } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/WriteLatexAPI.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/WriteLatexAPI.java index 524debfbaf..6783560dc0 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/WriteLatexAPI.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/WriteLatexAPI.java @@ -36,7 +36,6 @@ public class WriteLatexAPI implements WriteLatexDataSource { postbackManager = new PostbackManager(); projectLocks = new HashMap(); mainProjectLock = new ProjectLock(); -// postbackLock = new ProjectLock(); } @Override @@ -78,7 +77,8 @@ public class WriteLatexAPI implements WriteLatexDataSource { mainProjectLock.lockForProject(projectName); try { System.out.println("Pushing project: " + projectName); - CandidateSnapshot candidate = dataModel.createCandidateSnapshotFromProjectWithContents(projectName, directoryContents, hostname); + String postbackKey = postbackManager.makeKeyForProject(projectName); + CandidateSnapshot candidate = dataModel.createCandidateSnapshotFromProjectWithContents(projectName, directoryContents, hostname, postbackKey); SnapshotPushRequest snapshotPushRequest = new SnapshotPushRequest(candidate); snapshotPushRequest.request(); SnapshotPushRequestResult result = snapshotPushRequest.getResult(); @@ -100,13 +100,13 @@ public class WriteLatexAPI implements WriteLatexDataSource { /* Called by postback thread. */ @Override - public void postbackReceivedSuccessfully(String projectName, int versionID) throws UnexpectedPostbackException { - postbackManager.postVersionIDForProject(projectName, versionID); + public void postbackReceivedSuccessfully(String projectName, String postbackKey, int versionID) throws UnexpectedPostbackException { + postbackManager.postVersionIDForProject(projectName, versionID, postbackKey); } @Override - public void postbackReceivedWithException(String projectName, SnapshotPostException exception) throws UnexpectedPostbackException { - postbackManager.postExceptionForProject(projectName, exception); + public void postbackReceivedWithException(String projectName, String postbackKey, SnapshotPostException exception) throws UnexpectedPostbackException { + postbackManager.postExceptionForProject(projectName, exception, postbackKey); } } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/PostbackContents.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/PostbackContents.java index adcc7f4b2c..9b01fbf9cf 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/PostbackContents.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/PostbackContents.java @@ -7,11 +7,14 @@ import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostEx */ public class PostbackContents { + private final String postbackKey; + private boolean received; private int versionID; private SnapshotPostException exception; - public PostbackContents() { + public PostbackContents(String postbackKey) { + this.postbackKey = postbackKey; received = false; exception = null; } @@ -30,16 +33,20 @@ public class PostbackContents { return versionID; } - public synchronized void receivedVersionID(int versionID) { - this.versionID = versionID; - received = true; - notifyAll(); + public synchronized void receivedVersionID(int versionID, String postbackKey) { + if (postbackKey.equals(this.postbackKey)) { + this.versionID = versionID; + received = true; + notifyAll(); + } } - public synchronized void receivedException(SnapshotPostException exception) { - this.exception = exception; - received = true; - notifyAll(); + public synchronized void receivedException(SnapshotPostException exception, String postbackKey) { + if (postbackKey.equals(this.postbackKey)) { + this.exception = exception; + received = true; + notifyAll(); + } } } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/PostbackManager.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/PostbackManager.java index ff4ca9c4fa..33ef91454f 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/PostbackManager.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/api/request/push/PostbackManager.java @@ -17,19 +17,17 @@ public class PostbackManager { } public int getVersionID(String projectName) throws SnapshotPostException { - PostbackContents contents = new PostbackContents(); - postbackContentsTable.put(projectName, contents); - int versionID = contents.waitForPostback(); + int versionID = postbackContentsTable.get(projectName).waitForPostback(); postbackContentsTable.remove(projectName); return versionID; } - public void postVersionIDForProject(String projectName, int versionID) throws UnexpectedPostbackException { - getPostbackForProject(projectName).receivedVersionID(versionID); + public void postVersionIDForProject(String projectName, int versionID, String postbackKey) throws UnexpectedPostbackException { + getPostbackForProject(projectName).receivedVersionID(versionID, postbackKey); } - public void postExceptionForProject(String projectName, SnapshotPostException exception) throws UnexpectedPostbackException { - getPostbackForProject(projectName).receivedException(exception); + public void postExceptionForProject(String projectName, SnapshotPostException exception, String postbackKey) throws UnexpectedPostbackException { + getPostbackForProject(projectName).receivedException(exception, postbackKey); } private PostbackContents getPostbackForProject(String projectName) throws UnexpectedPostbackException { @@ -40,4 +38,11 @@ public class PostbackManager { return contents; } + public String makeKeyForProject(String projectName) { + String key = "postback"; + PostbackContents contents = new PostbackContents(key); + postbackContentsTable.put(projectName, contents); + return key; + } + } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLDataModel.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLDataModel.java index 3ece92980b..de0347e873 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLDataModel.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLDataModel.java @@ -45,11 +45,12 @@ public class WLDataModel implements CandidateSnapshotCallback { return projectStore.getProjectWithName(name); } - public CandidateSnapshot createCandidateSnapshotFromProjectWithContents(String projectName, RawDirectoryContents directoryContents, String hostname) throws SnapshotPostException, IOException, FailedConnectionException { + public CandidateSnapshot createCandidateSnapshotFromProjectWithContents(String projectName, RawDirectoryContents directoryContents, String hostname, String postbackKey) throws SnapshotPostException, IOException, FailedConnectionException { return new WLDirectoryNodeSnapshot(getProjectWithName(projectName), fileStore.createNextDirectoryNodeInProjectFromContents(getProjectWithName(projectName), directoryContents), hostname, + postbackKey, this); }