summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/taskmanager/tasks/DispatchRelay.java
blob: 470134d9d7baa88a767d165c0c620426a61de80f (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
package org.openslx.taskmanager.tasks;

import java.net.Socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import org.openslx.taskmanager.api.AbstractTask;

import com.google.gson.annotations.Expose;

public class DispatchRelay extends AbstractTask {

	private Output status;
	
	@Expose
	public String[] hosts;
	@Expose
	public int[] ports;
	@Expose
	public String[] descs;
	
	@Override
	protected boolean execute() 
	{
		Socket[] sockets;
		Thread[] relayThreads;
		try {
			sockets = createSockets();
			status.addMessage( "Sockets connected." );
		} catch ( IOException iox ) {
			status.addMessage( "Failed to connect sockets. " + iox.getMessage() );
			return false;
		}
		
		try {
			relayThreads = createRelayThreads( sockets );
			status.addMessage( "Relays created." );
		} catch ( IOException iox ) {
			status.addMessage( "Failed to create relays. " + iox.getMessage() );
			return false;
		}
		
		for ( Thread t : relayThreads )
			t.start();
		
		try {
			for ( Thread t : relayThreads )
				t.join();
		} catch ( InterruptedException inx ) {} finally {
			try {
				for ( Socket s : sockets ) {
					if ( !s.isClosed() )
						s.close();
				}
				status.addMessage( "Sockets closed properly." );
			} catch ( IOException iox ) {
				status.addMessage( "Failed to close Sockets. " + iox.getMessage() );	
			}	
		}
		return true;
	}

	@Override
	protected boolean initTask() 
	{		
		this.setStatusObject( status );
		System.out.println("Something happened.");
		
		if ( hosts.length != 2 || ports.length != 2 ) {
			status.addMessage( "Invalid host/port list." );
			return false;
		}
		return true;
	}
	
	protected Thread[] createRelayThreads ( Socket[] sockets) throws IOException
	{
		Thread[] t = new Thread[2];
		Socket[][] s = {sockets, {sockets[1], sockets[0]}};
		
		for ( int i = 0; i < 2; i++ ) {
			t[i] = new Thread( new Relay( s[i], status, descs[i] ));
		}
		return t;
	}
	
	protected Socket[] createSockets () throws IOException 
	{		
		Socket[] s = new Socket[2];
		
		for ( int i = 0; i < 2; i++ ) {
			s[i] = new Socket();
			s[i].connect( new InetSocketAddress( hosts[i], ports[i] ), 1200 );
		}
		return s;
	}
	
	public static class Output 
	{		
		protected String messages = null;
		
		public void addMessage( String str )
		{
			if ( messages == null ) 
			{
				messages = str;
			} else {
				messages += "\n" + str;
			}
		}
	}
	
	private class Relay implements Runnable {
	
		private InputStream in;
		private OutputStream out;
		
		private Output status;
		public final String desc;
		
		private byte[] buffer = new byte[16384];
		
		public Relay ( Socket[] s, Output status, String desc ) throws IOException 
		{		
			in = s[0].getInputStream();
			out = s[1].getOutputStream();
			this.status = status;
			this.desc = desc;
		}
		
		private void relay() throws IOException, InterruptedException 
		{		
			int readBytes = in.read( buffer );
			out.write( buffer, 0, readBytes );
		}
		
		public void close() 
		{
			try {
				if ( this.in != null )
					this.in.close();
				if ( this.out != null )
					this.out.close();
				this.status.addMessage( desc + ": input/output streams closed properly." );
			} catch ( IOException iox ) {
				this.status.addMessage( desc + ": failed to close input/ouput streams. " + iox.getMessage() );
			}	
		}
		
		@Override
		public void run() 
		{	
			while ( true ) {
				try {
					relay();
					this.status.addMessage( desc + ": relay operating." );
				} catch ( Exception x ) {
					this.status.addMessage( desc + ": close relay. " + x.getMessage() );
					this.close();
				}
			}	
		}
	}
}