This repository has been archived by the owner on Jul 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 218
/
WatchServer.java
185 lines (155 loc) · 6.39 KB
/
WatchServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Syncany, www.syncany.org
* Copyright (C) 2011-2014 Philipp C. Heckel <philipp.heckel@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.syncany.operations.daemon;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.syncany.config.Config;
import org.syncany.config.ConfigException;
import org.syncany.config.ConfigHelper;
import org.syncany.config.to.DaemonConfigTO;
import org.syncany.config.to.FolderTO;
import org.syncany.operations.daemon.messages.BadRequestResponse;
import org.syncany.operations.daemon.messages.ListWatchesRequest;
import org.syncany.operations.daemon.messages.ListWatchesResponse;
import org.syncany.operations.daemon.messages.Request;
import org.syncany.operations.daemon.messages.WatchRequest;
import org.syncany.operations.watch.WatchOperation;
import org.syncany.operations.watch.WatchOperationOptions;
import com.google.common.collect.Maps;
import com.google.common.eventbus.Subscribe;
/**
* The watch server can manage many different {@link WatchOperation}s. When started
* with {@link #start()} or {@link #reload()}, it first reads the daemon configuration file
* and then runs new threads for each configured Syncany folder. Invalid or non-existing folders
* are ignored.
*
* @author Philipp C. Heckel <philipp.heckel@gmail.com>
*/
public class WatchServer {
private static final Logger logger = Logger.getLogger(WatchServer.class.getSimpleName());
private Map<File, WatchRunner> watchOperations;
private LocalEventBus eventBus;
public WatchServer() {
this.watchOperations = new TreeMap<File, WatchRunner>();
this.eventBus = LocalEventBus.getInstance();
this.eventBus.register(this);
}
public void start(DaemonConfigTO daemonConfigTO) {
reload(daemonConfigTO);
}
public void reload(DaemonConfigTO daemonConfigTO) {
logger.log(Level.INFO, "Starting/reloading watch server ... ");
try {
Map<File, FolderTO> watchedFolders = getFolderMap(daemonConfigTO.getFolders());
// Stop all currently running operations
stopAllWatchOperations();
// Start all operations that are present in the config.
startWatchOperations(watchedFolders);
}
catch (Exception e) {
logger.log(Level.WARNING, "Cannot (re-)load config. Exception thrown.", e);
}
}
public void stop() {
logger.log(Level.INFO, "Stopping watch server ... ");
Map<File, WatchRunner> copyOfWatchOperations = Maps.newHashMap(watchOperations);
for (Map.Entry<File, WatchRunner> folderEntry : copyOfWatchOperations.entrySet()) {
File localDir = folderEntry.getKey();
WatchRunner watchOperationThread = folderEntry.getValue();
logger.log(Level.INFO, "- Stopping watch operation at " + localDir + " ...");
watchOperationThread.stop();
watchOperations.remove(localDir);
}
}
private void startWatchOperations(Map<File, FolderTO> newWatchedFolderTOs) throws ConfigException, ServiceAlreadyStartedException {
for (Map.Entry<File, FolderTO> folderEntry : newWatchedFolderTOs.entrySet()) {
File localDir = folderEntry.getKey();
WatchOperationOptions watchOperationOptions = folderEntry.getValue().getWatchOptions();
try {
Config watchConfig = ConfigHelper.loadConfig(localDir);
if (watchConfig != null) {
logger.log(Level.INFO, "- Starting watch operation at " + localDir + " ...");
WatchRunner watchOperationThread = new WatchRunner(watchConfig, watchOperationOptions);
watchOperationThread.start();
watchOperations.put(localDir, watchOperationThread);
}
else {
logger.log(Level.INFO, "- CANNOT start watch, because no config found at " + localDir + " ...");
}
}
catch (Exception e) {
logger.log(Level.SEVERE, " + Cannot start watch operation at " + localDir + ". IGNORING.", e);
}
}
}
/**
* stopAllWatchOperations stops all watchOperations and verifies if they actually have stopped.
*
*/
private void stopAllWatchOperations() {
for (File localDir : watchOperations.keySet()) {
WatchRunner watchOperationThread = watchOperations.get(localDir);
logger.log(Level.INFO, "- Stopping watch operation at " + localDir + " ...");
watchOperationThread.stop();
}
// Check if watch operations actually have stopped.
while (watchOperations.keySet().size() > 0) {
Map<File, WatchRunner> watchOperationsCopy = new TreeMap<File, WatchRunner>(watchOperations);
for (File localDir : watchOperationsCopy.keySet()) {
WatchRunner watchOperationThread = watchOperationsCopy.get(localDir);
if (watchOperationThread.hasStopped()) {
logger.log(Level.INFO, "- Watch operation at " + localDir + " has stopped");
watchOperations.remove(localDir);
}
}
}
}
private Map<File, FolderTO> getFolderMap(List<FolderTO> watchedFolders) {
Map<File, FolderTO> watchedFolderTOs = new TreeMap<File, FolderTO>();
for (FolderTO folderTO : watchedFolders) {
if (folderTO.isEnabled()) {
watchedFolderTOs.put(new File(folderTO.getPath()), folderTO);
}
}
return watchedFolderTOs;
}
@Subscribe
public void onRequestReceived(Request request) {
if (request instanceof ListWatchesRequest) {
processListWatchesRequest((ListWatchesRequest) request);
}
else if (request instanceof WatchRequest) {
processWatchRequest((WatchRequest) request);
}
}
private void processListWatchesRequest(ListWatchesRequest request) {
eventBus.post(new ListWatchesResponse(request.getId(), new ArrayList<File>(watchOperations.keySet())));
}
private void processWatchRequest(WatchRequest watchRequest) {
File rootFolder = new File(watchRequest.getRoot());
if (!watchOperations.containsKey(rootFolder)) {
eventBus.post(new BadRequestResponse(watchRequest.getId(), "Unknown root folder."));
}
}
}