/interpreter/tags/at2-build190607/src/edu/vub/at/actors/net/comm/MulticastServerThread.java

http://ambienttalk.googlecode.com/ · Java · 129 lines · 55 code · 17 blank · 57 comment · 5 complexity · 43ea3a47654747443f7497ad42fed9f1 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * (c) Programming Technology Lab, 2006 - 2007
  4. * Authors: Tom Van Cutsem & Stijn Mostinckx
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use,
  10. * copy, modify, merge, publish, distribute, sublicense, and/or
  11. * sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  20. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  22. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  23. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  24. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25. * OTHER DEALINGS IN THE SOFTWARE.
  26. */
  27. package edu.vub.at.actors.net.comm;
  28. import java.io.*;
  29. import java.net.*;
  30. import edu.vub.at.util.logging.Logging;
  31. /**
  32. * This thread is responsible for broadcasting a network message at a fixed
  33. * time rate.
  34. * The network message acts as a 'heartbeat' that signals to nearby virtual
  35. * machines that this VM is still alive.
  36. *
  37. * The network packet that is broadcast (via UDP) contains the {@link Address}
  38. * by which this VM is known on the communication bus.
  39. *
  40. * When two virtual machines 'hear' one another's heartbeat, and they were not
  41. * acquainted yet, one VM becomes 'master' and the other becomes 'slave':
  42. * the slave first connects to the master's socket, after which the master
  43. * connects to the slave. Determining who becomes master and who becomes
  44. * slave is done by comparing each other's address: the 'larger' address
  45. * becomes master.
  46. *
  47. * @author jededeck
  48. * @author tvcutsem
  49. */
  50. public class MulticastServerThread extends Thread {
  51. // this is the time interval in which a broadcast message is repeated
  52. public static final int REPEAT_TIME = 2000; // in milliseconds
  53. public static InetAddress MC_ADDR;
  54. static {
  55. try {
  56. MC_ADDR = InetAddress.getByName("224.0.0.1");
  57. } catch (UnknownHostException e) {
  58. e.printStackTrace();
  59. }
  60. }
  61. /** this is the port onto which broadcasts are posted */
  62. public static final int MC_PORT = 4446;
  63. /**
  64. * marked volatile such that all threads see the actual value of the variable,
  65. * rather than a cached copy (in a local register) which may contain a stale value
  66. */
  67. private volatile boolean isActive_ = true;
  68. /** the socket over which to broadcast heartbeats */
  69. private MulticastSocket socket_;
  70. /** the address that is emitted by each broadcast */
  71. private final byte[] myAddress_;
  72. public MulticastServerThread(Address myAddress) {
  73. super("MulticastServerThread for " + myAddress);
  74. myAddress_ = myAddress.serializedForm_;
  75. }
  76. public void stopBroadcasting() {
  77. isActive_ = false;
  78. }
  79. public void run() {
  80. try {
  81. while (isActive_) {
  82. try {
  83. // wait until sending another heartbeat
  84. try {
  85. sleep(REPEAT_TIME);
  86. } catch (InterruptedException e) { }
  87. // if socket not yet initialized, create a new one
  88. if (socket_ == null) {
  89. socket_ = new MulticastSocket(MC_PORT);
  90. }
  91. // send a UDP packet containing the address
  92. DatagramPacket packet = new DatagramPacket(myAddress_, myAddress_.length, MC_ADDR, MC_PORT);
  93. socket_.send(packet);
  94. } catch (SocketException e) {
  95. // considered fatal exception, reset socket
  96. Logging.Network_LOG.error(toString() + ": socket creation error: " + e.getMessage());
  97. socket_ = null;
  98. } catch (IOException ioe) {
  99. Logging.Network_LOG.error(toString() + ": error broadcasting key: " + ioe.getMessage());
  100. }
  101. }
  102. } finally {
  103. if (socket_ != null) {
  104. socket_.close();
  105. }
  106. Logging.Network_LOG.debug(toString() + ": shutting down.");
  107. }
  108. }
  109. public String toString() {
  110. return super.getName();
  111. }
  112. }