Java Mobile Agents & The Aglet SDK
by M. Tim Jones 

Example 1:
(a)
cd /usr/local/src
mv [where-you-downloaded-it]/Aglets_1.1.0.tar.gz .
tar xvfz Aglets_1.1.0.tar.gz

(b)
### Configurable part
JDK_HOME=/usr/java/jdk118_v1
AGLET_HOME=/usr/local/src/aglets1.1.0

Example 2:
(a)
Ticket ticket = new Ticket("atp://plato.mtjones.com");

// Prepare to migrate...
dispatch(ticket);

(b)
transient int localData = 0;


Listing One
// collector.java - Example Aglet.
// M. Tim Jones <mtj@mtjones.com>

import com.ibm.aglet.*;
import com.ibm.aglet.event.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.MalformedURLException;

public class collector extends Aglet {
  StringBuffer buffer;
  int stage = 0;
  //--------------------------------------------------------------------
  // onCreation()
  //--------------------------------------------------------------------
  public void onCreation(Object ini) {
    buffer = new StringBuffer();
    buffer.append("Collected Information:\r\n\r\n");
    addMobilityListener(new MobilityAdapter() {
      public void onArrival(MobilityEvent e) {
        getCurrentNodeInfo();
      }
    });
  }
  //--------------------------------------------------------------------
  // getCurrentNodeInfo()
  //--------------------------------------------------------------------
  private void getCurrentNodeInfo()
  {
    BufferedReader inReader;
    String line;
    String iface;

    buffer.append( "  Node " + 
                    getAgletContext().getHostingURL().toString()+" - \r\n");
    buffer.append( "    OS : " + 
                    System.getProperty("os.name") + " / " +
                    System.getProperty("os.version") + " / " +
                    System.getProperty("os.arch")+"\r\n" );
    buffer.append( "    Java Vers : " + 
                    System.getProperty("java.version")+"\r\n" );
    try {
      inReader = new BufferedReader(new FileReader("/proc/net/dev"));
      do {
        line = inReader.readLine();
        if (line != null) {
          iface = line.substring(0, 6).trim();
          if (iface.compareTo("eth0") == 0) {
            buffer.append(  "    " + line.substring(0, 6).trim() + ":" + 
                            "   Rx Bytes : " + parseItem(line, 1) + 
                            "   Tx Bytes : " + parseItem(line, 9) + "\r\n");
          }
        }
      } while (line != null);
    } catch (Exception excpt) { 
      excpt.printStackTrace();
    }
    buffer.append("\r\n");
  }
  private String parseItem(String line, int number) {
    int retValue = 0, startIndex = 7, endIndex = 0;
    if ((line != null) && ( number > 0)) {
      while (line.charAt(startIndex) == ' ') startIndex++;
      number--;
      while (number-- != 0) {
        // Find the start of a numeric entry
        while (line.charAt(startIndex) == ' ') startIndex++;
        // Skip a number
        while (line.charAt(startIndex) != ' ') startIndex++;
        while (line.charAt(startIndex) == ' ') startIndex++;
      }
      endIndex = startIndex + 1;
      // Find the end of a numeric entry
      while (line.charAt(endIndex) != ' ') endIndex++;
      return(line.substring(startIndex, endIndex));
    }
    return(null);
  }
  //--------------------------------------------------------------------
  // run()
  //--------------------------------------------------------------------
  public void run() {
    // Define a simple Quality of Communication
    QoC qoc = new QoC(QoC.NORMALINTEGRITY, QoC.NOCONFIDENTIALITY);
    try {
      if (stage == 0) {
        Ticket ticket = new Ticket("atp://192.168.1.5/", qoc);
        if (ticket == null) {
          System.out.println("Couldn't get ticket to alto");
          System.exit(-1);
        }
        try {
          stage = 1;
          dispatch(ticket);
        } catch(Exception excpt) {
          excpt.printStackTrace();
        }
      } else if (stage == 1) {
        Ticket ticket = new Ticket("atp://192.168.1.7/", qoc);
        stage = 2;
        if (ticket == null) {
          System.out.println("Couldn't get ticket to plato");
          System.exit(-1);
        }
        try {
          dispatch(ticket);
        } catch(Exception excpt) {
          excpt.printStackTrace();
        }
      } else if (stage == 2) {
        resultsWindow window = null;
        window = new resultsWindow();
        window.appendText(buffer);
        window.pack();
        window.show();
        stage = 3;
      }
    } catch(MalformedURLException excpt) {
      excpt.printStackTrace();
      System.exit(-1);
    }
  }
}
class resultsWindow extends Frame implements ActionListener {
  TextArea text = new TextArea();
  public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();
  }
  public resultsWindow() {
    super("Results");
    setLayout(new BorderLayout(5, 5));
    add("Center", text);

    text.setEditable(false);

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        setVisible(false);
      }
    });
  }
  public void appendText(StringBuffer str) {
    text.append( str.toString() + "\r\n");
  }
}


Listing Two
# Makefile
JDK_HOME = /usr/java/jdk118_v1
JAVAC = $(JDK_HOME)/bin/javac
CLASSPATH = /usr/local/src/aglets1.1.0/src:$(JDK_HOME)/lib/classes.zip

AGLETSLIBPATH = /usr/local/src/aglets1.1.0/lib
AGLETSPUBPATH = /usr/local/src/aglets1.1.0/public

JAVACPUB = $(JAVAC) -deprecation -classpath $(AGLETSLIBPATH):
                        $(AGLETSPUBPATH):$(CLASSPATH) -d $(AGLETSPUBPATH)
migtest:
    $(JAVACPUB) collector.java





1


