KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > tribe > adapters > MulticastRequestAdapterTest


1 /**
2  * Tribe: Group communication library.
3  * Copyright (C) 2002-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: tribe@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.tribe.adapters;
26
27 import java.io.Serializable JavaDoc;
28 import java.net.InetAddress JavaDoc;
29 import java.util.ArrayList JavaDoc;
30
31 import org.objectweb.tribe.channel.ReliableGroupChannelWithGms;
32 import org.objectweb.tribe.channel.tcp.TcpChannelPool;
33 import org.objectweb.tribe.common.GroupIdentifier;
34 import org.objectweb.tribe.common.IpAddress;
35 import org.objectweb.tribe.common.Member;
36 import org.objectweb.tribe.exceptions.AlreadyMemberException;
37 import org.objectweb.tribe.exceptions.ChannelException;
38 import org.objectweb.tribe.exceptions.NotConnectedException;
39 import org.objectweb.tribe.gms.GroupMembershipService;
40 import org.objectweb.tribe.gms.discovery.UdpDiscoveryService;
41 import org.objectweb.tribe.messages.MessageListener;
42
43 /**
44  * This class defines a MulticastRequestAdapterTest
45  *
46  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
47  * @version 1.0
48  */

49 public class MulticastRequestAdapterTest
50     implements
51       MessageListener,
52       MulticastRequestListener
53 {
54
55   private ReliableGroupChannelWithGms channel1;
56   private ReliableGroupChannelWithGms channel2;
57   private UdpDiscoveryService discovery;
58   private GroupMembershipService gms;
59   private final GroupIdentifier gid1 = new GroupIdentifier("group1");
60   private final String JavaDoc msg1 = "Hello from 1";
61   private final String JavaDoc msg2 = "Hello from 2";
62   private MulticastRequestAdapter adapter1;
63   private MulticastRequestAdapter adapter2;
64
65   /**
66    * Creates a new <code>MulticastRequestAdapterTest</code> object
67    *
68    * @throws Exception if an error occurs
69    */

70   public MulticastRequestAdapterTest() throws Exception JavaDoc
71   {
72     initializeChannels();
73   }
74
75   private void initializeChannels() throws Exception JavaDoc
76   {
77     final InetAddress JavaDoc MULTICAST_ADDRESS = InetAddress.getByName("224.7.65.23");
78     final int MULTICAST_PORT = 2288;
79     final IpAddress MULTICAST_IP = new IpAddress(MULTICAST_ADDRESS,
80         MULTICAST_PORT);
81     final InetAddress JavaDoc REPLY_ADDRESS = InetAddress.getLocalHost();
82     final int REPLY_PORT = 0; // Let the system choose a port
83
final IpAddress REPLY_IP = new IpAddress(REPLY_ADDRESS, REPLY_PORT);
84
85     discovery = new UdpDiscoveryService(MULTICAST_IP, REPLY_IP);
86     gms = new GroupMembershipService(REPLY_IP, TcpChannelPool.getChannelPool(),
87         discovery);
88     channel1 = new ReliableGroupChannelWithGms(gms);
89     channel2 = new ReliableGroupChannelWithGms(gms);
90
91     // Join
92

93     try
94     {
95       channel1.join(gid1);
96     }
97     catch (AlreadyMemberException e)
98     {
99       e.printStackTrace();
100       throw e;
101     }
102     try
103     {
104       channel2.join(gid1);
105     }
106     catch (AlreadyMemberException e)
107     {
108       e.printStackTrace();
109       throw e;
110     }
111
112     adapter1 = new MulticastRequestAdapter(channel1, this, this);
113     adapter2 = new MulticastRequestAdapter(channel2, this, this);
114
115   }
116
117   /**
118    * Test the group join/send/receive/quit primitives
119    */

120   public void sendMessages()
121   {
122     try
123     {
124       ArrayList JavaDoc members = channel1.getCurrentGroup().getMembers();
125       // Send msg1 on channel1
126
System.out.println("Sending '" + msg1 + "' to " + members.size()
127           + " members.");
128       MulticastResponse replies = adapter1.multicastMessage(members, msg1,
129           MulticastRequestAdapter.WAIT_ALL, 0);
130
131       // Display replies
132
for (int i = 0; i < members.size(); i++)
133       {
134         Member m = (Member) members.get(i);
135         Object JavaDoc answer = replies.getResult(m);
136         if (answer == null)
137           System.out.println("Member " + m + " failed.");
138         else
139           System.out.println("Member " + m + " replied: '" + answer + "'");
140       }
141
142       // Send msg2 on channel2
143
System.out.println("Sending '" + msg2 + "' to " + members.size()
144           + " members.");
145       adapter2.multicastMessage(members, msg2,
146           MulticastRequestAdapter.WAIT_ALL, 0);
147
148       // Display replies
149
for (int i = 0; i < members.size(); i++)
150       {
151         Member m = (Member) members.get(i);
152         Object JavaDoc answer = replies.getResult(m);
153         if (answer == null)
154           System.out.println("Member " + m + " failed.");
155         else
156           System.out.println("Member " + m + " replied: '" + answer + "'");
157       }
158     }
159     catch (Exception JavaDoc e1)
160     {
161       e1.printStackTrace();
162     }
163   }
164
165   /**
166    * Quit the group and closes the channels.
167    */

168   public void close()
169   {
170     // Stop the adapters
171
System.out.println("Stopping adapter1");
172     adapter1.stop();
173     System.out.println("Stopping adapter2");
174     adapter2.stop();
175
176     // Stop GMS and discovery
177
System.out.println("Stopping GMS");
178     gms.stop();
179     System.out.println("Stopping discovery service");
180     discovery.kill();
181
182     // Close the channels
183
try
184     {
185       System.out.println("Closing channel1");
186       channel1.close();
187     }
188     catch (ChannelException e)
189     {
190       e.printStackTrace();
191     }
192     catch (NotConnectedException e)
193     {
194       e.printStackTrace();
195     }
196     try
197     {
198       System.out.println("Closing channel2");
199       channel2.close();
200     }
201     catch (ChannelException e1)
202     {
203       e1.printStackTrace();
204     }
205     catch (NotConnectedException e1)
206     {
207       e1.printStackTrace();
208     }
209
210   }
211
212   //
213
// Message listener interface
214
//
215

216   /**
217    * @see org.objectweb.tribe.messages.MessageListener#receive(java.io.Serializable)
218    */

219   public void receive(Serializable JavaDoc msg)
220   {
221     System.out.println("Received msg: " + msg);
222   }
223
224   //
225
// MulticastRequestListener interface
226
//
227

228   /**
229    * Main method
230    *
231    * @param args command line arguments
232    */

233   public static void main(String JavaDoc[] args)
234   {
235     try
236     {
237       MulticastRequestAdapterTest adapter = new MulticastRequestAdapterTest();
238       adapter.sendMessages();
239       adapter.sendMessages();
240       adapter.sendMessages();
241       adapter.close();
242     }
243     catch (Exception JavaDoc e)
244     {
245       e.printStackTrace();
246     }
247   }
248
249   /**
250    * @see org.objectweb.tribe.adapters.MulticastRequestListener#handleMessageSingleThreaded(java.io.Serializable,
251    * org.objectweb.tribe.common.Member)
252    */

253   public Object JavaDoc handleMessageSingleThreaded(Serializable JavaDoc msg, Member sender)
254   {
255     System.out.println("Received single threaded '" + msg + "' from " + sender);
256     return "singleDone";
257   }
258
259   /**
260    * @see org.objectweb.tribe.adapters.MulticastRequestListener#handleMessageMultiThreaded(java.io.Serializable,
261    * org.objectweb.tribe.common.Member, java.lang.Object)
262    */

263   public Serializable JavaDoc handleMessageMultiThreaded(Serializable JavaDoc msg,
264       Member sender, Object JavaDoc handleMessageSingleThreadedResult)
265   {
266     System.out.println("Received multithreaded '" + msg + "' from " + sender
267         + " (" + handleMessageSingleThreadedResult + ")");
268     return "singleMulti";
269   }
270
271 }
Popular Tags