View Javadoc
1   package nl.tno.imb;
2   
3   import nl.tno.imb.TByteBuffer;
4   import nl.tno.imb.TConnection;
5   //import java.io.InputStream;
6   //import java.io.OutputStream;
7   //import java.util.Arrays;
8   import java.io.IOException;
9   import java.net.DatagramPacket;
10  import java.net.DatagramSocket;
11  import java.net.InetAddress;
12  //import java.net.Socket;
13  import java.net.SocketException;
14  
15  public class TLocator {
16      private static final int MaxUDPCommandBufferSize = 512-62;
17      private static final String ProtocolSep = "://";
18      public static String DecodeServerURIServer(String aServerURI)
19      {
20          if (!aServerURI.isEmpty())
21          {
22              int i = aServerURI.indexOf(ProtocolSep);
23              if (i>=0)
24              {
25                  String server = aServerURI.substring(i+ProtocolSep.length());
26                  i = server.indexOf('/');
27                  if (i>=0)
28                      server = server.substring(0, i);
29                  i = server.indexOf(':');
30                  if (i>=0)
31                      return server.substring(0, i);
32                  else
33                      return server;
34              }
35              else
36                  return "";
37          }
38          else
39              return "";
40      }
41      public static int DecodeServerURIPort(String aServerURI)
42      {
43          if (!aServerURI.isEmpty())
44          {
45              int i = aServerURI.indexOf(ProtocolSep);
46              if (i>=0)
47              {
48                  String server = aServerURI.substring(i+ProtocolSep.length());
49                  i = server.indexOf('/');
50                  if (i>=0)
51                      server = server.substring(0, i);
52                  i = server.indexOf(':');
53                  if (i>=0)
54                      return Integer.parseInt(server.substring(i+1));
55                  else
56                      return 4000;
57              }
58              else
59                  return 4000;
60          }
61          else
62              return 4000;
63      }
64      public static String LocateServerURI(boolean aIPv4, int aPort, int aTimeout)
65      {
66          try {
67              TByteBuffer Buffer = new TByteBuffer();
68              Buffer.prepare(TConnection.MAGIC_BYTES);
69              Buffer.prepare(TEventEntry.IC_HUB_LOCATE);
70              Buffer.prepare((int)0);
71              Buffer.prepareApply();
72              Buffer.qWrite(TConnection.MAGIC_BYTES);
73              Buffer.qWrite(TEventEntry.IC_HUB_LOCATE);
74              Buffer.qWrite((int) 0);
75              
76              DatagramSocket fSocket = new DatagramSocket();
77              try {
78                  if (aIPv4) {
79                      // IPv4 locator request
80                      fSocket.setSoTimeout(aTimeout);
81                      fSocket.setBroadcast(true);
82                      try {
83                          fSocket.send(new DatagramPacket(Buffer.getBuffer(), Buffer.getLength(), InetAddress.getByName("255.255.255.255"), aPort));
84                          //byte[] buffer = new byte[MaxUDPCommandBufferSize];
85                          Buffer.clear(MaxUDPCommandBufferSize);
86                          DatagramPacket receivedData = new DatagramPacket(Buffer.getBuffer(), MaxUDPCommandBufferSize);
87                          fSocket.receive(receivedData);
88                          // decode received data
89                          if (Buffer.compare(TConnection.MAGIC_BYTES, 0))
90                          {
91                              Buffer.skipReading(TConnection.MAGIC_BYTES.length);
92                              int command = Buffer.readInt32();
93                              if (command==TEventEntry.IC_HUB_FOUND)
94                              {
95                                  String server = Buffer.readString();
96                                  if (Buffer.compare(TConnection.MAGIC_STRING_CHECK, 0))
97                                  {
98                                      return server;
99                                  }
100                             }
101                         }
102                         return "server"; // TODO:
103                     }
104                     catch (IOException ex) {
105                         return "";
106                     }
107                 }
108                 else {
109                     // IPv6 locator request
110                 }
111             }
112             finally {
113                 fSocket.close();
114             }
115             return "";
116         }
117         catch (SocketException ex) {
118             return "";
119         }
120     }
121     
122 }