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

org.snapscript.studio.agent.local.LocalMessageConsumer Maven / Gradle / Ivy

There is a newer version: 1.4.6
Show newest version
package org.snapscript.studio.agent.local;

import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;

import org.snapscript.studio.agent.local.message.AttachRequest;
import org.snapscript.studio.agent.local.message.AttachResponse;
import org.snapscript.studio.agent.local.message.DetachRequest;
import org.snapscript.studio.agent.local.message.DetachResponse;

public class LocalMessageConsumer {

   private final LocalProcessController listener;
   
   public LocalMessageConsumer(LocalProcessController listener){
      this.listener = listener;
   }
   
   public void consume(Socket socket) {
      try {
         InputStream in = socket.getInputStream();
         OutputStream out = socket.getOutputStream();
         ObjectInput input = new ObjectInputStream(in);
         ObjectOutput output = new ObjectOutputStream(out);
         Object value = input.readObject();
         
         if(AttachRequest.class.isInstance(value)) {
            String process = listener.attachRequest((AttachRequest)value);
            AttachResponse response = new AttachResponse(process);
            
            try {
               output.writeObject(response);
            }finally {
               output.close();
            }
         }
         if(DetachRequest.class.isInstance(value)) {
            String process = listener.detachRequest((DetachRequest)value);
            DetachResponse response = new DetachResponse(process);
            
            try {
               output.writeObject(response);
            }finally {
               output.close();
            }
         }
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy