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.net.SocketAddress;
23  
24  import org.apache.mina.core.buffer.IoBuffer;
25  import org.apache.mina.core.service.IoHandlerAdapter;
26  import org.apache.mina.core.session.IdleStatus;
27  import org.apache.mina.core.session.IoSession;
28  
29  /**
30   * Class the extends IoHandlerAdapter in order to properly handle
31   * connections and the data the connections send
32   *
33   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
34   */
35  public class MemoryMonitorHandler extends IoHandlerAdapter {
36  
37      private MemoryMonitor server;
38  
39      public MemoryMonitorHandler(MemoryMonitor server) {
40          this.server = server;
41      }
42  
43      @Override
44      public void exceptionCaught(IoSession session, Throwable cause)
45              throws Exception {
46          cause.printStackTrace();
47          session.close(true);
48      }
49  
50      @Override
51      public void messageReceived(IoSession session, Object message)
52              throws Exception {
53  
54          if (message instanceof IoBuffer) {
55              IoBuffer buffer = (IoBuffer) message;
56              SocketAddress remoteAddress = session.getRemoteAddress();
57              server.recvUpdate(remoteAddress, buffer.getLong());
58          }
59      }
60  
61      @Override
62      public void sessionClosed(IoSession session) throws Exception {
63          System.out.println("Session closed...");
64          SocketAddress remoteAddress = session.getRemoteAddress();
65          server.removeClient(remoteAddress);
66      }
67  
68      @Override
69      public void sessionCreated(IoSession session) throws Exception {
70  
71          System.out.println("Session created...");
72  
73          SocketAddress remoteAddress = session.getRemoteAddress();
74          server.addClient(remoteAddress);
75      }
76  
77      @Override
78      public void sessionIdle(IoSession session, IdleStatus status)
79              throws Exception {
80          System.out.println("Session idle...");
81      }
82  
83      @Override
84      public void sessionOpened(IoSession session) throws Exception {
85          System.out.println("Session Opened...");
86      }
87  }