org.snapscript.agent.event.ExecuteEventMarshaller Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of snap-all Show documentation
Show all versions of snap-all Show documentation
Dynamic scripting for the JVM
package org.snapscript.agent.event;
import static org.snapscript.agent.event.ProcessEventType.EXECUTE;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class ExecuteEventMarshaller implements ProcessEventMarshaller {
@Override
public ExecuteEvent fromMessage(MessageEnvelope message) throws IOException {
byte[] array = message.getData();
int length = message.getLength();
int offset = message.getOffset();
ByteArrayInputStream buffer = new ByteArrayInputStream(array, offset, length);
DataInputStream input = new DataInputStream(buffer);
Map> breakpoints = new HashMap>();
String process = input.readUTF();
String project = input.readUTF();
String resource = input.readUTF();
int breakpointSize = input.readInt();
for(int i = 0; i < breakpointSize; i++) {
Map locations = new HashMap();
String script = input.readUTF();
int locationSize = input.readInt();
for(int j = 0; j < locationSize; j++) {
int line = input.readInt();
boolean enable = input.readBoolean();
locations.put(line, enable);
}
breakpoints.put(script, locations);
}
return new ExecuteEvent.Builder(process)
.withProject(project)
.withResource(resource)
.withBreakpoints(breakpoints)
.build();
}
@Override
public MessageEnvelope toMessage(ExecuteEvent event) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer);
Map> breakpoints = event.getBreakpoints();
Set scripts = breakpoints.keySet();
String process = event.getProcess();
String resource = event.getResource();
String project = event.getProject();
int breakpointSize = breakpoints.size();
output.writeUTF(process);
output.writeUTF(project);
output.writeUTF(resource);
output.writeInt(breakpointSize);
for(String script : scripts) {
Map locations = breakpoints.get(script);
Set lines = locations.keySet();
int locationSize = locations.size();
output.writeUTF(script);
output.writeInt(locationSize);
for(Integer line : lines) {
Boolean enable = locations.get(line);
output.writeInt(line);
output.writeBoolean(enable);
}
}
output.flush();
byte[] array = buffer.toByteArray();
return new MessageEnvelope(EXECUTE.code, array, 0, array.length);
}
}