/* * Copyright 2007 Kevin Roe, Daniel Mccarney * This file is part of Jriver. * * Jriver is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * Jriver is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.jriver.core; import java.util.*; import java.io.*; import org.jriver.core.*; import org.jriver.lib.*; public class HeartBeat implements Runnable { final static int HB_INTERVAL = 2000; private static HeartBeat heartBeat; public ArrayList<Living> livings = new ArrayList<Living>(); public HeartBeat() { } public static HeartBeat getHeartBeat() { if(heartBeat == null) heartBeat = new HeartBeat(); return heartBeat; } public void addLiving(Living living) { livings.add(living); System.out.println("HeartBeat activated in "+living.queryName()); } public void removeLiving(Living living) { livings.remove(living); System.out.println("HeartBeat deactivated in "+living.queryName()+"["+living.getID()+"]"); } public void run() { System.out.println("HeartBeat Singleton Initialized."); while(heartBeat != null) { try { Thread.currentThread().sleep(HB_INTERVAL); } catch (InterruptedException e) { e.printStackTrace(); } if(livings != null) { Object[] all = livings.toArray(); for(int x = 0; x < livings.size(); x++) { Living thisPlayer = (Living) all[x]; thisPlayer.heartBeat(); } } } } }