Mercurial Hosting > luan
comparison src/goodjava/lucene/backup/BackupServer.java @ 1674:af18eacf187c
automatic restore
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 29 May 2022 23:38:34 -0600 |
parents | 1b9f9fdb3b41 |
children | 445048f9eca7 |
comparison
equal
deleted
inserted
replaced
1673:1b9f9fdb3b41 | 1674:af18eacf187c |
---|---|
1 package goodjava.lucene.backup; | 1 package goodjava.lucene.backup; |
2 | 2 |
3 import java.io.File; | 3 import java.io.File; |
4 import java.io.OutputStream; | |
5 import java.io.FileOutputStream; | |
6 import java.io.BufferedOutputStream; | |
7 import java.io.Writer; | |
8 import java.io.FileWriter; | |
9 import java.io.Reader; | |
10 import java.io.FileReader; | |
11 import java.io.IOException; | 4 import java.io.IOException; |
12 import java.util.Map; | 5 import java.util.Map; |
13 import java.net.Socket; | 6 import java.net.Socket; |
14 import java.net.ServerSocket; | 7 import java.net.ServerSocket; |
15 import java.util.concurrent.Executors; | 8 import java.util.concurrent.Executors; |
78 logger.info("started server on port "+port); | 71 logger.info("started server on port "+port); |
79 } | 72 } |
80 | 73 |
81 private void handle(Socket socket) { | 74 private void handle(Socket socket) { |
82 RpcServer rpc = new RpcServer(socket); | 75 RpcServer rpc = new RpcServer(socket); |
83 Backup backup; | 76 Backup backup = null; |
84 { | 77 while( !rpc.isClosed() ) { |
85 RpcCall call = rpc.read(); | 78 RpcCall call = rpc.read(); |
86 if( !call.cmd.equals("login") ) { | 79 if( call == null ) |
80 break; | |
81 if( call.cmd.equals("exists") ) { | |
82 String name = (String)call.args[0]; | |
83 rpc.write( new RpcResult(new Object[]{new File(backupDir,name).exists()}) ); | |
84 } else if( call.cmd.equals("login") ) { | |
85 String name = (String)call.args[0]; | |
86 synchronized(backups) { | |
87 backup = backups.get(name); | |
88 if( backup == null ) { | |
89 backup = new Backup(new File(backupDir,name)); | |
90 backups.put(name,backup); | |
91 } | |
92 } | |
93 rpc.write(Rpc.OK); | |
94 } else if( backup != null ) { | |
95 backup.handle(rpc,call); | |
96 } else { | |
87 rpc.write( new RpcException("login expected") ); | 97 rpc.write( new RpcException("login expected") ); |
88 rpc.close(); | 98 rpc.close(); |
89 return; | 99 return; |
90 } | 100 } |
91 String name = (String)call.args[0]; | |
92 synchronized(backups) { | |
93 backup = backups.get(name); | |
94 if( backup == null ) { | |
95 backup = new Backup(new File(backupDir,name)); | |
96 backups.put(name,backup); | |
97 } | |
98 } | |
99 rpc.write(Rpc.OK); | |
100 } | |
101 while( !rpc.isClosed() ) { | |
102 RpcCall call = rpc.read(); | |
103 if( call == null ) | |
104 break; | |
105 backup.handle(rpc,call); | |
106 } | 101 } |
107 } | 102 } |
108 | 103 |
109 | 104 |
110 // for client | 105 // for client |
118 ((SSLSocket)socket).setEnabledCipherSuites(BackupServer.cipherSuites); | 113 ((SSLSocket)socket).setEnabledCipherSuites(BackupServer.cipherSuites); |
119 } | 114 } |
120 return new RpcClient(socket); | 115 return new RpcClient(socket); |
121 } | 116 } |
122 | 117 |
123 public static void getBackup(String backupDomain,String name,File zip) throws IOException, RpcException { | |
124 RpcClient rpc = BackupServer.rpcClient(backupDomain); | |
125 RpcCall call = new RpcCall("zip",name); | |
126 rpc.write(call); | |
127 RpcResult result = rpc.read(); | |
128 OutputStream out = new BufferedOutputStream(new FileOutputStream(zip)); | |
129 IoUtils.copyAll(result.in,out); | |
130 out.close(); | |
131 rpc.close(); | |
132 } | |
133 | |
134 } | 118 } |