1 // ============================================================================
7 // Michael Kircher (mk1@cs.wustl.edu)
8 // Modified for new protocol by Hans Ridder <ridder@veritas.com>
9 // Further improved by Kevin Regan <kregan@infoglide.com>
12 // Resolves the initial reference to the Naming service,
13 // the orb has to be given as a parameter to the
14 // resolve_name_service call.
16 // ============================================================================
18 import org
.omg
.CORBA
.*;
22 public class NS_Resolve
24 private static final String ACE_DEFAULT_MULTICAST_ADDR
= "224.9.9.2";
25 private static final int TAO_DEFAULT_NAME_SERVER_REQUEST_PORT
= 10013;
26 private static final String TAO_SERVICEID_NAMESERVICE
= "NameService";
30 public NS_Resolve (String nameServicePort
)
32 if (nameServicePort
!= null)
34 // If a name service port string was given, parse it
35 nameServicePort_
= Integer
.parseInt (nameServicePort
);
39 // Otherwise, just use the default TAO name service port
40 nameServicePort_
= TAO_DEFAULT_NAME_SERVER_REQUEST_PORT
;
44 public org
.omg
.CORBA
.Object
resolve_name_service (org
.omg
.CORBA
.ORB orb
)
46 MulticastSocket sendSocket
= null;
47 ServerSocket listenSocket
= null;
48 Socket replySocket
= null;
52 // Create the multicast socket at any port
53 sendSocket
= new MulticastSocket(0);
55 // Create a socket at any port for the Naming Service answer
56 listenSocket
= new ServerSocket(0);
58 // Create a message with the port and service name in it,
59 // length and port number are in network byte order
60 ByteArrayOutputStream msg
= new ByteArrayOutputStream();
61 int dataLength
= TAO_SERVICEID_NAMESERVICE
.length() + 3;
62 msg
.write((dataLength
>> 8) & 0xff);
63 msg
.write(dataLength
& 0xff);
64 msg
.write((listenSocket
.getLocalPort() >> 8) & 0xff);
65 msg
.write(listenSocket
.getLocalPort() & 0xff);
66 msg
.write(TAO_SERVICEID_NAMESERVICE
.getBytes());
69 // Define the group for the multicast
70 InetAddress group
= InetAddress
.getByName(ACE_DEFAULT_MULTICAST_ADDR
);
71 // Create a datagram with the message and send it
72 sendSocket
.send(new DatagramPacket(msg
.toByteArray(),
74 group
, nameServicePort_
));
76 // Wait 3 seconds for the Naming Service to connect
77 listenSocket
.setSoTimeout(3000);
78 replySocket
= listenSocket
.accept();
80 // @@ The restriction right now is that the length of the IOR cannot be longer than 4096
81 char[] reply
= new char[4096];
83 // Receive the reply (0 terminated string) or time out
84 replySocket
.setSoTimeout(3000);
85 InputStream in
= replySocket
.getInputStream();
87 for (length
= 0; length
< reply
.length
; length
++)
93 throw new IOException("Unexpected EOF.");
96 reply
[ length
] = (char) c
;
104 // Convert the String into ??
105 return orb
.string_to_object(new String(reply
, 2, length
-2));
107 catch (SocketException e
)
109 System
.err
.println (e
);
111 catch (java
.io
.InterruptedIOException e
)
113 System
.err
.println ("NS_Resolve: The receive lasted too long");
115 catch(org
.omg
.CORBA
.SystemException e
)
117 System
.err
.println(e
);
119 catch (java
.io
.IOException e
)
121 System
.err
.println (e
);
125 // Close the sockets.
127 if (sendSocket
!= null)
132 if (listenSocket
!= null)
136 listenSocket
.close();
138 catch (IOException e
) {}
141 if (replySocket
!= null)
147 catch (IOException e
) {}