diff 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
line wrap: on
line diff
--- a/src/goodjava/lucene/backup/BackupServer.java	Sat May 28 21:00:30 2022 -0600
+++ b/src/goodjava/lucene/backup/BackupServer.java	Sun May 29 23:38:34 2022 -0600
@@ -1,13 +1,6 @@
 package goodjava.lucene.backup;
 
 import java.io.File;
-import java.io.OutputStream;
-import java.io.FileOutputStream;
-import java.io.BufferedOutputStream;
-import java.io.Writer;
-import java.io.FileWriter;
-import java.io.Reader;
-import java.io.FileReader;
 import java.io.IOException;
 import java.util.Map;
 import java.net.Socket;
@@ -80,29 +73,31 @@
 
 	private void handle(Socket socket) {
 		RpcServer rpc = new RpcServer(socket);
-		Backup backup;
-		{
+		Backup backup = null;
+		while( !rpc.isClosed() ) {
 			RpcCall call = rpc.read();
-			if( !call.cmd.equals("login") ) {
+			if( call == null )
+				break;
+			if( call.cmd.equals("exists") ) {
+				String name = (String)call.args[0];
+				rpc.write( new RpcResult(new Object[]{new File(backupDir,name).exists()}) );
+			} else if( call.cmd.equals("login") ) {
+				String name = (String)call.args[0];
+				synchronized(backups) {
+					backup = backups.get(name);
+					if( backup == null ) {
+						backup = new Backup(new File(backupDir,name));
+						backups.put(name,backup);
+					}
+				}
+				rpc.write(Rpc.OK);
+			} else if( backup != null ) {
+				backup.handle(rpc,call);
+			} else {
 				rpc.write( new RpcException("login expected") );
 				rpc.close();
 				return;
 			}
-			String name = (String)call.args[0];
-			synchronized(backups) {
-				backup = backups.get(name);
-				if( backup == null ) {
-					backup = new Backup(new File(backupDir,name));
-					backups.put(name,backup);
-				}
-			}
-			rpc.write(Rpc.OK);
-		}
-		while( !rpc.isClosed() ) {
-			RpcCall call = rpc.read();
-			if( call == null )
-				break;
-			backup.handle(rpc,call);
 		}
 	}
 
@@ -120,15 +115,4 @@
 		return new RpcClient(socket);
 	}
 
-	public static void getBackup(String backupDomain,String name,File zip) throws IOException, RpcException {
-		RpcClient rpc = BackupServer.rpcClient(backupDomain);
-		RpcCall call = new RpcCall("zip",name);
-		rpc.write(call);
-		RpcResult result = rpc.read();
-		OutputStream out = new BufferedOutputStream(new FileOutputStream(zip));
-		IoUtils.copyAll(result.in,out);
-		out.close();
-		rpc.close();
-	}
-
 }