summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/thrift/ThriftManager.java
blob: 716c4439d1291bc23a5623085e67e15243c958fa (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
package thrift;

import org.apache.log4j.Logger;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.openslx.imagemaster.thrift.iface.ImageServer;
import org.openslx.sat.thrift.iface.Server;

public class ThriftManager {

	private final static Logger LOGGER = Logger.getLogger(ThriftManager.class);
	
	/** 
	 * Public variables to represent client types
	 * TODO: Public needed?
	 */
	public static enum ClientType {
		MASTER, SATELLITE
	}
	/**
	 * Private singleton instances of itself and the satellite/master clients
	 */
	private static ThreadLocal<ThriftManager> _instance = null;
	private static ThreadLocal<Server.Client> _satClient = new ThreadLocal<>();
	private static ThreadLocal<ImageServer.Client> _masterClient = new ThreadLocal<>();
	
	/**
	 * Private members for master connection information
	 */
	private static final String MASTERSERVER_IP = "132.230.4.16";
	private static final int 		MASTERSERVER_PORT = 9090;
	private static final int 		MASTERSERVER_TIMEOUT = 30000;
	
	
	/**
	 * Private members for satellite connection information
	 */
	private static String 			SATELLITE_IP = null;
	private static final int 		SATELLITE_PORT = 9090;
	private static final int 		SATELLITE_TIMEOUT = 10000;
	
	/**
	 * Protected constructor to avoid external instantiation 
	 */
	protected ThriftManager() {
		if (_instance.get() == null) {
			_instance.set(new ThriftManager());
		}
		if (_instance.get() == null) {
			// something very wrong...
			LOGGER.error("Could not initialize Thrift Manager, this should not happen. Contact a developper.");
			// TODO handle: exit? message box?
		}
	}
	
	/**
	 * Initialise the client of type 'type' with give ip, port and timeout.
	 * 
	 * @param type type of the client. Valid are 'MASTER' or 'SATELLITE'
	 * @param ip ip of the server to connect to
	 * @param port port of the server to connect to
	 * @param timeout timeout of the connection in milliseconds
	 * @return true if initializing the client worked, false otherwise
	 */
	private static boolean init(ClientType type, String ip, int port, int timeout) {

		/*
		 * Master Connection
		 */
		if (type == ClientType.MASTER) {
			TTransport transport = 
					new TFramedTransport(new TSocket(MASTERSERVER_IP, MASTERSERVER_PORT, MASTERSERVER_TIMEOUT));
			try {
				transport.open();
			} catch (TTransportException e) {
				LOGGER.error("Could not open transport to thrift's server with IP: " + MASTERSERVER_IP);
				return false;
			}
			final TProtocol protocol = new TBinaryProtocol(transport);
			// now we are ready to create the client, according to ClientType!
			_masterClient.set(new ImageServer.Client(protocol));
			LOGGER.info("Masterserver '" + MASTERSERVER_IP + "' reachable. Client initialised.");
		}
		/*
		 * Satellite Connection
		 */
		if (type == ClientType.SATELLITE) {
			// first check if we have a sat ip
			if (SATELLITE_IP == null) {
				LOGGER.error("Satellite ip adress was not set prior to getting the sat client. Use setSatellite(<ip>).");
				return false;
			}
			// ok lets do it
			TTransport transport = 
					new TSocket(SATELLITE_IP, SATELLITE_PORT, SATELLITE_TIMEOUT);
			try {
				transport.open();
			} catch (TTransportException e) {
				LOGGER.error("Could not open transport to thrift's server with IP: " + SATELLITE_IP);
				return false;
			}
			final TProtocol protocol = new TBinaryProtocol(transport);
			// now we are ready to create the client, according to ClientType!
			_satClient.set(new Server.Client(protocol));
		}
		return true;
	}
	/**
	 * Sets the IP of the satellite to connect to
	 * @param ip the ip of the satellite as String
	 * @return true if setting the ip worked, false otherwise
	 */
	public static boolean setSatellite(String ip) {
		if (ip.isEmpty()) {
			LOGGER.error("Given IP for satellite is empty.");
			return false;
		}
		SATELLITE_IP = ip;
		return true;
	}
	/** 
	 * Returns the singleton client of the thrift connection to the satellite
	 * @return the thrift client to the satellite server
	 */
	public static Server.Client getSatClient() {
			if (_satClient.get() == null) {
				init(ClientType.SATELLITE, SATELLITE_IP, SATELLITE_PORT, SATELLITE_TIMEOUT);
			}
			if (_satClient.get() == null) {
				LOGGER.error("Satelite client still null. Initialisation must have failed. Contact developper.");
				return null;
			}
			return _satClient.get();
	}
	
	/**
	 * Returns the singleton client of the master thrift connection
	 * @return the thrift client to the master server
	 */
	public static ImageServer.Client getMasterClient() {
		if (_masterClient.get() == null) {
			LOGGER.debug("Initialising master thrift client...");
			init(ClientType.MASTER, MASTERSERVER_IP, MASTERSERVER_PORT, MASTERSERVER_TIMEOUT);
		}
		if (_masterClient.get() == null) {
			LOGGER.error("Satellite client still null. Initialisation must have failed. Contact developper.");
			return null;
		}
		return _masterClient.get();
	}
}