comparison src/goodjava/lucene/backup/BackupServer.java @ 1512:31b543826ca9

lucene.backup login
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 23 May 2020 19:01:57 -0600
parents 0ba144491a42
children 8dd8c556c449
comparison
equal deleted inserted replaced
1511:fb5be0e8ca54 1512:31b543826ca9
2 2
3 import java.io.File; 3 import java.io.File;
4 import java.io.OutputStream; 4 import java.io.OutputStream;
5 import java.io.FileOutputStream; 5 import java.io.FileOutputStream;
6 import java.io.BufferedOutputStream; 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;
7 import java.io.IOException; 11 import java.io.IOException;
8 import java.util.Map; 12 import java.util.Map;
9 import java.net.Socket; 13 import java.net.Socket;
10 import java.net.ServerSocket; 14 import java.net.ServerSocket;
11 import java.util.concurrent.Executors; 15 import java.util.concurrent.Executors;
16 import goodjava.io.IoUtils; 20 import goodjava.io.IoUtils;
17 import goodjava.rpc.RpcServer; 21 import goodjava.rpc.RpcServer;
18 import goodjava.rpc.RpcCall; 22 import goodjava.rpc.RpcCall;
19 import goodjava.rpc.RpcClient; 23 import goodjava.rpc.RpcClient;
20 import goodjava.rpc.RpcResult; 24 import goodjava.rpc.RpcResult;
25 import goodjava.rpc.Rpc;
21 import goodjava.rpc.RpcException; 26 import goodjava.rpc.RpcException;
22 import goodjava.logging.Logger; 27 import goodjava.logging.Logger;
23 import goodjava.logging.LoggerFactory; 28 import goodjava.logging.LoggerFactory;
24 29
25 30
73 logger.info("started server on port "+port); 78 logger.info("started server on port "+port);
74 } 79 }
75 80
76 private void handle(Socket socket) { 81 private void handle(Socket socket) {
77 RpcServer rpc = new RpcServer(socket); 82 RpcServer rpc = new RpcServer(socket);
78 while( !rpc.isClosed() ) { 83 Backup backup;
84 {
79 RpcCall call = rpc.read(); 85 RpcCall call = rpc.read();
80 if( call == null ) 86 if( !call.cmd.equals("login") ) {
81 break; 87 rpc.write( new RpcException("login expected") );
88 rpc.close();
89 return;
90 }
82 String name = (String)call.args[0]; 91 String name = (String)call.args[0];
83 Backup backup; 92 String password = (String)call.args[1];
84 synchronized(backups) { 93 synchronized(backups) {
85 backup = backups.get(name); 94 backup = backups.get(name);
86 if( backup == null ) { 95 if( backup == null ) {
87 backup = new Backup(new File(backupDir,name)); 96 backup = new Backup(new File(backupDir,name));
88 backups.put(name,backup); 97 backups.put(name,backup);
89 } 98 }
90 } 99 }
100 File pwd = new File(backupDir,name+".pwd");
101 try {
102 if( !pwd.exists() ) {
103 Writer out = new FileWriter(pwd);
104 out.write(password);
105 out.close();
106 } else {
107 Reader in = new FileReader(pwd);
108 if( !IoUtils.readAll(in).equals(password) ) {
109 rpc.write( new RpcException("wrong password") );
110 rpc.close();
111 return;
112 }
113 }
114 } catch(IOException e) {
115 throw new RuntimeException(e);
116 }
117 rpc.write(Rpc.OK);
118 }
119 while( !rpc.isClosed() ) {
120 RpcCall call = rpc.read();
121 if( call == null )
122 break;
91 backup.handle(rpc,call); 123 backup.handle(rpc,call);
92 } 124 }
93 } 125 }
94 126
95 127