001 package toolbus.adapter.eclipse;
002
003 import java.net.InetAddress;
004 import java.net.UnknownHostException;
005 import java.nio.ByteBuffer;
006
007 import toolbus.ToolBusEclipsePlugin;
008 import toolbus.adapter.java.AbstractJavaTool;
009 import aterm.ATerm;
010 import aterm.ATermAppl;
011 import aterm.ATermBlob;
012 import aterm.ATermList;
013 import aterm.pure.PureFactory;
014 import aterm.pure.binary.BinaryReader;
015
016 public class EclipseTool extends AbstractJavaTool{
017 protected static final String TIME_OUT = "time-out";
018
019 protected static PureFactory factory = ToolBusEclipsePlugin.getFactory();
020
021 private static InetAddress host;
022 static{
023 try{
024 host = InetAddress.getLocalHost();
025 }catch(UnknownHostException uhex){
026 uhex.printStackTrace();
027 }
028 }
029 private final static int port = ToolBusEclipsePlugin.getPort();
030
031 private final String name;
032
033 public EclipseTool(String name){
034 this.name = name;
035 }
036
037 public void connect(){
038 try{
039 //connect(new String[0]);
040 connectDirectly();
041 }catch(Exception e){
042 e.printStackTrace();
043 }
044 }
045
046 protected ATerm decode(ATerm encoded) {
047 if (encoded.getType() == ATerm.APPL) {
048 ATermAppl tmp = (ATermAppl) encoded;
049 ATermList chunkList = (ATermList) tmp.getArgument(0);
050 int nrOfChunks = chunkList.getLength();
051 BinaryReader reader = new BinaryReader((PureFactory) encoded.getFactory());
052
053 do {
054 ATermBlob chunk = (ATermBlob) chunkList.elementAt(--nrOfChunks);
055 byte[] data = chunk.getBlobData();
056
057 ByteBuffer buffer = ByteBuffer.allocate(data.length);
058 buffer.put(data);
059 buffer.flip();
060
061 reader.deserialize(buffer);
062 } while(nrOfChunks > 0);
063
064 return reader.getRoot();
065 }
066
067 return encoded;
068 }
069
070 private void connectDirectly() throws Exception{
071 connectDirectly(ToolBusEclipsePlugin.getToolBus(), name, -1);
072 }
073
074 public String getName() {
075 return name;
076 }
077
078 public void connect(String[] args) throws Exception{
079 EclipseToolBridge bridge = new EclipseToolBridge(factory, this, name, -1, host, port);
080 setToolBridge(bridge);
081 bridge.run();
082 }
083
084 public void receiveTerminate(ATerm aTerm){
085 // Intentionally left empty.
086 }
087
088 public void receiveAckEvent(ATerm aTerm){
089 // Intentionally left empty.
090 }
091 }