1 28 29 package org.objectweb.comanche.lib; 30 31 import java.net.InetAddress ; 32 import java.net.DatagramPacket ; 33 import java.net.DatagramSocket ; 34 import java.net.MulticastSocket ; 35 36 import java.io.IOException ; 37 38 43 public class MulticastServer 44 extends Thread 45 { 46 49 protected static final int s_port = 8888; 50 53 protected static final int ms_port = 8880; 54 57 protected String ior_str_ = null; 58 61 protected String add_group_; 62 65 protected String config_dir_; 66 67 71 public MulticastServer(String addgroup, String config_dir_) { 72 add_group_ = addgroup; 73 this.config_dir_ = config_dir_; 74 } 75 76 80 public static String get_ior_from_file(String infilename) { 81 String inFileName = infilename; 83 java.io.FileInputStream inStream = null; 84 String ior_str; 85 86 java.io.File isFileExist = new java.io.File (infilename); 87 88 if (isFileExist.exists()) { 89 try { 90 inStream = new java.io.FileInputStream (inFileName); 91 } catch (java.io.FileNotFoundException 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 ex) { 103 System.err.println( 104 "Can't read from " + inFileName + ": " + ex.getMessage()); 105 } 106 107 ior_str = new String (file); 108 } else { 109 ior_str="file_not_exist"; 110 } 111 return ior_str; 112 } 113 114 117 public void run() { 118 InetAddress groupAddress = null; 119 while (true) { 120 byte[] mem = new byte[1000]; 121 DatagramPacket recv = new DatagramPacket (mem, mem.length); 122 123 MulticastSocket msocket = null; 125 DatagramSocket socket; 126 127 try { 128 msocket = new MulticastSocket (ms_port); 129 msocket.joinGroup(InetAddress.getByName(add_group_)); 130 msocket.receive(recv); 131 byte[] ior_req = recv.getData(); 132 133 String ior_filename = new String (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 try { 147 Thread.sleep(3000); 148 } catch (InterruptedException ex) { 149 } 151 152 String 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 client_address = recv.getAddress(); 160 161 DatagramPacket envoi = 162 new DatagramPacket ( 163 ior_str.getBytes(), 164 ior_str.length(), 165 client_address, 166 s_port + 1); 167 168 socket = new DatagramSocket (); 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 e) { 179 } 180 } 181 } 182 } 183 | Popular Tags |