summaryrefslogtreecommitdiff
path: root/TAO/examples/Simulator/Sim_Display/NS_Resolve.java
blob: b6a39e63dec61e1c30affacab7b2657cb107e795 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// $Id$ 
// ============================================================================
//
// 
// = FILENAME
//    NS_Resolve.java
//
// = AUTHOR
//    Michael Kircher (mk1@cs.wustl.edu)
//
// = DESCRIPTION
//   Resolves the initial reference to the Naming service,
//   the orb has to be given as a parameter to the
//   resolve_name_service call. 
// 
//
// ============================================================================




import org.omg.CORBA.*;
import java.net.*;
import java.io.*;



public class NS_Resolve 
{
  
  private static final String ACE_DEFAULT_MULTICAST_ADDR = "224.9.9.2";
  private static final int TAO_DEFAULT_NAME_SERVER_REQUEST_PORT = 10013;
  private static final int MULTICAST_SEND_PORT = 10060;
  private static final int MULTICAST_RECEIVE_PORT = 10061;
  private static final int TAO_SERVICEID_NAMESERVICE = 0;
  private static final int TAO_SERVICEID_TRADINGSERVICE = 1;

  org.omg.CORBA.Object name_service_;


  public org.omg.CORBA.Object resolve_name_service (org.omg.CORBA.ORB orb)
    {
      try
	{
	  // Create a message with the multicast receive port in it
	  ByteArrayOutputStream byte_stream_ = new ByteArrayOutputStream ();
      	  byte_stream_.write ((int)((MULTICAST_RECEIVE_PORT&0xff00)>>>8));
	  byte_stream_.write ((int)(MULTICAST_RECEIVE_PORT&0x00ff));
      	  byte_stream_.write ((int)((TAO_SERVICEID_NAMESERVICE&0xff00)>>>8));
	  byte_stream_.write ((int)(TAO_SERVICEID_NAMESERVICE&0x00ff));
	  byte[] msg = byte_stream_.toByteArray();


	  // Define the group for the multicast

	  InetAddress group = InetAddress.getByName(ACE_DEFAULT_MULTICAST_ADDR);
	  // Create the multicast socket at any port you want
	  MulticastSocket multicastsocket_ = new MulticastSocket(MULTICAST_SEND_PORT);
	  // Create a socket for the answer of the Naming Service 
	  DatagramSocket socket_ = new DatagramSocket (MULTICAST_RECEIVE_PORT);
	  // Give three seconds time for the Naming Service to respond
	  socket_.setSoTimeout (3000);
	  // Build a packet with the port number in it
	  DatagramPacket  hello = new DatagramPacket(msg, msg.length,
						     group, TAO_DEFAULT_NAME_SERVER_REQUEST_PORT);

	  // Send the packet
	  multicastsocket_.send (hello);

	  // @@ The restriction right now is that the length of the IOR cannot be longer than 4096
	  byte[] buf = new byte[4096];
	  // Set up packet which can be received
	  DatagramPacket recv = new DatagramPacket (buf, buf.length);	  
	  // Receive a packet or time out
	  socket_.receive (recv);

	  // Determine the length of the IOR
	  int length;
	  for (length = 0; buf[length] != 0; length++);

	  // Store the IOR in a String
       	  String name_service_ior_ = new String (recv.getData (),0,length);

	  // Convert the String into
	  return orb.string_to_object (name_service_ior_);
	}
      catch (SocketException e)
	{
	  System.err.println (e);
	}
      catch (java.io.InterruptedIOException e)
	{
	  System.err.println ("NS_Resolve: The receive lasted too long");
	} 
      catch(org.omg.CORBA.SystemException e) 
	{
	  System.err.println(e);
	}	
      catch (java.io.IOException e)
	{
	  System.err.println (e);
	}
      return null;
      
    } 

};