Search in sources :

Example 1 with FileTransferFinishedEvent

use of me.matoosh.undernet.event.ftp.FileTransferFinishedEvent in project UnderNet by itsMatoosh.

the class FileResource method onPushReceive.

/**
 * Called after the resource push has been received.
 *
 * @param msg
 * @param receivedFrom
 */
@Override
public void onPushReceive(ResourcePushMessage msg, Node receivedFrom) {
    // Requesting the file trasnfer.
    transfer = UnderNet.router.fileTransferManager.requestFileTransfer(receivedFrom, (FileResource) msg.resource);
    EventManager.registerHandler(new EventHandler() {

        @Override
        public void onEventCalled(Event e) {
            FileTransferFinishedEvent transferFinishedEvent = (FileTransferFinishedEvent) e;
            if (transferFinishedEvent.transfer == FileResource.this.transfer) {
                // Transfer of the resource has finished. The resource is ready to push.
                onPushReady();
            }
        }
    }, FileTransferFinishedEvent.class);
}
Also used : FileTransferFinishedEvent(me.matoosh.undernet.event.ftp.FileTransferFinishedEvent) EventHandler(me.matoosh.undernet.event.EventHandler) FileTransferFinishedEvent(me.matoosh.undernet.event.ftp.FileTransferFinishedEvent) Event(me.matoosh.undernet.event.Event)

Example 2 with FileTransferFinishedEvent

use of me.matoosh.undernet.event.ftp.FileTransferFinishedEvent in project UnderNet by itsMatoosh.

the class FileTransfer method startSending.

/**
 * Starts the file transfer.
 */
public void startSending() {
    if (fileTransferType.equals(FileTransferType.OUTBOUND)) {
        // File sending logic.
        UnderNet.router.fileTransferManager.executor.submit(new Callable() {

            @Override
            public Object call() throws Exception {
                // Amount of bytes read from the send stream.
                int totalRead = 0;
                try {
                    // The send buffer.
                    byte[] buffer = new byte[BUFFER_SIZE];
                    int read = 0;
                    while ((read = inputStream.read(buffer)) > 0) {
                        totalRead += read;
                        byte[] data = new byte[read];
                        System.arraycopy(buffer, 0, data, 0, read);
                        sendChunk(data);
                        logger.debug("Chunk sent " + totalRead + "/" + fileInfo.fileLength);
                    }
                } catch (IOException e) {
                    FileTransferManager.logger.error("Error reading " + BUFFER_SIZE + " chunk from file: " + file, e);
                    EventManager.callEvent(new FileTransferErrorEvent(FileTransfer.this, e));
                } finally {
                    // File sent or error.
                    EventManager.callEvent(new FileTransferFinishedEvent(FileTransfer.this));
                }
                return null;
            }
        });
    }
}
Also used : FileTransferErrorEvent(me.matoosh.undernet.event.ftp.FileTransferErrorEvent) FileTransferFinishedEvent(me.matoosh.undernet.event.ftp.FileTransferFinishedEvent) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with FileTransferFinishedEvent

use of me.matoosh.undernet.event.ftp.FileTransferFinishedEvent in project UnderNet by itsMatoosh.

the class FileTransfer method onChunkReceived.

/**
 * Called when a file chunk has been received.
 * @param chunk
 */
public void onChunkReceived(FileChunk chunk) {
    if (fileTransferType == FileTransferType.INBOUND) {
        // Adding the data to the data byte[] of the transfer.
        try {
            outputStream.write(chunk.data);
            written += chunk.data.length;
            logger.debug("File chunk received for: " + id + " " + written + "/" + fileInfo.fileLength);
            if (written >= fileInfo.fileLength) {
                // File fully received.
                EventManager.callEvent(new FileTransferFinishedEvent(this));
            }
        } catch (IOException e) {
            FileTransferManager.logger.error("Error appending the received file chunk to file!", e);
        }
    }
}
Also used : FileTransferFinishedEvent(me.matoosh.undernet.event.ftp.FileTransferFinishedEvent) IOException(java.io.IOException)

Example 4 with FileTransferFinishedEvent

use of me.matoosh.undernet.event.ftp.FileTransferFinishedEvent in project UnderNet by itsMatoosh.

the class FileTransferManager method onEventCalled.

/**
 * Called when the handled event is called.
 *
 * @param e
 */
@Override
public void onEventCalled(Event e) {
    if (e instanceof ChannelMessageReceivedEvent) {
        ChannelMessageReceivedEvent messageReceivedEvent = (ChannelMessageReceivedEvent) e;
        if (messageReceivedEvent.message.msgId == MsgType.FILE_REQ.ordinal()) {
            // File request received.
            // Deserializing msg.
            FileTransferRequestMessage requestMsg = (FileTransferRequestMessage) NetworkMessage.deserializeMessage(messageReceivedEvent.message.data.array());
            logger.info("Received a file transfer request for: " + requestMsg.transferId);
            // A file was requested from this node. Checking if the requested transfer has been prepared.
            for (final FileTransfer transfer : outboundTransfers) {
                if (NetworkID.compare(transfer.id.data, requestMsg.transferId.data) == 0) {
                    // Checking if the recipient is the same.
                    if (transfer.recipient == messageReceivedEvent.remoteNode) {
                        executor.submit(new Runnable() {

                            @Override
                            public void run() {
                                transfer.startSending();
                            }
                        });
                    }
                }
            }
        } else if (messageReceivedEvent.message.msgId == MsgType.FILE_CHUNK.ordinal()) {
            // File chunk received.
            // Deserializing msg.
            FileChunk fileChunk = (FileChunk) NetworkMessage.deserializeMessage(messageReceivedEvent.message.data.array());
            // A file was requested from this node. Checking if the requested transfer has been prepared.
            for (final FileTransfer transfer : inboundTransfers) {
                if (NetworkID.compare(transfer.id.data, fileChunk.transferId.data) == 0) {
                    // Locating the right file transfer.
                    // Running chunk received callback.
                    transfer.onChunkReceived(fileChunk);
                    return;
                }
            }
        }
    } else if (e instanceof FileTransferFinishedEvent) {
        // Removing the transfer from the queue.
        FileTransferFinishedEvent fileTransferFinishedEvent = (FileTransferFinishedEvent) e;
        if (fileTransferFinishedEvent.transfer.fileTransferType == FileTransferType.INBOUND) {
            inboundTransfers.remove(fileTransferFinishedEvent.transfer);
        } else {
            outboundTransfers.remove(fileTransferFinishedEvent.transfer);
        }
        logger.info("File transfer " + fileTransferFinishedEvent.transfer + " finished.");
    }
}
Also used : ChannelMessageReceivedEvent(me.matoosh.undernet.event.channel.message.ChannelMessageReceivedEvent) FileTransferFinishedEvent(me.matoosh.undernet.event.ftp.FileTransferFinishedEvent)

Aggregations

FileTransferFinishedEvent (me.matoosh.undernet.event.ftp.FileTransferFinishedEvent)4 IOException (java.io.IOException)2 FileNotFoundException (java.io.FileNotFoundException)1 Callable (java.util.concurrent.Callable)1 Event (me.matoosh.undernet.event.Event)1 EventHandler (me.matoosh.undernet.event.EventHandler)1 ChannelMessageReceivedEvent (me.matoosh.undernet.event.channel.message.ChannelMessageReceivedEvent)1 FileTransferErrorEvent (me.matoosh.undernet.event.ftp.FileTransferErrorEvent)1