Package common.connection

Source Code of common.connection.AbstractConnector

package common.connection;


import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import javax.net.ssl.SSLSocket;

import common.util.NonNullObjectInputStream;


public abstract class AbstractConnector extends AbstractCommunicationSet
{
  protected AbstractConnector()
  {
    super();
  }

  protected AbstractConnector(AbstractCommunicationSet source)
  {
    super(source);
  }

  protected AbstractConnector(ServerSocket listener) throws IOException
  {
    super();
    startConnection(listener.accept());
  }

  private void setSocket(Socket socket) throws IOException
  {
    this.socket = socket;
    initializeStreams();
  }

  protected final AbstractConnection startConnection(Socket socket) throws IOException
  {
    setSocket(socket);

    AbstractConnection connection = createConnectionObject();
    new ConnectionRunner(connection);

    return connection;
  }

  protected abstract void initializeStreams() throws IOException;

  protected final void initializeClientSideStreams() throws IOException
  {
    is = new NonNullObjectInputStream(socket.getInputStream());
    os = new ObjectOutputStream(socket.getOutputStream());
    os.flush();
  }

  protected final void initializeServerSideStreams() throws IOException
  {
    os = new ObjectOutputStream(socket.getOutputStream());
    os.flush();

    is = new NonNullObjectInputStream(socket.getInputStream());
  }

  protected abstract AbstractConnection createConnectionObject() throws IOException;

  public final void secure() throws IOException
  {
    SSLSocket newSocket = encryptConnection(socket);
    setAppropriatedSocketMode(newSocket);
    setSocket(newSocket);
  }

  protected abstract SSLSocket encryptConnection(Socket socket) throws IOException;

  protected abstract void setAppropriatedSocketMode(SSLSocket socket);

}
TOP

Related Classes of common.connection.AbstractConnector

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.