package org.openslx.bwlp.sat;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.net.ssl.SSLContext;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.thrift.transport.TTransportException;
import org.openslx.bwlp.sat.database.Database;
import org.openslx.bwlp.sat.database.mappers.DbImage;
import org.openslx.bwlp.sat.fileserv.FileServer;
import org.openslx.bwlp.sat.maintenance.DeleteOldImages;
import org.openslx.bwlp.sat.maintenance.DeleteOldLectures;
import org.openslx.bwlp.sat.maintenance.MailFlusher;
import org.openslx.bwlp.sat.maintenance.SendExpireWarning;
import org.openslx.bwlp.sat.thrift.BinaryListener;
import org.openslx.bwlp.sat.thrift.cache.OperatingSystemList;
import org.openslx.bwlp.sat.thrift.cache.OrganizationList;
import org.openslx.bwlp.sat.util.Configuration;
import org.openslx.bwlp.sat.util.Identity;
import org.openslx.bwlp.sat.web.WebServer;
import org.openslx.bwlp.thrift.iface.ImageSummaryRead;
import org.openslx.bwlp.thrift.iface.NetDirection;
import org.openslx.bwlp.thrift.iface.NetRule;
import org.openslx.bwlp.thrift.iface.UserInfo;
import org.openslx.thrifthelper.ThriftManager;
import org.openslx.thrifthelper.ThriftManager.ErrorCallback;
import org.openslx.util.Json;
import org.openslx.util.QuickTimer;
import org.openslx.util.QuickTimer.Task;
public class App {
private static Logger LOGGER = Logger.getLogger(App.class);
public static boolean DEBUG = false;
private static final Set<String> failFastMethods = new HashSet<>();
public static void main(String[] args) throws TTransportException, NoSuchAlgorithmException, IOException,
KeyManagementException {
//get going and show basic information in log file
BasicConfigurator.configure();
if (args.length != 0 && args[0].equals("debug")) {
DEBUG = true;
}
LOGGER.info("****************************************************************");
LOGGER.info("******************* Starting Application ***********************");
LOGGER.info("****************************************************************");
// get Configuration
try {
LOGGER.info("Loading configuration");
Configuration.load();
} catch (Exception e1) {
LOGGER.fatal("Could not load configuration", e1);
System.exit(1);
}
if (Identity.loadCertificate() == null) {
LOGGER.error("Could not set up TLS/SSL requirements, exiting");
System.exit(1);
}
failFastMethods.add("getVirtualizers");
failFastMethods.add("getOperatingSystems");
failFastMethods.add("getOrganizations");
ThriftManager.setMasterErrorCallback(new ErrorCallback() {
@Override
public boolean thriftError(int failCount, String method, Throwable t) {
if (failFastMethods.contains(method))
return false;
if (failCount > 2 || t == null || !(t instanceof TTransportException)) {
LOGGER.warn("Thrift Client error for " + method + ", FAIL.");
return false;
}
LOGGER.info("Thrift transport error " + ((TTransportException) t).getType() + " for "
+ method + ", retrying...");
try {
Thread.sleep(failCount * 250);
} catch (InterruptedException e) {
}
return true;
}
});
SSLContext ctx = null;
if (Configuration.getMasterServerSsl()) {
ctx = SSLContext.getInstance("TLSv1.2");
ctx.init(null, null, null);
}
ThriftManager.setMasterServerAddress(ctx, Configuration.getMasterServerAddress(),
Configuration.getMasterServerPort(), 30000);
// Load useful things from master server
OrganizationList.get();
OperatingSystemList.get();
// Start file transfer server
if (!FileServer.instance().start()) {
LOGGER.error("Could not start internal file server.");
return;
}
// Start watch dog to ensure nobody else is messing with the vmstore
QuickTimer.scheduleAtFixedDelay(new StorageUseCheck(), 10000, 60000);
// Set up maintenance tasks
DeleteOldImages.init();
SendExpireWarning.init();
MailFlusher.init();
DeleteOldLectures.init();
// Start Thrift Server
Thread t;
// Plain
t = new Thread(new BinaryListener(9090, false));
t.setDaemon(true);
t.start();
// SSL
t = new Thread(new BinaryListener(9091, true));
t.start();
// Start httpd
t = new Thread(new WebServer(9080));
t.setDaemon(true);
t.start();
// DEBUG
if (DEBUG) {
Database.printCharsetInformation();
List<ImageSummaryRead> allVisible;
try {
allVisible = DbImage.getAllVisible(new UserInfo("bla", "blu", null, null, null), null, 0);
LOGGER.info("Got " + allVisible.size());
} catch (SQLException e) {
LOGGER.warn("could not test query getallvisible");
}
QuickTimer.scheduleAtFixedDelay(new Task() {
@Override
public void fire() {
Database.printDebug();
}
}, 100, 5000);
NetRule nr = new NetRule(2, NetDirection.OUT, "dsafg", 1336);
String data = Json.serialize(nr);
LOGGER.info(data);
Json.registerThriftClass(NetRule.class);
NetRule nn = Json.deserializeThrift(data, NetRule.class);
LOGGER.info(nn);
}
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
QuickTimer.cancel();
LOGGER.info(new Date() + " - all Servers shut down, exiting...\n");
}
});
}
}