/tags/1.0.0/java/kouchat/src/net/usikkert/kouchat/net/OperatingSystemNetworkInfo.java

http://kouchat.googlecode.com/ · Java · 164 lines · 76 code · 18 blank · 70 comment · 5 complexity · 2b4d5c55b913c782247d780361fd4903 MD5 · raw file

  1. /***************************************************************************
  2. * Copyright 2006-2009 by Christian Ihle *
  3. * kontakt@usikkert.net *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. package net.usikkert.kouchat.net;
  21. import java.net.InetAddress;
  22. import java.net.NetworkInterface;
  23. import java.net.SocketException;
  24. import java.net.UnknownHostException;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import net.usikkert.kouchat.Constants;
  28. import net.usikkert.kouchat.misc.Settings;
  29. import net.usikkert.kouchat.util.Tools;
  30. /**
  31. * This class can find information about the network interface
  32. * the operating system has chosen for multicast.
  33. *
  34. * @author Christian Ihle
  35. */
  36. public class OperatingSystemNetworkInfo
  37. {
  38. /** The logger. */
  39. private static final Logger LOG = Logger.getLogger( OperatingSystemNetworkInfo.class.getName() );
  40. /** The message receiver. */
  41. private final MessageReceiver receiver;
  42. /** The message sender. */
  43. private final MessageSender sender;
  44. /**
  45. * Default constructor.
  46. */
  47. public OperatingSystemNetworkInfo()
  48. {
  49. receiver = new MessageReceiver( Constants.NETWORK_TEMP_IP, Constants.NETWORK_TEMP_PORT );
  50. sender = new MessageSender( Constants.NETWORK_TEMP_IP, Constants.NETWORK_TEMP_PORT );
  51. }
  52. /**
  53. * Finds the network interface the operating system has chosen for
  54. * sending and receiving multicast messages.
  55. *
  56. * <p>If no network interface has been found within 2 seconds,
  57. * then <code>null</code> is returned.
  58. *
  59. * @return The network interface, or <code>null</code>.
  60. */
  61. public NetworkInterface getOperatingSystemNetworkInterface()
  62. {
  63. String message = createMessageToSend();
  64. SimpleReceiverListener listener = new SimpleReceiverListener( message );
  65. connect( listener );
  66. sender.send( message );
  67. waitForMessage( listener );
  68. disconnect();
  69. return findNetworkInterface( listener );
  70. }
  71. /**
  72. * Connects the sender and receiver to the network,
  73. * and registers the message listener.
  74. *
  75. * @param listener The message listener.
  76. */
  77. private void connect( final SimpleReceiverListener listener )
  78. {
  79. receiver.registerReceiverListener( listener );
  80. receiver.startReceiver( null );
  81. sender.startSender( null );
  82. }
  83. /**
  84. * Disconnects the sender and receiver from the network.
  85. */
  86. private void disconnect()
  87. {
  88. sender.stopSender();
  89. receiver.stopReceiver();
  90. }
  91. /**
  92. * Waits for up to 2 seconds for an ip address to be registered in the listener.
  93. *
  94. * @param listener The message listener.
  95. */
  96. private void waitForMessage( final SimpleReceiverListener listener )
  97. {
  98. for ( int i = 0; i < 40; i++ )
  99. {
  100. if ( listener.getIpAddress() == null )
  101. Tools.sleep( 50 );
  102. else
  103. break;
  104. }
  105. }
  106. /**
  107. * Gets the ip address from the listener, and tries to convert
  108. * it into a network interface.
  109. *
  110. * <p>Returns <code>null</code> if no ip address is found,
  111. * or the conversion fails.</p>
  112. *
  113. * @param listener The message listener.
  114. * @return The found network interface, or <code>null</code>.
  115. */
  116. private NetworkInterface findNetworkInterface( final SimpleReceiverListener listener )
  117. {
  118. if ( listener.getIpAddress() == null )
  119. return null;
  120. try
  121. {
  122. InetAddress osAddress = InetAddress.getByName( listener.getIpAddress() );
  123. return NetworkInterface.getByInetAddress( osAddress );
  124. }
  125. catch ( final UnknownHostException e )
  126. {
  127. LOG.log( Level.SEVERE, e.toString(), e );
  128. }
  129. catch ( final SocketException e )
  130. {
  131. LOG.log( Level.SEVERE, e.toString(), e );
  132. }
  133. return null;
  134. }
  135. /**
  136. * Returns a message with the text <code>getOperatingSystemNetworkInterface(user code)</code>,
  137. * where user code is taken from the application user.
  138. *
  139. * @return A message.
  140. */
  141. private String createMessageToSend()
  142. {
  143. int code = Settings.getSettings().getMe().getCode();
  144. return "getOperatingSystemNetworkInterface(" + code + ")";
  145. }
  146. }