com.github.wolf480pl.ircd.netty.NettySession.java Source code

Java tutorial

Introduction

Here is the source code for com.github.wolf480pl.ircd.netty.NettySession.java

Source

/*
 * This file is part of IRCd4j.
 *
 * Copyright (c) 2014 Wolf480pl <wolf480@interia.pl>
 * IRCd4j is licensed under the GNU Lesser General Public License.
 *
 * IRCd4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * IRCd4j is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/* This file incorporates work covered by the following copyright and
 * permission notice:
 *
 *     This file is part of Flow Networking, licensed under the MIT License (MIT).
 *
 *     Copyright (c) 2013 Spout LLC <http://www.spout.org/>
 *
 *     Permission is hereby granted, free of charge, to any person obtaining a copy
 *     of this software and associated documentation files (the "Software"), to deal
 *     in the Software without restriction, including without limitation the rights
 *     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *     copies of the Software, and to permit persons to whom the Software is
 *     furnished to do so, subject to the following conditions:
 *
 *     The above copyright notice and this permission notice shall be included in
 *     all copies or substantial portions of the Software.
 *
 *     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *     THE SOFTWARE.
 */
package com.github.wolf480pl.ircd.netty;

import java.net.SocketAddress;

import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;

import org.slf4j.Logger;

import com.github.wolf480pl.ircd.Message;
import com.github.wolf480pl.ircd.Session;
import com.github.wolf480pl.ircd.SessionHandler;

public class NettySession implements Session {
    private final Channel channel;
    private final SessionHandler handler;

    public NettySession(Channel channel, SessionHandler handler) {
        this.channel = channel;
        this.handler = handler;
    }

    @Override
    public void send(Message msg) {
        sendWithFuture(msg);
    }

    public ChannelFuture sendWithFuture(Message msg) {
        getLogger().debug("" + getRemoteAddress() + " <- " + msg);
        if (!channel.isActive()) {
            throw new IllegalStateException("Trying to send a message when a session is inactive!");
        }
        return channel.writeAndFlush(msg).addListener(new GenericFutureListener<Future<? super Void>>() {
            @Override
            public void operationComplete(Future<? super Void> future) throws Exception {
                if (future.cause() != null) {
                    handler.onOutboundThrowable(NettySession.this, future.cause());
                }
            }
        });

    }

    @Override
    public void disconnect() {
        channel.disconnect();
    };

    public void validate(Channel ch) {
        if (this.channel != ch) {
            throw new IllegalStateException("Not our channel!");
        }
    }

    @Override
    public SocketAddress getRemoteAddress() {
        return channel.remoteAddress();
    }

    @Override
    public Logger getLogger() {
        return handler.getLogger(this);
    }
}