KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PingPong


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.goodies.BasicMobileObject;
30 import org.objectweb.mobilitools.smi.api.Agency;
31 import java.util.Properties JavaDoc;
32
33
34 /**
35  * MobiliTools $Name: $, $Id: PingPong.java,v 1.1.1.1 2003/03/28 14:47:58 dillense Exp $
36  * <P>
37  * This agent example class shows the visibility rules between classes of
38  * agents loaded with different classloaders and, more important, with different codebases.
39  * These rules typically depends on the class loader adapter (factory) used.
40  * By default, SMI uses the SMIClassLoader which comes with the following principles:
41  * <UL>
42  * <LI>each agent has its own SMIClassLoader instance to get locally unresolved classes
43  * <LI>however, classloaders with the same codebase share the same class objects
44  * </UL>
45  * As a result, agents with the same codebase may use/invoke each other's classes/instances,
46  * while agents of different codebases will get an exception trying to do so.
47  * <P>
48  * The easiest way to use this example is to use GUI-enabled agencies. Create, suspend/resume
49  * or move PingPong agents with a variety of codebases, to see how they can interact with each
50  * other in an agency (have a look at the messages printed in the associated terminal).
51  */

52 public class PingPong extends BasicMobileObject
53 {
54     String JavaDoc my_name;
55
56
57     public PingPong()
58     {
59     }
60
61
62     /**
63      * This method is invoked on each other by PingPong agents.
64      */

65     public String JavaDoc ping(PingPong agent)
66     {
67         return my_agency.getAgentInfo(agent).getName().toString() + " pings " + my_name;
68     }
69
70
71     ////////////////////////////////
72
// override BasicMobileObject //
73
////////////////////////////////
74

75
76     /**
77      * Set an appropriate property for the PingPong agent to be found by other PingPong agents.
78      */

79     public void afterBirth(AgentSystem agency, AgentInfo info, Object JavaDoc arg)
80     throws BadOperation
81     {
82         super.afterBirth(agency, info, arg);
83         my_name = info.getName().toString();
84         try
85         {
86             my_agency.setLocalAgentProperty(this, "ping-pong", "yes");
87         }
88         catch (BadOperation ex)
89         {
90             ex.printStackTrace();
91         }
92         beforeResume();
93     }
94
95
96     /**
97      * Lookup PingPong agents in current agency and try to invoke their <code>ping()</code> method
98      */

99     public void beforeResume()
100     {
101         Properties JavaDoc prop = new Properties JavaDoc();
102         prop.put("ping-pong", "yes");
103         Name[] list = my_agency.listLocalAgents(my_agency.makeAgentProfile(prop));
104         for (int i = 0 ; i < list.length ; ++i)
105         {
106             AgentInfo info = my_agency.getAgentInfo(list[i]);
107             try
108             {
109                 PingPong agent = (PingPong)my_agency.getAgentInfo(list[i]).getAgent();
110                 if (agent != this)
111                 {
112                     System.out.println(agent.ping(this));
113                 }
114             }
115             catch (ClassCastException JavaDoc ex)
116             {
117                 System.err.println(my_name + " gets " + ex + " with agent " + info.getName());
118             }
119         }
120     }
121
122
123     /**
124      * Lookup PingPong agents in current agency and try to invoke their <code>ping()</code> method
125      */

126     public void afterMove(AgentSystem agency, Location location, String JavaDoc place)
127     throws BadOperation
128     {
129         super.afterMove(agency, location, place);
130         beforeResume();
131     }
132 }
133
Popular Tags