Package connection.server

Source Code of connection.server.ServerConnection

package connection.server;


import handlers.AbstractContextualHandler;
import handlers.sets.ClientServerHandlerSet;

import java.io.IOException;
import java.io.ObjectOutputStream;

import main.settings.ServerList;
import main.settings.Settings;

import common.ClientServerProtocol;
import common.connection.ConnectionWriterInterface;
import common.connection.client.AbstractClientSideConnector;
import common.handlers.AbstractHandlerSet;
import connection.AbstractContextualConnection;


public class ServerConnection extends AbstractContextualConnection
{
  private static final ClientServerHandlerSet HANDLER_SET = new ClientServerHandlerSet();

  private ConnectionManagerInterface manager;

  public ServerConnection(AbstractClientSideConnector source,
      ConnectionManagerInterface manager) throws IOException
  {
    super(source);
    this.manager = manager;
  }

  public void initializeConnection() throws IOException
  {
    manager.notifyConnected(this);
    sendInitialMessages();
  }

  public void finalizeConnection()
  {
    manager.notifyDisconnection();

    ServerList.writeToDataFile();
  }

  private void sendInitialMessages() throws IOException
  {
    setPort();
    setNickName();
  }

  protected AbstractHandlerSet getHandlerSet()
  {
    return HANDLER_SET;
  }

  public AbstractContextualHandler getContextualHandler(int protocolId, int opcode)
      throws IOException, ClassNotFoundException
  {
    return ClientServerHandlerSet.HANDLERS[protocolId][opcode]
        .createContext(this, is);
  }

  public void setPort() throws IOException
  {
    int protocolId = ClientServerProtocol.PROTOCOL_LINK;
    int opcode = ClientServerProtocol.Link.OP_SET_PORT;
    sendOverlapped(protocolId, opcode, new ConnectionWriterInterface()
    {
      public void run(ObjectOutputStream os) throws IOException
      {
        os.writeInt(Settings.connection.port);
      }

    });
  }

  public void setNickName() throws IOException
  {
    int protocolId = ClientServerProtocol.PROTOCOL_NAMING;
    int opcode = ClientServerProtocol.Naming.OP_SET_NAME;
    sendOverlapped(protocolId, opcode, new ConnectionWriterInterface()
    {
      public void run(ObjectOutputStream os) throws IOException
      {
        os.writeUTF(Settings.application.nickname);
      }

    });
  }

}
TOP

Related Classes of connection.server.ServerConnection

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.