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
/
ControlServer.java
128 lines (108 loc) · 4.07 KB
/
ControlServer.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
/*
* 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.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.input.Tailer;
import org.apache.commons.io.input.TailerListener;
import org.syncany.config.UserConfig;
/**
* The control server watches the daemon control file for changes and
* reacts on certain commands.
*
* <p>Although it is not a real socket, it can be seen as a cross-platform
* unix-like socket. Due to the nature of the commands in the control
* file (shutdown/reload), a normal TCP socket is not possible.
*
* <p>The central method is {@link #enterLoop()}: This method tails file changes
* in the daemon control file in the currently active thread. It does not
* fork a new thread. It <b>blocks</b> and waits for commands until
* <b>shutdown</b> is received.
*
* @author Philipp C. Heckel <philipp.heckel@gmail.com>
*/
public class ControlServer implements TailerListener {
private static final Logger logger = Logger.getLogger(ControlServer.class.getSimpleName());
private static final String CONTROL_FILE = "daemon.ctrl";
public enum ControlCommand {
SHUTDOWN, RELOAD
}
private File controlFile;
private Tailer controlFileTailer;
private LocalEventBus eventBus;
public ControlServer() {
this.controlFile = new File(UserConfig.getUserConfigDir(), CONTROL_FILE);
this.controlFileTailer = new Tailer(controlFile, this, 1000, true);
this.eventBus = LocalEventBus.getInstance();
}
public void enterLoop() throws IOException, ServiceAlreadyStartedException {
File userAppDir = UserConfig.getUserConfigDir();
userAppDir.mkdirs();
controlFile.delete();
controlFile.createNewFile();
controlFile.deleteOnExit();
logger.log(Level.INFO, "Monitoring control file for commands at " + controlFile + " ...");
logger.log(Level.INFO, " (Note: This is a blocking operation. The 'main' thread is now blocked until '" + ControlCommand.SHUTDOWN + "' is received.)");
controlFileTailer.run(); // This blocks!
}
/**
* Functions that handle tailing the control file.
*/
@Override
public void fileNotFound() {
logger.log(Level.SEVERE, "Control file not found. FATAL. EXITING.");
throw new RuntimeException("Control file not found. FATAL. EXITING.");
}
@Override
public void handle(String command) {
try {
ControlCommand controlCommand = ControlCommand.valueOf(command.trim().toUpperCase());
switch (controlCommand) {
case SHUTDOWN:
logger.log(Level.INFO, "Control file: Received shutdown command. Shutting down.");
eventBus.post(controlCommand);
controlFileTailer.stop();
break;
case RELOAD:
logger.log(Level.INFO, "Control file: Received reload command. Reloading config ...");
eventBus.post(controlCommand);
break;
default:
throw new RuntimeException("This command should have been handled.");
}
}
catch (Exception e) {
logger.log(Level.WARNING, "Control file: Ignoring unknown command: " + command, e);
}
}
@Override
public void handle(Exception e) {
logger.log(Level.SEVERE, "Control file tailer exception received. FATAL. EXITING.", e);
throw new RuntimeException("Control file tailer exception received. FATAL. EXITING.", e);
}
@Override
public void init(Tailer tailer) {
// Don't care
}
@Override
public void fileRotated() {
// Don't care
}
}