summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/filetransfer/ClassTest.java
blob: 04dc40d7a64f2587050065c78083ee613134582c (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
/**
 * File transfer between master server and satellite server.
 * The connection should always be from satellite to master, because of
 * open port knowledge on master server.
 * 
 * For uploading file to master, satellite should send a request with
 * token "U" for want upload. --> start Uploader(IP, PORT).
 * 
 * For downloading a file from master, satellite should send a request
 * with token "D" for want download. --> start Downloader(IP, PORT).
 * 
 * Means the master server has to start the opposite part:
 * If master receives token "U" --> start Downloader(socket)
 * If master receives token "D" --> start Uploader(socket)
 */

package org.openslx.filetransfer;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.KeyStore;

import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;

import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.DefaultConfiguration;

public class ClassTest
{
	
	private static final int CHUNK_SIZE = 11111111;

	private static String inFile;
	private static String outFile;

	public static void main( String[] args ) throws Exception
	{
		Configurator.initialize(new DefaultConfiguration());

		if ( args.length != 4 ) {
			System.out.println( "Need 4 argument: <keystore> <passphrase> <infile> <outfile>" );
			System.exit( 1 );
		}
		String pathToKeyStore = args[0];
		final char[] passphrase = args[1].toCharArray();
		inFile = args[2];
		outFile = args[3];
		KeyStore keystore = KeyStore.getInstance( "JKS" );
		keystore.load( new FileInputStream( pathToKeyStore ), passphrase );
		KeyManagerFactory kmf = KeyManagerFactory.getInstance( KeyManagerFactory.getDefaultAlgorithm() );
		kmf.init( keystore, passphrase );
		SSLContext context = SSLContext.getInstance( "TLSv1.2" );
		KeyManager[] keyManagers = kmf.getKeyManagers();

		context.init( keyManagers, null, null );

		Listener listener = new Listener( new Test(), context, 6789, 10000 );
		listener.start();

		Thread.sleep( 2000 );

		TrustManagerFactory tmf = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm() );
		tmf.init( keystore );

		context = SSLContext.getInstance( "TLSv1.2" );
		TrustManager[] trustManagers = tmf.getTrustManagers();

		context.init( null, trustManagers, null );

		Downloader d = new Downloader( "localhost", 6789, 10000, context, "xyz" );
		boolean res = d.download( outFile, new WantRangeCallback() {
			long pos = 0;
			long size = -1;

			@Override
			public FileRange get()
			{
				if ( size == -1 ) {
					try {
						size = Files.size( Paths.get( inFile ) );
					} catch ( IOException e ) {
						return null;
					}
				}
				if ( pos >= size )
					return null;
				long end = Math.min( pos + CHUNK_SIZE, size );
				FileRange range = new FileRange( pos, end );
				pos += CHUNK_SIZE;
				return range;
			}
		} );
		if ( res )
			System.out.println( "Active Download OK" );
		else
			System.out.println( "Active Download FAILED" );

		/*
		String pathToKeyStore =
				"/home/bjoern/javadev/DataTransfer/mySrvKeyStore.jks";
		 char[] passphrase = "test123".toCharArray();
		 KeyStore keystore = KeyStore.getInstance("JKS");
		 keystore.load(new FileInputStream(pathToKeyStore), passphrase);
		 KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
		 kmf.init(keystore, passphrase);
		 SSLContext context = SSLContext.getInstance("TLSv1.2");
		 KeyManager[] keyManagers = kmf.getKeyManagers();

		 context.init(keyManagers, null, null);

		Uploader u = new Uploader("localhost", 6789, context);
		u.sendToken("xyz");
		
		RandomAccessFile file = new RandomAccessFile(new File("test.txt"), "rw");
		long length = file.length();
		file.close();
		
		int diff = 0;
		for (int i = 0; (i + 5) < length; i += 5) {
			u.sendRange(i, i + 5);
			u.sendFile("test.txt");
			diff = (int) (length - i);
		}
		
		u.sendRange((int)(length - diff), (int)length);
		u.sendFile("test.txt");
		*/
	}

	// Implementing IncomingEvent for testing case.
	static class Test implements IncomingEvent
	{
		public void incomingDownloadRequest( Uploader uploader ) throws IOException
		{
			if ( uploader.getToken() == null ) {
				System.out.println( "Incoming uploader: could not get token!" );
				return;
			}
			if ( !uploader.upload( inFile ) )
				System.out.println( "Incoming uploader failed!" );
			else
				System.out.println( "Incomgin uploader OK" );
		}

		public void incomingUploadRequest( Downloader downloader ) throws IOException
		{
			if ( downloader.getToken() == null ) {
				System.out.println( "Incoming downloader: could not get token!" );
				return;
			}
			// TODO: if (!downloader.download( destinationFile, callback ))
		}
	}

}