Commit 2133218c authored by Benedikt Geißler's avatar Benedikt Geißler 🐸

Merge branch 'replace-gson-and-add-enum-and-coresize' into 'master'

Replace gson and add enum and coresize

- Replace com.google.gson library by org.json
- Adjust some content tye (LoginContent etc.)
- Move RcStandard and ClientRole to gone.lib.common

See merge request !7
parents 15ecfcac db8a3642
......@@ -3,7 +3,7 @@ plugins {
id 'net.ltgt.errorprone' version '0.0.8'
}
version = '0.0.4'
version = '0.1.0'
group = 'gone.corewars.library'
apply plugin: 'java'
......@@ -21,7 +21,8 @@ repositories {
jcenter()
}
dependencies {
compile 'com.google.code.gson:gson:2.+'
compile 'org.json:json:20160810'
compile 'org.everit.json:org.everit.json.schema:1.+'
testCompile 'junit:junit:4.+'
}
......
package gone.lib.common;
/**
* Created by winfr on 27.09.2016.
*/
public enum ClientRole {
Player,
Viewer
}
\ No newline at end of file
package gone.lib.network.tcp;
package gone.lib.common;
/**
* Created by Winfried on 23.09.2016.
*/
public class TcpConstants {
public final class NetworkConstants {
public static final String STARTINGLINE = "--..--";
public static final String FINISHINGLINE = "..--..";
public static final String REDCODESEPARATOR = "§";
}
package gone.lib.common;
/**
* Created by matthias on 22.06.16.
*/
public enum RcStandard {
ICWS88,
ICWS94
}
......@@ -45,6 +45,11 @@ public class CwClient implements IClient, ITcpListener {
client.sendMessage(serializer.serializeReadyMessage(telegram));
}
@Override
public IClientCallback getCallback() {
return callback;
}
@Override
public void receivedMessage(String sessionId, String data) {
CwMessageType type = interpreter.getMessageType(data);
......
......@@ -14,4 +14,6 @@ public interface IClient {
void sendLoginTelegram(CwLoginContent telegram);
void sendReadyTelegram(CwReadyContent telegram);
IClientCallback getCallback();
}
......@@ -9,5 +9,5 @@ public class CwGameStatusContent {
public int oldIpIndex;
public int newIpIndex;
public String executingPlayerName;
public boolean playerOneActive;
}
\ No newline at end of file
package gone.lib.network.json;
import gone.lib.common.ClientRole;
/**
* Created by winfr on 23.08.2016.
*/
public class CwLoginContent {
public String name;
public String role;
public ClientRole role;
}
package gone.lib.network.json;
import gone.lib.common.RcStandard;
/**
* Created by winfr on 27.09.2016.
*/
public class CwLoginOkContent {
boolean clientMustWait;
public boolean clientMustWait;
public RcStandard standard;
public int coreSize;
public int lineLength;
}
package gone.lib.network.serialization;
import com.google.gson.Gson;
import gone.lib.common.ClientRole;
import gone.lib.common.NetworkConstants;
import gone.lib.common.RcStandard;
import gone.lib.network.json.CwGameStatusContent;
import gone.lib.network.json.CwGameStatusMessage;
import gone.lib.network.json.CwLoginContent;
......@@ -14,20 +15,33 @@ import gone.lib.network.json.CwReadyMessage;
import gone.lib.network.json.CwStartContent;
import gone.lib.network.json.CwStartMessage;
import org.everit.json.schema.Schema;
import org.everit.json.schema.ValidationException;
import org.everit.json.schema.loader.SchemaLoader;
import org.json.*;
import org.everit.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
/**
* Created by winfr on 23.08.2016.
*/
public class IncomingMessagesInterpreter {
public CwMessageType getMessageType(String data) {
if (data.contains("login")) {
JSONObject object = new JSONObject(data);
if (object.getString("packageType").equals("login")) {
return CwMessageType.Login;
} else if (data.contains("start")) {
} else if (object.getString("packageType").equals("start")) {
return CwMessageType.Start;
} else if (data.contains("loginOk")) {
} else if (object.getString("packageType").equals("loginOk")) {
return CwMessageType.LoginOk;
} else if (data.contains("ready")) {
} else if (object.getString("packageType").equals("ready")) {
return CwMessageType.Ready;
} else if (data.contains("status")) {
} else if (object.getString("packageType").equals("status")) {
return CwMessageType.GameStatus;
}
......@@ -35,37 +49,93 @@ public class IncomingMessagesInterpreter {
}
public CwLoginContent parseLoginMessage(String json) {
Gson gson = new Gson();
CwLoginMessage obj = gson.fromJson(json, CwLoginMessage.class);
JSONObject object = new JSONObject(json);
validate(object, "loginSchema.json");
CwLoginContent result = new CwLoginContent();
result.name = object.getJSONObject("content").getString("name");
return obj.content;
if(object.getJSONObject("content").getString("role").equals("Player")) {
result.role = ClientRole.Player;
}
else if(object.getJSONObject("content").getString("standard").equals("Viewer")) {
result.role = ClientRole.Viewer;
}
return result;
}
public CwStartContent parseStartMessage(String json) {
Gson gson = new Gson();
CwStartMessage obj = gson.fromJson(json, CwStartMessage.class);
JSONObject object = new JSONObject(json);
validate(object, "startSchema.json");
CwStartContent result = new CwStartContent();
result.fullState = object.getJSONObject("content").getString("fullState");
result.players = object.getJSONObject("content").getInt("players");
return obj.content;
return result;
}
public CwLoginOkContent parseLoginOkMessage(String json) {
Gson gson = new Gson();
CwLoginOkMessage obj = gson.fromJson(json, CwLoginOkMessage.class);
JSONObject object = new JSONObject(json);
validate(object, "loginOkSchema.json");
CwLoginOkContent result = new CwLoginOkContent();
result.clientMustWait = object.getJSONObject("content").getBoolean("clientMustWait");
result.coreSize = object.getJSONObject("content").getInt("coreSize");
result.lineLength = object.getJSONObject("content").getInt("lineLength");
if(object.getJSONObject("content").getString("standard").equals("ICWS88")) {
result.standard = RcStandard.ICWS88;
}
else if(object.getJSONObject("content").getString("standard").equals("ICWS94")) {
result.standard = RcStandard.ICWS94;
}
return obj.content;
return result;
}
public CwReadyContent parseReadyMessage(String json) {
Gson gson = new Gson();
CwReadyMessage obj = gson.fromJson(json, CwReadyMessage.class);
JSONObject object = new JSONObject(json);
validate(object, "readySchema.json");
CwReadyContent result = new CwReadyContent();
String[] redcodeStatements = object.getJSONObject("content").
getString("warriorRedcode").split(NetworkConstants.REDCODESEPARATOR);
List<String> redcodeStatementList = new LinkedList<>();
for (String redcodeStatement : redcodeStatements) {
redcodeStatementList.add(redcodeStatement);
}
return obj.content;
result.warriorRedcode = redcodeStatementList;
return result;
}
public CwGameStatusContent parseGameStatusMessage(String json) {
Gson gson = new Gson();
CwGameStatusMessage obj = gson.fromJson(json, CwGameStatusMessage.class);
JSONObject object = new JSONObject(json);
validate(object, "gameStatusSchema.json");
CwGameStatusContent result = new CwGameStatusContent();
result.playerOneActive = object.getJSONObject("content").getBoolean("playerOneActive");
result.indexOfChangedCoreElement = object.getJSONObject("content").getInt("indexOfChangedCoreElement");
result.newIpIndex = object.getJSONObject("content").getInt("newIpIndex");
result.newOpCode = object.getJSONObject("content").getString("newOpCode");
result.oldIpIndex = object.getJSONObject("content").getInt("oldIpIndex");
return obj.content;
return result;
}
private void validate(JSONObject object, String name)
{
try (InputStream inputStream = getClass().getResourceAsStream(name)) {
JSONObject rawSchema = new JSONObject(new JSONTokener(inputStream));
Schema schema = SchemaLoader.load(rawSchema);
schema.validate(object);
} catch (IOException exc) {
exc.printStackTrace();
}
}
}
\ No newline at end of file
package gone.lib.network.serialization;
import com.google.gson.Gson;
import gone.lib.common.NetworkConstants;
import gone.lib.network.json.CwGameStatusContent;
import gone.lib.network.json.CwGameStatusMessage;
import gone.lib.network.json.CwLoginContent;
......@@ -13,6 +12,8 @@ import gone.lib.network.json.CwReadyMessage;
import gone.lib.network.json.CwStartContent;
import gone.lib.network.json.CwStartMessage;
import org.json.JSONStringer;
/**
* Created by winfr on 26.08.2016.
*/
......@@ -25,67 +26,77 @@ public class OutgoingMessagesSerializer {
}
public String serializeGameStatusMessage(CwGameStatusContent content) {
CwGameStatusMessage obj = new CwGameStatusMessage();
obj.content = content;
obj.version = packetVersion;
obj.packageType = "status";
Gson gson = new Gson();
String json = gson.toJson(obj);
return json;
String result = new JSONStringer()
.object()
.key("content")
.object()
.key("indexOfChangedCoreElement").value(content.indexOfChangedCoreElement)
.key("newOpCode").value(content.newOpCode)
.key("oldIpIndex").value(content.oldIpIndex)
.key("newIpIndex").value(content.newIpIndex)
.key("playerOneActive").value(content.playerOneActive)
.endObject()
.key("version").value(packetVersion)
.key("packageType").value("status")
.endObject().toString();
return result;
}
public String serializeLoginMessage(CwLoginContent content) {
CwLoginMessage obj = new CwLoginMessage();
obj.content = content;
obj.version = packetVersion;
obj.packageType = "login";
Gson gson = new Gson();
String json = gson.toJson(obj);
return json;
String result = new JSONStringer()
.object()
.key("content")
.object()
.key("name").value(content.name)
.key("role").value(content.role.toString())
.endObject()
.key("version").value(packetVersion)
.key("packageType").value("login")
.endObject().toString();
return result;
}
public String serializeReadyMessage(CwReadyContent content) {
CwReadyMessage obj = new CwReadyMessage();
obj.content = content;
obj.version = packetVersion;
obj.packageType = "ready";
Gson gson = new Gson();
String json = gson.toJson(obj);
return json;
String result = new JSONStringer()
.object()
.key("content")
.object()
.key("warriorRedcode").value(String.join(
NetworkConstants.REDCODESEPARATOR, content.warriorRedcode))
.endObject()
.key("version").value(packetVersion)
.key("packageType").value("ready")
.endObject().toString();
return result;
}
public String serializeLoginOkMessage(CwLoginOkContent content) {
CwLoginOkMessage obj = new CwLoginOkMessage();
obj.content = content;
obj.version = packetVersion;
obj.packageType = "loginOk";
Gson gson = new Gson();
String json = gson.toJson(obj);
return json;
String result = new JSONStringer()
.object()
.key("content")
.object()
.key("clientMustWait").value(content.clientMustWait)
.key("coreSize").value(content.coreSize)
.key("lineLength").value(content.lineLength)
.key("standard").value(content.standard.toString())
.endObject()
.key("version").value(packetVersion)
.key("packageType").value("loginOk")
.endObject().toString();
return result;
}
public String serializeStartMessage(CwStartContent content) {
CwStartMessage obj = new CwStartMessage();
obj.content = content;
obj.version = packetVersion;
obj.packageType = "start";
Gson gson = new Gson();
String json = gson.toJson(obj);
return json;
String result = new JSONStringer()
.object()
.key("content")
.object()
.key("fullState").value(content.fullState)
.key("players").value(content.players)
.endObject()
.key("version").value(packetVersion)
.key("packageType").value("start")
.endObject().toString();
return result;
}
}
\ No newline at end of file
package gone.lib.network.tcp;
import gone.lib.common.NetworkConstants;
import gone.lib.network.contracts.ITcpClientModule;
import gone.lib.network.contracts.ITcpListener;
......@@ -43,8 +44,8 @@ public class TcpClientModule implements ITcpClientModule, ITcpListener {
@Override
public void sendMessage(String data) {
try {
outToServer.writeBytes(TcpConstants.STARTINGLINE + "\n" + data
+ "\n" + TcpConstants.FINISHINGLINE + "\n");
outToServer.writeBytes(NetworkConstants.STARTINGLINE + "\n" + data
+ "\n" + NetworkConstants.FINISHINGLINE + "\n");
} catch (IOException exception) {
exception.printStackTrace();
}
......
package gone.lib.network.tcp;
import gone.lib.common.NetworkConstants;
import gone.lib.network.contracts.ITcpListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
......@@ -37,18 +37,18 @@ public class TcpReceiveMessage implements Runnable {
do {
line = this.reader.readLine();
if (!line.equals(TcpConstants.FINISHINGLINE)) {
if (!line.equals(NetworkConstants.FINISHINGLINE)) {
clientData.append(line);
}
}
while (!line.equals(TcpConstants.FINISHINGLINE));
while (!line.equals(NetworkConstants.FINISHINGLINE));
for (ITcpListener listener : listeners) {
listener.receivedMessage(this.sessionId, clientData.toString());
}
}
} catch (Exception exception) {
exception.printStackTrace();
//exception.printStackTrace();
}
}
}
......@@ -53,7 +53,7 @@ public class TcpServerAccept implements Runnable {
this.accepted.acceptedConnection(sessionId);
}
} catch (Exception exception) {
exception.printStackTrace();
//exception.printStackTrace();
}
}
......
package gone.lib.network.tcp;
import gone.lib.common.NetworkConstants;
import gone.lib.network.contracts.ITcpListener;
import gone.lib.network.contracts.ITcpServerAccepted;
import gone.lib.network.contracts.ITcpServerModule;
......@@ -39,8 +40,8 @@ public class TcpServerModule implements ITcpServerModule, ITcpServerAccepted, IT
TcpClientConnection connection = tcpServerAccept.clientConnections.get(sessionId);
try {
connection.output.writeBytes(TcpConstants.STARTINGLINE
+ "\n" + data + TcpConstants.FINISHINGLINE + "\n");
connection.output.writeBytes(NetworkConstants.STARTINGLINE
+ "\n" + data + NetworkConstants.FINISHINGLINE + "\n");
} catch (IOException exception) {
exception.printStackTrace();
}
......
{
"$schema": "http://json-schema.org/draft-04/schema#",
"packagetype": "must be loginOk",
"version":
{
"description": "version of standard",
"type": "integer"
},
"content":
{
"type": "object",
"properties":
{
"indexOfChangedCoreElement": {"type": "integer"},
"newOpCode": {"type": "string"},
"oldIpIndex": {"type": "integer"},
"newIpIndex": {"type": "integer"},
"playerOneActive": {"type": "boolean"}
},
"required": ["indexOfChangedCoreElement", "newOpCode", "oldIpIndex", "newIpIndex", "playerOneActive"]
}
}
\ No newline at end of file
{
"$schema": "http://json-schema.org/draft-04/schema#",
"packagetype": "package type",
"version":
{
"description": "version of standard",
"type": "integer"
},
"content":
{
}
}
\ No newline at end of file
{
"$schema": "http://json-schema.org/draft-04/schema#",
"packagetype": "must be loginOk",
"version":
{
"description": "version of standard",
"type": "integer"
},
"content":
{
"type": "object",
"properties":
{
"standard": {"type": "string", "description": "programming code standard"},
"coreSize": {"type": "integer"},
"lineLength": {"type": "integer"},
"clientMustWait": {"type": "boolean"}
},
"required": ["standard", "coreSize", "lineLength", "clientMustWait"]
}
}
\ No newline at end of file
{
"$schema": "http://json-schema.org/draft-04/schema#",
"packagetype": "must be login",
"version":
{
"description": "version of standard",
"type": "integer"
},
"content":
{
"type": "object",
"properties":
{
"name": {"type": "string"},
"role": {"type": "string"}
},
"required": ["name", "role"]
}
}
\ No newline at end of file
{
"$schema": "http://json-schema.org/draft-04/schema#",
"packagetype": "must be ready",
"version":
{
"description": "version of standard",
"type": "integer"
},
"content":
{
"type": "object",
"properties":
{
"redcode": {"type": "string"}
},
"required": ["redcode"]
}
}
\ No newline at end of file
{
"$schema": "http://json-schema.org/draft-04/schema#",
"packagetype": "must be start",
"version":
{
"description": "version of standard",
"type": "integer"
},<