Added better logging and browser support.
We still need to work on the Linux browser a bit.
This commit is contained in:
@@ -29,6 +29,8 @@ dependencies {
|
||||
implementation 'org.apache.logging.log4j:log4j-core:2.13.3'
|
||||
implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.13.3'
|
||||
|
||||
implementation 'com.google.code.gson:gson:2.8.7'
|
||||
|
||||
// Declare the dependency for your favourite test framework you want to use in your tests.
|
||||
// TestNG is also supported by the Gradle Test task. Just change the
|
||||
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
|
||||
@@ -40,12 +42,14 @@ abstract class BuildManifest extends DefaultTask {
|
||||
@InputDirectory
|
||||
final abstract DirectoryProperty inputFile = project.objects.directoryProperty()
|
||||
@OutputFile
|
||||
final abstract RegularFileProperty outputFile = project.objects.fileProperty().convention(project.layout.buildDirectory.file("manifest.json"))
|
||||
final abstract RegularFileProperty outputFile = project.objects.fileProperty().convention(project.layout.buildDirectory.file("universalmusic.json"))
|
||||
|
||||
@TaskAction
|
||||
void join() {
|
||||
File startupScript
|
||||
Stream<File> files = Arrays.stream(inputFile.get().asFile.listFiles())
|
||||
File binFiles = new File(inputFile.get().asFile, "bin")
|
||||
print binFiles
|
||||
Stream<File> files = Arrays.stream(binFiles.listFiles())
|
||||
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
|
||||
startupScript = files.filter(file -> file.getName().endsWith(".bat")).findFirst().get()
|
||||
}
|
||||
@@ -58,7 +62,7 @@ abstract class BuildManifest extends DefaultTask {
|
||||
outputFile.get().asFile.text = "{\n" +
|
||||
" \"name\": \"universalmusic\",\n" +
|
||||
" \"description\": \"A link between the Universal Music Player and the web browser.\",\n" +
|
||||
" \"path\": \"" + startupScript.getAbsolutePath() + "\",\n" +
|
||||
" \"path\": \"" + startupScript.getAbsolutePath().replace("\\", "\\\\") + "\",\n" +
|
||||
" \"type\": \"stdio\",\n" +
|
||||
" \"allowed_extensions\": [\"universalmusic@regis.edu\"]\n" +
|
||||
"}"
|
||||
@@ -66,50 +70,25 @@ abstract class BuildManifest extends DefaultTask {
|
||||
}
|
||||
|
||||
TaskProvider<BuildManifest> buildManifestP = tasks.register("buildManifest", BuildManifest) {
|
||||
inputFile = tasks.named("startScripts", org.gradle.jvm.application.tasks.CreateStartScripts).get().outputDir
|
||||
inputFile = tasks.named("installDist", Sync).get().destinationDir
|
||||
}
|
||||
|
||||
buildManifest.dependsOn(startScripts)
|
||||
buildManifest.dependsOn(installDist)
|
||||
|
||||
abstract class InstallAddon extends DefaultTask {
|
||||
@InputFile
|
||||
final abstract RegularFileProperty inputFile = project.objects.fileProperty()
|
||||
@Internal
|
||||
final abstract DirectoryProperty build = project.layout.buildDirectory
|
||||
@InputDirectory
|
||||
final abstract DirectoryProperty libsDir = project.objects.directoryProperty()
|
||||
|
||||
@TaskAction
|
||||
void join() {
|
||||
Path lib = libsDir.get().asFile.toPath()
|
||||
Path link = new File(build.get().asFile, "lib").toPath()
|
||||
try {
|
||||
// This takes up more space than I would like, but Windows doesn't allow symbolic links without superuser privliges
|
||||
Files.walk(lib).forEach(file -> {
|
||||
Path fileName = lib.relativize(file)
|
||||
Path dest = link.resolve(fileName)
|
||||
println fileName.toString() + ": " + file.toString() + " to " + dest.toString()
|
||||
if (!Files.exists(dest)) {
|
||||
try {
|
||||
Files.copy(file, dest)
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new RuntimeException("Could not link " + file.toString() + " to " + dest.toString(), e)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new RuntimeException("Could not link " + lib.toString() + " to " + link.toString(), e)
|
||||
}
|
||||
|
||||
|
||||
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
|
||||
Runtime.getRuntime()
|
||||
.exec("REG ADD HKEY_CURRENT_USER\\SOFTWARE\\Mozilla\\NativeMessagingHosts /v universalmusic /d \""
|
||||
+ inputFile.get().asFile.getAbsolutePath() + "\" ")
|
||||
Process p = Runtime.getRuntime()
|
||||
.exec("REG DELETE HKEY_CURRENT_USER\\SOFTWARE\\Mozilla\\NativeMessagingHosts\\universalmusic /f ")
|
||||
p.waitFor()
|
||||
p = Runtime.getRuntime()
|
||||
.exec("REG ADD HKEY_CURRENT_USER\\SOFTWARE\\Mozilla\\NativeMessagingHosts\\universalmusic /ve /d \""
|
||||
+ inputFile.get().asFile.getAbsolutePath() + "\" /f ")
|
||||
p.waitFor()
|
||||
}
|
||||
else if (Os.isFamily(Os.FAMILY_MAC)) {
|
||||
Files.copy(inputFile.get().asFile.toPath(), Paths.get(System.getProperty("user.home"), "Library/Application Support/Mozilla/NativeMessagingHosts", inputFile.get().asFile.getName()))
|
||||
@@ -122,7 +101,6 @@ abstract class InstallAddon extends DefaultTask {
|
||||
|
||||
TaskProvider<InstallAddon> installAddonP = tasks.register("installAddon", InstallAddon) {
|
||||
inputFile = buildManifestP.get().outputFile
|
||||
libsDir = jar.destinationDirectory
|
||||
}
|
||||
|
||||
installAddon.dependsOn(buildManifest)
|
||||
|
||||
@@ -4,33 +4,181 @@
|
||||
|
||||
package edu.regis.universeplayer.addon;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class Main
|
||||
{
|
||||
private static Logger logger = LoggerFactory.getLogger(Main.class);
|
||||
|
||||
private static final Gson gson = new Gson();
|
||||
|
||||
private static BufferedInputStream browserIn;
|
||||
private static BufferedOutputStream browserOut;
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
boolean running = true;
|
||||
logger.info("This is an INFO level log message!");
|
||||
logger.error("This is an ERROR level log message!");
|
||||
logger.warn("This could be problimatic");
|
||||
logger.info("https://sematext.com/blog/log4j2-tutorial/#toc-log4j-2-configuration-1");
|
||||
while (running)
|
||||
int rawLength, messageLength;
|
||||
byte[] header = new byte[4];
|
||||
byte[] message;
|
||||
JsonElement messageJson;
|
||||
JsonPrimitive jsonPrimative;
|
||||
JsonObject jsonObject;
|
||||
ByteBuffer headerReader;
|
||||
|
||||
System.err.printf("Working from directory %s\n", System.getProperty("user.dir"));
|
||||
if (ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN))
|
||||
{
|
||||
logger.info("Incoming messages will be Big-endian");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.info("Incoming messages will be Little-endian");
|
||||
}
|
||||
try
|
||||
{
|
||||
browserIn = new BufferedInputStream(System.in);
|
||||
browserOut = new BufferedOutputStream(System.out);
|
||||
while (running)
|
||||
{
|
||||
try
|
||||
{
|
||||
rawLength = browserIn.read(header);
|
||||
|
||||
if (rawLength <= 0)
|
||||
{
|
||||
running = false;
|
||||
break;
|
||||
}
|
||||
|
||||
headerReader = ByteBuffer.wrap(header, 0, rawLength);
|
||||
/*
|
||||
* The browser outputs in whatever the native endian order,
|
||||
* which may not be what Java does.
|
||||
*/
|
||||
headerReader.order(ByteOrder.nativeOrder());
|
||||
messageLength = headerReader.getInt();
|
||||
logger.debug("Receiving message of {} length", messageLength);
|
||||
message = new byte[messageLength];
|
||||
browserIn.read(message);
|
||||
logger.debug("Unpacking message {} (length {})", new String(message, "UTF-8"), messageLength);
|
||||
|
||||
messageJson = gson.fromJson(new String(message, StandardCharsets.UTF_8), JsonElement.class);
|
||||
logger.debug("Reading JSON element {} (type: {})", messageJson, messageJson.getClass());
|
||||
|
||||
if (messageJson.isJsonPrimitive())
|
||||
{
|
||||
jsonPrimative = ((JsonPrimitive) messageJson);
|
||||
if (jsonPrimative.isBoolean())
|
||||
{
|
||||
handleMessage(jsonPrimative.getAsBoolean());
|
||||
}
|
||||
else if (jsonPrimative.isNumber())
|
||||
{
|
||||
handleMessage(jsonPrimative.getAsNumber());
|
||||
}
|
||||
else if (jsonPrimative.isString())
|
||||
{
|
||||
handleMessage(jsonPrimative.getAsString());
|
||||
}
|
||||
}
|
||||
else if (messageJson.isJsonObject())
|
||||
{
|
||||
jsonObject = ((JsonObject) messageJson);
|
||||
// TODO - Parse the object into Java objects
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
logger.error("Could not create input stream", e);
|
||||
// Forward it to the browser via STDERR
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
logger.info("Waiting1");
|
||||
logger.debug("Waiting");
|
||||
try
|
||||
{
|
||||
Thread.sleep(500);
|
||||
browserIn.close();
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
catch (IOException e1)
|
||||
{
|
||||
logger.trace("Something interrupted us", e);
|
||||
running = false;
|
||||
logger.error("Could not close browser input", e1);
|
||||
}
|
||||
try
|
||||
{
|
||||
browserOut.close();
|
||||
}
|
||||
catch (IOException e1)
|
||||
{
|
||||
logger.error("Could not close browser output", e1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A callback for when a message is sent from the browser
|
||||
*
|
||||
* @param message - The message sent.
|
||||
*/
|
||||
private static void handleMessage(Object message)
|
||||
{
|
||||
if (message instanceof String)
|
||||
{
|
||||
if (message.equals("ping"))
|
||||
{
|
||||
sendMessage("pong");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A callback for when a message is sent from the browser
|
||||
*
|
||||
* @param message - The message sent.
|
||||
*/
|
||||
public static void sendMessage(Object message)
|
||||
{
|
||||
String messageJson;
|
||||
byte[] messageData;
|
||||
ByteBuffer headerWriter;
|
||||
|
||||
logger.debug("Sending message {}", message);
|
||||
|
||||
messageJson = gson.toJson(message);
|
||||
messageData = messageJson.getBytes(StandardCharsets.UTF_8);
|
||||
headerWriter = ByteBuffer.allocate(4);
|
||||
/*
|
||||
* The browser outputs in whatever the native endian order,
|
||||
* which may not be what Java does.
|
||||
*/
|
||||
headerWriter.order(ByteOrder.nativeOrder());
|
||||
headerWriter.putInt(messageData.length);
|
||||
logger.debug("Writing message {} {}", headerWriter.array(), messageJson);
|
||||
try
|
||||
{
|
||||
browserOut.write(headerWriter.array());
|
||||
browserOut.write(messageData);
|
||||
browserOut.flush();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
logger.error("Could not write message", e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user