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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
package org.openslx.filetransfer;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public class Downloader
{
// Some instance variables.
private SSLSocketFactory sslSocketFactory;
private SSLSocket satelliteSocket;
private DataOutputStream dataToServer;
private DataInputStream dataFromServer;
private String TOKEN = null;
private String RANGE = null;
private String outputFilename = null;
private String ERROR = null;
/***********************************************************************/
/**
* Constructor for satellite downloader.
* Tries to connect to specific ip and port and sending type of action.
*
* @param ip
* @param port
* @throws IOException
* @throws KeyStoreException
* @throws CertificateException
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public Downloader( String ip, int port, SSLContext context )
{
try {
// TODO: Remove old code, that's why we have git.. ;)
// create socket.
sslSocketFactory = context.getSocketFactory();
satelliteSocket = (SSLSocket)sslSocketFactory.createSocket( ip, port );
dataToServer = new DataOutputStream( satelliteSocket.getOutputStream() );
dataToServer.writeByte( 'D' );
dataFromServer = new DataInputStream( satelliteSocket.getInputStream() );
} catch (Exception e) {
e.printStackTrace();
}
}
/***********************************************************************/
/**
* Constructor for master downloader.
* Given parameter is the socket over which the transfer is going.
*
* @param socket
*/
public Downloader( SSLSocket socket )
{
try {
satelliteSocket = socket;
dataToServer = new DataOutputStream( satelliteSocket.getOutputStream() );
dataFromServer = new DataInputStream( satelliteSocket.getInputStream() );
} catch (IOException e) {
e.printStackTrace();
}
}
/***********************************************************************/
/**
* Method for setting outputFilename.
*
* @param filename
*/
public void setOutputFilename( String filename )
{
outputFilename = filename;
}
/***********************************************************************/
/**
* Method for getting outputFilename.
*
*/
public String getOutputFilename()
{
return outputFilename;
}
/***********************************************************************/
/**
* Method for sending token for identification from satellite to master.
*
* @param t
*/
public Boolean sendToken( String token )
{
try {
TOKEN = token;
String sendToken = "TOKEN=" + TOKEN;
byte[] data = sendToken.getBytes( StandardCharsets.UTF_8 );
dataToServer.writeByte( data.length );
dataToServer.write( data );
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/***********************************************************************/
/**
* Method to send range of the file, which should be uploaded.
* Helpful for knowing how much was already uploaded if
* connection aborts.
*
* @param a
* @param b
*/
public Boolean sendRange( int a, int b )
{
try {
RANGE = a + ":" + b;
String sendRange = "RANGE=" + RANGE;
byte[] data = sendRange.getBytes( StandardCharsets.UTF_8 );
dataToServer.writeByte( data.length );
dataToServer.write( data );
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/***********************************************************************/
/**
* Method for reading incoming token for identification.
*
*/
public String getToken()
{
return TOKEN;
}
/***********************************************************************/
/**
* Method for reading range of file, which is downloaded.
* Helpful for knowing how much is already downloaded if connection aborts.
*/
public String getRange()
{
return RANGE;
}
/***********************************************************************/
/**
* Getter for beginning of RANGE.
*
* @return
*/
public int getStartOfRange()
{
if ( RANGE != null ) {
String[] splitted = RANGE.split( ":" );
return Integer.parseInt( splitted[0] );
}
return -1;
}
/***********************************************************************/
/**
* Getter for end of RANGE.
*
* @return
*/
public int getEndOfRange()
{
if ( RANGE != null ) {
String[] splitted = RANGE.split( ":" );
return Integer.parseInt( splitted[1] );
}
return -1;
}
/***********************************************************************/
/**
* Method for returning difference of current Range.
*
* @return
*/
public int getDiffOfRange()
{
int diff = Math.abs( getEndOfRange() - getStartOfRange() );
return diff;
}
/***********************************************************************/
/**
* Method for reading MetaData, like TOKEN and FileRange.
* Split incoming bytes after first '=' and store value to specific
* variable.
*
*/
public Boolean readMetaData()
{
try {
while ( true ) {
byte[] incoming = new byte[ 255 ];
// First get length.
dataFromServer.read( incoming, 0, 1 );
int length = incoming[0];
System.out.println( "length: " + length );
if ( length == 0 )
break;
/**
* Read the next available bytes and split by '=' for
* getting TOKEN or RANGE.
*/
int hasRead = 0;
while ( hasRead < length ) {
int ret = dataFromServer.read( incoming, hasRead, length - hasRead );
if ( ret == -1 ) {
System.out.println( "Error occured while reading Metadata." );
return false;
}
hasRead += ret;
}
String data = new String( incoming, 0, length, "UTF-8" );
// System.out.println(data);
String[] splitted = data.split( "=" );
// System.out.println("splitted[0]: " + splitted[0]);
// System.out.println("splitted[1]: " + splitted[1]);
if ( splitted[0] != null && splitted[0].equals( "TOKEN" ) ) {
if ( splitted[1] != null )
TOKEN = splitted[1];
System.out.println( "TOKEN: " + TOKEN );
}
else if ( splitted[0].equals( "RANGE" ) ) {
if ( splitted[1] != null )
RANGE = splitted[1];
System.out.println( "RANGE: '" + RANGE + "'" );
}
else if ( splitted[0].equals( "ERROR" ) ) {
if ( splitted[1] != null )
ERROR = splitted[1];
System.err.println( "ERROR: " + ERROR );
this.close();
return false;
}
}
} catch ( Exception e ) {
e.printStackTrace();
return false;
}
return true;
}
/***********************************************************************/
/**
* Method for reading Binary. Reading the current Range of incoming binary.
*
*/
public Boolean readBinary()
{
try {
int length = getDiffOfRange();
byte[] incoming = new byte[ 4000 ];
int hasRead = 0;
while ( hasRead < length ) {
int ret = dataFromServer.read( incoming, hasRead, length - hasRead );
if ( ret == -1 ) {
System.out.println( "Error occured in Downloader.readBinary(),"
+ " while reading binary." );
return false;
}
hasRead += ret;
}
RandomAccessFile file;
file = new RandomAccessFile( new File( outputFilename ), "rw" );
file.seek( getStartOfRange() );
file.write( incoming, 0, length );
file.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/***********************************************************************/
/**
* Method for sending error Code to server. For example in case of wrong
* token, send code for wrong token.
*
*/
public Boolean sendErrorCode( String errString )
{
try {
String sendError = "ERROR=" + errString;
byte[] data = sendError.getBytes( StandardCharsets.UTF_8 );
dataToServer.writeByte( data.length );
dataToServer.write( data );
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/***********************************************************************/
/**
* Method for closing connection, if download has finished.
*
*/
public void close()
{
try {
this.satelliteSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|