/**
 * This file is part of Pulse, licensed under the MIT License (MIT).
 *
 * Copyright (c) 2014 InspireNXE <http://inspirenxe.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 org.inspirenxe.server.network;

import com.flowpowered.networking.Message;
import com.flowpowered.networking.protocol.AbstractProtocol;
import com.flowpowered.networking.session.BasicSession;
import io.netty.channel.Channel;
import org.inspirenxe.server.Game;

/**
 * Represents an open connection to a client. All {@link com.flowpowered.networking.Message}s are sent through the session.
 */
public class ServerSession extends BasicSession {
    private final Game game;
    private String uuid;
    private String username;

    /**
     * Constructs a new server session from the game, channel and protocol.
     *
     * @param game The game
     * @param channel The network channel
     * @param protocol The abstract protocol
     */
    public ServerSession(Game game, Channel channel, ServerProtocol protocol) {
        super(channel, protocol);
        this.game = game;
    }

    @Override
    public void messageReceived(Message message) {
        final ChannelMessage channelMessage = (ChannelMessage) message;
        channelMessage.setSession(this);
        game.getLogger().info(channelMessage);
        for (ChannelMessage.Channel channel : channelMessage.getChannels()) {
            game.getNetwork().offer(channel, channelMessage);
        }
    }

    @Override
    public void setProtocol(AbstractProtocol protocol) {
        if (!(protocol instanceof ServerProtocol)) {
            throw new IllegalArgumentException(protocol + " must be an extension of ServerProtocol!");
        }
        super.setProtocol(protocol);
    }

    @Override
    public void onThrowable(Throwable t) {
        game.getLogger().fatal(t);
    }

    /**
     * Returns the ID of the session.
     *
     * @return The session's ID
     */
    public String getUUID() {
        return uuid;
    }

    /**
     * Sets the session's ID
     *
     * @param uuid The session ID
     */
    public void setUUID(String uuid) {
        this.uuid = uuid;
    }

    /**
     * Returns the username of the player for the session.
     *
     * @return The player's username
     */
    public String getUsername() {
        return username;
    }

    /**
     * Sets the player's username
     *
     * @param username The player's username
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * Returns the game for the session.
     *
     * @return The game
     */
    public Game getGame() {
        return game;
    }
}


@@ -2284,55 +2284,8 @@
 s);%0A
-        game.getLogger().info(channelMessage);%0A
     
@@ -2750,16 +2750,23 @@
  void on
+Inbound
 Throwabl
@@ -2778,16 +2778,24 @@
 owable t
+hrowable
 ) %7B%0A    
@@ -2821,17 +2821,228 @@
 ).fatal(
-t
+%22Exception caught on inbound messsage%22, throwable);%0A    %7D%0A%0A    @Override%0A    public void onOutboundThrowable(Throwable throwable) %7B%0A        game.getLogger().fatal(%22Exception caught on outbound message%22, throwable
 );%0A    %7D
@@ -3399,23 +3399,8 @@
 the 
-player for the 
 sess
@@ -3426,30 +3426,31 @@
 @return The 
-player
+session
 's username%0A
@@ -3547,22 +3547,23 @@
 ets the 
-player
+session
 's usern
@@ -3561,24 +3561,25 @@
 n's username
+.
 %0A     *%0A    
@@ -3605,14 +3605,15 @@
 The 
-player
+session
 's u

Update for flow-networking changes