Package handlers.peer.transfer

Source Code of handlers.peer.transfer.RequestFileHandler$AbstractFileRequestHandler

package handlers.peer.transfer;


import handlers.AbstractContextualHandler;
import handlers.peer.AbstractGenericPeerHandler;

import java.io.IOException;
import java.io.ObjectInputStream;

import common.files.SerializableFile;

import main.PeerProtocol;

import transfer.upload.FileUploadConnection;

import connection.peer.GenericPeerConnection;

import static main.ClientMain.*;


public class RequestFileHandler extends AbstractGenericPeerHandler
{
  private SerializableFile file;
  private long offset;

  private AbstractFileRequestHandler handler;

  public RequestFileHandler(final GenericPeerConnection connection, ObjectInputStream is)
      throws IOException, ClassNotFoundException
  {
    super(connection);

    file = (SerializableFile) is.readObject();
    offset = is.readLong();

    if (getSharedFilesTable().hasFile(file))
    {
      handleValidRequest();
    }
    else
    {
      handleInvalidRequest();
    }
  }

  private abstract class AbstractFileRequestHandler
  {
    public abstract void execute() throws IOException;

  }

  private void handleValidRequest() throws IOException
  {
    final FileUploadConnection specializedConnection = connection
        .convertToFileUpload(file);
    handler = new AbstractFileRequestHandler()
    {
      public void execute() throws IOException
      {
        specializedConnection.upload(offset);
      }

    };
  }

  private void handleInvalidRequest()
  {
    handler = new AbstractFileRequestHandler()
    {
      public void execute()
      {
        int protocolId = PeerProtocol.PROTOCOL_TRANSFER;
        int opcode = PeerProtocol.Transfer.OP_INVALID_FILE;
        connection.send(protocolId, opcode);
      }

    };
  }

  public RequestFileHandler()
  {
  }

  public AbstractContextualHandler createContext(GenericPeerConnection connection,
      ObjectInputStream is) throws IOException, ClassNotFoundException
  {
    return new RequestFileHandler(connection, is);
  }

  public void execute() throws IOException
  {
    handler.execute();
  }

}
TOP

Related Classes of handlers.peer.transfer.RequestFileHandler$AbstractFileRequestHandler

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.