Skip to content

Commit

Permalink
Streaming Asynchronous Callback for Submit.
Browse files Browse the repository at this point in the history
JENKINS-44427
  • Loading branch information
p4paul committed May 25, 2017
1 parent a0afc4a commit d4f9b37
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 22 deletions.
31 changes: 9 additions & 22 deletions src/main/java/org/jenkinsci/plugins/p4/client/ClientHelper.java
Expand Up @@ -745,10 +745,16 @@ private void submitFiles(IChangelist change, boolean reopen) throws Exception {
SubmitOptions submitOpts = new SubmitOptions();
submitOpts.setReOpen(reopen);

List<IFileSpec> submitted = change.submit(submitOpts);
validate.check(submitted, "Submitted as change");
// submit with asynchronous callback
SubmitStreamingCallback callback = new SubmitStreamingCallback(iclient.getServer(), listener);
synchronized (callback) {
change.submit(submitOpts, callback, 0);
while (!callback.isDone()) {
callback.wait();
}
}

long cngNumber = findSubmittedChange(submitted);
long cngNumber = callback.getChange();
if (cngNumber > 0) {
log("... submitted in change: " + cngNumber);
} else {
Expand Down Expand Up @@ -789,25 +795,6 @@ private void shelveFiles(IChangelist change, List<IFileSpec> files, boolean reve
iclient.revertFiles(files, revertOpts);
}

private long findSubmittedChange(List<IFileSpec> submitted) {
long change = 0;
for (IFileSpec spec : submitted) {
if (spec.getOpStatus() != FileSpecOpStatus.VALID) {
String msg = spec.getStatusMessage();
String cng = "Submitted as change ";
if (msg.startsWith(cng)) {
try {
String id = msg.substring(cng.length());
change = Long.parseLong(id);
} catch (NumberFormatException e) {
change = -1;
}
}
}
}
return change;
}

private boolean isOpened(List<IFileSpec> files) throws Exception {
OpenedFilesOptions openOps = new OpenedFilesOptions();
List<IFileSpec> open = iclient.openedFiles(files, openOps);
Expand Down
@@ -0,0 +1,56 @@
package org.jenkinsci.plugins.p4.client;

import com.perforce.p4java.exception.P4JavaException;
import com.perforce.p4java.impl.mapbased.server.Server;
import com.perforce.p4java.server.IServer;
import com.perforce.p4java.server.callback.IStreamingCallback;
import hudson.model.TaskListener;

import java.util.Map;

public class SubmitStreamingCallback implements IStreamingCallback {

private boolean done = false;
private long change = 0;

private final Server server;
private final Validate validate;

public SubmitStreamingCallback(IServer iserver, TaskListener listener) {
this.server = (Server) iserver;
this.validate = new Validate(listener);
}

@Override
public boolean startResults(int key) throws P4JavaException {
return true;
}

@Override
public boolean endResults(int key) throws P4JavaException {
done = true;
return true;
}

@Override
public boolean handleResult(Map<String, Object> map, int id) throws P4JavaException {
String key = "submittedChange";
if(map.containsKey(key)) {
try {
change = Long.parseLong((String) map.get(key));
} catch (NumberFormatException e) {
change = -1;
}
}
return true;
}

public boolean isDone() {
return done;
}

public long getChange() {
return change;
}

}

0 comments on commit d4f9b37

Please sign in to comment.