summaryrefslogblamecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/App.java
blob: 2f913ba0a5fac0cc8a0b6b391529073a87b2b8c6 (plain) (tree)
1
2
3
4
5
6
7
8
9

                             
                           
                                              
                             


                           
 

                                






                                                       
                                                             

                                                          
                                          
                                      
                                                      

                                                  
                                              
                                              
                                                            

                                        
 

                  
                                                                   




                                                                
                                                                                                                  




                                                                  


                                                                                                


                                    
                                                             

                                             
                                                                         


                                       




                                                                                       
                                                                          


                                                                                               
                                                                                                        


                                                                                                     

                                                                                                                     







                                                                      

                                                                                      
 

                                                        
                                          


                                                     
                                                                              

                               
                                      
                         
                        


                                                                



                                                               


                                                           

                                                          
                                                                                                                          
                                                                        
                                                  
                                                                                  
                         
                                                                    
                                         
                                                    


                                                              





                                                                                     













                                                                  
                                                                                   
         
 
package org.openslx.bwlp.sat;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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.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.util.Json;
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.QuickTimer;
import org.openslx.util.QuickTimer.Task;

public class App {

	private static Logger LOGGER = Logger.getLogger(App.class);

	private static List<Thread> servers = new ArrayList<>();

	public static boolean DEBUG = false;

	public static void main(String[] args) throws TTransportException, NoSuchAlgorithmException, IOException {
		//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);
		}

		ThriftManager.setMasterErrorCallback(new ErrorCallback() {

			@Override
			public boolean thriftError(int failCount, String method, Throwable t) {
				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;
			}
		});

		ThriftManager.setMasterServerAddress(SSLContext.getDefault(),
				"bwlp-masterserver.ruf.uni-freiburg.de", 9091, 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 Thrift Server
		Thread t;
		// Plain
		t = new Thread(new BinaryListener(9090, false));
		servers.add(t);
		t.start();
		// SSL
		t = new Thread(new BinaryListener(9091, true));
		servers.add(t);
		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);
		}
		// Wait for servers
		for (Thread wait : servers) {
			boolean success = false;
			while (!success) {
				try {
					wait.join();
					success = true;
				} catch (InterruptedException e) {
					// Do nothing...
				}
			}
		}
		QuickTimer.cancel();
		LOGGER.info(new Date() + " - all Servers shut down, exiting...\n");
	}
}