001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.example.udp;
021
022import java.awt.BorderLayout;
023import java.awt.Dimension;
024import java.io.IOException;
025import java.net.InetSocketAddress;
026import java.net.SocketAddress;
027import java.util.concurrent.ConcurrentHashMap;
028
029import javax.swing.JFrame;
030import javax.swing.JLabel;
031import javax.swing.JPanel;
032import javax.swing.JTabbedPane;
033
034import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
035import org.apache.mina.filter.logging.LoggingFilter;
036import org.apache.mina.transport.socket.DatagramSessionConfig;
037import org.apache.mina.transport.socket.nio.NioDatagramAcceptor;
038
039/**
040 * The class that will accept and process clients in order to properly
041 * track the memory usage.
042 *
043 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
044 */
045public class MemoryMonitor {
046    public static final int PORT = 18567;
047
048    protected static final Dimension PANEL_SIZE = new Dimension(300, 200);
049
050    private JFrame frame;
051
052    private JTabbedPane tabbedPane;
053
054    private ConcurrentHashMap<SocketAddress, ClientPanel> clients;
055
056    public MemoryMonitor() throws IOException {
057
058        NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
059        acceptor.setHandler(new MemoryMonitorHandler(this));
060
061        DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
062        chain.addLast("logger", new LoggingFilter());
063
064        DatagramSessionConfig dcfg = acceptor.getSessionConfig();
065        dcfg.setReuseAddress(true);
066
067        frame = new JFrame("Memory monitor");
068        tabbedPane = new JTabbedPane();
069        tabbedPane.add("Welcome", createWelcomePanel());
070        frame.add(tabbedPane, BorderLayout.CENTER);
071        clients = new ConcurrentHashMap<SocketAddress, ClientPanel>();
072        frame.pack();
073        frame.setLocation(300, 300);
074        frame.setVisible(true);
075
076        acceptor.bind(new InetSocketAddress(PORT));
077        System.out.println("UDPServer listening on port " + PORT);
078    }
079
080    private JPanel createWelcomePanel() {
081        JPanel panel = new JPanel();
082        panel.setPreferredSize(PANEL_SIZE);
083        panel.add(new JLabel("Welcome to the Memory Monitor"));
084        return panel;
085    }
086
087    protected void recvUpdate(SocketAddress clientAddr, long update) {
088        ClientPanel clientPanel = clients.get(clientAddr);
089        if (clientPanel != null) {
090            clientPanel.updateTextField(update);
091        } else {
092            System.err.println("Received update from unknown client");
093        }
094    }
095
096    protected void addClient(SocketAddress clientAddr) {
097        if (!containsClient(clientAddr)) {
098            ClientPanel clientPanel = new ClientPanel(clientAddr.toString());
099            tabbedPane.add(clientAddr.toString(), clientPanel);
100            clients.put(clientAddr, clientPanel);
101        }
102    }
103
104    protected boolean containsClient(SocketAddress clientAddr) {
105        return clients.contains(clientAddr);
106    }
107
108    protected void removeClient(SocketAddress clientAddr) {
109        clients.remove(clientAddr);
110    }
111
112    public static void main(String[] args) throws IOException {
113        new MemoryMonitor();
114    }
115}