@Override public String handleCommand(String aSessionId, String anInput) { if (anInput.startsWith(Command.FILE.name())) { // just get the pipe protocol to make sure it is there and to add the listener to it try { getPipeProtocol(); String theFileName = anInput.substring(Command.FILE.name().length() + 1); if (myFileHandler == null) { return Response.REFUSED.name(); } File theFile = myFileHandler.acceptFile(theFileName); if (theFile == null) { return Response.REFUSED.name(); } FileStatus theStatus = new FileStatus(theFileName, theFile); UUID theUID = UUID.randomUUID(); myMapping.put(theUID, theStatus); return Response.ACCEPTED.name() + " " + theUID.toString(); } catch (ProtocolException e) { return ProtocolContainer.Response.UNKNOWN_PROTOCOL.name(); } } else if (anInput.startsWith(Command.WAIT_FOR_FILE.name())) { String[] theFileAttributes = anInput.split(" "); String theFileId = theFileAttributes[1]; long theFileSize = Long.parseLong(theFileAttributes[2]); UUID theFileUUID = UUID.fromString(theFileId); if (!myMapping.containsKey(theFileUUID)) { return Response.FILE_NOK.name(); } else { try { FileStatus theFileStatus = myMapping.get(theFileUUID); FileStatus.Status theStatus = theFileStatus.waitForStatus(); if (theStatus == FileStatus.Status.OK) { // check the file size long theRealLength = theFileStatus.getFile().length(); if (theRealLength == theFileSize) { return Response.FILE_OK.name(); } else { return Response.BAD_FILE_SIZE.name(); } } else { return Response.FILE_NOK.name(); } } catch (InterruptedException e) { return Response.FILE_NOK.name(); } } } return Response.UNKNOWN_COMMAND.name(); }
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) { } } } } }