mirror of
https://github.com/correl/mage.git
synced 2025-01-12 19:25:44 +00:00
* Server: added comments support in news feed (server.msg.txt), removed outdated code;
This commit is contained in:
parent
cf640b734b
commit
fb1c230761
1 changed files with 19 additions and 49 deletions
|
@ -25,14 +25,12 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
|
|||
*/
|
||||
public enum ServerMessagesUtil {
|
||||
instance;
|
||||
private static final Logger log = Logger.getLogger(ServerMessagesUtil.class);
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(ServerMessagesUtil.class);
|
||||
private static final String SERVER_MSG_TXT_FILE = "server.msg.txt";
|
||||
|
||||
private final List<String> messages = new ArrayList<>();
|
||||
private final ReadWriteLock lock = new ReentrantReadWriteLock();
|
||||
|
||||
private static String pathToExternalMessages = null;
|
||||
|
||||
private static boolean ignore = false;
|
||||
|
||||
private static long startDate;
|
||||
|
@ -43,16 +41,11 @@ public enum ServerMessagesUtil {
|
|||
private static final AtomicInteger lostConnection = new AtomicInteger(0);
|
||||
private static final AtomicInteger reconnects = new AtomicInteger(0);
|
||||
|
||||
static {
|
||||
pathToExternalMessages = System.getProperty("messagesPath");
|
||||
}
|
||||
|
||||
ServerMessagesUtil() {
|
||||
ScheduledExecutorService updateExecutor = Executors.newSingleThreadScheduledExecutor();
|
||||
updateExecutor.scheduleAtFixedRate(this::reloadMessages, 5, 5 * 60, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
|
||||
public List<String> getMessages() {
|
||||
lock.readLock().lock();
|
||||
try {
|
||||
|
@ -63,7 +56,7 @@ public enum ServerMessagesUtil {
|
|||
}
|
||||
|
||||
private void reloadMessages() {
|
||||
log.debug("Reading server messages...");
|
||||
LOGGER.debug("Reading server messages...");
|
||||
List<String> motdMessages = readFromFile();
|
||||
List<String> newMessages = new ArrayList<>();
|
||||
newMessages.addAll(motdMessages);
|
||||
|
@ -83,54 +76,36 @@ public enum ServerMessagesUtil {
|
|||
if (ignore) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
File externalFile = null;
|
||||
if (pathToExternalMessages != null) {
|
||||
externalFile = new File(pathToExternalMessages);
|
||||
if (!externalFile.exists()) {
|
||||
log.warn("Couldn't find server.msg.txt using external path: " + pathToExternalMessages);
|
||||
pathToExternalMessages = null; // not to repeat error action again
|
||||
externalFile = null;
|
||||
} else if (!externalFile.canRead()) {
|
||||
log.warn("Couldn't read (no access) server.msg.txt using external path: " + pathToExternalMessages);
|
||||
pathToExternalMessages = null; // not to repeat error action again
|
||||
}
|
||||
}
|
||||
|
||||
InputStream is = null;
|
||||
if (externalFile != null) {
|
||||
try {
|
||||
is = new FileInputStream(externalFile);
|
||||
} catch (Exception f) {
|
||||
log.error(f, f);
|
||||
pathToExternalMessages = null; // not to repeat error action again
|
||||
}
|
||||
File file = new File(SERVER_MSG_TXT_FILE);
|
||||
if (!file.exists() || !file.canRead()) {
|
||||
LOGGER.warn("Couldn't find server messages file using path: " + file.getAbsolutePath());
|
||||
} else {
|
||||
File file = new File(SERVER_MSG_TXT_FILE);
|
||||
if (!file.exists() || !file.canRead()) {
|
||||
log.warn("Couldn't find server.msg.txt using path: " + SERVER_MSG_TXT_FILE);
|
||||
} else {
|
||||
try {
|
||||
is = new FileInputStream(file);
|
||||
} catch (Exception f) {
|
||||
log.error(f, f);
|
||||
ignore = true;
|
||||
}
|
||||
try {
|
||||
is = new FileInputStream(file);
|
||||
ignore = false;
|
||||
} catch (Exception f) {
|
||||
LOGGER.error(f, f);
|
||||
ignore = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (is == null) {
|
||||
log.warn("Couldn't find server.msg");
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<String> newMessages = new ArrayList<>();
|
||||
try (Scanner scanner = new Scanner(is)) {
|
||||
while (scanner.hasNextLine()) {
|
||||
String message = scanner.nextLine();
|
||||
if (!message.trim().isEmpty()) {
|
||||
newMessages.add(message.trim());
|
||||
String message = scanner.nextLine().trim();
|
||||
if (message.startsWith("//") || message.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
newMessages.add(message.trim());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
LOGGER.error(e.getMessage(), e);
|
||||
} finally {
|
||||
StreamUtils.closeQuietly(is);
|
||||
}
|
||||
|
@ -157,11 +132,6 @@ public enum ServerMessagesUtil {
|
|||
return statistics;
|
||||
}
|
||||
|
||||
// private Timer timer = new Timer(1000 * 60, new ActionListener() {
|
||||
// public void actionPerformed(ActionEvent e) {
|
||||
// reloadMessages();
|
||||
// }
|
||||
// });
|
||||
public void setStartDate(long milliseconds) {
|
||||
startDate = milliseconds;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue