Search in sources :

Example 1 with IFileTransferEvent

use of org.eclipse.ecf.filetransfer.events.IFileTransferEvent in project ecf by eclipse.

the class URLPartialRetrieveTest method testReceiveHttp.

protected void testReceiveHttp(final long start, final long end, String url) throws Exception {
    assertNotNull(transferInstance);
    final IFileTransferListener listener = new IFileTransferListener() {

        public void handleTransferEvent(IFileTransferEvent event) {
            if (event instanceof IIncomingFileTransferReceiveResumedEvent) {
                try {
                    final IIncomingFileTransferReceiveResumedEvent rse = (IIncomingFileTransferReceiveResumedEvent) event;
                    session = rse.receive(outs);
                } catch (final Exception e) {
                    fail(e.getLocalizedMessage());
                }
            } else if (event instanceof IIncomingFileTransferReceiveStartEvent) {
                final IIncomingFileTransferReceiveStartEvent rse = (IIncomingFileTransferReceiveStartEvent) event;
                try {
                    outs = new FileOutputStream(FILENAME);
                    session = rse.receive(outs);
                    pausable = (IFileTransferPausable) session.getAdapter(IFileTransferPausable.class);
                    if (pausable == null)
                        fail("pausable is null");
                } catch (final IOException e) {
                    fail(e.getLocalizedMessage());
                }
            } else if (event instanceof IIncomingFileTransferReceiveDataEvent) {
                System.out.println("data=" + event);
            } else if (event instanceof IIncomingFileTransferReceivePausedEvent) {
                System.out.println("paused=" + event);
            } else if (event instanceof IIncomingFileTransferReceiveDoneEvent) {
                closeOutputStream();
                System.out.println("done=" + event);
                synchronized (notify) {
                    isDone = true;
                    notify.notify();
                }
                session = ((IIncomingFileTransferReceiveDoneEvent) event).getSource();
            }
        }
    };
    final IFileID fileID = FileIDFactory.getDefault().createFileID(transferInstance.getRetrieveNamespace(), url);
    IFileRangeSpecification rangeSpecification = null;
    if (start != -1) {
        rangeSpecification = new IFileRangeSpecification() {

            public long getEndPosition() {
                return end;
            }

            public long getStartPosition() {
                return start;
            }
        };
    }
    transferInstance.sendRetrieveRequest(fileID, rangeSpecification, listener, null);
    if (!isDone) {
        synchronized (notify) {
            notify.wait();
        }
    }
    final Exception e = session.getException();
    if (e != null)
        throw e;
    incomingFile = new File(FILENAME);
    final long fileLength = incomingFile.length();
    final long bytesReceived = session.getBytesReceived();
    System.out.println("start=" + start);
    System.out.println("end=" + end);
    System.out.println("bytes received=" + bytesReceived);
    System.out.println("fileLength=" + fileLength);
    if (start != -1) {
        assertTrue(fileLength == bytesReceived);
        if (end != -1) {
            assertTrue(fileLength == (end - start + 1));
        }
    }
}
Also used : IIncomingFileTransferReceiveResumedEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveResumedEvent) IFileRangeSpecification(org.eclipse.ecf.filetransfer.IFileRangeSpecification) IFileTransferPausable(org.eclipse.ecf.filetransfer.IFileTransferPausable) IIncomingFileTransferReceiveStartEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent) IFileTransferListener(org.eclipse.ecf.filetransfer.IFileTransferListener) IOException(java.io.IOException) IOException(java.io.IOException) IncomingFileTransferException(org.eclipse.ecf.filetransfer.IncomingFileTransferException) IFileTransferEvent(org.eclipse.ecf.filetransfer.events.IFileTransferEvent) IFileID(org.eclipse.ecf.filetransfer.identity.IFileID) FileOutputStream(java.io.FileOutputStream) IIncomingFileTransferReceiveDoneEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent) IIncomingFileTransferReceivePausedEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceivePausedEvent) IIncomingFileTransferReceiveDataEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDataEvent) File(java.io.File)

Example 2 with IFileTransferEvent

use of org.eclipse.ecf.filetransfer.events.IFileTransferEvent in project ecf by eclipse.

the class XMPPCompoundContributionItem method sendFileToTarget.

private void sendFileToTarget(ISendFileTransferContainerAdapter fileTransfer, final ID targetID) {
    final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
    final FileDialog fd = new FileDialog(shell, SWT.OPEN);
    // XXX this should be some default path set by preferences
    // $NON-NLS-1$
    fd.setFilterPath(System.getProperty("user.home"));
    fd.setText(NLS.bind(Messages.XMPPCompoundContributionItem_CHOOSE_FILE, targetID.getName()));
    final String res = fd.open();
    if (res != null) {
        final File aFile = new File(res);
        try {
            final IFileID targetFileID = FileIDFactory.getDefault().createFileID(fileTransfer.getOutgoingNamespace(), new Object[] { targetID, res });
            fileTransfer.sendOutgoingRequest(targetFileID, aFile, new IFileTransferListener() {

                public void handleTransferEvent(final IFileTransferEvent event) {
                    Display.getDefault().asyncExec(new Runnable() {

                        public void run() {
                            // bar?)
                            if (event instanceof IOutgoingFileTransferResponseEvent) {
                                if (!((IOutgoingFileTransferResponseEvent) event).requestAccepted())
                                    MessageDialog.openInformation(shell, Messages.XMPPCompoundContributionItem_FILE_SEND_REFUSED_TITLE, NLS.bind(Messages.XMPPCompoundContributionItem_FILE_SEND_REFUSED_MESSAGE, res, targetID.getName()));
                            }
                        }
                    });
                }
            }, null);
        } catch (final Exception e) {
            MessageDialog.openError(shell, Messages.XMPPCompoundContributionItem_SEND_ERROR_TITLE, NLS.bind(Messages.XMPPCompoundContributionItem_SEND_ERROR_MESSAGE, res, e.getLocalizedMessage()));
        }
    }
}
Also used : Shell(org.eclipse.swt.widgets.Shell) IFileTransferEvent(org.eclipse.ecf.filetransfer.events.IFileTransferEvent) IFileID(org.eclipse.ecf.filetransfer.identity.IFileID) IOutgoingFileTransferResponseEvent(org.eclipse.ecf.filetransfer.events.IOutgoingFileTransferResponseEvent) IFileTransferListener(org.eclipse.ecf.filetransfer.IFileTransferListener) FileDialog(org.eclipse.swt.widgets.FileDialog) File(java.io.File)

Example 3 with IFileTransferEvent

use of org.eclipse.ecf.filetransfer.events.IFileTransferEvent in project ecf by eclipse.

the class AbstractFileSendAction method createDefaultFileTransferListener.

/**
 * @return IFileTransferListener to use as the default listener. Must not be
 *         <code>null</code>.
 */
protected IFileTransferListener createDefaultFileTransferListener() {
    return new IFileTransferListener() {

        public void handleTransferEvent(final IFileTransferEvent event) {
            // then a custom IFileTransferListener should be provided.
            if (event instanceof IOutgoingFileTransferSendDoneEvent) {
                final IOutgoingFileTransferSendDoneEvent oftsde = (IOutgoingFileTransferSendDoneEvent) event;
                final Exception errorException = oftsde.getSource().getException();
                Display.getDefault().asyncExec(new Runnable() {

                    public void run() {
                        if (errorException == null) {
                            // $NON-NLS-1$
                            MessageDialog.openInformation(// $NON-NLS-1$
                            null, // $NON-NLS-1$
                            Messages.getString("AbstractFileSendAction.TITLE_FILE_TRANSFER_SUCESSFUL"), // $NON-NLS-1$
                            NLS.bind(// $NON-NLS-1$
                            Messages.getString("AbstractFileSendAction.MESSAGE_FILE_TRANSFER_SUCCESSFUL"), getFileTransferInfo().getFile().getName()));
                        } else {
                            // $NON-NLS-1$
                            MessageDialog.openError(// $NON-NLS-1$
                            null, // $NON-NLS-1$
                            Messages.getString("AbstractFileSendAction.TITLE_FILE_TRANSFER_FAILED"), // $NON-NLS-1$
                            NLS.bind(// $NON-NLS-1$
                            Messages.getString("AbstractFileSendAction.MESSAGE_FILE_TRANSFER_FAILED"), errorException.getLocalizedMessage()));
                        }
                    }
                });
            }
        }
    };
}
Also used : IFileTransferEvent(org.eclipse.ecf.filetransfer.events.IFileTransferEvent) IOutgoingFileTransferSendDoneEvent(org.eclipse.ecf.filetransfer.events.IOutgoingFileTransferSendDoneEvent)

Example 4 with IFileTransferEvent

use of org.eclipse.ecf.filetransfer.events.IFileTransferEvent in project ecf by eclipse.

the class URLRetrievePauseResumeTest method testReceiveHttp.

protected void testReceiveHttp(String url) throws Exception {
    assertNotNull(transferInstance);
    final IFileTransferListener listener = new IFileTransferListener() {

        public void handleTransferEvent(IFileTransferEvent event) {
            if (event instanceof IIncomingFileTransferReceiveResumedEvent) {
                try {
                    IIncomingFileTransferReceiveResumedEvent rse = (IIncomingFileTransferReceiveResumedEvent) event;
                    session = rse.receive(outs);
                } catch (Exception e) {
                    fail(e.getLocalizedMessage());
                }
            } else if (event instanceof IIncomingFileTransferReceiveStartEvent) {
                IIncomingFileTransferReceiveStartEvent rse = (IIncomingFileTransferReceiveStartEvent) event;
                try {
                    incomingFile = new File(FILENAME);
                    outs = new FileOutputStream(incomingFile);
                    session = rse.receive(outs);
                    pausable = (IFileTransferPausable) session.getAdapter(IFileTransferPausable.class);
                    if (pausable == null)
                        fail("pausable is null");
                } catch (IOException e) {
                    fail(e.getLocalizedMessage());
                }
            } else if (event instanceof IIncomingFileTransferReceiveDataEvent) {
                System.out.println("data=" + event);
            } else if (event instanceof IIncomingFileTransferReceivePausedEvent) {
                System.out.println("paused=" + event);
            } else if (event instanceof IIncomingFileTransferReceiveDoneEvent) {
                closeOutputStream();
                System.out.println("done=" + event);
                synchronized (notify) {
                    notify.notify();
                }
            }
        }
    };
    transferInstance.sendRetrieveRequest(FileIDFactory.getDefault().createFileID(transferInstance.getRetrieveNamespace(), url), listener, null);
    // Now if we can do pausing, then pause, wait a while and resume
    if (pausable != null) {
        Thread.sleep(500);
        System.out.println("pausable.pause()=" + pausable.pause());
        System.out.println("Pausing " + PAUSE_TIME / 1000 + " seconds");
        Thread.sleep(PAUSE_TIME);
        final boolean success = pausable.resume();
        System.out.println("pausable.resume()=" + success);
        if (!success) {
            System.out.println("session=" + session);
            final Exception e = session.getException();
            System.out.println("  exception=" + e);
            if (e != null)
                e.printStackTrace();
            System.out.println("  isDone=" + session.isDone());
            return;
        }
        System.out.println();
    }
    synchronized (notify) {
        notify.wait();
    }
    final Exception e = session.getException();
    if (e != null)
        throw e;
}
Also used : IIncomingFileTransferReceiveResumedEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveResumedEvent) IFileTransferPausable(org.eclipse.ecf.filetransfer.IFileTransferPausable) IIncomingFileTransferReceiveStartEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent) IFileTransferListener(org.eclipse.ecf.filetransfer.IFileTransferListener) IOException(java.io.IOException) IOException(java.io.IOException) IFileTransferEvent(org.eclipse.ecf.filetransfer.events.IFileTransferEvent) FileOutputStream(java.io.FileOutputStream) IIncomingFileTransferReceiveDoneEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent) IIncomingFileTransferReceivePausedEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceivePausedEvent) File(java.io.File) IIncomingFileTransferReceiveDataEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDataEvent)

Example 5 with IFileTransferEvent

use of org.eclipse.ecf.filetransfer.events.IFileTransferEvent in project ecf by eclipse.

the class BitTorrentConnectWizard method performFinish.

public boolean performFinish() {
    workbenchPage = workbench.getActiveWorkbenchWindow().getActivePage();
    final IRetrieveFileTransferContainerAdapter irftca = (IRetrieveFileTransferContainerAdapter) container.getAdapter(IRetrieveFileTransferContainerAdapter.class);
    try {
        targetID = FileIDFactory.getDefault().createFileID(irftca.getRetrieveNamespace(), page.getTorrentName());
    } catch (final FileCreateException e) {
        new ContainerConnectErrorDialog(workbench.getActiveWorkbenchWindow().getShell(), 1, "The target ID to connect to could not be created", page.getTorrentName(), e).open();
        return true;
    }
    try {
        irftca.sendRetrieveRequest(targetID, new IFileTransferListener() {

            public void handleTransferEvent(final IFileTransferEvent e) {
                if (e instanceof IIncomingFileTransferReceiveStartEvent) {
                    try {
                        final IFileTransfer ift = ((IIncomingFileTransferReceiveStartEvent) e).receive(new File(page.getTargetName()));
                        workbenchPage.getWorkbenchWindow().getShell().getDisplay().asyncExec(new Runnable() {

                            public void run() {
                                FileTransfersView.addTransfer(ift);
                            }
                        });
                    } catch (final IOException ioe) {
                        new ContainerConnectErrorDialog(workbench.getActiveWorkbenchWindow().getShell(), 1, "Could not write to " + page.getTargetName(), page.getTargetName(), null).open();
                    }
                } else if (e instanceof IIncomingFileTransferEvent) {
                    final FileTransfersView ftv = (FileTransfersView) workbenchPage.findView(FileTransfersView.ID);
                    if (ftv != null) {
                        workbenchPage.getWorkbenchWindow().getShell().getDisplay().asyncExec(new Runnable() {

                            public void run() {
                                ftv.update(((IIncomingFileTransferEvent) e).getSource());
                            }
                        });
                    }
                }
            }
        }, null);
    } catch (final IncomingFileTransferException e) {
        new ContainerConnectErrorDialog(workbench.getActiveWorkbenchWindow().getShell(), 1, "Could not send retrieval request.", targetID.getName(), e).open();
    }
    return true;
}
Also used : FileCreateException(org.eclipse.ecf.filetransfer.identity.FileCreateException) IRetrieveFileTransferContainerAdapter(org.eclipse.ecf.filetransfer.IRetrieveFileTransferContainerAdapter) IIncomingFileTransferReceiveStartEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent) FileTransfersView(org.eclipse.ecf.filetransfer.ui.FileTransfersView) ContainerConnectErrorDialog(org.eclipse.ecf.ui.dialogs.ContainerConnectErrorDialog) IFileTransferListener(org.eclipse.ecf.filetransfer.IFileTransferListener) IOException(java.io.IOException) IFileTransferEvent(org.eclipse.ecf.filetransfer.events.IFileTransferEvent) IIncomingFileTransferEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferEvent) IncomingFileTransferException(org.eclipse.ecf.filetransfer.IncomingFileTransferException) IFileTransfer(org.eclipse.ecf.filetransfer.IFileTransfer) File(java.io.File)

Aggregations

IFileTransferEvent (org.eclipse.ecf.filetransfer.events.IFileTransferEvent)6 File (java.io.File)5 IFileTransferListener (org.eclipse.ecf.filetransfer.IFileTransferListener)5 IOException (java.io.IOException)4 FileOutputStream (java.io.FileOutputStream)3 IncomingFileTransferException (org.eclipse.ecf.filetransfer.IncomingFileTransferException)3 IIncomingFileTransferReceiveDoneEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent)3 IIncomingFileTransferReceiveStartEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent)3 IFileTransferPausable (org.eclipse.ecf.filetransfer.IFileTransferPausable)2 IIncomingFileTransferReceiveDataEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDataEvent)2 IIncomingFileTransferReceivePausedEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceivePausedEvent)2 IIncomingFileTransferReceiveResumedEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveResumedEvent)2 IFileID (org.eclipse.ecf.filetransfer.identity.IFileID)2 FileNotFoundException (java.io.FileNotFoundException)1 OutputStream (java.io.OutputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 IAdapterManager (org.eclipse.core.runtime.IAdapterManager)1 ID (org.eclipse.ecf.core.identity.ID)1 IFileRangeSpecification (org.eclipse.ecf.filetransfer.IFileRangeSpecification)1