annotate src/goodjava/lucene/backup/BackupServer.java @ 1504:f443542d8650

threading
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 11 May 2020 11:13:16 -0600
parents 8a7b6b32c691
children d80395468b4e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.lucene.backup;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.File;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.io.IOException;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.util.Map;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import java.net.Socket;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 import java.net.ServerSocket;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 import java.util.concurrent.Executors;
1502
8a7b6b32c691 minor threads
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
9 import java.util.concurrent.ExecutorService;
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 import javax.net.ssl.SSLServerSocketFactory;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 import javax.net.ssl.SSLServerSocket;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 import goodjava.util.SoftCacheMap;
1501
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
13 import goodjava.io.IoUtils;
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 import goodjava.rpc.RpcServer;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 import goodjava.rpc.RpcCall;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 import goodjava.logging.Logger;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 import goodjava.logging.LoggerFactory;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 public class BackupServer {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 private static final Logger logger = LoggerFactory.getLogger(BackupServer.class);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 public static int port = 9101;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 public static String[] cipherSuites = new String[] {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 "TLS_DH_anon_WITH_AES_128_GCM_SHA256",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 "TLS_DH_anon_WITH_AES_128_CBC_SHA256",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 "TLS_ECDH_anon_WITH_AES_128_CBC_SHA",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 "TLS_DH_anon_WITH_AES_128_CBC_SHA",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 "TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 "SSL_DH_anon_WITH_3DES_EDE_CBC_SHA",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 "TLS_ECDH_anon_WITH_RC4_128_SHA",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 "SSL_DH_anon_WITH_RC4_128_MD5",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 "SSL_DH_anon_WITH_DES_CBC_SHA",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 "SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 "SSL_DH_anon_EXPORT_WITH_RC4_40_MD5",
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 };
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 static {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 cipherSuites = null; // for now, until I figure out disgusting java security
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 private final File backupDir;
1502
8a7b6b32c691 minor threads
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
42 private static final ExecutorService threadPool = Executors.newCachedThreadPool();
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 private static final Map<String,Backup> backups = new SoftCacheMap<String,Backup>();
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44
1501
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
45 public BackupServer(File backupDir) throws IOException {
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 this.backupDir = backupDir;
1501
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
47 IoUtils.mkdirs(backupDir);
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 public synchronized void start() throws IOException {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 final ServerSocket ss;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 if( cipherSuites == null ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 ss = new ServerSocket(port);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 } else {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 ss = SSLServerSocketFactory.getDefault().createServerSocket(port);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 ((SSLServerSocket)ss).setEnabledCipherSuites(cipherSuites);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 threadPool.execute(new Runnable(){public void run() {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 try {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 while(!threadPool.isShutdown()) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 final Socket socket = ss.accept();
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 threadPool.execute(new Runnable(){public void run() {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 handle(socket);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 }});
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 } catch(IOException e) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 logger.error("",e);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 }});
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 logger.info("started server on port "+port);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 private void handle(Socket socket) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 RpcServer rpc = new RpcServer(socket);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 while( !rpc.isClosed() ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 RpcCall call = rpc.read();
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 if( call == null )
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 break;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 String name = (String)call.args[0];
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 Backup backup;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 synchronized(backups) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 backup = backups.get(name);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 if( backup == null ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 backup = new Backup(new File(backupDir,name));
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 backups.put(name,backup);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 backup.handle(rpc,call);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 }