org.snapscript.studio.agent.ProcessClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of snap-agent Show documentation
Show all versions of snap-agent Show documentation
Dynamic scripting for the JVM
package org.snapscript.studio.agent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.snapscript.core.ResourceManager;
import org.snapscript.studio.agent.core.ExecuteLatch;
import org.snapscript.studio.agent.core.ExecuteStatus;
import org.snapscript.studio.agent.debug.BreakpointMatcher;
import org.snapscript.studio.agent.event.ExitEvent;
import org.snapscript.studio.agent.event.ProcessEventChannel;
import org.snapscript.studio.agent.task.ProcessExecutor;
import org.snapscript.studio.agent.worker.store.WorkerStore;
public class ProcessClient {
private final Map> breakpoints;
private final ProcessEventChannel client;
private final ProcessExecutor executor;
private final ProcessContext context;
private final String process;
public ProcessClient(ProcessContext context, ProcessEventChannel client, ProcessExecutor executor, String process) {
this.breakpoints = new HashMap>();
this.executor = executor;
this.client = client;
this.context = context;
this.process = process;
}
public String loadScript(String project, String resource) {
ResourceManager manager = context.getManager();
String path = WorkerStore.getPath(project, resource);
return manager.getString(path);
}
public void createBreakpoint(String resource, int line) {
Map lines = breakpoints.get(resource);
BreakpointMatcher matcher = context.getMatcher();
if(lines == null) {
lines = new HashMap();
breakpoints.put(resource, lines);
}
lines.put(line, Boolean.TRUE);
matcher.update(breakpoints);
}
public void removeBreakpoint(String resource, int line){
Map lines = breakpoints.get(resource);
BreakpointMatcher matcher = context.getMatcher();
if(lines == null) {
lines = new HashMap();
breakpoints.put(resource, lines);
}
lines.put(line, Boolean.FALSE);
matcher.update(breakpoints);
}
public void beginExecute(String project, String resource, String dependencies, List arguments, boolean debug) {
BreakpointMatcher matcher = context.getMatcher();
ProcessStore store = context.getStore();
matcher.update(breakpoints);
store.update(project);
executor.beginExecute(client, project, resource, dependencies, arguments, debug);
}
public void attachProcess(String project, String resource) {
BreakpointMatcher matcher = context.getMatcher();
ProcessStore store = context.getStore();
matcher.update(breakpoints);
store.update(project);
executor.attachProcess(client, project, resource);
matcher.suspend();
}
public boolean waitUntilFinish(long time) {
ExecuteLatch latch = context.getLatch();
try {
latch.wait(ExecuteStatus.FINISHED, time);
}catch(Exception e) {
return false;
}
return true;
}
public boolean detachClient() {
ExitEvent event = new ExitEvent.Builder(process)
.withDuration(0)
.withMode(context.getMode())
.build();
try {
ExecuteLatch latch = context.getLatch();
latch.disconnect();
client.send(event);
}catch(Exception e) {
return false;
}
try {
client.close("Client detach");
}catch(Exception e) {
return false;
}
return true;
}
}