-
-
Notifications
You must be signed in to change notification settings - Fork 558
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added a new module fxgl-intelligence
- Loading branch information
Showing
5 changed files
with
300 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
14 changes: 14 additions & 0 deletions
14
fxgl-intelligence/src/main/java/com/almasb/fxgl/intelligence/SpeechRecognitionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
} |
181 changes: 181 additions & 0 deletions
181
fxgl-intelligence/src/main/java/com/almasb/fxgl/ws/LocalWebSocketServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters