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 _instance = null; private static ThreadLocal _satClient = new ThreadLocal<>(); private static ThreadLocal _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()."); 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(); } }