/interpreter/branches/net_refactor+bt/switchboard_src/edu/vub/at/actors/net/comm/switchboard/SBHeartbeatServerThread.java
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
- /**
- * AmbientTalk/2 Project
- * (c) Programming Technology Lab, 2006 - 2007
- * Authors: Tom Van Cutsem & Stijn Mostinckx
- *
- * Permission is hereby granted, free of charge, to any person
- * obtaining a copy of this software and associated documentation
- * files (the "Software"), to deal in the Software without
- * restriction, including without limitation the rights to use,
- * copy, modify, merge, publish, distribute, sublicense, and/or
- * sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following
- * conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- * OTHER DEALINGS IN THE SOFTWARE.
- */
- package edu.vub.at.actors.net.comm.switchboard;
- import java.io.*;
- import java.net.*;
- /**
- *
- * @author kpinte
- *
- */
- public class SBHeartbeatServerThread extends Thread {
-
- // this is the time interval in which a broadcast message is repeated
- public static final int REPEAT_TIME = 2000; // in milliseconds
-
- /** this is the port onto which broadcasts are posted */
- public static final int SB_HB_PORT = 4447;
-
- /**
- * marked volatile such that all threads see the actual value of the variable,
- * rather than a cached copy (in a local register) which may contain a stale value
- */
- private volatile boolean isActive_ = true;
-
- /** the socket over which to broadcast heartbeats */
- private DatagramSocket socket_;
- private final SBNetAddress sbAddress_;
-
- private final SBIdentAddress identificationAddr_;
- public SBHeartbeatServerThread(SBNetAddress sbAddress, SBIdentAddress identificationAddr) {
- super("SBHeartbeatServerThread for " + sbAddress);
- sbAddress_ = sbAddress;
- identificationAddr_ = identificationAddr;
- }
-
- public void startServing(DatagramSocket socket) {
- socket_ = socket;
- this.start();
- }
-
- public void stopBroadcasting() {
- isActive_ = false;
- }
-
- public void run() {
- try {
- while (isActive_) {
- try {
- // wait until sending another heartbeat
- try {
- sleep(REPEAT_TIME);
- } catch (InterruptedException e) { }
-
- // send a UDP packet containing the address
- DatagramPacket packet = new DatagramPacket(identificationAddr_.serializedForm_, identificationAddr_.serializedForm_.length, sbAddress_.ipAddress_, sbAddress_.port_);
- socket_.send(packet);
- } catch (IOException ioe) {
- SBCommunicationBus.SwitchboardLogger.error(toString() + ": error broadcasting key: " + ioe.getMessage());
- }
- }
- } finally {
- if (socket_ != null) {
- socket_.close();
- }
- SBCommunicationBus.SwitchboardLogger.debug(toString() + ": shutting down.");
- }
- }
-
- public String toString() {
- return super.getName();
- }
-
- }