diff src/goodjava/lucene/backup/BackupServer.java @ 1499:22e15cf73040

lucene.backup
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 09 May 2020 23:14:13 -0600
parents
children e66e3d50b289
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/lucene/backup/BackupServer.java	Sat May 09 23:14:13 2020 -0600
@@ -0,0 +1,91 @@
+package goodjava.lucene.backup;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Map;
+import java.net.Socket;
+import java.net.ServerSocket;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.Executors;
+import javax.net.ssl.SSLServerSocketFactory;
+import javax.net.ssl.SSLServerSocket;
+import goodjava.util.SoftCacheMap;
+import goodjava.rpc.RpcServer;
+import goodjava.rpc.RpcCall;
+import goodjava.logging.Logger;
+import goodjava.logging.LoggerFactory;
+
+
+public class BackupServer {
+	private static final Logger logger = LoggerFactory.getLogger(BackupServer.class);
+
+	public static int port = 9101;
+	public static String[] cipherSuites = new String[] {
+		"TLS_DH_anon_WITH_AES_128_GCM_SHA256",
+		"TLS_DH_anon_WITH_AES_128_CBC_SHA256",
+		"TLS_ECDH_anon_WITH_AES_128_CBC_SHA",
+		"TLS_DH_anon_WITH_AES_128_CBC_SHA",
+		"TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA",
+		"SSL_DH_anon_WITH_3DES_EDE_CBC_SHA",
+		"TLS_ECDH_anon_WITH_RC4_128_SHA",
+		"SSL_DH_anon_WITH_RC4_128_MD5",
+		"SSL_DH_anon_WITH_DES_CBC_SHA",
+		"SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA",
+		"SSL_DH_anon_EXPORT_WITH_RC4_40_MD5",
+	};
+	static {
+		cipherSuites = null;  // for now, until I figure out disgusting java security
+	}
+
+	private final File backupDir;
+	private static final ThreadPoolExecutor threadPool = (ThreadPoolExecutor)Executors.newCachedThreadPool();
+	private static final Map<String,Backup> backups = new SoftCacheMap<String,Backup>();
+
+	public BackupServer(File backupDir) {
+		this.backupDir = backupDir;
+		backupDir.mkdirs();
+	}
+
+	public synchronized void start() throws IOException {
+		final ServerSocket ss;
+		if( cipherSuites == null ) {
+			ss = new ServerSocket(port);
+		} else {
+			ss = SSLServerSocketFactory.getDefault().createServerSocket(port);
+			((SSLServerSocket)ss).setEnabledCipherSuites(cipherSuites);
+		}
+		threadPool.execute(new Runnable(){public void run() {
+			try {
+				while(!threadPool.isShutdown()) {
+					final Socket socket = ss.accept();
+					threadPool.execute(new Runnable(){public void run() {
+						handle(socket);
+					}});
+				}
+			} catch(IOException e) {
+				logger.error("",e);
+			}
+		}});
+		logger.info("started server on port "+port);
+	}
+
+	private void handle(Socket socket) {
+		RpcServer rpc = new RpcServer(socket);
+		while( !rpc.isClosed() ) {
+			RpcCall call = rpc.read();
+			if( call == null )
+				break;
+			String name = (String)call.args[0];
+			Backup backup;
+			synchronized(backups) {
+				backup = backups.get(name);
+				if( backup == null ) {
+					backup = new Backup(new File(backupDir,name));
+					backups.put(name,backup);
+				}
+			}
+			backup.handle(rpc,call);
+		}
+	}
+
+}