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

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


package org.snapscript.agent.event;

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

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 BreakpointsEventMarshaller implements ProcessEventMarshaller {

   @Override
   public BreakpointsEvent 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();
      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 BreakpointsEvent.Builder(process)
         .withBreakpoints(breakpoints)
         .build();
   }

   @Override
   public MessageEnvelope toMessage(BreakpointsEvent event) throws IOException {
      ByteArrayOutputStream buffer = new ByteArrayOutputStream();
      DataOutputStream output = new DataOutputStream(buffer);
      Map> breakpoints = event.getBreakpoints();
      Set scripts = breakpoints.keySet();
      String process = event.getProcess();
      int breakpointSize = breakpoints.size();
      
      output.writeUTF(process);
      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(BREAKPOINTS.code, array, 0, array.length);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy