001/*
002 * #%L
003 * Netarchivesuite - archive
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023package dk.netarkivet.archive.bitarchive.distribute;
024
025import dk.netarkivet.archive.distribute.ArchiveMessage;
026import dk.netarkivet.archive.distribute.ArchiveMessageVisitor;
027import dk.netarkivet.common.distribute.ChannelID;
028import dk.netarkivet.common.distribute.Channels;
029import dk.netarkivet.common.exceptions.ArgumentNotValid;
030
031/**
032 * Simple class representing a HeartBeat message from a bit archive application. A heartbeat has an applicationId, that
033 * identifies the application that generated the heartbeat.
034 * <p>
035 * TODO This class should probably contain more status data from bit archive application later.
036 */
037@SuppressWarnings({"serial"})
038public class HeartBeatMessage extends ArchiveMessage {
039
040    /**
041     * time when heartbeat occurred. Note that timestamps cannot be compared between processes.
042     */
043    private long timestamp;
044    /** id of the application sending the heartbeat. */
045    private String applicationId;
046
047    /**
048     * Creates a heartbeat message. The time of the heartbeat is set to the creation of this object.
049     *
050     * @param inReceiver ChannelID for the recipient of this message.
051     * @param applicationId - id of the application that sent the heartbeat
052     */
053    public HeartBeatMessage(ChannelID inReceiver, String applicationId) {
054        super(inReceiver, Channels.getError());
055        ArgumentNotValid.checkNotNullOrEmpty(applicationId, "applicationId");
056        timestamp = System.currentTimeMillis();
057        this.applicationId = applicationId;
058    }
059
060    /**
061     * @return time of heartbeat occurrence.
062     */
063    public long getTimestamp() {
064        return timestamp;
065    }
066
067    /**
068     * @return id of the application that generated the heartbeat.
069     */
070    public String getBitarchiveID() {
071        return applicationId;
072    }
073
074    /**
075     * Retrieval of a string representation of this instance.
076     *
077     * @return The string representation of this instance.
078     */
079    public String toString() {
080        return ("Heartbeat for " + applicationId + " at " + timestamp);
081    }
082
083    /**
084     * Should be implemented as a part of the visitor pattern. fx.: public void accept(ArchiveMessageVisitor v) {
085     * v.visit(this); }
086     *
087     * @param v A message visitor
088     */
089    public void accept(ArchiveMessageVisitor v) {
090        v.visit(this);
091    }
092}