org.snapscript.agent.event.EvaluateEventMarshaller 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.EVALUATE;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
public class EvaluateEventMarshaller implements ProcessEventMarshaller {
@Override
public EvaluateEvent fromMessage(MessageEnvelope message) throws IOException {
byte[] array = message.getData();
int length = message.getLength();
int offset = message.getOffset();
Set expand = new HashSet();
ByteArrayInputStream buffer = new ByteArrayInputStream(array, offset, length);
DataInputStream input = new DataInputStream(buffer);
String process = input.readUTF();
String expression = input.readUTF();
String thread = input.readUTF();
boolean refresh = input.readBoolean();
int count = input.readInt();
for(int i = 0; i < count; i++) {
String path = input.readUTF();
expand.add(path);
}
return new EvaluateEvent.Builder(process)
.withExpression(expression)
.withRefresh(refresh)
.withThread(thread)
.withExpand(expand)
.build();
}
@Override
public MessageEnvelope toMessage(EvaluateEvent event) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer);
Set expand = event.getExpand();
String expression = event.getExpression();
String process = event.getProcess();
String thread = event.getThread();
boolean refresh = event.isRefresh();
int count = expand.size();
output.writeUTF(process);
output.writeUTF(expression);
output.writeUTF(thread);
output.writeBoolean(refresh);
output.writeInt(count);
for(String name : expand) {
output.writeUTF(name);
}
output.flush();
byte[] array = buffer.toByteArray();
return new MessageEnvelope(EVALUATE.code, array, 0, array.length);
}
}