@Override
    public void incomingPipe(Pipe aPipe) {
      if (aPipe.getPipeDescription().startsWith("FILE:")) {
        String[] theAttributes = aPipe.getPipeDescription().split(":");

        UUID theFileUUID = UUID.fromString(theAttributes[1]);

        long theLength = Long.parseLong(theAttributes[2]);

        if (!myMapping.containsKey(theFileUUID)) {
          LOGGER.error(
              "The file for file id'" + theAttributes[1] + "' is not stored in file mapping");
          try {
            aPipe.getSocket().close();
          } catch (Exception e) {
          }
        } else {

          FileStatus theFileStatus = myMapping.get(theFileUUID);
          File theFile = theFileStatus.getFile();
          FileOutputStream theOut = null;
          try {
            theOut = new FileOutputStream(theFileStatus.getFile());
            IOTools.copyStream(
                aPipe.getSocket().getInputStream(),
                theOut,
                new FileTransferHandler(theFile, theLength));
            // the file is received, close the socket
            theOut.flush();
            theOut.close();

            myFileHandler.fileSaved(theFile);
            aPipe.getSocket().close();
            theFileStatus.setStatus(FileStatus.Status.OK);
          } catch (Exception e) {
            theFileStatus.setStatus(FileStatus.Status.NOK);
            myMapping.remove(theFileUUID);
            if (theFileStatus.getFile() != null) {
              myFileHandler.fileTransferInterrupted(theFile);
            }

            if (theOut != null) {
              try {
                theOut.close();
              } catch (Exception e2) {
              }
            }
            LOGGER.error("An error occured while receiving file", e);
            if (theFile != null && theFile.exists()) {
              theFile.delete();
            }
          } finally {

            if (theOut != null) {
              try {
                theOut.close();
              } catch (Exception e3) {
              }
            }
            try {
              aPipe.getSocket().close();
            } catch (Exception e) {
            }
          }
        }
      }
    }
  public void sendFile(File aFile, String aPeerid) throws FileTransferException {
    String theResponse = null;

    ProtocolContainer theProtocolContainer = findProtocolContainer();
    AbstractPeer thePeer = null;
    try {
      thePeer = getRoutingTable().getEntryForPeer(aPeerid).getPeer();
      MessageProtocol theMessageProtocol =
          (MessageProtocol) theProtocolContainer.getProtocol("MSG");
      Message theMessage = new Message();
      theMessage.setDestination(thePeer);
      theMessage.setMessage(createMessage(Command.FILE.name() + " " + aFile.getName()));
      theMessage.setProtocolMessage(true);
      theMessage.setMessageTimeoutInSeconds(-1);
      theResponse = theMessageProtocol.sendMessage(theMessage);
    } catch (ProtocolException e) {
      throw new FileTransferException(
          "Could not transfer file because message protocol is not known", e);
    } catch (MessageException e) {
      throw new FileTransferException(
          "Could not transfer file because message could not be delivered", e);
    } catch (UnknownPeerException e) {
      throw new FileTransferException(
          "Could not transfer file because given peer id is not known", e);
    }

    if (!theResponse.startsWith(Response.ACCEPTED.name())) {
      throw new FileTransferException(
          "The file: '"
              + aFile.getName()
              + "' was refused by peer: '"
              + thePeer.getPeerId()
              + "' with response '"
              + theResponse
              + "'");
    } else {
      String theFileId = theResponse.split(" ")[1];
      // TODO we probably should not just cast to SocketPeer
      Pipe thePipe = new Pipe((SocketPeer) thePeer);
      thePipe.setPipeDescription("FILE:" + theFileId + ":" + aFile.length());
      FileInputStream theInputStream = null;
      try {
        PipeProtocol thePipeProtocol = getPipeProtocol();
        thePipeProtocol.openPipe(thePipe);
        theInputStream = new FileInputStream(aFile);
        IOTools.copyStream(theInputStream, thePipe.getSocket().getOutputStream());
        thePipeProtocol.closePipe(thePipe);

        theResponse =
            getPeerSender()
                .send(
                    thePeer,
                    createMessage(
                        Command.WAIT_FOR_FILE.name() + " " + theFileId + " " + aFile.length()))
                .getReply();

        if (theResponse.equalsIgnoreCase(Response.BAD_FILE_SIZE.name())) {
          throw new FileTransferException("Received file had bad file size");
        }

        if (theResponse.equalsIgnoreCase(Response.FILE_NOK.name())) {
          throw new FileTransferException("Sending file failed");
        }
      } catch (Exception e) {
        throw new FileTransferException(
            "Transferring file to peer: '" + thePeer.getPeerId() + "' could not be initiated", e);
      } finally {
        if (theInputStream != null) {
          try {
            theInputStream.close();
          } catch (IOException e) {
          }
        }
      }
    }
  }