summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/App.java
blob: 54a25de3af04759d212b856b0d6234c19e5e22ca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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.maintenance.DeleteOldImages;
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;
		}
		
		// Set up maintenance tasks
		DeleteOldImages.init();
		
		// 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");
	}
}