Stabilized the browser shutdown process.
This commit is contained in:
@@ -6,6 +6,7 @@ package edu.regis.universeplayer.addon;
|
||||
|
||||
import edu.regis.universeplayer.browserCommands.BrowserConstants;
|
||||
import edu.regis.universeplayer.browserCommands.MessageHandler;
|
||||
import org.apache.logging.log4j.core.LogEvent;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -18,7 +19,7 @@ import java.util.concurrent.Future;
|
||||
public class Main
|
||||
{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Main.class);
|
||||
|
||||
|
||||
public static void main(String[] args) throws IOException
|
||||
{
|
||||
MessageHandler interfaceLink;
|
||||
@@ -32,14 +33,22 @@ public class Main
|
||||
try
|
||||
{
|
||||
logger.debug("Setting up connection");
|
||||
socket = new Socket(BrowserConstants.IP,BrowserConstants.PORT);
|
||||
socket = new Socket(BrowserConstants.IP, BrowserConstants.PORT);
|
||||
logger.debug("Connection established");
|
||||
|
||||
|
||||
Socket finalSocket = socket;
|
||||
interfaceLink = new MessageHandler("InterfaceHandler", finalSocket.getInputStream(), finalSocket.getOutputStream()) {
|
||||
interfaceLink = new MessageHandler("InterfaceHandler", finalSocket.getInputStream(), finalSocket.getOutputStream())
|
||||
{
|
||||
private long lastPing = 0;
|
||||
|
||||
@Override
|
||||
protected boolean onRun()
|
||||
{
|
||||
/*
|
||||
* How many milliseconds must pass between browser
|
||||
* pings.
|
||||
*/
|
||||
final long PING_RATE = 5000;
|
||||
if (!finalSocket.isConnected() || finalSocket.isClosed() || finalSocket.isInputShutdown() || finalSocket.isOutputShutdown())
|
||||
{
|
||||
logger.debug("Interface socket closed, shutting down");
|
||||
@@ -48,10 +57,22 @@ public class Main
|
||||
/*
|
||||
* Make sure that it is active.
|
||||
*/
|
||||
// this.sendUpdate("ping");
|
||||
if (SocketAppender.hasLogs())
|
||||
{
|
||||
lastPing = System.currentTimeMillis();
|
||||
for (LogEvent event : SocketAppender.retrieveLogEvents())
|
||||
{
|
||||
this.sendUpdate(event);
|
||||
}
|
||||
}
|
||||
else if (System.currentTimeMillis() - lastPing >= PING_RATE)
|
||||
{
|
||||
lastPing = System.currentTimeMillis();
|
||||
this.sendUpdate("ping");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onClose()
|
||||
{
|
||||
@@ -72,20 +93,22 @@ public class Main
|
||||
* Pretty much just forwards any messages to the browser and
|
||||
* returns their value.
|
||||
*/
|
||||
browserLink.addUpdateListener((update, link) -> {
|
||||
browserLink.addUpdateListener((update, link) ->
|
||||
{
|
||||
logger.debug("Sending update {}", update);
|
||||
interfaceLink.sendUpdate(update);
|
||||
});
|
||||
interfaceLink.addListener((providedValue, previousReturn) -> {
|
||||
interfaceLink.addListener((providedValue, previousReturn) ->
|
||||
{
|
||||
logger.debug("Forwarding message to browser: {}", providedValue);
|
||||
Object returnValue = browserLink.sendObject(providedValue).get();
|
||||
logger.debug("Forwarding return to interface: {}", returnValue);
|
||||
return returnValue;
|
||||
});
|
||||
|
||||
|
||||
browserThread = new Thread(browserLink);
|
||||
interfaceThread = new Thread(interfaceLink);
|
||||
|
||||
|
||||
logger.debug("Starting threads.");
|
||||
browserThread.start();
|
||||
interfaceThread.start();
|
||||
@@ -136,7 +159,7 @@ public class Main
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void testMain() throws ExecutionException, InterruptedException
|
||||
{
|
||||
LinkedList<Future<Object>> requests = new LinkedList<>();
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
package edu.regis.universeplayer.addon;
|
||||
|
||||
import org.apache.logging.log4j.core.*;
|
||||
import org.apache.logging.log4j.core.appender.AbstractAppender;
|
||||
import org.apache.logging.log4j.core.config.Property;
|
||||
import org.apache.logging.log4j.core.config.plugins.Plugin;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* This appender will store all messages sent to it, for the interface link to
|
||||
* later collect and forward to the interface.
|
||||
*
|
||||
* @author William Hubbard
|
||||
*/
|
||||
@Plugin(
|
||||
name = "Socket",
|
||||
category = Core.CATEGORY_NAME,
|
||||
elementType = Appender.ELEMENT_TYPE)
|
||||
public class SocketAppender extends AbstractAppender
|
||||
{
|
||||
private static final LinkedList<LogEvent> events = new LinkedList<>();
|
||||
|
||||
/**
|
||||
* Builds FileAppender instances.
|
||||
*
|
||||
* @param <B> The type to build
|
||||
*/
|
||||
public static class Builder<B extends Builder<B>> extends AbstractAppender.Builder<B>
|
||||
implements org.apache.logging.log4j.core.util.Builder<SocketAppender>
|
||||
{
|
||||
@Override
|
||||
public SocketAppender build()
|
||||
{
|
||||
return new SocketAppender(getName(), getFilter(), getOrCreateLayout(), isIgnoreExceptions(), getPropertyArray());
|
||||
}
|
||||
}
|
||||
|
||||
@PluginBuilderFactory
|
||||
public static <B extends Builder<B>> B newBuilder()
|
||||
{
|
||||
return new Builder<B>().asBuilder();
|
||||
}
|
||||
|
||||
public SocketAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout,
|
||||
final boolean ignoreExceptions, final Property[] properties)
|
||||
{
|
||||
super(name, filter, layout, ignoreExceptions, properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs a LogEvent using whatever logic this Appender wishes to use. It is
|
||||
* typically recommended to use a bridge pattern not only for the benefits
|
||||
* from decoupling an Appender from its implementation, but it is also handy
|
||||
* for sharing resources which may require some form of locking.
|
||||
*
|
||||
* @param event The LogEvent.
|
||||
*/
|
||||
@Override
|
||||
public void append(LogEvent event)
|
||||
{
|
||||
synchronized (events)
|
||||
{
|
||||
events.add(event);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if any logs are available.
|
||||
* @return
|
||||
*/
|
||||
public static boolean hasLogs()
|
||||
{
|
||||
synchronized (events)
|
||||
{
|
||||
return !events.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public static List<LogEvent> retrieveLogEvents()
|
||||
{
|
||||
synchronized (events)
|
||||
{
|
||||
List<LogEvent> logs = events.stream().toList();
|
||||
events.clear();
|
||||
return logs;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user