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 <a href="http://mina.apache.org">Apache MINA Project</a>
44   */
45  public class MemoryMonitor {
46      public static final int PORT = 18567;
47  
48      protected static final Dimension PANEL_SIZE = new Dimension(300, 200);
49  
50      private JFrame frame;
51  
52      private JTabbedPane tabbedPane;
53  
54      private ConcurrentHashMap<SocketAddress, ClientPanel> clients;
55  
56      public MemoryMonitor() throws IOException {
57  
58          NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
59          acceptor.setHandler(new MemoryMonitorHandler(this));
60  
61          DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
62          chain.addLast("logger", new LoggingFilter());
63  
64          DatagramSessionConfig dcfg = acceptor.getSessionConfig();
65          dcfg.setReuseAddress(true);
66  
67          frame = new JFrame("Memory monitor");
68          tabbedPane = new JTabbedPane();
69          tabbedPane.add("Welcome", createWelcomePanel());
70          frame.add(tabbedPane, BorderLayout.CENTER);
71          clients = new ConcurrentHashMap<SocketAddress, ClientPanel>();
72          frame.pack();
73          frame.setLocation(300, 300);
74          frame.setVisible(true);
75  
76          acceptor.bind(new InetSocketAddress(PORT));
77          System.out.println("UDPServer listening on port " + PORT);
78      }
79  
80      private JPanel createWelcomePanel() {
81          JPanel panel = new JPanel();
82          panel.setPreferredSize(PANEL_SIZE);
83          panel.add(new JLabel("Welcome to the Memory Monitor"));
84          return panel;
85      }
86  
87      protected void recvUpdate(SocketAddress clientAddr, long update) {
88          ClientPanel clientPanel = clients.get(clientAddr);
89          if (clientPanel != null) {
90              clientPanel.updateTextField(update);
91          } else {
92              System.err.println("Received update from unknown client");
93          }
94      }
95  
96      protected void addClient(SocketAddress clientAddr) {
97          if (!containsClient(clientAddr)) {
98              ClientPanel clientPanel = new ClientPanel(clientAddr.toString());
99              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 }