Package game.impl

Source Code of game.impl.NetworkGameControllerClient$Handler

package game.impl;

import game.ClientRemovedEvent;
import game.FireEvent;
import game.GameAdminEvent;
import game.GameCommands;
import game.GameEvent;
import game.GameListener;
import game.GameObjectLocation;
import game.NewClientEvent;
import game.ObjectMoveEvent;
import game.ObjectsUpdateEvent;
import game.RemoveClientEvent;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.handler.codec.serialization.ObjectDecoder;
import org.jboss.netty.handler.codec.serialization.ObjectEncoder;

public class NetworkGameControllerClient implements GameCommands {

  private Executor executor;
  private GameListener gameListener;
  private Channel channel;

  public void setGameListener(GameListener gameListener) {
    this.gameListener = gameListener;
  }

  public NetworkGameControllerClient(GameListener listener) throws IOException {
    gameListener = listener;
    executor = Executors.newCachedThreadPool();
    ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(executor, executor));
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
      @Override
      public ChannelPipeline getPipeline() throws Exception {
        return Channels.pipeline(new ObjectEncoder(), new ObjectDecoder(), new Handler());
      }
    });
    bootstrap.setOption("tcpNoDelay", true);
    bootstrap.setOption("keepAlive", true);
    bootstrap.connect(new InetSocketAddress("localhost", NetworkGameControllerServer.GAME_PORT));
  }

  class Handler extends SimpleChannelHandler {

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
      Object message = e.getMessage();
      if (message instanceof GameEvent) {
        dispatch((GameEvent) message);
      }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
      e.getCause().printStackTrace();
      super.exceptionCaught(ctx, e);
    }

    @Override
    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
      channel = e.getChannel();
      super.channelConnected(ctx, e);
    }

  }

  private void dispatch(GameEvent o) {
    if (o instanceof ObjectsUpdateEvent) {
      gameListener.onObjectsUpdate((ObjectsUpdateEvent) o);
    } else if (o instanceof ClientRemovedEvent) {
      gameListener.onClientRemoved((ClientRemovedEvent) o);
    }
  }

  @Override
  public void onNewClient(NewClientEvent e) {
    channel.write(e);
  }

  @Override
  public void onRemoveClient(RemoveClientEvent e) {
    channel.write(e);
  }

  @Override
  public void onGameAdmin(GameAdminEvent e) {
    channel.write(e);
  }

  @Override
  public void onUserControl(ObjectMoveEvent e) {
    channel.write(e);
  }

  @Override
  public void onFire(FireEvent e) {
    channel.write(e);
  }

  @Override
  public Collection<GameObjectLocation> getAllObjects() {
    return Collections.EMPTY_LIST;
  }

  public void shutdown() {
    channel.disconnect();
  }
}
TOP

Related Classes of game.impl.NetworkGameControllerClient$Handler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.