package pddemo;

import com.ibm.aglet.*;

import java.net.URL;

public class RetractionExample extends Aglet {

   public void run() {
      print("STARTING");
      try {
      	 print("Creating the child...");
         AgletProxy proxy = getAgletContext().createAglet(getCodeBase(), 
      	                                                  "pddemo.RetractionChild", 
      	                                                  null);
         try {
	    String host = getAgletContext().getHostingURL().toString();
		String dest = "atp://cx9108464:500/"; // change
            URL destination = new URL (dest);
	    
            print("Destination is \'" + destination.toString() + "\'");
            print("Dispatching the child...");
            AgletID aid = proxy.getAgletID();
            proxy.dispatch(destination);
            print("Finished dispatching the child.");
            print("Retracting the child...");
            try {
               getAgletContext().retractAglet(destination, aid);
               print("Finished retracting the child.");
            }  catch (Exception e) { 
               print("Failed to retract the child.");
               print(e.getMessage());
            }
         }  catch (Exception e) { 
            print("Failed to dispatch the child.");
            print(e.getMessage());
         }
      } catch (Exception e) { 
         print("Failed to create the child.");
         print(e.getMessage());
      }
   }

   static public String NAME = "Retraction";
   private void print(String s) { System.out.println(NAME + " (parent): " + s); }
   private static long SLEEP = 3000;
   private void pause() { try { Thread.sleep(SLEEP); } catch (InterruptedException ie) { } }
}
