ca.lambtoncollege.hauntedhouse.client.ClientHandler.java Source code

Java tutorial

Introduction

Here is the source code for ca.lambtoncollege.hauntedhouse.client.ClientHandler.java

Source

/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package ca.lambtoncollege.hauntedhouse.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * Handles a client-side channel.
 */
public class ClientHandler extends SimpleChannelInboundHandler<String> {
    private Canvas ca;

    public void messageReceived(ChannelHandlerContext ctx, String msg) {
        //System.err.println(msg);

        String[] array = PropertyMgr.split(msg);
        if (array[0].equals(PropertyMgr.CONNECTION_ESTABLISHED + "")) {
            if (ca == null)
                ca = new Canvas(ctx);
            ca.lanchFrame();
        } else if (array[0].equals(PropertyMgr.WAITING_FOR_ANOTHER_PLAYER + "")) {
            ca.waiting(PropertyMgr.STR_WAITING_FOR_ANOTHER_PLAYER);
        } else if (array[0].equals(PropertyMgr.JOIN_A_GAME + "")) {
            ca.setTeams(Integer.parseInt(array[1]));
            System.err.println(msg);
            ca.start();
        } else if (array[0].equals(PropertyMgr.DURING_THE_GAME + "")) {
            int id = Integer.parseInt(array[1]);
            if (id == PropertyMgr.FRAME_ID)
                ca.unregisterRemotely();
            else if (array[2].equals(PropertyMgr.CLICK + ""))
                ca.getBc().selectRemotely(id);
            else {
                ca.getBc().updateBase(array[1], Integer.parseInt(array[2]), Integer.parseInt(array[3]));
            }
        } else if (array[0].equals(PropertyMgr.ANOTHER_PLAYER_LOST_THE_CONNECTION + "")) {
            ca.stop(PropertyMgr.STR_ANOTHER_PLAYER_LOST_THE_CONNECTION);
        }
        //String s = ctx.pipeline().channel().a;
        //System.out.println("messageReceived==");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        //System.out.println("Client channelRead0===========");
        messageReceived(ctx, msg);
    }

}