summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/thrift
diff options
context:
space:
mode:
Diffstat (limited to 'dozentenmodul/src/main/java/thrift')
-rw-r--r--dozentenmodul/src/main/java/thrift/MasterThriftConnection.java42
-rw-r--r--dozentenmodul/src/main/java/thrift/ThriftConnection.java77
-rw-r--r--dozentenmodul/src/main/java/thrift/ThriftManager.java155
3 files changed, 155 insertions, 119 deletions
diff --git a/dozentenmodul/src/main/java/thrift/MasterThriftConnection.java b/dozentenmodul/src/main/java/thrift/MasterThriftConnection.java
deleted file mode 100644
index 686d0944..00000000
--- a/dozentenmodul/src/main/java/thrift/MasterThriftConnection.java
+++ /dev/null
@@ -1,42 +0,0 @@
-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.Client;
-
-public class MasterThriftConnection {
-
- private final static Logger LOGGER = Logger.getLogger(MasterThriftConnection.class);
-
- public static final String MASTERSERVER_IP = "132.230.4.16";
- public static final int MASTERSERVER_PORT = 9090;
- public static final int MASTERSERVER_TIMEOUT_MS = 30000;
-
- final TTransport transport = new TFramedTransport(new TSocket(
- MASTERSERVER_IP, MASTERSERVER_PORT, MASTERSERVER_TIMEOUT_MS));
-
- public Client getMasterThriftConnection() {
-
- try {
- transport.open();
- } catch (TTransportException e) {
- LOGGER.error("Keine Verbindung möglich!");
- return null;
- }
-
- final TProtocol protocol = new TBinaryProtocol(transport);
- final Client client = new Client(protocol);
- LOGGER.info("Masterserver '" + MASTERSERVER_IP + "' erreichbar.");
-
- return client;
- }
-
- public void closeMasterThriftConnection() {
- transport.close();
- }
-}
diff --git a/dozentenmodul/src/main/java/thrift/ThriftConnection.java b/dozentenmodul/src/main/java/thrift/ThriftConnection.java
deleted file mode 100644
index a026ef03..00000000
--- a/dozentenmodul/src/main/java/thrift/ThriftConnection.java
+++ /dev/null
@@ -1,77 +0,0 @@
-package thrift;
-
-import javax.swing.JOptionPane;
-
-import models.SessionData;
-
-import org.apache.log4j.Logger;
-import org.apache.thrift.protocol.TBinaryProtocol;
-import org.apache.thrift.protocol.TProtocol;
-import org.apache.thrift.transport.TSocket;
-import org.apache.thrift.transport.TTransport;
-import org.openslx.sat.thrift.iface.Server;
-import org.openslx.sat.thrift.iface.Server.Client;
-import org.openslx.sat.thrift.version.Version;
-
-public class ThriftConnection {
-
- private final static Logger LOGGER = Logger
- .getLogger(ThriftConnection.class);
-
- private String satAddress = SessionData.session.getServerAdress();
- final TTransport transport = new TSocket(satAddress, 9090, 10000);
-
- public Client getThriftConnection() {
-
- try {
- transport.open();
- } catch (Exception e) {
- LOGGER.error("Keine Verbindung möglich! Satellit: " + satAddress, e);
- JOptionPane.showMessageDialog(null,
- "Konnte keine Verbindung zum Satellit '" + satAddress
- + "' aufbauen!", "Debug-Message",
- JOptionPane.ERROR_MESSAGE);
- return null;
- }
-
- final TProtocol protocol = new TBinaryProtocol(transport);
- final Server.Client client = new Server.Client(protocol);
- LOGGER.info("Verbindung zu " + satAddress + " wurde aufgebaut.");
-
- long remoteVersion;
- try {
- remoteVersion = client.getVersion();
- } catch (Exception e) {
- LOGGER.error(
- "Could not query sat server version after successful connect! Assuming untagged version 1",
- e);
- remoteVersion = 1;
- }
- String text = null;
- if (remoteVersion > Version.VERSION) {
- text = "Das von Ihnen verwendete Dozentenmodul ist zu alt, um mit dem Satelliten-Server zu verbinden.\n"
- + "Sie verwenden Version "
- + Version.VERSION
- + ", der Satellit "
- + remoteVersion
- + "\n"
- + "Aktuelle Version unter http://bwlehrpool.hs-offenburg.de/";
- } else if (remoteVersion < Version.VERSION) {
- text = "Das von Ihnen verwendete Dozentenmodul ist zu neu, um mit dem Satelliten-Server zu verbinden.\n"
- + "Sie verwenden Version "
- + Version.VERSION
- + ", der Satellit " + remoteVersion;
- }
- if (text != null) {
- JOptionPane.showMessageDialog(null, text, "Fehler",
- JOptionPane.ERROR_MESSAGE);
- }
-
- return client;
- }
-
- public void closeThriftConnection() {
- LOGGER.info("Verbindung wird geplant getrennt.");
- transport.close();
- }
-}
diff --git a/dozentenmodul/src/main/java/thrift/ThriftManager.java b/dozentenmodul/src/main/java/thrift/ThriftManager.java
new file mode 100644
index 00000000..716c4439
--- /dev/null
+++ b/dozentenmodul/src/main/java/thrift/ThriftManager.java
@@ -0,0 +1,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();
+ }
+}