1
1
package saros .negotiation ;
2
2
3
+ import java .io .DataInputStream ;
3
4
import java .io .File ;
5
+ import java .io .FileOutputStream ;
4
6
import java .io .IOException ;
7
+ import java .io .OutputStream ;
5
8
import java .util .HashMap ;
6
9
import java .util .List ;
7
10
import java .util .Map ;
8
11
import java .util .Map .Entry ;
12
+ import org .apache .commons .io .IOUtils ;
9
13
import org .apache .log4j .Logger ;
10
- import org .jivesoftware .smack .XMPPException ;
11
- import org .jivesoftware .smackx .filetransfer .IncomingFileTransfer ;
14
+ import saros .SarosPluginContext ;
12
15
import saros .exceptions .LocalCancellationException ;
13
16
import saros .exceptions .SarosCancellationException ;
14
17
import saros .filesystem .IChecksumCache ;
18
21
import saros .monitoring .IProgressMonitor ;
19
22
import saros .monitoring .SubProgressMonitor ;
20
23
import saros .negotiation .NegotiationTools .CancelOption ;
24
+ import saros .net .IConnectionManager ;
21
25
import saros .net .IReceiver ;
26
+ import saros .net .IStreamConnection ;
22
27
import saros .net .ITransmitter ;
23
28
import saros .net .xmpp .JID ;
24
29
import saros .net .xmpp .XMPPConnectionService ;
25
30
import saros .observables .FileReplacementInProgressObservable ;
31
+ import saros .repackaged .picocontainer .annotations .Inject ;
26
32
import saros .session .ISarosSession ;
27
33
import saros .session .ISarosSessionManager ;
28
34
import saros .util .CoreUtils ;
@@ -35,6 +41,9 @@ public class ArchiveIncomingProjectNegotiation extends AbstractIncomingProjectNe
35
41
36
42
private static final Logger LOG = Logger .getLogger (ArchiveIncomingProjectNegotiation .class );
37
43
44
+ // TODO move to factory
45
+ @ Inject private IConnectionManager connectionManager ;
46
+
38
47
public ArchiveIncomingProjectNegotiation (
39
48
final JID peer , //
40
49
final String negotiationID , //
@@ -60,6 +69,9 @@ public ArchiveIncomingProjectNegotiation(
60
69
connectionService ,
61
70
transmitter ,
62
71
receiver );
72
+
73
+ // FIXME remove
74
+ SarosPluginContext .initComponent (this );
63
75
}
64
76
65
77
@ Override
@@ -73,22 +85,20 @@ protected void transfer(
73
85
74
86
// the host do not send an archive if we do not need any files
75
87
if (filesMissing ) {
76
- receiveAndUnpackArchive (projectMapping , transferListener , monitor );
88
+ receiveAndUnpackArchive (projectMapping , monitor );
77
89
}
78
90
}
79
91
80
92
/** Receives the archive with all missing files and unpacks it. */
81
93
private void receiveAndUnpackArchive (
82
- final Map <String , IProject > localProjectMapping ,
83
- final TransferListener archiveTransferListener ,
84
- final IProgressMonitor monitor )
94
+ final Map <String , IProject > localProjectMapping , final IProgressMonitor monitor )
85
95
throws IOException , SarosCancellationException {
86
96
87
97
// waiting for the big archive to come in
88
98
89
99
monitor .beginTask (null , 100 );
90
100
91
- File archiveFile = receiveArchive (archiveTransferListener , new SubProgressMonitor (monitor , 50 ));
101
+ File archiveFile = receiveArchive (new SubProgressMonitor (monitor , 50 ));
92
102
93
103
/*
94
104
* FIXME at this point it makes no sense to report the cancellation to
@@ -146,37 +156,63 @@ private void unpackArchive(
146
156
// TODO: now add the checksums into the cache
147
157
}
148
158
149
- private File receiveArchive (TransferListener archiveTransferListener , IProgressMonitor monitor )
159
+ private File receiveArchive (IProgressMonitor monitor )
150
160
throws IOException , SarosCancellationException {
151
161
152
162
monitor .beginTask ("Receiving archive file..." , 100 );
153
- LOG .debug ("waiting for incoming archive stream request" );
154
-
155
- monitor .subTask ("Host is compressing project files. Waiting for the archive file..." );
156
-
157
- awaitTransferRequest ();
163
+ LOG .debug ("connecting to " + getPeer () + " to receive archive file" );
158
164
159
- monitor .subTask ("Receiving archive file ..." );
165
+ monitor .subTask ("Connecting to " + getPeer (). getName () + " ..." );
160
166
161
- LOG .debug (this + " : receiving archive" );
162
-
163
- IncomingFileTransfer transfer = archiveTransferListener .getRequest ().accept ();
167
+ IStreamConnection connection =
168
+ connectionManager .connectStream (TRANSFER_ID_PREFIX + getID (), getPeer ());
164
169
165
170
File archiveFile = File .createTempFile ("saros_archive_" + System .currentTimeMillis (), null );
166
171
172
+ OutputStream out = null ;
173
+
167
174
boolean transferFailed = true ;
168
175
169
176
try {
170
- transfer .recieveFile (archiveFile );
171
177
172
- monitorFileTransfer (transfer , monitor );
178
+ out = new FileOutputStream (archiveFile );
179
+
180
+ connection .setReadTimeout (60 * 60 * 1000 );
181
+ monitor .subTask ("Host is compressing project files. Waiting for the archive file..." );
182
+
183
+ DataInputStream dis = new DataInputStream (connection .getInputStream ());
184
+
185
+ long remainingDataSize = dis .readLong ();
186
+
187
+ monitor .subTask ("Receiving archive file..." );
188
+
189
+ LOG .debug (this + " : receiving archive" );
190
+
191
+ final byte buffer [] = new byte [BUFFER_SIZE ];
192
+
193
+ while (remainingDataSize > 0 ) {
194
+ int read = dis .read (buffer );
195
+
196
+ if (read == -1 ) break ;
197
+
198
+ out .write (buffer , 0 , read );
199
+ remainingDataSize -= read ;
200
+
201
+ checkCancellation (CancelOption .NOTIFY_PEER );
202
+ }
203
+
204
+ if (remainingDataSize > 0 )
205
+ localCancel (
206
+ "The receiving of the archive file was not successful." , CancelOption .NOTIFY_PEER );
207
+
173
208
transferFailed = false ;
174
- } catch (XMPPException e ) {
175
- throw new IOException (e .getMessage (), e .getCause ());
176
209
} finally {
177
210
if (transferFailed && !archiveFile .delete ()) {
178
211
LOG .warn ("Could not clean up archive file " + archiveFile .getAbsolutePath ());
179
212
}
213
+
214
+ IOUtils .closeQuietly (out );
215
+ connection .close ();
180
216
}
181
217
182
218
monitor .done ();
0 commit comments