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 The Apache MINA Project (dev@mina.apache.org)
34   * @version $Rev$, $Date$
35   */
36  public class MemoryMonitorHandler extends IoHandlerAdapter {
37  
38      private MemoryMonitor server;
39  
40      public MemoryMonitorHandler(MemoryMonitor server) {
41          this.server = server;
42      }
43  
44      @Override
45      public void exceptionCaught(IoSession session, Throwable cause)
46              throws Exception {
47          cause.printStackTrace();
48          session.close();
49      }
50  
51      @Override
52      public void messageReceived(IoSession session, Object message)
53              throws Exception {
54  
55          if (message instanceof IoBuffer) {
56              IoBuffer buffer = (IoBuffer) message;
57              SocketAddress remoteAddress = session.getRemoteAddress();
58              server.recvUpdate(remoteAddress, buffer.getLong());
59          }
60      }
61  
62      @Override
63      public void sessionClosed(IoSession session) throws Exception {
64          System.out.println("Session closed...");
65          SocketAddress remoteAddress = session.getRemoteAddress();
66          server.removeClient(remoteAddress);
67      }
68  
69      @Override
70      public void sessionCreated(IoSession session) throws Exception {
71  
72          System.out.println("Session created...");
73  
74          SocketAddress remoteAddress = session.getRemoteAddress();
75          server.addClient(remoteAddress);
76      }
77  
78      @Override
79      public void sessionIdle(IoSession session, IdleStatus status)
80              throws Exception {
81          System.out.println("Session idle...");
82      }
83  
84      @Override
85      public void sessionOpened(IoSession session) throws Exception {
86          System.out.println("Session Opened...");
87      }
88  }