Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7b66892b7440e0b4b6e2b0bc4017bdbef76d9645 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
 * Copyright (c) 2004 - 2009 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http:www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.net4j.examples.transfer;

import org.eclipse.net4j.connector.IConnector;
import org.eclipse.net4j.internal.examples.bundle.OM;
import org.eclipse.net4j.signal.RequestWithMonitoring;
import org.eclipse.net4j.signal.SignalProtocol;
import org.eclipse.net4j.util.container.IPluginContainer;
import org.eclipse.net4j.util.io.ExtendedDataInputStream;
import org.eclipse.net4j.util.io.ExtendedDataOutputStream;
import org.eclipse.net4j.util.om.monitor.EclipseMonitor;
import org.eclipse.net4j.util.om.monitor.MonitorCanceledException;
import org.eclipse.net4j.util.om.monitor.OMMonitor;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

/**
 * @author Eike Stepper
 * @since 2.0
 */
public class UploadClientAction implements IWorkbenchWindowActionDelegate, UploadProtocol
{
  private IWorkbenchWindow window;

  public UploadClientAction()
  {
  }

  public void init(IWorkbenchWindow window)
  {
    this.window = window;
  }

  public void run(IAction action)
  {
    FileDialog fileDialog = new FileDialog(window.getShell());
    final String path = fileDialog.open();
    if (path != null)
    {
      final File file = new File(path);
      new Job("Uploading file")
      {
        @Override
        protected IStatus run(IProgressMonitor monitor)
        {
          try
          {
            boolean replaced = transferFile(file, monitor);
            OM.LOG.info("File " + path + (replaced ? " replaced" : " stored") + " on the Net4j server.");
            return Status.OK_STATUS;
          }
          catch (MonitorCanceledException ex)
          {
            return Status.CANCEL_STATUS;
          }
          catch (Exception ex)
          {
            return new Status(IStatus.ERROR, OM.BUNDLE_ID, "Problem with upload of " + path, ex);
          }
          finally
          {
            monitor.done();
          }
        }
      }.schedule();
    }
  }

  public void selectionChanged(IAction action, ISelection selection)
  {
  }

  public void dispose()
  {
  }

  private boolean transferFile(final File file, IProgressMonitor monitor) throws Exception
  {
    SignalProtocol<Object> protocol = null;

    try
    {
      // Start a connector that represents the client side of a physical connection
      IConnector connector = (IConnector)IPluginContainer.INSTANCE.getElement("org.eclipse.net4j.connectors", "tcp",
          "localhost:2036");

      // Open a virtual channel with the ECHO protocol, send an ECHO request and close the channel
      protocol = new SignalProtocol<Object>(PROTOCOL_NAME);
      protocol.open(connector);

      UploadRequest request = new UploadRequest(protocol, file);
      return request.send(new EclipseMonitor(monitor));
    }
    finally
    {
      protocol.close();
    }
  }

  /**
   * @author Eike Stepper
   */
  public static final class UploadRequest extends RequestWithMonitoring<Boolean>
  {
    private File file;

    public UploadRequest(SignalProtocol<?> protocol, File file)
    {
      super(protocol, UPLOAD_SIGNAL_ID);
      this.file = file;
    }

    @Override
    protected void requesting(ExtendedDataOutputStream out, OMMonitor monitor) throws Exception
    {
      long size = file.length();
      out.writeLong(size);
      out.writeString(file.getName());

      monitor.begin((int)size);
      BufferedInputStream in = null;

      try
      {
        in = new BufferedInputStream(new FileInputStream(file));
        while (size != 0L)
        {
          int chunk = BUFFER_SIZE;
          if (size < BUFFER_SIZE)
          {
            chunk = (int)size;
          }

          byte[] buffer = new byte[chunk];
          in.read(buffer);
          out.writeByteArray(buffer);

          monitor.worked(chunk);
          size -= chunk;
        }
      }
      finally
      {
        monitor.done();
        in.close();
      }
    }

    @Override
    protected Boolean confirming(ExtendedDataInputStream in, OMMonitor monitor) throws Exception
    {
      return in.readBoolean();
    }
  }
}

Back to the top