private static void join() { System.out.println("Joining server group..."); Properties newRMIconfig = localServer.getRmiSettings(); findActive(); if (remoteServer != null) { try { String tempselfName = remoteServer.serverJoin(newRMIconfig.getProperty("publicIP")); if (tempselfName != null) { selfName = tempselfName; newRMIconfig.setProperty("name", selfName); try { PropertyHandler.save(RMI_CONFIG_FILE, newRMIconfig); init(); } catch (Exception ex) { System.out.println("save rmiserver config failed"); } System.out.println("You have joined the server group of " + remoteServer.getServerName()); } else { System.out.println("you are already in the group of " + remoteServer.getServerName()); } } catch (RemoteException ex) { System.out.println("no active server"); System.exit(1); } startClientMode(); } else { System.out.println("no active server"); System.exit(1); } }
private static void startSync() { RemoteException rex; Properties serverList = null; if (serverPortal != null) { while (true) { try { System.out.println(serverPortal.hello()); serverList = serverPortal.getServerList(); try { PropertyHandler.save(SERVER_LIST_FILE, serverList); ClientConnection.getInstance().reload(); } catch (Exception ex) { System.out.println("server list save failed"); } sync.setServerPortal(serverPortal); sync.start(); wait(syncInterval); } catch (RemoteException ex) { rex = ex; break; } } System.out.println("Disconnected, trying to reconnect..."); reconnect(rex); startSync(); } else { System.out.println("No ative server"); } }
private static void startClientMode() { System.out.println("***back-up mode***"); try { activeServerIndex = ClientConnection.getServerIndex(remoteServer.getServerName()); System.out.println(remoteServer.hello()); ServerSync(); // loop } catch (RemoteException ex) { electNewActive(); } }
private static void leave() { System.out.println("Leaving server group..."); findActive(); if (remoteServer != null) { try { if (remoteServer.serverLeave(selfName)) { System.out.println( "You have left from the server group of " + remoteServer.getServerName()); } else { System.out.println("You are not part of server group of " + remoteServer.getServerName()); } } catch (RemoteException ex) { System.out.println("no active server"); System.exit(1); } } }
private static void ServerSync() throws RemoteException { String userPattern = localServer.getRmiSettings().getProperty("userdatapath"); String metaPattern = localServer.getRmiSettings().getProperty("metadatapath"); String destFilePath = userPattern.replace("$ftpuser", remoteServer.getFTPUser()); String metaFolder = metaPattern.replace("$ftpuser", remoteServer.getFTPUser()); String userListFileName = metaFolder + localServer.getRmiSettings().getProperty("userlistfile"); String accountManagerFileName = metaFolder + localServer.getRmiSettings().getProperty("amfile"); String serverFileMatrixFileName = metaFolder + localServer.getRmiSettings().getProperty("sfmfile"); int refreshInterval = clientModeConnection.getRefreshInterval(); final int MAX_THREADS = 99; // thread pool to optimize the resouce String serverID = selfName; SFTP sftp = new SFTP(remoteServer.getIP(), remoteServer.getFTPUser(), remoteServer, destFilePath); ArrayList<String> downloadList = new ArrayList<>(); // Create metadata folder FileOperation.createFolder(metaFolder); // Create users folder FileOperation.createFolder(destFilePath); ExecutorService pool = Executors.newFixedThreadPool(MAX_THREADS); while (true) { try { Thread.sleep(refreshInterval * 1000); } catch (InterruptedException ex) { } System.out.println("Sync..."); AccountManager am = remoteServer.getAccountManager(); Properties userlist = remoteServer.getUserList(); Properties serverlist = remoteServer.getServerList(); FileDeviceMatrix matrix = remoteServer.getServerMatrix(); FileOperation.save(am, accountManagerFileName); FileOperation.save(matrix, serverFileMatrixFileName); try { PropertyHandler.saveAbs(userListFileName, userlist); } catch (Exception ex1) { System.out.println("save userList failed!"); } try { PropertyHandler.save(SERVER_LIST_FILE, serverlist); } catch (Exception ex1) { System.out.println("save userList failed!"); } System.out.println("ServerSync for: " + serverID + "\n" + matrix); for (FileDeviceList node : matrix.getMatrix()) { if (!node.getServerNode(serverID).getUpdated().equals(DeviceInfoNode.Updated.TRUE)) { if (am.getAccountByUser(node.getUserName()) .getFileList() .findFile(node.getFileName()) .getState() .equals(FileAttr.State.DELETE)) { FileOperation.deleteFile( remoteServer.getDestFolder(node.getUserName(), false) + node.getFileName()); remoteServer.setSelfTrueByServer(node.getUserName(), serverID, node.getFileName()); remoteServer.removeNodeIfAllT(node.getUserName(), node.getFileName()); } else { FileOperation.createFolder(remoteServer.getDestFolder(node.getUserName(), true)); pool.execute( new ServerDownload( sftp, node.getUserName(), node.getFileName(), remoteServer.getDestFolder(node.getUserName(), true), serverID, downloadList, remoteServer)); } } } } }