summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/thrifthelper/ThriftManager.java
diff options
context:
space:
mode:
authorSimon Rettberg2015-03-05 12:32:53 +0100
committerSimon Rettberg2015-03-05 12:32:53 +0100
commit420642007326d99cef3669cb039a74d191a31c34 (patch)
treec720fcc7161d0db40d53a6bec67d8d9bda88ef1b /src/main/java/org/openslx/thrifthelper/ThriftManager.java
parentmoved ThriftManager and ThriftHandler to master-sync-shared (diff)
downloadmaster-sync-shared-420642007326d99cef3669cb039a74d191a31c34.tar.gz
master-sync-shared-420642007326d99cef3669cb039a74d191a31c34.tar.xz
master-sync-shared-420642007326d99cef3669cb039a74d191a31c34.zip
Cleanup thrift shandling stuff
Diffstat (limited to 'src/main/java/org/openslx/thrifthelper/ThriftManager.java')
-rw-r--r--src/main/java/org/openslx/thrifthelper/ThriftManager.java216
1 files changed, 130 insertions, 86 deletions
diff --git a/src/main/java/org/openslx/thrifthelper/ThriftManager.java b/src/main/java/org/openslx/thrifthelper/ThriftManager.java
index c6d8986..6072030 100644
--- a/src/main/java/org/openslx/thrifthelper/ThriftManager.java
+++ b/src/main/java/org/openslx/thrifthelper/ThriftManager.java
@@ -1,8 +1,6 @@
package org.openslx.thrifthelper;
import java.lang.reflect.Proxy;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import org.apache.log4j.Logger;
import org.apache.thrift.protocol.TBinaryProtocol;
@@ -13,135 +11,181 @@ 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;
-import org.openslx.thrifthelper.ThriftHandler.ClientCreationCallback;
+import org.openslx.thrifthelper.ThriftHandler.EventCallback;
-public class ThriftManager {
+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 final static Logger LOGGER = Logger.getLogger( ThriftManager.class );
+
+ public interface ErrorCallback
+ {
+ /**
+ * Called if connecting/reconnecting to the thrift server failed.
+ *
+ * @param t the exception that occured last (may be null)
+ * @param message an optional message describing the circumstances
+ */
+ public void thriftError( Throwable t, String message );
}
+
+ private static ErrorCallback _errorCallback = null;
+
/**
- * Private singleton instances of itself and the satellite/master clients
+ * Private members for master connection information
*/
- private static Server.Iface _satClient = (Server.Iface) Proxy.newProxyInstance(
- Server.Iface.class.getClassLoader(),
- new Class[] { Server.Iface.class }, new ThriftHandler<Server.Client>(Server.Client.class, new ClientCreationCallback<Server.Client>() {
+ private static final String MASTERSERVER_ADDRESS = "bwlp-masterserver.ruf.uni-freiburg.de";
+ private static final int MASTERSERVER_PORT = 9090;
+ private static final int MASTERSERVER_TIMEOUT = 15000;
- @Override
- public Server.Client get() {
- // 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 null;
- }
- // 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 null;
- }
- final TProtocol protocol = new TBinaryProtocol(transport);
- // now we are ready to create the client, according to ClientType!
- LOGGER.info("Satellite '" + SATELLITE_IP + "' reachable. Client initialised.");
- return new Server.Client(protocol);
- }
- }));
+ /**
+ * 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 = 15000;
- private static ImageServer.Iface _masterClient = (ImageServer.Iface) Proxy.newProxyInstance(
+ /**
+ * Sat connection. Initialized when we know the sat server IP.
+ */
+ private static Server.Iface _satClient = null;
+
+ /**
+ * Master connection. As its address is known in advance, create the object right away.
+ */
+ private static ImageServer.Iface _masterClient = (ImageServer.Iface)Proxy.newProxyInstance(
ImageServer.Iface.class.getClassLoader(),
- new Class[] { ImageServer.Iface.class }, new ThriftHandler<ImageServer.Client>(ImageServer.Client.class, new ClientCreationCallback<ImageServer.Client>() {
+ new Class[] { ImageServer.Iface.class }, new ThriftHandler<ImageServer.Client>( ImageServer.Client.class, new EventCallback<ImageServer.Client>() {
@Override
- public ImageServer.Client get() {
+ public ImageServer.Client getNewClient()
+ {
// ok lets do it
- TTransport transport =
- new TFramedTransport(new TSocket(MASTERSERVER_IP, MASTERSERVER_PORT, MASTERSERVER_TIMEOUT));
+ TTransport transport =
+ new TFramedTransport( new TSocket( MASTERSERVER_ADDRESS, MASTERSERVER_PORT, MASTERSERVER_TIMEOUT ) );
try {
transport.open();
- } catch (TTransportException e) {
- LOGGER.error("Could not open transport to thrift's server with IP: " + MASTERSERVER_IP);
+ } catch ( TTransportException e ) {
+ LOGGER.error( "Could not open transport to thrift's server with IP: " + MASTERSERVER_ADDRESS );
+ transport.close();
return null;
}
- final TProtocol protocol = new TBinaryProtocol(transport);
+ final TProtocol protocol = new TBinaryProtocol( transport );
// now we are ready to create the client, according to ClientType!
- return new ImageServer.Client(protocol);
+ return new ImageServer.Client( protocol );
}
- }));
- /**
- * 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;
-
+
+ @Override
+ public void error( Throwable t, String message )
+ {
+ synchronized ( LOGGER ) {
+ if ( _errorCallback != null )
+ _errorCallback.thriftError( t, message );
+ }
+ }
+ } ) );
+
/**
* 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])$";
+ 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])$";
/**
* 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.");
+ 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);
+ if ( !ip.matches( IP_VALID_PATTERN ) ) {
+ LOGGER.error( "Given form of IP is invalid: " + ip );
return false;
}
// finally set it
SATELLITE_IP = ip;
- // last check: try to connect
- if (getSatClient() == null) {
- // init failed
- LOGGER.error("Could not initialise new client to satellite: " + SATELLITE_IP);
- return false;
- }
- // TODO final last: get version from server
+ // Create monster proxy class from interface
+ _satClient = (Server.Iface)Proxy.newProxyInstance(
+ Server.Iface.class.getClassLoader(),
+ new Class[] { Server.Iface.class }, new ThriftHandler<Server.Client>( Server.Client.class, new EventCallback<Server.Client>() {
+
+ @Override
+ public Server.Client getNewClient()
+ {
+ // 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 null;
+ }
+ // 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 );
+ transport.close();
+ return null;
+ }
+ final TProtocol protocol = new TBinaryProtocol( transport );
+ // now we are ready to create the client, according to ClientType!
+ LOGGER.info( "Satellite '" + SATELLITE_IP + "' reachable. Client initialised." );
+ return new Server.Client( protocol );
+ }
+
+ @Override
+ public void error( Throwable t, String message )
+ {
+ synchronized ( LOGGER ) {
+ if ( _errorCallback != null )
+ _errorCallback.thriftError( t, message );
+ }
+ }
+ } ) );
return true;
}
- /**
+
+ /**
* Returns the singleton client of the thrift connection to the satellite
+ *
* @return the thrift client to the satellite server
*/
- public static Server.Iface getSatClient() {
- return _satClient;
+ public static Server.Iface getSatClient()
+ {
+ return _satClient;
}
-
+
/**
* Returns the singleton client of the master thrift connection
+ *
* @return the thrift client to the master server
*/
- public static ImageServer.Iface getMasterClient() {
+ public static ImageServer.Iface getMasterClient()
+ {
return _masterClient;
}
+
+ /**
+ * Set the callback class for errors that occur on one of the
+ * thrift connections.
+ *
+ * @param cb
+ */
+ public static void setErrorCallback( ErrorCallback cb )
+ {
+ synchronized ( LOGGER ) {
+ _errorCallback = cb;
+ }
+ }
}