Package net.cis.client.main.ctrl

Source Code of net.cis.client.main.ctrl.HandshakeController

package net.cis.client.main.ctrl;

import net.cis.client.login.AbstractLoginController.ILoginFailedCallback;
import net.cis.common.model.system.message.AuthorizeMessage;
import net.cis.common.model.system.message.GameInformationMessage;
import net.cis.common.model.system.message.GameVersionMessage;

import org.apache.commons.logging.LogFactory;

import com.jme3.network.Client;
import com.jme3.network.ClientStateListener;
import com.jme3.network.Message;
import com.jme3.network.MessageListener;

public class HandshakeController implements ClientStateListener, MessageListener<Client> {
  private final byte GAME_VERSION = 0x00;

  private final Client client;

  private final ILoginFailedCallback callback;

  public HandshakeController(Client client, ILoginFailedCallback callback) {
    this.client = client;
    this.callback = callback;
  }

  public void initHandshake() {
    client.send(new GameVersionMessage(GAME_VERSION));
  }

  public void authenticate(String username, String password) {
    callback.statusMessage("Waiting for connect to Server...");
    for (int i = 0; i < 6; i++) {
      if (client.isConnected())
        break;
      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
        // intterupt is ok, ignore it
      }
    }

    if (!client.isConnected()) {
      callback.loginFailed("Could not connect to Server!");
      return;
    }

    callback.statusMessage("Authenticating...");
    client.send(new AuthorizeMessage(username, password));
  }

  @Override
  public void messageReceived(Client source, Message m) {
    if (m instanceof GameVersionMessage) { // something is wrong with client version
      GameVersionMessage message = (GameVersionMessage) m;
      if (message.getGameVersion() != GAME_VERSION) {
        // TODO Handle wrong GameVersion, update
        String msg = "Wrong GameVersion! You must Update your client!";
        callback.statusMessage(msg);
        LogFactory.getLog(HandshakeController.class).info(msg);
      } else {
        LogFactory.getLog(HandshakeController.class).error("This shouldn't happen!");
        System.exit(-1);
      }
    } else if (m instanceof GameInformationMessage) { // client version ok, some data about game
      GameInformationMessage message = (GameInformationMessage) m;
      // TODO: Handle GameInformatiomMessage
      String msg = "Got game information!";
      callback.statusMessage(msg);
      LogFactory.getLog(HandshakeController.class).info(msg);
    }
  }

  @Override
  public void clientConnected(Client c) {
    callback.statusMessage("Connected to Server! Checking game version...");
    initHandshake();
  }

  @Override
  public void clientDisconnected(Client c, DisconnectInfo info) {
    System.out.println("Disconnected!");
  }
}
TOP

Related Classes of net.cis.client.main.ctrl.HandshakeController

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.