Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ See also the list of [contributors](https://github.com/neuralm/Neuralm-Java-Clie

## License

This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details
4 changes: 2 additions & 2 deletions src/main/java/net/neuralm/client/NeuralmClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public class NeuralmClient {
private final int port;
private final ISerializer serializer;
private final ReadHandler readHandler;
AtomicBoolean isWriting = new AtomicBoolean(false);
final AtomicBoolean isWriting = new AtomicBoolean(false);
private AsynchronousTlsChannel channel;
private ByteBuffer writeBuffer = ByteBuffer.allocate(0);
private Queue<Message> sendQueue = new LinkedBlockingDeque<>();
private final Queue<Message> sendQueue = new LinkedBlockingDeque<>();


/**
Expand Down
30 changes: 19 additions & 11 deletions src/main/java/net/neuralm/client/ReadHandler.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package net.neuralm.client;

import java.nio.ByteBuffer;
import java.nio.channels.CompletionHandler;
import net.neuralm.client.messages.Message;
import net.neuralm.client.messages.MessageHeader;
import net.neuralm.client.messages.responses.Response;
import net.neuralm.client.messages.serializer.ISerializer;

import java.nio.ByteBuffer;
import java.nio.channels.CompletionHandler;

public class ReadHandler implements CompletionHandler<Integer, ByteBuffer> {

private final NeuralmClient client;

MessageHeader currentHeader;
ISerializer serializer;
final ISerializer serializer;

ByteBuffer currentBody;

public ReadHandler(NeuralmClient client, ISerializer serializer) {
this.client = client;
Expand All @@ -36,19 +39,24 @@ public void completed(Integer result, ByteBuffer attachment) {
}

client.startReading(currentHeader.getBodySize());
currentBody = ByteBuffer.allocate(currentHeader.getBodySize());
} else {
currentBody.put(bytes);

client.startReading();

Object response = Message.deconstructMessageBody(serializer, currentHeader, bytes);
if (response instanceof Response) {
client.addResponse((Response) response);
} else {
System.err.println(String.format("Received response that wasn't a response! Bytes: %s", bytes));
}
if (currentBody.position() == currentBody.capacity()) {
Object response = Message.deconstructMessageBody(serializer, currentHeader, currentBody.array());

currentHeader = null;
}
if (response instanceof Response) {
client.addResponse((Response) response);
} else {
System.err.println(String.format("Received response that wasn't a response! Bytes: %s", currentBody.array()));
}

currentHeader = null;
}
}
}

@Override
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/net/neuralm/client/entities/TrainingSession.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package net.neuralm.client.entities;

import java.util.UUID;
import net.neuralm.client.neat.TrainingRoom;

import java.util.UUID;

public class TrainingSession {

public UUID Id;
public String StartedTimestamp;
public String EndedTimestamp; //TODO: Make this an actual date object if possible, or maybe unix time
public UUID UserId;
public UUID id;
public String startedTimestamp;
public String endedTimestamp; //TODO: Make this an actual date object if possible, or maybe unix time
public UUID userId;
public TrainingRoom trainingRoom;

}
18 changes: 17 additions & 1 deletion src/main/java/net/neuralm/client/messages/Message.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package net.neuralm.client.messages;

import net.neuralm.client.messages.serializer.ISerializer;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import net.neuralm.client.messages.serializer.ISerializer;

public class Message {

Expand All @@ -15,6 +16,13 @@ private Message(byte[] headerBytes, byte[] bodyBytes) {
this.bodyBytes = bodyBytes;
}

/**
* Construct a message object from the given object with the given serializer.
* It will be turned into bytes and a header will be created.
* @param serializer The serializer to use.
* @param message The object that should be turned into a message
* @return The message with the headerBytes and bodyBytes.
*/
public static Message constructMessage(ISerializer serializer, Object message) {
byte[] bodyBytes = serializer.serialize(message);
byte[] messageTypeBytes = message.getClass().getSimpleName().getBytes(StandardCharsets.UTF_8);
Expand All @@ -25,6 +33,14 @@ public static Message constructMessage(ISerializer serializer, Object message) {
return new Message(headerBytes, bodyBytes);
}

/**
* Turn a byte array representing an object into that object.
* It will deserialize the bytes using the given serializer.
* @param serializer The serializer to use.
* @param header The header corresponding to the data being deserialized.
* @param bodyBytes The data to deserialize
* @return null if the serializer fails or no class corresponding to {@link MessageHeader#getTypeName()} is found.
*/
public static Object deconstructMessageBody(ISerializer serializer, MessageHeader header, byte[] bodyBytes) {
Class<?> clazz;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class MessageHeader {

private final int bodySize;
private String typeName;
private final String typeName;

private MessageHeader(int bodySize, String typeName) {
this.bodySize = bodySize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

public class GetOrganismsRequest extends Request {

public UUID trainingSessionID;
public final UUID trainingSessionID;

public int amount;
public final int amount;

public GetOrganismsRequest(UUID trainingSessionID, int amount) {
this.trainingSessionID = trainingSessionID;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package net.neuralm.client.messages.requests;

import net.neuralm.client.neat.Organism;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import net.neuralm.client.neat.Organism;

public class PostOrganismsScoreRequest extends Request {

public UUID trainingSessionId;
public final UUID trainingSessionId;

public Map<UUID, Double> organismScores;
public final Map<UUID, Double> organismScores;

/**
* Create a PostOrganismsScoreRequest to send the organisms' score to the server
Expand All @@ -33,7 +34,7 @@ public PostOrganismsScoreRequest(UUID trainingSessionId, List<Organism> organism
this(trainingSessionId, new HashMap<>(organisms.size()));

for (Organism organism : organisms) {
organismScores.put(organism.id, organism.score);
organismScores.put(organism.getId(), organism.getScore());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Request {
public Request() {
id = UUID.randomUUID();
ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC);
//noinspection SpellCheckingInspection
date = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'.'SSSSSSS'Z'"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public UUID getRequestId() {
* The time date this response was send.
* @return The date as a string in the following format: "yyyy-MM-dd'T'HH:mm:ss'.'SSSSSSS'Z'"
*/
@SuppressWarnings("SpellCheckingInspection")
public String getDateTime() {
return dateTime;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;

import java.nio.charset.StandardCharsets;

public class JsonSerializer implements ISerializer {
Expand All @@ -13,17 +15,31 @@ public JsonSerializer() {
this.gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
}

/**
* {@inheritDoc}
*/
@Override
public byte[] serialize(Object message) {
String json = gson.toJson(message);

return json.getBytes(StandardCharsets.UTF_8);
}

/**
* {@inheritDoc}
*
* @return The deserialized object, or null if a {@link JsonSyntaxException} occurred.
*/
@Override
public <T> T deserialize(byte[] bytes, Class<T> type) {
String json = new String(bytes, StandardCharsets.UTF_8);

return gson.fromJson(json, type);
try {
return gson.fromJson(json, type);
} catch (JsonSyntaxException exception) {
System.err.println(json);
exception.printStackTrace();
return null;
}
}
}
119 changes: 0 additions & 119 deletions src/main/java/net/neuralm/client/neat/Brain.java

This file was deleted.

Loading