001/*
002 * $Id: MonitorManager.java,v 1.5 2011/03/24 16:06:33 davep Exp $
003 *
004 * This file is part of McIDAS-V
005 *
006 * Copyright 2007-2011
007 * Space Science and Engineering Center (SSEC)
008 * University of Wisconsin - Madison
009 * 1225 W. Dayton Street, Madison, WI 53706, USA
010 * https://www.ssec.wisc.edu/mcidas
011 * 
012 * All Rights Reserved
013 * 
014 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
015 * some McIDAS-V source code is based on IDV and VisAD source code.  
016 * 
017 * McIDAS-V is free software; you can redistribute it and/or modify
018 * it under the terms of the GNU Lesser Public License as published by
019 * the Free Software Foundation; either version 3 of the License, or
020 * (at your option) any later version.
021 * 
022 * McIDAS-V is distributed in the hope that it will be useful,
023 * but WITHOUT ANY WARRANTY; without even the implied warranty of
024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
025 * GNU Lesser Public License for more details.
026 * 
027 * You should have received a copy of the GNU Lesser Public License
028 * along with this program.  If not, see http://www.gnu.org/licenses.
029 */
030package edu.wisc.ssec.mcidasv.monitors;
031
032import java.util.Map;
033import java.util.concurrent.ConcurrentHashMap;
034import java.util.concurrent.Executors;
035import java.util.concurrent.ScheduledExecutorService;
036import java.util.concurrent.ScheduledFuture;
037import java.util.concurrent.TimeUnit;
038
039import edu.wisc.ssec.mcidasv.monitors.memory.MemoryMonitor;
040import edu.wisc.ssec.mcidasv.monitors.time.TimeMonitor;
041
042import ucar.unidata.util.CacheManager;
043
044public class MonitorManager {
045
046    public enum MonitorType { MEMORY, TIME };
047
048    private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(3);
049
050    private final Map<MonitorType, Monitorable> monitors = new ConcurrentHashMap<MonitorType, Monitorable>();
051
052    private final Map<Monitorable, ScheduledFuture<?>> woot = new ConcurrentHashMap<Monitorable, ScheduledFuture<?>>();
053
054    public MonitorManager() {
055        monitors.put(MonitorType.MEMORY, new MemoryMonitor(this, 75, 95));
056        monitors.put(MonitorType.TIME, new TimeMonitor());
057    }
058
059    public void addListener(final MonitorType type, final Monitoring listener) {
060        Monitorable m = monitors.get(type);
061        if (!m.hasMonitors())
062            woot.put(m, scheduler.scheduleWithFixedDelay(m, 0, 2, TimeUnit.SECONDS));
063        m.addMonitor(listener);
064    }
065
066    public void removeListener(final MonitorType type, final Monitoring listener) {
067        Monitorable m = monitors.get(type);
068        m.removeMonitor(listener);
069        if (!m.hasMonitors()) {
070            ScheduledFuture<?> handle = woot.remove(m);
071            if (handle != null) {
072                handle.cancel(false);
073            }
074        }
075    }
076
077    public void scheduleClearCache() {
078        Runnable r = new Runnable() {
079            public void run() {
080                CacheManager.clearCache();
081            }
082        };
083        scheduler.schedule(r, 1, TimeUnit.SECONDS);
084    }
085}