~ubuntu-branches/ubuntu/wily/libjboss-remoting-java/wily

« back to all changes in this revision

Viewing changes to src/org/jboss/remoting/detection/multicast/MulticastDetector.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
* JBoss, Home of Professional Open Source
3
 
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
 
* by the @authors tag. See the copyright.txt in the distribution for a
5
 
* full listing of individual contributors.
6
 
*
7
 
* This is free software; you can redistribute it and/or modify it
8
 
* under the terms of the GNU Lesser General Public License as
9
 
* published by the Free Software Foundation; either version 2.1 of
10
 
* the License, or (at your option) any later version.
11
 
*
12
 
* This software is distributed in the hope that it will be useful,
13
 
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
 
* Lesser General Public License for more details.
16
 
*
17
 
* You should have received a copy of the GNU Lesser General Public
18
 
* License along with this software; if not, write to the Free
19
 
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
 
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21
 
*/
22
 
package org.jboss.remoting.detection.multicast;
23
 
 
24
 
import org.jboss.remoting.detection.AbstractDetector;
25
 
import org.jboss.remoting.detection.Detection;
26
 
import org.jboss.remoting.util.SecurityUtility;
27
 
 
28
 
import java.io.ByteArrayInputStream;
29
 
import java.io.ByteArrayOutputStream;
30
 
import java.io.IOException;
31
 
import java.io.ObjectInputStream;
32
 
import java.io.ObjectOutputStream;
33
 
import java.net.DatagramPacket;
34
 
import java.net.InetAddress;
35
 
import java.net.InetSocketAddress;
36
 
import java.net.MulticastSocket;
37
 
import java.net.SocketAddress;
38
 
import java.net.UnknownHostException;
39
 
import java.security.AccessController;
40
 
import java.security.PrivilegedActionException;
41
 
import java.security.PrivilegedExceptionAction;
42
 
 
43
 
/**
44
 
 * MulticastDetector is a remoting detector that broadcasts detection messages using
45
 
 * muliticast.  The default multicast ip is 224.1.9.1 and port 2410.
46
 
 *
47
 
 * @author <a href="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
48
 
 * @author <a href="mailto:adrian.brock@happeningtimes.com">Adrian Brock</a>
49
 
 * @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
50
 
 * @version $Revision: 5000 $
51
 
 */
52
 
public class MulticastDetector extends AbstractDetector implements MulticastDetectorMBean
53
 
{
54
 
   private static int threadCounter = 0;
55
 
 
56
 
   private String defaultIP = "224.1.9.1";
57
 
 
58
 
   private InetAddress addr;
59
 
   private InetAddress bindAddr;
60
 
   private int port = 2410;
61
 
   private MulticastSocket socket;
62
 
   private Listener listener = new Listener("Remoting Multicast Detector - Listener Thread: " + threadCounter++);
63
 
   private int bufferSize = 10000;
64
 
 
65
 
 
66
 
   /**
67
 
    * @return The IP that is used to broadcast detection messages on via multicast.
68
 
    */
69
 
   public String getDefaultIP()
70
 
   {
71
 
      return defaultIP;
72
 
   }
73
 
 
74
 
   /**
75
 
    * @param defaultIP The IP that is used to broadcast detection messages on via multicast.
76
 
    */
77
 
   public void setDefaultIP(String defaultIP)
78
 
   {
79
 
      this.defaultIP = defaultIP;
80
 
   }
81
 
 
82
 
   /**
83
 
    * return the multicast address of the detector
84
 
    *
85
 
    * @return
86
 
    */
87
 
   public InetAddress getAddress()
88
 
   {
89
 
      return addr;
90
 
   }
91
 
 
92
 
   /**
93
 
    * set the interface address of the multicast
94
 
    *
95
 
    * @param ip
96
 
    */
97
 
   public void setAddress(InetAddress ip)
98
 
   {
99
 
      this.addr = ip;
100
 
   }
101
 
 
102
 
   /**
103
 
    * return the bind address of the detector
104
 
    *
105
 
    * @return
106
 
    */
107
 
   public InetAddress getBindAddress()
108
 
   {
109
 
      return bindAddr;
110
 
   }
111
 
 
112
 
   /**
113
 
    * set the bind address of the multicast
114
 
    *
115
 
    * @param ip
116
 
    */
117
 
   public void setBindAddress(InetAddress ip)
118
 
   {
119
 
      this.bindAddr = ip;
120
 
   }
121
 
 
122
 
   /**
123
 
    * get the port that the detector is multicasting to
124
 
    *
125
 
    * @return
126
 
    */
127
 
   public int getPort()
128
 
   {
129
 
      return port;
130
 
   }
131
 
 
132
 
   /**
133
 
    * set the port for detections to be multicast to
134
 
    *
135
 
    * @param port
136
 
    */
137
 
   public void setPort(int port)
138
 
   {
139
 
      this.port = port;
140
 
   }
141
 
 
142
 
 
143
 
   public int getBufferSize()
144
 
   {
145
 
      return bufferSize;
146
 
   }
147
 
 
148
 
   public void setBufferSize(int bufferSize)
149
 
   {
150
 
      this.bufferSize = bufferSize;
151
 
   }
152
 
   
153
 
   /**
154
 
    * called by MBeanServer to start the mbean lifecycle
155
 
    *
156
 
    * @throws Exception
157
 
    */
158
 
   public void start() throws Exception
159
 
   {
160
 
      if(addr == null)
161
 
      {
162
 
         addr = getAddressByName(defaultIP);
163
 
      }
164
 
      
165
 
      // check to see if we're running on a machine with loopback and no NIC
166
 
      InetAddress localHost = getLocalHost();
167
 
      if(bindAddr == null && localHost.getHostAddress().equals("127.0.0.1"))
168
 
      {
169
 
         // use this to bind so multicast will work w/o network
170
 
         this.bindAddr = localHost;
171
 
      }
172
 
 
173
 
      try
174
 
      {
175
 
         final SocketAddress saddr = new InetSocketAddress(bindAddr, port);
176
 
         AccessController.doPrivileged( new PrivilegedExceptionAction()
177
 
         {
178
 
            public Object run() throws IOException
179
 
            {
180
 
               socket = new MulticastSocket(saddr);
181
 
               socket.joinGroup(addr);
182
 
               return null;
183
 
            }
184
 
         });
185
 
      }
186
 
      catch (PrivilegedActionException e)
187
 
      {
188
 
         throw (IOException) e.getCause();
189
 
      }
190
 
 
191
 
      super.start();
192
 
 
193
 
      if(listener == null)
194
 
      {
195
 
         listener = new Listener("Remoting Multicast Detector - Listener Thread: " + threadCounter++);
196
 
      }
197
 
      listener.start();
198
 
   }
199
 
 
200
 
   /**
201
 
    * called by the MBeanServer to stop the mbean lifecycle
202
 
    *
203
 
    * @throws Exception
204
 
    */
205
 
   public void stop() throws Exception
206
 
   {
207
 
      super.stop();
208
 
      if(listener != null)
209
 
      {
210
 
         try
211
 
         {
212
 
            listener.running = false;
213
 
            listener.interrupt();
214
 
         }
215
 
         catch (Exception e)
216
 
         {
217
 
            {
218
 
               log.warn(this + " Error stopping multicast detector.  " + e.getMessage());
219
 
            }         }
220
 
         listener = null;
221
 
      }
222
 
      if(socket != null)
223
 
      {
224
 
         try
225
 
         {
226
 
            socket.leaveGroup(addr);
227
 
            socket.close();
228
 
         }
229
 
         catch (IOException e)
230
 
         {
231
 
            log.warn(this + " Error stopping multicast detector.  " + e.getMessage());
232
 
         }
233
 
         socket = null;
234
 
      }
235
 
   }
236
 
 
237
 
   /**
238
 
    * subclasses must implement to provide the specific heartbeat protocol
239
 
    * for this server to send out to other servers on the network
240
 
    */
241
 
   protected void heartbeat()
242
 
   {
243
 
      if(socket != null)
244
 
      {
245
 
         Detection msg = createDetection();
246
 
         if (msg == null)
247
 
            return;
248
 
         
249
 
         try
250
 
         {
251
 
            if(log.isTraceEnabled())
252
 
            {
253
 
               log.trace(this + " sending heartbeat: " + msg);
254
 
            }
255
 
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
256
 
            ObjectOutputStream objectOut = new ObjectOutputStream(byteOut);
257
 
            objectOut.writeObject(msg);
258
 
            objectOut.flush();
259
 
            byteOut.flush();
260
 
            byte buf[] = byteOut.toByteArray();
261
 
            DatagramPacket p = new DatagramPacket(buf, buf.length, addr, port);
262
 
            socket.send(p);
263
 
         }
264
 
         catch(Throwable ex)
265
 
         {
266
 
            // its failed
267
 
            log.debug(this + " heartbeat failed", ex);
268
 
         }
269
 
      }
270
 
   }
271
 
 
272
 
   protected void forceHeartbeat()
273
 
   {
274
 
      if(socket != null)
275
 
      {
276
 
         String msg = "Send heartbeat";
277
 
         try
278
 
         {
279
 
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
280
 
            ObjectOutputStream objectOut = new ObjectOutputStream(byteOut);
281
 
            objectOut.writeObject(msg);
282
 
            objectOut.flush();
283
 
            byteOut.flush();
284
 
            byte buf[] = byteOut.toByteArray();
285
 
            DatagramPacket p = new DatagramPacket(buf, buf.length, addr, port);
286
 
            socket.send(p);
287
 
 
288
 
            /**
289
 
             * This is a bit of a hack, but am going to wait a few seconds to
290
 
             * allow for any group members (other multicast detectors) to receive
291
 
             * the msg and then respond themselves with their detection messages.
292
 
             * Since don't know number of servers that are members of the group,
293
 
             * can't really wait until all detection messages, just hope they come
294
 
             * in before end of waiting.
295
 
             */
296
 
            Thread.currentThread().sleep(2000);
297
 
 
298
 
         }
299
 
         catch(Throwable ex)
300
 
         {
301
 
            // its failed
302
 
            log.debug(this + " forced heartbeat failed", ex);
303
 
         }
304
 
      }
305
 
   }
306
 
 
307
 
   private void listen(DatagramPacket p, byte[] buf)
308
 
   {
309
 
      if(socket != null)
310
 
      {
311
 
         try
312
 
         {
313
 
            // should block until we get a multicast
314
 
            socket.receive(p);
315
 
 
316
 
            // take the multicast, and deserialize into the detection event
317
 
            ByteArrayInputStream byteInput = new ByteArrayInputStream(buf);
318
 
            ObjectInputStream objectInput = new ObjectInputStream(byteInput);
319
 
            Object obj = objectInput.readObject();
320
 
            if(obj instanceof Detection)
321
 
            {
322
 
               Detection msg = (Detection)obj;
323
 
               if(log.isTraceEnabled())
324
 
               {
325
 
                  log.trace(this + " received detection: " + msg);
326
 
               }
327
 
 
328
 
               // let the subclass do the hard work off handling detection
329
 
               detect(msg);
330
 
            }
331
 
            else
332
 
            {
333
 
               // for now, assume anything *not* of type Detection
334
 
               // is a prompt to send out detection msg
335
 
               heartbeat();
336
 
            }
337
 
         }
338
 
         catch(Throwable e)
339
 
         {
340
 
            if(e instanceof java.io.InvalidClassException)
341
 
            {
342
 
               return;
343
 
            }
344
 
            if(socket != null)
345
 
            {
346
 
               log.debug(this + " Error receiving detection", e);
347
 
            }
348
 
         }
349
 
      }
350
 
   }
351
 
 
352
 
   private final class Listener extends Thread
353
 
   {
354
 
      boolean running = true;
355
 
 
356
 
      public Listener(String name)
357
 
      {
358
 
         super(name);
359
 
      }
360
 
 
361
 
      public void run()
362
 
      {
363
 
         log.debug("using bufferSize: " + bufferSize);
364
 
         byte[] buf = new byte[bufferSize];
365
 
         DatagramPacket p = new DatagramPacket(buf, 0, buf.length);
366
 
         //p.setAddress(addr);
367
 
         //p.setPort(port);
368
 
         while(running)
369
 
         {
370
 
            listen(p, buf);
371
 
         }
372
 
      }
373
 
   }
374
 
   
375
 
   static private InetAddress getLocalHost() throws UnknownHostException
376
 
   {
377
 
      if (SecurityUtility.skipAccessControl())
378
 
      {
379
 
         try
380
 
         {
381
 
            return InetAddress.getLocalHost();
382
 
         }
383
 
         catch (IOException e)
384
 
         {
385
 
            return InetAddress.getByName("127.0.0.1");
386
 
         }
387
 
      }
388
 
 
389
 
      try
390
 
      {
391
 
         return (InetAddress) AccessController.doPrivileged( new PrivilegedExceptionAction()
392
 
         {
393
 
            public Object run() throws IOException
394
 
            {
395
 
               try
396
 
               {
397
 
                  return InetAddress.getLocalHost();
398
 
               }
399
 
               catch (IOException e)
400
 
               {
401
 
                  return InetAddress.getByName("127.0.0.1");
402
 
               }
403
 
            }
404
 
         });
405
 
      }
406
 
      catch (PrivilegedActionException e)
407
 
      {
408
 
         throw (UnknownHostException) e.getCause();
409
 
      }
410
 
   }
411
 
   
412
 
   static private InetAddress getAddressByName(final String host) throws UnknownHostException
413
 
   {
414
 
      if (SecurityUtility.skipAccessControl())
415
 
      {
416
 
         return InetAddress.getByName(host);
417
 
      }
418
 
      
419
 
      try
420
 
      {
421
 
         return (InetAddress)AccessController.doPrivileged( new PrivilegedExceptionAction()
422
 
         {
423
 
            public Object run() throws IOException
424
 
            {
425
 
               return InetAddress.getByName(host);
426
 
            }
427
 
         });
428
 
      }
429
 
      catch (PrivilegedActionException e)
430
 
      {
431
 
         throw (UnknownHostException) e.getCause();
432
 
      }
433
 
   }
434
 
}