View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.example.udp;
21  
22  import java.awt.BorderLayout;
23  import java.awt.Dimension;
24  import java.io.IOException;
25  import java.net.InetSocketAddress;
26  import java.net.SocketAddress;
27  import java.util.concurrent.ConcurrentHashMap;
28  
29  import javax.swing.JFrame;
30  import javax.swing.JLabel;
31  import javax.swing.JPanel;
32  import javax.swing.JTabbedPane;
33  
34  import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
35  import org.apache.mina.filter.logging.LoggingFilter;
36  import org.apache.mina.transport.socket.DatagramSessionConfig;
37  import org.apache.mina.transport.socket.nio.NioDatagramAcceptor;
38  
39  /**
40   * The class that will accept and process clients in order to properly
41   * track the memory usage.
42   *
43   * @author The Apache MINA Project (dev@mina.apache.org)
44   */
45  public class MemoryMonitor {
46  
47      private static final long serialVersionUID = 1L;
48  
49      public static final int PORT = 18567;
50  
51      protected static final Dimension PANEL_SIZE = new Dimension(300, 200);
52  
53      private JFrame frame;
54  
55      private JTabbedPane tabbedPane;
56  
57      private ConcurrentHashMap<SocketAddress, ClientPanel> clients;
58  
59      public MemoryMonitor() throws IOException {
60  
61          NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
62          acceptor.setHandler(new MemoryMonitorHandler(this));
63  
64          DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
65          chain.addLast("logger", new LoggingFilter());
66  
67          DatagramSessionConfig dcfg = acceptor.getSessionConfig();
68          dcfg.setReuseAddress(true);
69  
70          frame = new JFrame("Memory monitor");
71          tabbedPane = new JTabbedPane();
72          tabbedPane.add("Welcome", createWelcomePanel());
73          frame.add(tabbedPane, BorderLayout.CENTER);
74          clients = new ConcurrentHashMap<SocketAddress, ClientPanel>();
75          frame.pack();
76          frame.setLocation(300, 300);
77          frame.setVisible(true);
78  
79          acceptor.bind(new InetSocketAddress(PORT));
80          System.out.println("UDPServer listening on port " + PORT);
81      }
82  
83      private JPanel createWelcomePanel() {
84          JPanel panel = new JPanel();
85          panel.setPreferredSize(PANEL_SIZE);
86          panel.add(new JLabel("Welcome to the Memory Monitor"));
87          return panel;
88      }
89  
90      protected void recvUpdate(SocketAddress clientAddr, long update) {
91          ClientPanel clientPanel = clients.get(clientAddr);
92          if (clientPanel != null) {
93              clientPanel.updateTextField(update);
94          } else {
95              System.err.println("Received update from unknown client");
96          }
97      }
98  
99      protected void addClient(SocketAddress clientAddr) {
100         if (!containsClient(clientAddr)) {
101             ClientPanel clientPanel = new ClientPanel(clientAddr.toString());
102             tabbedPane.add(clientAddr.toString(), clientPanel);
103             clients.put(clientAddr, clientPanel);
104         }
105     }
106 
107     protected boolean containsClient(SocketAddress clientAddr) {
108         return clients.contains(clientAddr);
109     }
110 
111     protected void removeClient(SocketAddress clientAddr) {
112         clients.remove(clientAddr);
113     }
114 
115     public static void main(String[] args) throws IOException {
116         new MemoryMonitor();
117     }
118 }