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   * @version $Rev$, $Date$
45   */
46  public class MemoryMonitor {
47  
48      private static final long serialVersionUID = 1L;
49  
50      public static final int PORT = 18567;
51  
52      protected static final Dimension PANEL_SIZE = new Dimension(300, 200);
53  
54      private JFrame frame;
55  
56      private JTabbedPane tabbedPane;
57  
58      private ConcurrentHashMap<SocketAddress, ClientPanel> clients;
59  
60      public MemoryMonitor() throws IOException {
61  
62          NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
63          acceptor.setHandler(new MemoryMonitorHandler(this));
64  
65          DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
66          chain.addLast("logger", new LoggingFilter());
67  
68          DatagramSessionConfig dcfg = acceptor.getSessionConfig();
69          dcfg.setReuseAddress(true);
70  
71          frame = new JFrame("Memory monitor");
72          tabbedPane = new JTabbedPane();
73          tabbedPane.add("Welcome", createWelcomePanel());
74          frame.add(tabbedPane, BorderLayout.CENTER);
75          clients = new ConcurrentHashMap<SocketAddress, ClientPanel>();
76          frame.pack();
77          frame.setLocation(300, 300);
78          frame.setVisible(true);
79  
80          acceptor.bind(new InetSocketAddress(PORT));
81          System.out.println("UDPServer listening on port " + PORT);
82      }
83  
84      private JPanel createWelcomePanel() {
85          JPanel panel = new JPanel();
86          panel.setPreferredSize(PANEL_SIZE);
87          panel.add(new JLabel("Welcome to the Memory Monitor"));
88          return panel;
89      }
90  
91      protected void recvUpdate(SocketAddress clientAddr, long update) {
92          ClientPanel clientPanel = clients.get(clientAddr);
93          if (clientPanel != null) {
94              clientPanel.updateTextField(update);
95          } else {
96              System.err.println("Received update from unknown client");
97          }
98      }
99  
100     protected void addClient(SocketAddress clientAddr) {
101         if (!containsClient(clientAddr)) {
102             ClientPanel clientPanel = new ClientPanel(clientAddr.toString());
103             tabbedPane.add(clientAddr.toString(), clientPanel);
104             clients.put(clientAddr, clientPanel);
105         }
106     }
107 
108     protected boolean containsClient(SocketAddress clientAddr) {
109         return clients.contains(clientAddr);
110     }
111 
112     protected void removeClient(SocketAddress clientAddr) {
113         clients.remove(clientAddr);
114     }
115 
116     public static void main(String[] args) throws IOException {
117         new MemoryMonitor();
118     }
119 }