Skip to content

Commit

Permalink
feat: added a new module fxgl-intelligence
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmasB committed Jan 26, 2024
1 parent 9114311 commit cbeefa0
Show file tree
Hide file tree
Showing 5 changed files with 300 additions and 0 deletions.
3 changes: 3 additions & 0 deletions fxgl-intelligence/README.md
Original file line number Diff line number Diff line change
@@ -0,0 1,3 @@
### fxgl-intelligence

This module does not have module-info as some of its dependencies do not provide a proper module-info.java
99 changes: 99 additions & 0 deletions fxgl-intelligence/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 1,99 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>fxgl-framework</artifactId>
<groupId>com.github.almasb</groupId>
<version>21 dev-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>fxgl-intelligence</artifactId>

<dependencies>
<dependency>
<groupId>com.github.almasb</groupId>
<artifactId>fxgl-core</artifactId>
<version>${fxgl.version}</version>
</dependency>

<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>${websocket.version}</version>
</dependency>

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<!-- Compile java -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>

<!-- Compile kotlin -->
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
</plugin>

<!-- Create sources.jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>

<!-- Create javadoc.jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>

<!-- Attach kotlin sources -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
</plugin>

<!-- Generate test coverage reports -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<destFile>${project.build.directory}/coverage-reports/jacoco-unit.exec</destFile>
<dataFile>${project.build.directory}/coverage-reports/jacoco-unit.exec</dataFile>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 1,14 @@
/*
* FXGL - JavaFX Game Library. The MIT License (MIT).
* Copyright (c) AlmasB ([email protected]).
* See LICENSE for details.
*/

package com.almasb.fxgl.intelligence;

/**
* @author Almas Baim (https://github.com/AlmasB)
*/
public class SpeechRecognitionService {

}
Original file line number Diff line number Diff line change
@@ -0,0 1,181 @@
/*
* FXGL - JavaFX Game Library. The MIT License (MIT).
* Copyright (c) AlmasB ([email protected]).
* See LICENSE for details.
*/

package com.almasb.fxgl.ws;

import com.almasb.fxgl.logging.Logger;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.function.Consumer;

/**
* A simple localhost websocket server.
* All methods are internally run in a background thread,
* so they are safe to be called from any thread.
*
* @author Almas Baimagambetov ([email protected])
*/
public final class LocalWebSocketServer extends WebSocketServer {

private static final String POISON_PILL = "62jkasdy732qjkkhs63jASY_HSF";

private final Logger log;

private boolean isLoggingEnabled = true;

private String serverName;
private List<Consumer<String>> messageHandlers = new ArrayList<>();

private SendMessageThread thread;
private WebSocket socket = null;

/**
* Create a server without a name at given port.
*/
public LocalWebSocketServer(int port) {
this("Unnamed", port);
}

/**
* Create a server with the given dev-friendly name at given port.
*/
public LocalWebSocketServer(String serverName, int port) {
super(new InetSocketAddress("localhost", port));
this.serverName = serverName;

log = Logger.get("WSServer " serverName ":" port);
thread = new SendMessageThread(serverName);
}

public boolean isLoggingEnabled() {
return isLoggingEnabled;
}

public void setLoggingEnabled(boolean isLoggingEnabled) {
this.isLoggingEnabled = isLoggingEnabled;
}

/**
* Add a handler for new messages.
*/
public void addMessageHandler(Consumer<String> handler) {
messageHandlers.add(handler);
}

/**
* Remove an existing handler.
*/
public void removeMessageHandler(Consumer<String> handler) {
messageHandlers.remove(handler);
}

/**
* @return last connected socket
*/
public Optional<WebSocket> socket() {
return Optional.ofNullable(socket);
}

/**
* Send a String to the other end of the socket.
*/
public void send(String data) {
if (!isConnected()) {
log.warning("Cannot send <" data "> Socket is not connected");
return;
}

try {
thread.messages.put(data);
} catch (Exception e) {
log.warning("Failed to send data to SendMessageThread", e);
}
}

public boolean isConnected() {
return socket != null;
}

@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
log.info("Opened connection: " conn.getRemoteSocketAddress());

socket = conn;
}

@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
log.info("Closed connection, code: " code);
}

@Override
public void onMessage(WebSocket conn, String message) {
messageHandlers.forEach(h -> h.accept(message));
}

@Override
public void onError(WebSocket conn, Exception ex) {
log.warning("WS error", ex);
}

@Override
public void onStart() {
log.info("Server started successfully");
}

@Override
public void start() {
try {
thread.start();
super.start();
} catch (Exception e) {
log.warning("Failed to start WS server", e);
}
}

@Override
public void stop() {
try {
thread.messages.put(POISON_PILL);
super.stop();
} catch (Exception e) {
log.warning("Failed to stop WS server", e);
}
}

private class SendMessageThread extends Thread {
BlockingQueue<String> messages = new ArrayBlockingQueue<>(10);

SendMessageThread(String name) {
super(name);
}

@Override
public void run() {
while (true) {
try {
var data = messages.take();

if (data.equals(POISON_PILL))
break;

socket.send(data);

} catch (Exception e) {
log.warning("Failed to send data", e);
}
}
}
}
}
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 20,7 @@
<module>fxgl</module>
<module>fxgl-test</module>
<module>fxgl-controllerinput</module>
<module>fxgl-intelligence</module>
<module>fxgl-gameplay</module>
<module>fxgl-samples</module>
<module>fxgl-tools</module>
Expand Down Expand Up @@ -93,6 94,8 @@
<kotlin.version>1.8.10</kotlin.version>
<attach.version>4.0.17</attach.version>
<jackson.version>2.14.2</jackson.version>
<websocket.version>1.5.5</websocket.version>
<selenium.version>4.17.0</selenium.version>

<!-- test dependencies -->
<junit.jupiter.version>5.10.0</junit.jupiter.version>
Expand Down

0 comments on commit cbeefa0

Please sign in to comment.