Package ds.shared.rmi

Source Code of ds.shared.rmi.FileServerImpl

package ds.shared.rmi;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Properties;

import org.apache.log4j.Logger;

import ds.shared.socket.ClientSocketHandler;
import ds.shared.socket.ServerSocketHandler;

/**
* The remote object that the Node exports.
*
* @author save
*/
@SuppressWarnings("serial")
public abstract class FileServerImpl extends UnicastRemoteObject implements FileServerInterface {
 
  // Define a static logger variable
  private final static Logger LOGGER = Logger.getLogger(FileServerImpl.class);
 
  // ServerSocketHandler used for new socket connections
  ServerSocketHandler handler = null;
 
  // Properties file
  protected Properties conf = null;

  /**
   * Construct a remote object
   *
   * @param handler
   *         Serversocket used to assign clients a new connection
   * @param conf
   *         Configuration options
   */
  public FileServerImpl(ServerSocketHandler handler, Properties conf) throws RemoteException {
    this.handler = handler;
    this.conf = conf;
  }

 
  /**
   * Starts the serversocket and returns the port.
   * This allows the client to connect and the server to know a connection is going to happen.
   *
   * @throws RemoteException
   *                if the object handle cannot be constructed.
   */
  public Integer getServerSocketPort() throws RemoteException {
    // Start waiting for a connection
    LOGGER.info("Start listening on ServerSocketPort");
    try {
      // Start server socket handler thread
      Thread t = new Thread(this.handler);
      t.start();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    // Return the port to connect to
    LOGGER.info("Returning ServerSocketPort");

    return this.handler.getPort();
  }

  /**
   * Informs the server a client wishes to send a file.
   * Actions are performed in the ClientSocketHandler.
   *
   * @param file
   *       File name
   * @param size
   *       File size
   * @return boolean
   *       True if we can receive this file.
   * @throws RemoteException
   *                if the object handle cannot be constructed.
   */
  public boolean putFile(String file, long size) throws RemoteException {
    // Inform the clienthandler of the pending file
   
    ClientSocketHandler clientHandler = this.handler.getClient();
   
    clientHandler.setAction("receive");
    clientHandler.setFile(file, size);

    // Start the client handler thread
    Thread t = new Thread(clientHandler);
    t.start();
   
    return true;
  }
 
  /**
   * Informs the server a client wishes to download a file.
   * Actions are performed in the ClientSocketHandler
   *
   * @param file
   *       File name
   * @return long
   *       File size if the transfer is possible, else -1.
   * @throws RemoteException
   *                if the object handle cannot be constructed.
   */
  public boolean getFile(String file) throws RemoteException {
    if (!this.exists(file)) {
      LOGGER.debug("File not found " + file);
      return false;
    }
   
    // Inform the clienthandler that it will need to start sending
    ClientSocketHandler clientHandler = this.handler.getClient();
   
    clientHandler.setAction("send");
    clientHandler.setFile(file);

    // Start the client handler thread
    Thread t = new Thread(clientHandler);
    t.start();
   
    return true;
  }
 
 

}
TOP

Related Classes of ds.shared.rmi.FileServerImpl

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.