summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/filetransfer/Uploader.java
blob: 3deb2728fbdbaf131255684431aa9aa79010c6d7 (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
package org.openslx.filetransfer;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.SocketTimeoutException;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;

import org.apache.log4j.Logger;

public class Uploader extends Transfer
{

	private static final Logger log = Logger.getLogger( Uploader.class );

	/***********************************************************************/
	/**
	 * Actively establish upload connection to given peer.
	 * 
	 * @param host Host name or address to connect to
	 * @param port Port to connect to
	 * @param context ssl context for establishing a secure connection
	 * @throws IOException
	 */
	public Uploader( String host, int port, SSLContext context ) throws IOException
	{
		super( host, port, context, log );
		dataToServer.writeByte( 'U' );
	}

	/***********************************************************************/
	/**
	 * Constructor for master uploader.
	 * Sends back the socket for datatransfer.
	 * 
	 * @throws IOException
	 */
	public Uploader( SSLSocket socket ) throws IOException
	{
		super( socket, log );
	}

	/***********************************************************************/
	/**
	 * Used by the peer that initiated the connection to tell the remote
	 * peer which part of the file is being uploaded
	 * 
	 * @param startOffset start offset in bytes in the file (inclusive)
	 * @param endOffset end offset in file (exclusive)
	 * @return
	 */
	public boolean prepareSendRange( long startOffset, long endOffset )
	{
		return super.sendRange( startOffset, endOffset );
	}

	/***********************************************************************/
	/**
	 * Method for sending File with filename.
	 * 
	 * @param filename
	 */
	public boolean sendFile( String filename )
	{
		if ( getStartOfRange() == -1 ) {
			this.close( "sendFile called when no range is set" );
			return false;
		}

		RandomAccessFile file = null;
		try {
			file = new RandomAccessFile( new File( filename ), "r" );
			file.seek( getStartOfRange() );

			byte[] data = new byte[ 64000 ];
			int hasRead = 0;
			int length = getDiffOfRange();
			//			System.out.println( "diff of Range: " + length );
			while ( hasRead < length ) {
				int ret = file.read( data, 0, Math.min( length - hasRead, data.length ) );
				if ( ret == -1 ) {
					this.close( "Error occured in Uploader.sendFile(),"
							+ " while reading from File to send." );
					return false;
				}
				hasRead += ret;
				dataToServer.write( data, 0, ret );
			}
		} catch ( SocketTimeoutException ste ) {
			ste.printStackTrace();
			sendErrorCode( "timeout" );
			this.close( "Socket timeout occured ... close connection." );
			return false;
		} catch ( IOException ioe ) {
			ioe.printStackTrace();
			readMetaData();
			if ( ERROR != null ) {
				if ( ERROR.equals( "timeout" ) ) {
					this.close( "Remote Socket timeout occured ... close connection." );
					return false;
				}
			}
			this.close( "Sending RANGE " + getStartOfRange() + ":" + getEndOfRange() + " of File "
					+ filename + " failed..." );
			return false;
		} catch ( Exception e ) {
			e.printStackTrace();
			this.close( e.toString() );
			return false;
		} finally {
			if ( file != null ) {
				try {
					file.close();
				} catch ( IOException e ) {
				}
			}
		}
		return true;
	}

}