Package ch.marcsladek.jrtnp.client

Source Code of ch.marcsladek.jrtnp.client.Client

package ch.marcsladek.jrtnp.client;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import ch.marcsladek.jrtnp.connection.Connection;
import ch.marcsladek.jrtnp.connection.ConnectionFactory;

public class Client {

  protected static Connection server;

  /**
   *
   * @param host
   *          ip the client connects to
   * @param port
   *          port the client connects to
   * @param connectionFactoryClassName
   *          class name of the factory used to create objects of own Connection
   *          implementation
   * @throws InstantiationException
   *           when unable to create instance of given class name
   * @throws IllegalAccessException
   *           when unable to access given class name
   * @throws ClassNotFoundException
   *           when unable to find given class name
   * @throws UnknownHostException
   *           when unable to create Socket for host
   * @throws IOException
   *           when unable to get streams for socket
   */
  @SuppressWarnings("unchecked")
  public Client(String host, int port, String connectionFactoryClassName)
      throws InstantiationException, IllegalAccessException, ClassNotFoundException,
      UnknownHostException, IOException {
    ConnectionFactory factory = ((Class<? extends ConnectionFactory>) Class
        .forName(connectionFactoryClassName)).newInstance();
    server = factory.newInstance(new Socket(host, port));
  }

  /**
   * Starts the listener for the server
   */
  public final void start() {
    server.start();
  }

  /**
   * Stops the listener for the server
   *
   * @throws IOException
   */
  public final void shutdown() throws IOException {
    server.shutdown();
  }

  @Override
  public String toString() {
    return server.toString();
  }

}
TOP

Related Classes of ch.marcsladek.jrtnp.client.Client

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.