diff options
| author | Jonathan Bauer | 2015-03-04 17:03:25 +0100 |
|---|---|---|
| committer | Jonathan Bauer | 2015-03-04 17:03:25 +0100 |
| commit | 17c7608810c023354068699b21a8c6882d9f130a (patch) | |
| tree | 4ab23ccb171bc0fab98cd58cd905667eada70a33 /dozentenmodul/src/main/java/thrift/ThriftManager.java | |
| parent | fix wrong class for logging (diff) | |
| download | tutor-module-17c7608810c023354068699b21a8c6882d9f130a.tar.gz tutor-module-17c7608810c023354068699b21a8c6882d9f130a.tar.xz tutor-module-17c7608810c023354068699b21a8c6882d9f130a.zip | |
Thrift: reconnect on transport error
Moved ThriftManager and ThriftHandler to master-sync-shared
Diffstat (limited to 'dozentenmodul/src/main/java/thrift/ThriftManager.java')
| -rw-r--r-- | dozentenmodul/src/main/java/thrift/ThriftManager.java | 183 |
1 files changed, 0 insertions, 183 deletions
diff --git a/dozentenmodul/src/main/java/thrift/ThriftManager.java b/dozentenmodul/src/main/java/thrift/ThriftManager.java deleted file mode 100644 index cc085ecd..00000000 --- a/dozentenmodul/src/main/java/thrift/ThriftManager.java +++ /dev/null @@ -1,183 +0,0 @@ -package thrift; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -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; - - /** - * IP Validation Regex - */ - private static final String IP_VALID_PATTERN = - "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + - "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + - "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + - "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"; - - /** - * 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)); - LOGGER.info("Satellite '" + SATELLITE_IP + "' reachable. Client initialised."); - } - 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; - } - // validate - Matcher matcher = Pattern.compile(IP_VALID_PATTERN).matcher(ip); - if (!matcher.matches()) { - LOGGER.error("Given form of IP is invalid: " + ip); - return false; - } - // finally set it - SATELLITE_IP = ip; - - // last check: try to connect - if (!init(ClientType.SATELLITE, SATELLITE_IP, SATELLITE_PORT, SATELLITE_TIMEOUT)) { - // init failed - LOGGER.error("Could not initialise new client to satellite: " + SATELLITE_IP); - return false; - } - // TODO final last: get version from server - 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(); - } -} |
