KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > discovery > DiscoveryListener


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or any later
9  * version.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19  *
20  */

21
22 package org.objectweb.jonas.discovery;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.net.DatagramPacket JavaDoc;
28 import java.net.InetAddress JavaDoc;
29 import java.net.MulticastSocket JavaDoc;
30 import java.net.SocketException JavaDoc;
31 import java.net.UnknownHostException JavaDoc;
32
33 import javax.management.Notification JavaDoc;
34 import org.objectweb.jonas.common.Log;
35 import org.objectweb.util.monolog.api.BasicLevel;
36 import org.objectweb.util.monolog.api.Logger;
37
38 /**
39  * @author <a HREF="mailto:Takoua.Abdellatif@inria.fr">Takoua Abdellatif </a>
40  * @version 1.0
41  */

42 public class DiscoveryListener implements Runnable JavaDoc {
43     /**
44      * Management notification type for <i>discovery</i> events
45      */

46     public static final String JavaDoc DISCOVERY_TYPE = "jonas.management.discovery";
47
48     private static int RECEIVE_BUFFER_SIZE = 1024;
49     /**
50      * My enroller's listening port
51      */

52     private int port;
53     /**
54      * My enroller's multicast IP address
55      */

56     private InetAddress JavaDoc destAddress;
57     /**
58      * My enroller
59      */

60     private Enroller enroller;
61     /**
62      * Time-to-live for multicatst packets
63      */

64     private int ttl = 1; // why 1 ??
65
/**
66      * Used to receive multicasted discovery events
67      */

68     private MulticastSocket JavaDoc multicastSocket;
69     /**
70      * Socket state
71      */

72     private boolean notStopped = true;
73     /**
74      * Used to construct JMX notifications
75      */

76     private long sequenceNumber = 0;
77     private static Logger logger = Log.getLogger(Log.JONAS_DISCOVERY_PREFIX);
78
79     /**
80      * Constructs a DiscoveryListener associated to the Enroller
81      * @param enroller Enroller to which this thread is associated
82      */

83     public DiscoveryListener(Enroller enroller) {
84         this.port = enroller.getListeningPort();
85         try {
86             this.destAddress = InetAddress.getByName(enroller.getListeningIp());
87             this.ttl = enroller.getTimeToLive();
88         } catch (UnknownHostException JavaDoc e) {
89             logger.log(BasicLevel.ERROR, e);
90         }
91         this.enroller = enroller;
92     }
93     /**
94      * Creates a MulticastSocket and joins the group of multicas host
95      * identified by the InetAddress <code>destAddress</code>
96       */

97     private void join() {
98         try {
99             multicastSocket = new MulticastSocket JavaDoc(port);
100             multicastSocket.setTimeToLive(ttl);
101             multicastSocket.joinGroup(destAddress);
102             if (logger.isLoggable(BasicLevel.DEBUG)) {
103                 logger.log(BasicLevel.DEBUG, "multicast ip address is "
104                         + destAddress);
105
106                 logger.log(BasicLevel.DEBUG, "multicast port is " + port);
107             }
108         } catch (IOException JavaDoc e) {
109             logger.log(BasicLevel.ERROR, "io problem");
110             // TODO Auto-generated catch block
111
e.printStackTrace();
112         }
113     }
114
115     /**
116      * Handles the received DiscEvent messages by creating a JMX notification to be sent
117      * by the Enroller. The notification type is jonas.management.discovery and the data
118      * contains the DiscEvent message.
119      *
120      * @param msg
121      * the notification message
122      */

123     private void handleDiscEvent(DiscEvent msg) {
124         if (logger.isLoggable(BasicLevel.DEBUG)) {
125             logger.log(BasicLevel.DEBUG, "discovery event received: " + msg);
126         }
127
128         // create a JMX notification for all listeners of a discovery event
129
Notification JavaDoc notif = new Notification JavaDoc(DISCOVERY_TYPE, enroller,
130                 sequenceNumber++, System.currentTimeMillis(), msg.getState());
131         notif.setUserData(msg);
132         enroller.sendNotification(notif);
133     }
134
135     public void run() {
136         // Create a DatagramPacket to receive messages (DiscoverEvents)
137
DatagramPacket JavaDoc datagram = new DatagramPacket JavaDoc(new byte[RECEIVE_BUFFER_SIZE],
138                 RECEIVE_BUFFER_SIZE);
139         // Object contained in a message
140
Object JavaDoc objReceived = null;
141         ObjectInputStream JavaDoc in = null;
142
143         // Join the group in order to receive multicast messages
144
join();
145
146         try {
147             while (notStopped) {
148                 multicastSocket.receive(datagram);
149                 in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(datagram.getData()));
150                 objReceived = in.readObject();
151
152                 if (objReceived != null) {
153                     if (objReceived instanceof DiscEvent) {
154                         // Treat DiscEvents
155
DiscEvent msg = (DiscEvent) objReceived;
156                         // Trust the datagram packet for source address instead of the message.
157
msg.setSourceAddress(datagram.getAddress().getHostAddress());
158                         handleDiscEvent(msg);
159                     }
160                 }
161             }
162         }
163         catch (SocketException JavaDoc e) {
164             logger.log(BasicLevel.ERROR, "Enroller: Socket closed" + e);
165             notStopped = false;
166         } catch (IOException JavaDoc e1) {
167             e1.printStackTrace();
168         } catch (ClassNotFoundException JavaDoc e) {
169             e.printStackTrace();
170         }
171     }
172
173     public void stopListener() {
174         notStopped = false;
175     }
176
177 }
Popular Tags