Lab' Work 2 Basic Programming Examples Kenji Taguchi Dept' Information Science Uppsala University - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Lab' Work 2 Basic Programming Examples Kenji Taguchi Dept' Information Science Uppsala University

Description:

{ deactivate(10 * SECOND); print('You should never see this on your console! ... { print('Failed to deactivate itself.'); print(e.getMessage()); pause(); pause ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 29
Provided by: user62
Category:

less

Transcript and Presenter's Notes

Title: Lab' Work 2 Basic Programming Examples Kenji Taguchi Dept' Information Science Uppsala University


1
Lab. Work 2- Basic Programming Examples -
Kenji TaguchiDept. Information ScienceUppsala
University
2
Example Programs
  • CreationExample.java
  • CreationChild.Java
  • DisposalExample.java
  • CloningExample.java
  • DispatchingExample.java
  • MA.java
  • RetractionExample.java
  • RetractionChild.java
  • DeactivationExample.java

3
CreationExample
import com.ibm.aglet. public class
CreationExample extends Aglet public void
run() print("STARTING --------------------
") print("Creating the child...")
try getAgletContext().createAglet(getCodeB
ase(), "CreationChild", null)
print("Finished creating the child.")
catch (Exception e) print("Failed to
create the child.") print(e.getMessage()
) static public String NAME
"Creation" private void print(String s)
System.out.println(NAME " (parent) " s)
4
CreationChild
import com.ibm.aglet. public class
CreationChild extends Aglet public
CreationChild() print("Constructor is
starting...") pause() print("Constructor is
finishing.") public void
onCreation(Object init)
print("\'onCreation()\' is starting...")
pause() print("\'onCreation()\' is
finishing.") public void run()
print("\'run()\' is starting...") pause()
print("\'run()\' is finishing.")
private void print(String s) System.out.println(
" (child) " s) private static long
SLEEP 3000 private void pause() try
Thread.sleep(SLEEP) catch (InterruptedException
ie)
5
DisposalExample
import com.ibm.aglet. public class
DisposalExample extends Aglet public void
onDisposing() print("\'onDisposing()\' is
starting...") pause() print("\'onDisposing
()\' is finishing.") public void run()
print("STARTING --------------------")
print("\'run()\' is starting...")
print("Disposing of itself...") dispose()
print("You should never see this on your
console!") static public String NAME
"Disposal" private void print(String s)
System.out.println(NAME " (itself) " s)
private static long SLEEP 3000 private
void pause() try Thread.sleep(SLEEP) catch
(InterruptedException ie)
6
CloningExample (1)
import com.ibm.aglet. import com.ibm.aglet.event
. public class CloningExample extends Aglet
boolean _theClone false public void
onCreation(Object o) addCloneListener(
new CloneAdapter() public void
onCloning(CloneEvent e)
print("\'onCloning()\' is starting...")
try print("\'onCloning()
\' Cloned by \'" e.getAgletProxy().getAgletID()
"\'") catch (InvalidAgletException iae)
pause()
print("\'onCloning()\' is finishing.")

7
CloningExample (2)
public void onClone(CloneEvent e)
_theClone true //-- Yes, I am the
clone. print("\'onClone()\' is starting...")
pause()
print("\'onClone()\' is finishing.")
public void onCloned(CloneEvent e)
print("\'onCloned()\' is starting...")
pause()
print("\'onCloned()\' is finishing.")
)
8
CloningExample (3)
public void run() if (!_theClone)
print("STARTING --------------------")
print("\'run()\' is starting...")
pause() print("Cloning...")
try clone()
print("Finished cloning.") catch
(Exception e) print("Failed to
clone.") print(e.getMessage())
pause() pause()
print("\'run()\' is finishing.") else
print("\'run()\' is starting...")
pause() print("\'run()\' is
finishing.")
9
CloningExample (4)
static public String NAME "Cloning" void
print(String s) System.out.println(NAME
(_theClone ? " (clone) " " (original) ")
s) private static long SLEEP 3000
void pause() try Thread.sleep(SLEEP) catch
(InterruptedException ie)
10
Caution
  • Once the java file CloneExample.java is compiled,
    you can find two files,
  • CloneExample.class
  • CloneExample1.class
  • Copy those files under
  • /aglets1.1.0/public

11
Event Listeners in Java
  • Event Listeners will handle events.
  • A listener generally includes many methods, which
    might have empty bodies.
  • So that an adapter class is introduced to avoid
    making a messy program.

12
DispatchingExample (1)
import com.ibm.aglet. import com.ibm.aglet.event
. import java.net.URL public class
DispatchingExample extends Aglet boolean
_theRemote false public void
onCreation(Object o) addMobilityListener(
new MobilityAdapter() public void
onDispatching(MobilityEvent e)
print("\'onDispatching()\' is starting...")
print("\'onDispatching()\' Destination
is \'" e.getLocation() "\'")
pause() print("\'onDispatching()\'
is finishing.")
13
DispatchingExample (2)
public void onArrival(MobilityEvent e)
_theRemote true //-- Yes, I am the
remote aglet. print("\'onArrival()\
' is starting...") pause()
print("\'onArrival()\' is finishing.")
)
14
DispatchingExample (3)
public void run() if (!_theRemote)
print("STARTING --------------------")
print("\'run()\' is starting...")
pause() try
print("Dispatching itself...")
String host getAgletContext().getHostingURL().to
String() URL destination new
URL((String)getAgletContext().getProperty("locatio
n", host)) print("Destination is \'"
destination.toString() "\'")
dispatch(destination) print("You
should never see this on your console!")
catch (Exception e)
print("Failed to dispatch itself.")
print(e.getMessage())
pause() pause() print("\'run()\' is
finishing.") else
print("\'run()\' is starting...") pause()
print("\'run()\' is finishing.")
15
DispatchingExample (4)
static public String NAME "Dispatching"
void print(String s) System.out.println(NAME
(_theRemote ? " (remote) " " (original) ")
s) private static long SLEEP 3000
void pause() try Thread.sleep(SLEEP) catch
(InterruptedException ie)
16
MA (1)
import com.ibm.aglet. import com.ibm.aglet.event
. import java.net.URL public class MA extends
Aglet boolean _theRemote false
public URL remoteContext public void
onCreation( Object init ) System.out.println("I
am created ...") addMobilityListener(
new MobilityAdapter() public void
onDispatching(MobilityEvent e) print("\'onDis
patching()\' is starting...") print("\'onDispa
tching()\' Destination is \'" remoteContext
"\'") pause() print("\'onDispatching()\'
is finishing.")
17
MA (2)
public void onArrival(MobilityEvent e)
_theRemote true //-- Yes, I am the
remote aglet. print("\'onArrival
()\' is starting...") pause() print("\'onA
rrival()\' I arrived at \'" e.getLocation()
"\'") print("\'onArrival()\' is
finishing.") )
18
MA (3)
public void run() if (!_theRemote)
setText("Hello, my name is " NAME ".")
try remoteContext new URL("atp//ida.dis.uu.s
e9901") // Change this destination try
dispatch(remoteContext) catch ( Exception
e) System.out.println(e.getMessage())
catch ( Exception e) System.out.println(
e.getMessage()) else
print("\'run()\' is starting...")
pause() print("\'run()\' is
finishing.")
19
MA (4)
public void onDisposing() System.out.print
ln("Bye ...") static public String
NAME "MA" void print(String s)
System.out.println(NAME (_theRemote ? "
(remote) " " (original) ") s) private
static long SLEEP 3000 void pause() try
Thread.sleep(SLEEP) catch (InterruptedExcepti
on ie)
20
RetractionExample (1)
import com.ibm.aglet. import java.net.URL publ
ic class RetractionExample extends Aglet
public void run() print("STARTING
--------------------") try
print("Creating the child...")
AgletProxy proxy getAgletContext().createAglet(g
etCodeBase(), "RetractionChild", null)
try String host
getAgletContext().getHostingURL().toString()
URL destination new URL((String)getAglet
Context().getProperty("location", host))
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...")
21
RetractionExample (2)
try getAgletContext().retractAgle
t(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)
22
RetractionChild (1)
import com.ibm.aglet. import com.ibm.aglet.event
. import java.net.URL public class
RetractionChild extends Aglet boolean
_reverted false boolean _dispatched
false public void onCreation(Object o)
addMobilityListener( new
MobilityAdapter() public void
onReverting(MobilityEvent e)
_reverted true
print("\'onReverting()\' is starting...")
pause()
print("\'onReverting()\' is finishing.")

23
RetractionChild (2)
public void onArrival(MobilityEvent e)
_dispatched true if
(_reverted)
print("\'onArrival()\' is starting...")
pause()
print("\'onArrival()\' is finishing.")

)
24
RetractionChild (3)
public void run() if (_dispatched)
print("\'run()\' is starting...")
pause() pause() print("\'run()\' is
finishing.") void print(String
s) System.out.println(" (child) " s)
private static long SLEEP 3000 void pause()
try Thread.sleep(SLEEP) catch
(InterruptedException ie)
25
DeactivationExample (1)
import com.ibm.aglet. import com.ibm.aglet.event
. public class DeactivationExample extends
Aglet private static long SECOND 1000
boolean _activated false public void
onCreation(Object o) addPersistencyListener(
new PersistencyAdapter() public
void onDeactivating(PersistencyEvent e)
print("\'onDeactivating()\' is
starting...")
print("\'onDeactivating()\' Duration is \'"
e.getDuration() "\'")
pause() print("\'onDeactivating(
)\' is finishing.")

26
DeactivationExample (2)
public void onActivation(PersistencyEvent e)
_activated true //-- Yes, I am
the activated aglet.
print("\'onActivation()\' is starting...")
pause()
print("\'onActivation()\' is finishing.")
)
27
DeactivationExample (3)
public void run() if (!_activated)
print("STARTING --------------------")
print("\'run()\' is starting...")
pause() print("Deactivating
itself...") try
deactivate(10 SECOND) print("You
should never see this on your console!")
catch (Exception e)
print("Failed to deactivate itself.")
print(e.getMessage())
pause() pause() print("\'run()\' is
finishing.") else
print("\'run()\' is starting...")
pause() print("\'run()\' is
finishing.")
28
DeactivationExample (4)
static public String NAME "Deactivation"
void print(String s) System.out.println(NAME
(_activated ? " (activated) " " (original)
") s) private static long SLEEP 3000
void pause() try Thread.sleep(SLEEP)
catch (InterruptedException ie)
Write a Comment
User Comments (0)
About PowerShow.com