KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > EscapeAgent


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.*;
29 import org.objectweb.mobilitools.smi.api.Agency;
30 import org.objectweb.mobilitools.smi.goodies.*;
31
32
33 /**
34  * MobiliTools $Name: $, $Id: EscapeAgent.java,v 1.1.1.1 2003/03/28 14:47:58 dillense Exp $
35  * <P>
36  * This agent example class shows how an agent may escape from an agency before it shuts down.
37  * <BR>The easiest way to try this example is to run at least 2 GUI-enabled agencies, to
38  * create at least one EscapeAgent instance in one of the agencies, and to shut it down.
39  * <P>NB: THIS EXAMPLE REQUIRES THE MAFFINDER TO BE RUNNING BEFORE LAUNCHING AGENCIES AND AGENTS.
40  */

41 public class EscapeAgent extends BasicMobileObject
42 {
43     Name my_name;
44
45
46     public EscapeAgent()
47     {
48     }
49
50
51     public void afterBirth(AgentSystem agency, AgentInfo entry, Object JavaDoc argument)
52         throws BadOperation
53     {
54         super.afterBirth(agency, entry, argument);
55         my_name = entry.getName();
56     }
57
58
59     /**
60      * Get the list of agencies in current region, and escape to the first one in the list (if any).
61      */

62     public synchronized void beforeShutdown()
63     {
64         try
65         {
66             Location[] locations = (new RegionManager(my_agency.getORB())).listAgencies(my_agency.getRegion(), null);
67             if (locations.length > 0)
68             {
69                 System.out.println(my_name + " is escaping to agency " + locations[0]);
70                 try
71                 {
72                     my_agency.moveLocalAgent(this, locations[0], "anyplace");
73                 }
74                 catch (BadOperation e)
75                 {
76                     System.err.println(e.getMessage());
77                 }
78             }
79             else
80             {
81                 System.out.println("No agency available - " + my_name + " giving up.");
82             }
83         }
84         catch (Exception JavaDoc e)
85         {
86             System.err.println("Can't get another agency - " + my_name + " giving up on exception:\n" + e.toString());
87         }
88     }
89
90
91     public void beforeDeath()
92     {
93         System.out.println(my_name + " is dead.");
94     }
95 }
96
Popular Tags