001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.example.udp;
021
022import java.net.SocketAddress;
023
024import org.apache.mina.core.buffer.IoBuffer;
025import org.apache.mina.core.service.IoHandlerAdapter;
026import org.apache.mina.core.session.IdleStatus;
027import org.apache.mina.core.session.IoSession;
028
029/**
030 * Class the extends IoHandlerAdapter in order to properly handle
031 * connections and the data the connections send
032 *
033 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
034 */
035public class MemoryMonitorHandler extends IoHandlerAdapter {
036
037    private MemoryMonitor server;
038
039    public MemoryMonitorHandler(MemoryMonitor server) {
040        this.server = server;
041    }
042
043    @Override
044    public void exceptionCaught(IoSession session, Throwable cause)
045            throws Exception {
046        cause.printStackTrace();
047        session.closeNow();
048    }
049
050    @Override
051    public void messageReceived(IoSession session, Object message)
052            throws Exception {
053
054        if (message instanceof IoBuffer) {
055            IoBuffer buffer = (IoBuffer) message;
056            SocketAddress remoteAddress = session.getRemoteAddress();
057            server.recvUpdate(remoteAddress, buffer.getLong());
058        }
059    }
060
061    @Override
062    public void sessionClosed(IoSession session) throws Exception {
063        System.out.println("Session closed...");
064        SocketAddress remoteAddress = session.getRemoteAddress();
065        server.removeClient(remoteAddress);
066    }
067
068    @Override
069    public void sessionCreated(IoSession session) throws Exception {
070
071        System.out.println("Session created...");
072
073        SocketAddress remoteAddress = session.getRemoteAddress();
074        server.addClient(remoteAddress);
075    }
076
077    @Override
078    public void sessionIdle(IoSession session, IdleStatus status)
079            throws Exception {
080        System.out.println("Session idle...");
081    }
082
083    @Override
084    public void sessionOpened(IoSession session) throws Exception {
085        System.out.println("Session Opened...");
086    }
087}