All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.snapscript.agent.event.FaultEventMarshaller Maven / Gradle / Ivy

There is a newer version: 1.4.6
Show newest version

package org.snapscript.agent.event;

import static org.snapscript.agent.event.ProcessEventType.FAULT;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Map;

import org.snapscript.agent.debug.ScopeVariableTree;

public class FaultEventMarshaller implements ProcessEventMarshaller {
   
   private final MapMarshaller marshaller;
   
   public FaultEventMarshaller() {
      this.marshaller = new MapMarshaller();
   }

   @Override
   public FaultEvent 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);
      String process = input.readUTF();
      String resource = input.readUTF();
      String thread = input.readUTF();
      String cause = input.readUTF();
      int line = input.readInt();
      int change = input.readInt();
      Map> local = marshaller.readMap(input);
      Map> evaluation = marshaller.readMap(input);
      
      ScopeVariableTree tree = new ScopeVariableTree.Builder(change)
         .withLocal(local)
         .withEvaluation(evaluation)
         .build();
     
      return new FaultEvent.Builder(process)
         .withVariables(tree)
         .withThread(thread)
         .withCause(cause)
         .withResource(resource)
         .withLine(line)
         .build();
      
   }

   @Override
   public MessageEnvelope toMessage(FaultEvent event) throws IOException {
      ByteArrayOutputStream buffer = new ByteArrayOutputStream();
      DataOutputStream output = new DataOutputStream(buffer);
      ScopeVariableTree tree = event.getVariables();
      Map> local = tree.getLocal();
      Map> evaluation = tree.getEvaluation();
      String process = event.getProcess();
      String resource = event.getResource();
      String thread = event.getThread();
      String cause = event.getCause();
      int change = tree.getChange();
      int line = event.getLine();
      
      output.writeUTF(process);
      output.writeUTF(resource);
      output.writeUTF(thread);
      output.writeUTF(cause);
      output.writeInt(line);
      output.writeInt(change);
      marshaller.writeMap(output, local);
      marshaller.writeMap(output, evaluation);
      output.flush();
      
      byte[] array = buffer.toByteArray();
      return new MessageEnvelope(FAULT.code, array, 0, array.length);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy