PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/interpreter/branches/net_refactor+bt/switchboard_src/edu/vub/at/actors/net/comm/switchboard/SBHeartbeatServerThread.java

http://ambienttalk.googlecode.com/
Java | 101 lines | 46 code | 15 blank | 40 comment | 3 complexity | 6adffc4b5517bf2e1b00baefa24cfaf8 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.0, LGPL-2.1
  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.switchboard;
  28. import java.io.*;
  29. import java.net.*;
  30. /**
  31. *
  32. * @author kpinte
  33. *
  34. */
  35. public class SBHeartbeatServerThread extends Thread {
  36. // this is the time interval in which a broadcast message is repeated
  37. public static final int REPEAT_TIME = 2000; // in milliseconds
  38. /** this is the port onto which broadcasts are posted */
  39. public static final int SB_HB_PORT = 4447;
  40. /**
  41. * marked volatile such that all threads see the actual value of the variable,
  42. * rather than a cached copy (in a local register) which may contain a stale value
  43. */
  44. private volatile boolean isActive_ = true;
  45. /** the socket over which to broadcast heartbeats */
  46. private DatagramSocket socket_;
  47. private final SBNetAddress sbAddress_;
  48. private final SBIdentAddress identificationAddr_;
  49. public SBHeartbeatServerThread(SBNetAddress sbAddress, SBIdentAddress identificationAddr) {
  50. super("SBHeartbeatServerThread for " + sbAddress);
  51. sbAddress_ = sbAddress;
  52. identificationAddr_ = identificationAddr;
  53. }
  54. public void startServing(DatagramSocket socket) {
  55. socket_ = socket;
  56. this.start();
  57. }
  58. public void stopBroadcasting() {
  59. isActive_ = false;
  60. }
  61. public void run() {
  62. try {
  63. while (isActive_) {
  64. try {
  65. // wait until sending another heartbeat
  66. try {
  67. sleep(REPEAT_TIME);
  68. } catch (InterruptedException e) { }
  69. // send a UDP packet containing the address
  70. DatagramPacket packet = new DatagramPacket(identificationAddr_.serializedForm_, identificationAddr_.serializedForm_.length, sbAddress_.ipAddress_, sbAddress_.port_);
  71. socket_.send(packet);
  72. } catch (IOException ioe) {
  73. SBCommunicationBus.SwitchboardLogger.error(toString() + ": error broadcasting key: " + ioe.getMessage());
  74. }
  75. }
  76. } finally {
  77. if (socket_ != null) {
  78. socket_.close();
  79. }
  80. SBCommunicationBus.SwitchboardLogger.debug(toString() + ": shutting down.");
  81. }
  82. }
  83. public String toString() {
  84. return super.getName();
  85. }
  86. }