org.rdv.rbnb.MarkerManager.java Source code

Java tutorial

Introduction

Here is the source code for org.rdv.rbnb.MarkerManager.java

Source

/*
 * RDV
 * Real-time Data Viewer
 * http://rdv.googlecode.com/
 * 
 * Copyright (c) 2005-2007 University at Buffalo
 * Copyright (c) 2005-2007 NEES Cyberinfrastructure Center
 * Copyright (c) 2008 Palta Software
 *
 * 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.
 * 
 * $URL: https://rdv.googlecode.com/svn/trunk/src/org/rdv/rbnb/MarkerManager.java $
 * $Revision: 1149 $
 * $Date: 2008-07-03 10:23:04 -0400 (Thu, 03 Jul 2008) $
 * $Author: jason@paltasoftware.com $
 */

package org.rdv.rbnb;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * A class to manage available event markers and put new markers on the server.
 * 
 * @author Jason P. Hanley
 */
public class MarkerManager implements DataListener {
    private static final Log log = LogFactory.getLog(MarkerManager.class.getName());
    private String rbnbSourceName = "_Events";
    private String rbnbChannel = "EventsChannel";

    private RBNBController rbnbController;

    private List<EventMarker> markers;

    private List<EventMarkerListener> markerListeners;

    /**
     * Create the marker manager and starts listening for new markers from the
     * metadata manager.
     * 
     * @param rbnbController  the rbnb interface
     */
    public MarkerManager(RBNBController rbnbController) {
        super();

        try {
            InetAddress addr = InetAddress.getLocalHost();
            String hostname = addr.getHostName();
            rbnbSourceName += "@" + hostname;
        } catch (UnknownHostException e) {
        }

        this.rbnbController = rbnbController;

        markers = new ArrayList<EventMarker>();

        markerListeners = new ArrayList<EventMarkerListener>();

        rbnbController.getMetadataManager().addMarkerListener(this);
    }

    /**
     * Called when new markers are available.
     * 
     * @param channelMap  the channel map containing the marker data.
     */
    @Override
    public void postData(final SubscriptionResponse r) {
        //ChannelMap channelMap=r.getChannelMap();
        TimeSeriesData tsd = r.getTimeSeries(null);

        if (!tsd.hasData()) {
            log.debug("clearing markers");
            markers.clear();
            fireMarkersCleared();
            return;
        }
        //if (tsd.getChannelCount() == 0) {
        //  return;
        //}

        for (int i = 0; i < tsd.getChannelCount(); i++) {
            Iterator<Double> timeIt = tsd.getTimes(i).iterator();

            while (timeIt.hasNext()) {
                String markerString = tsd.getDataAsString(i, timeIt.next());
                EventMarker marker = new EventMarker();
                try {
                    marker.setFromEventXml(markerString);
                } catch (Exception e) {
                    e.printStackTrace();
                    continue;
                }

                if (!markers.contains(marker)) {
                    markers.add(marker);
                    Collections.sort(markers);
                    fireNewMarker(marker);
                }
            }
        }
    }

    /**
     * Retruns a list of all event markers.
     * 
     * @return  a list of event markers
     */
    public List<EventMarker> getMarkers() {
        return Collections.unmodifiableList(markers);
    }

    /**
     * Puts the event marker on the server.
     * 
     * @param eventMarker  the event marker to put
     * @throws Exception   if the marker could not be sent to the server
     */
    public void putMarker(EventMarker eventMarker) throws Exception {
        DataTurbine markerSource = new DataTurbine(rbnbSourceName);
        markerSource.setServerName(rbnbController.getRBNBConnectionString());
        markerSource.open();
        markerSource.putMarker(eventMarker, rbnbChannel);
        markerSource.closeAndKeep();

        rbnbController.updateMetadata();
    }

    /**
     * Add a listener for new event markers.
     * 
     * @param listener  the event marker listener to add
     */
    public void addMarkerListener(EventMarkerListener listener) {
        markerListeners.add(listener);
    }

    /**
     * Remove a listener for new event markers.
     * 
     * @param listener  the event marker listener to remove
     */
    public void removeMarkerListener(EventMarkerListener listener) {
        markerListeners.remove(listener);
    }

    /**
     * Send the new event marker to the registered listeners.
     * 
     * @param marker  the new event marker to send
     */
    protected void fireNewMarker(EventMarker marker) {
        for (EventMarkerListener listener : markerListeners) {
            listener.eventMarkerAdded(marker);
        }
    }

    /**
     * Send the markers cleared signal to the registered listeners.
     */
    protected void fireMarkersCleared() {
        for (EventMarkerListener listener : markerListeners) {
            listener.eventMarkersCleared();
        }
    }
}