KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SmiTest


1 /*
2 * MobiliTools: an implementation of the Object Management Group's
3 * Mobile Agent Facility specification.
4 * Copyright (C) 2003 France Telecom R&D
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * MobiliTools $Name: $
21 *
22 * Contact: bruno.dillenseger@francetelecom.com
23 *
24 * Authors: Bruno Dillenseger
25 */

26
27
28 import org.objectweb.mobilitools.smi.api.Agency;
29 import org.objectweb.mobilitools.smi.api.*;
30 import java.applet.*;
31 import java.io.*;
32
33
34 /**
35  * MobiliTools $Name: $, $Id: SmiTest.java,v 1.1.1.1 2003/03/28 14:47:58 dillense Exp $
36  * <P>
37  * This example class shows MobileObject lifecycle through a console-based user interaction, as well as resource loading.
38  * The easiest way to use this example is to use GUI-enabled agencies.
39  * <BR>Note that this agent has no autonomous activity.
40  */

41 public class SmiTest implements MobileObject
42 {
43     /** display-friendly name, set once for all at creation */
44     String JavaDoc my_name;
45     /** true if the agent is currently active, false if it is suspended */
46     boolean running;
47     /** reference to current host agency, set after each move - must be transient because non-serializable */
48     transient Agency my_agency;
49
50
51     /**
52      * Default constructor is invoked when no argument is provided for agent creation.
53      */

54     public SmiTest()
55     {
56         System.out.println("SmiTest default constructor");
57     }
58
59
60     /**
61      * (Generic) Example of specific constructor. Displays the argument provided
62      * for agent creation.
63      */

64     public SmiTest(Object JavaDoc arg)
65     {
66         System.out.println("SmiTest constructor with argument " + arg);
67     }
68
69
70     //////////////////////////////////////////////
71
// implementation of MobileObject interface //
72
//////////////////////////////////////////////
73

74
75     /**
76      * Displays a message and plays a sound on agent creation. Aks confirmation of creation.
77      * Answer 'y' or 'n' to respectively accept or deny agent creation. If creation is denied,
78      * the creation-caller gets an exception.
79      */

80     public void afterBirth(AgentSystem agency, AgentInfo entry, Object JavaDoc argument)
81         throws BadOperation
82     {
83         my_agency = (Agency)agency;
84         my_name = new String JavaDoc(entry.getName().identity());
85         System.out.println("agent " + (new Name(entry.getName().getStringRepresentation())).toString());
86         running = true;
87         playSound("12312.au");
88         if (! confirm(my_name + ".afterBirth() is OK ([y]/n)?", true))
89         {
90             throw new BadOperation(BadOperation.REJECTED, "Agent creation is denied.");
91         }
92     }
93
94
95     /**
96      * Asks confirmation before moving. Answer 'y' or 'n' in the console to respectively
97      * confirm or abort the move. In case you abort, an exception is raised, the agent
98      * is not serialized, and the move-caller gets an exception.
99      */

100     public void beforeMove(Location location, String JavaDoc place)
101         throws BadOperation
102     {
103         if (! confirm(
104             my_name
105             + ".beforeMove("
106             + location.getRegion() + " "
107             + location.getAgency() + ", "
108             + place + ") is OK ([y]/n)?",
109             true))
110         {
111             throw new BadOperation(BadOperation.REJECTED, "Agent move is denied.");
112         }
113     }
114
115
116     /**
117      * Plays a sound and asks installation confirmation after deserialization in the target agency.
118      * Answer 'y' or 'n' to respectively accept or deny installation. Denying will drop the agent copy
119      * in the target agency and invoke <code>afterMoveFailed()</code> on the agent in source agency.
120      */

121     public void afterMove(AgentSystem agency, Location location, String JavaDoc place)
122         throws BadOperation
123     {
124         my_agency = (Agency)agency;
125         playSound("12312.au");
126         if (!confirm(
127             my_name
128             + ".afterMove("
129             + location.getRegion() + " "
130             + location.getAgency() + ", "
131             + place + ") is OK ([y]/n)?",
132             true))
133         {
134             throw new BadOperation(BadOperation.REJECTED, "Agent re-installation is denied.");
135         }
136     }
137
138
139     /**
140      * Plays a sound and displays a message in the source agency after the agent has been serialized
141      * but its installation in the target agency has failed (probably rejected by the agent itself)
142      */

143     public void afterMoveFailed(
144         Location location,
145         String JavaDoc place,
146         int reason,
147         String JavaDoc message)
148     {
149         playSound("12312.au");
150         System.out.println(
151             my_name
152             + ".afterMoveFailed("
153             + location.getRegion() + " "
154             + location.getAgency() + ", "
155             + place + ")");
156         System.out.println("code " + String.valueOf(reason) + ": " + message);
157     }
158
159
160     /**
161      * Displays a message on host agency shutdown
162      */

163     public void beforeShutdown()
164     {
165         System.out.println(my_name + ".beforeShutdown()");
166     }
167
168
169     /**
170      * Displays a message on termination
171      */

172     public void beforeDeath()
173     {
174         System.out.println(my_name + ".beforeDeath()");
175     }
176
177
178     /**
179      * Displays an acknowledgement message on agent activity resumption if and only if it was suspended,
180      * otherwise the resume-caller gets an exception.
181      */

182     public void beforeResume()
183         throws BadOperation
184     {
185         if (running)
186         {
187             throw new BadOperation(BadOperation.RUNNING, "Agent activity was already running.");
188         }
189         running = true;
190         System.out.println(my_name + ".resume()");
191     }
192
193
194     /**
195      * Displays an acknowledgement message on agent activity suspension if and only if it was active,
196      * otherwise the suspend-caller gets an exception.
197      */

198     public void beforeSuspend()
199         throws BadOperation
200     {
201         if (! running)
202         {
203             throw new BadOperation(BadOperation.RUNNING, "Agent activity was already suspended.");
204         }
205         running = false;
206         System.out.println(my_name + ".suspend()");
207     }
208
209
210     /////////////////////////
211
// own support methods //
212
/////////////////////////
213

214
215     /**
216      * Displays a message and reads a confirmation ('y' or 'n' whatever upper or lower case)
217      * in the attached console/terminal
218      * @param message confirmation question to be displayed
219      * @param defaultYes default answer (i.e. when neither 'y' nor 'n' is entered)
220      * @return true if the user answered 'y', false if the user answered 'n', defaultYes otherwise.
221      */

222     boolean confirm(String JavaDoc message, boolean defaultYes)
223     {
224         try
225         {
226             System.out.print(message);
227             String JavaDoc reply = (new BufferedReader(new InputStreamReader(System.in))).readLine();
228             return reply.equalsIgnoreCase("y") || (defaultYes && ! reply.equalsIgnoreCase("n"));
229         }
230         catch (IOException e)
231         {
232             e.printStackTrace();
233         }
234         return defaultYes;
235     }
236
237
238     /**
239      * Loads a sound designated by the provided resource name using the agent's class classloader and plays it.
240      * @param resourcename the name of the resource (should be an audio file)
241      */

242     void playSound(String JavaDoc resourcename)
243     {
244         try
245         {
246             Applet.newAudioClip(getClass().getClassLoader().getResource(resourcename)).play();
247         }
248         catch (Exception JavaDoc ex)
249         {
250             System.err.println("could not load and play audio resource " + resourcename + "\n" + ex);
251
252         }
253     }
254 }
255
Popular Tags