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: */
020: package org.apache.mina.example.udp;
021:
022: import java.awt.BorderLayout;
023: import java.awt.Dimension;
024: import java.io.IOException;
025: import java.net.InetSocketAddress;
026: import java.net.SocketAddress;
027: import java.util.concurrent.ConcurrentHashMap;
028:
029: import javax.swing.JFrame;
030: import javax.swing.JLabel;
031: import javax.swing.JPanel;
032: import javax.swing.JTabbedPane;
033:
034: import org.apache.mina.common.DefaultIoFilterChainBuilder;
035: import org.apache.mina.filter.logging.LoggingFilter;
036: import org.apache.mina.transport.socket.DatagramSessionConfig;
037: import 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="mailto:dev@directory.apache.org">Apache Directory Project</a>
044: * @version $Rev$, $Date$
045: */
046: public class MemoryMonitor {
047:
048: private static final long serialVersionUID = 1L;
049:
050: public static final int PORT = 18567;
051:
052: protected static final Dimension PANEL_SIZE = new Dimension(300,
053: 200);
054:
055: private JFrame frame;
056:
057: private JTabbedPane tabbedPane;
058:
059: private ConcurrentHashMap<SocketAddress, ClientPanel> clients;
060:
061: public MemoryMonitor() throws IOException {
062:
063: NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
064: acceptor.setHandler(new MemoryMonitorHandler(this ));
065:
066: DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
067: chain.addLast("logger", new LoggingFilter());
068:
069: DatagramSessionConfig dcfg = acceptor.getSessionConfig();
070: dcfg.setReuseAddress(true);
071:
072: frame = new JFrame("Memory monitor");
073: tabbedPane = new JTabbedPane();
074: tabbedPane.add("Welcome", createWelcomePanel());
075: frame.add(tabbedPane, BorderLayout.CENTER);
076: clients = new ConcurrentHashMap<SocketAddress, ClientPanel>();
077: frame.pack();
078: frame.setLocation(300, 300);
079: frame.setVisible(true);
080:
081: acceptor.bind(new InetSocketAddress(PORT));
082: System.out.println("UDPServer listening on port " + PORT);
083: }
084:
085: private JPanel createWelcomePanel() {
086: JPanel panel = new JPanel();
087: panel.setPreferredSize(PANEL_SIZE);
088: panel.add(new JLabel("Welcome to the Memory Monitor"));
089: return panel;
090: }
091:
092: protected void recvUpdate(SocketAddress clientAddr, long update) {
093: ClientPanel clientPanel = clients.get(clientAddr);
094: if (clientPanel != null) {
095: clientPanel.updateTextField(update);
096: } else {
097: System.err.println("Received update from unknown client");
098: }
099: }
100:
101: protected void addClient(SocketAddress clientAddr) {
102: if (!containsClient(clientAddr)) {
103: ClientPanel clientPanel = new ClientPanel(clientAddr
104: .toString());
105: tabbedPane.add(clientAddr.toString(), clientPanel);
106: clients.put(clientAddr, clientPanel);
107: }
108: }
109:
110: protected boolean containsClient(SocketAddress clientAddr) {
111: return clients.contains(clientAddr);
112: }
113:
114: protected void removeClient(SocketAddress clientAddr) {
115: clients.remove(clientAddr);
116: }
117:
118: public static void main(String[] args) throws IOException {
119: new MemoryMonitor();
120: }
121: }
|