KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > comanche > lib > MulticastServer


1 /*====================================================================
2  
3  OpenCCM: The Open CORBA Component Model Platform
4  Copyright (C) 2004 INRIA - USTL - LIFL - GOAL
5  Contact: openccm@objectweb.org
6  
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or any later version.
11  
12  This library 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 library; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  USA
21  
22  Initial developer(s): Areski Flissi.
23  Contributor(s):
24  
25  ====================================================================
26  $Id: MulticastServer.java,v 1.1 2004/10/12 14:39:36 rouvoy Exp $
27  ====================================================================*/

28
29 package org.objectweb.comanche.lib;
30
31 import java.net.InetAddress JavaDoc;
32 import java.net.DatagramPacket JavaDoc;
33 import java.net.DatagramSocket JavaDoc;
34 import java.net.MulticastSocket JavaDoc;
35
36 import java.io.IOException JavaDoc;
37
38 /**
39  *
40  * @author Areski Flissi
41  * @version 0.1
42  */

43 public class MulticastServer
44      extends Thread JavaDoc
45 {
46     /**
47      * Comment for <code>s_port</code>
48      */

49     protected static final int s_port = 8888;
50     /**
51      * Comment for <code>ms_port</code>
52      */

53     protected static final int ms_port = 8880;
54     /**
55      * Comment for <code>ior_str_</code>
56      */

57     protected String JavaDoc ior_str_ = null;
58     /**
59      * Comment for <code>add_group_</code>
60      */

61     protected String JavaDoc add_group_;
62     /**
63      * Comment for <code>config_dir_</code>
64      */

65     protected String JavaDoc config_dir_;
66     
67     /**
68      * @param addgroup
69      * @param config_dir_
70      */

71     public MulticastServer(String JavaDoc addgroup, String JavaDoc config_dir_) {
72         add_group_ = addgroup;
73         this.config_dir_ = config_dir_;
74     }
75         
76     /**
77      * @param infilename
78      * @return
79      */

80     public static String JavaDoc get_ior_from_file(String JavaDoc infilename) {
81         // Read data from the file to be transferred
82
String JavaDoc inFileName = infilename;
83         java.io.FileInputStream JavaDoc inStream = null;
84         String JavaDoc ior_str;
85         
86         java.io.File JavaDoc isFileExist = new java.io.File JavaDoc(infilename);
87         
88         if (isFileExist.exists()) {
89             try {
90                 inStream = new java.io.FileInputStream JavaDoc(inFileName);
91             } catch (java.io.FileNotFoundException JavaDoc ex) {
92                 System.err.println(
93                       "Can't open file " + inFileName + ": " + ex.getMessage());
94             }
95             
96             byte[] file = null;
97             
98             try {
99                 file = new byte[inStream.available()];
100                 inStream.read(file);
101                 inStream.close();
102             } catch (java.io.IOException JavaDoc ex) {
103                 System.err.println(
104                       "Can't read from " + inFileName + ": " + ex.getMessage());
105             }
106             
107             ior_str = new String JavaDoc(file);
108         } else {
109             ior_str="file_not_exist";
110         }
111         return ior_str;
112     }
113     
114     /* (non-Javadoc)
115      * @see java.lang.Runnable#run()
116      */

117     public void run() {
118         InetAddress JavaDoc groupAddress = null;
119         while (true) {
120             byte[] mem = new byte[1000];
121             DatagramPacket JavaDoc recv = new DatagramPacket JavaDoc(mem, mem.length);
122             
123             // Receiving request from clients
124
MulticastSocket JavaDoc msocket = null;
125             DatagramSocket JavaDoc socket;
126             
127             try {
128                 msocket = new MulticastSocket JavaDoc(ms_port);
129                 msocket.joinGroup(InetAddress.getByName(add_group_));
130                 msocket.receive(recv);
131                 byte[] ior_req = recv.getData();
132                 
133                 String JavaDoc ior_filename = new String JavaDoc(ior_req);
134                 ior_req = null;
135                 
136                 System.out.println(
137                         "receive multicast request from "
138                         + recv.getAddress().getHostName()
139                         + ":"
140                         + recv.getPort());
141                 
142                 msocket.disconnect();
143                 msocket.close();
144                 
145                 // Gros dodo pendant 3 seconde
146
try {
147                     Thread.sleep(3000);
148                 } catch (InterruptedException JavaDoc ex) {
149                     // None
150
}
151                 
152                 // Send IOR if request from client has been received
153
// Read IOR from file
154
String JavaDoc ior_str =
155                     get_ior_from_file(config_dir_ + "\\" + ior_filename);
156                 
157                 if (ior_str != "file_not_exist") {
158                     System.out.println("Sending IOR to client...");
159                     InetAddress JavaDoc client_address = recv.getAddress();
160                     
161                     DatagramPacket JavaDoc envoi =
162                         new DatagramPacket JavaDoc(
163                                 ior_str.getBytes(),
164                                 ior_str.length(),
165                                 client_address,
166                                 s_port + 1);
167                     
168                     socket = new DatagramSocket JavaDoc();
169                     socket.send(envoi);
170                     System.out.println(
171                             "IOR sent to "
172                             + client_address.getHostName()
173                             + ":"
174                             + s_port);
175                     socket.disconnect();
176                     socket.close();
177                 }
178             } catch (IOException JavaDoc e) {
179             }
180         }
181     }
182 }
183
Popular Tags