Commit f27d8f82 authored by Benedikt Geißler's avatar Benedikt Geißler 🐸

Merge branch 'bugFixSerialization' into 'master'

fixed Serialization bug, also tested this

important changes, some Messages were not serialized correctly, switched to UTF-8 serialization

See merge request !15
parents 54041cc4 8ca0e41f
......@@ -3,7 +3,7 @@ plugins {
id 'net.ltgt.errorprone' version '0.0.8'
}
version = '0.2.4'
version = '0.2.5'
group = 'gone.corewars.library'
apply plugin: 'java'
......
package gone.lib.network.tcp;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;
......@@ -13,7 +14,7 @@ public class TcpClientConnection {
/**
* Buffered reader.
*/
public BufferedReader reader;
public DataInputStream input;
/**
* Buffered writer.
......
......@@ -4,12 +4,10 @@ import gone.lib.common.NetworkConstants;
import gone.lib.network.contracts.ITcpClientModule;
import gone.lib.network.contracts.ITcpListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.LinkedList;
import java.util.List;
......@@ -18,7 +16,7 @@ import java.util.List;
*/
public class TcpClientModule implements ITcpClientModule, ITcpListener {
private DataOutputStream outToServer;
private BufferedReader inFromServer;
private DataInputStream inFromServer;
private Socket clientSocket;
private List<ITcpListener> listeners = new LinkedList<>();
......@@ -28,8 +26,7 @@ public class TcpClientModule implements ITcpClientModule, ITcpListener {
clientSocket = new Socket(address, port);
outToServer = new DataOutputStream(clientSocket.getOutputStream());
inFromServer = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream(), Charset.defaultCharset()));
inFromServer = new DataInputStream(clientSocket.getInputStream());
TcpReceiveMessage receiveMessage = new TcpReceiveMessage(inFromServer, "server", false);
receiveMessage.addListener(this);
......@@ -44,8 +41,7 @@ public class TcpClientModule implements ITcpClientModule, ITcpListener {
@Override
public void sendMessage(String data) {
try {
outToServer.writeBytes(NetworkConstants.STARTINGLINE + "\n" + data
+ "\n" + NetworkConstants.FINISHINGLINE + "\n");
outToServer.writeUTF(data);
} catch (IOException exception) {
exception.printStackTrace();
}
......
......@@ -4,6 +4,7 @@ import gone.lib.common.NetworkConstants;
import gone.lib.network.contracts.ITcpListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.util.LinkedList;
import java.util.List;
......@@ -11,13 +12,13 @@ import java.util.List;
* Created by Winfried on 23.09.2016.
*/
public class TcpReceiveMessage implements Runnable {
private BufferedReader reader;
private DataInputStream input;
private boolean waitOnce;
private String sessionId;
private List<ITcpListener> listeners = new LinkedList<>();
public TcpReceiveMessage(BufferedReader reader, String sessionId, boolean waitOnce) {
this.reader = reader;
public TcpReceiveMessage(DataInputStream input, String sessionId, boolean waitOnce) {
this.input = input;
this.sessionId = sessionId;
this.waitOnce = waitOnce;
}
......@@ -30,21 +31,10 @@ public class TcpReceiveMessage implements Runnable {
public void run() {
try {
while (!waitOnce) {
StringBuilder clientData = new StringBuilder();
String line;
this.reader.readLine();
do {
line = this.reader.readLine();
if (!line.equals(NetworkConstants.FINISHINGLINE)) {
clientData.append(line);
}
}
while (!line.equals(NetworkConstants.FINISHINGLINE));
String data = this.input.readUTF();
for (ITcpListener listener : listeners) {
listener.receiveMessage(this.sessionId, clientData.toString());
listener.receiveMessage(this.sessionId, data);
}
}
} catch (Exception exception) {
......
package gone.lib.network.tcp;
import com.sun.corba.se.spi.activation.Server;
import gone.lib.network.contracts.ITcpServerAccepted;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
......@@ -39,14 +36,13 @@ public class TcpServerAccept implements Runnable {
// accept incoming clients
while (true) {
Socket clientSocket = welcomeSocket.accept();
BufferedReader reader = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream(), Charset.defaultCharset()));
DataInputStream input = new DataInputStream(clientSocket.getInputStream());
DataOutputStream output = new DataOutputStream(clientSocket.getOutputStream());
TcpClientConnection newConnection = new TcpClientConnection();
newConnection.clientSocket = clientSocket;
newConnection.output = output;
newConnection.reader = reader;
newConnection.input = input;
String sessionId = nextSessionId();
clientConnections.put(sessionId, newConnection);
......
......@@ -40,8 +40,7 @@ public class TcpServerModule implements ITcpServerModule, ITcpServerAccepted, IT
TcpClientConnection connection = tcpServerAccept.clientConnections.get(sessionId);
try {
connection.output.writeBytes(NetworkConstants.STARTINGLINE
+ "\n" + data + "\n" + NetworkConstants.FINISHINGLINE + "\n");
connection.output.writeUTF(data);
} catch (IOException exception) {
exception.printStackTrace();
}
......@@ -50,7 +49,7 @@ public class TcpServerModule implements ITcpServerModule, ITcpServerAccepted, IT
@Override
public void acceptedConnection(String sessionId) {
TcpReceiveMessage receiveMessage = new TcpReceiveMessage(
tcpServerAccept.clientConnections.get(sessionId).reader, sessionId, false);
tcpServerAccept.clientConnections.get(sessionId).input, sessionId, false);
receiveMessage.addListener(this);
Thread receiveThread = new Thread(receiveMessage);
......
......@@ -41,10 +41,10 @@ public class ClientLoginTest {
TcpServerModule server = new TcpServerModule();
ServerListener listener = new ServerListener();
server.registerListener(listener);
server.start(5556);
server.start(5557);
TcpClientModule client = new TcpClientModule();
client.connect("localhost", 5556);
client.connect("localhost", 5557);
OutgoingMessagesSerializer serializer = new OutgoingMessagesSerializer();
CwLoginContent content = new CwLoginContent();
content.name = "testWarrior";
......
package gone.lib.network.test;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.awaitility.Awaitility.await;
import static org.awaitility.Awaitility.fieldIn;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import gone.lib.common.ClientRole;
import gone.lib.common.RcStandard;
import gone.lib.network.CwClient;
import gone.lib.network.CwServer;
import gone.lib.network.contracts.IClient;
import gone.lib.network.contracts.IClientCallback;
import gone.lib.network.contracts.IServer;
import gone.lib.network.contracts.IServerCallback;
import gone.lib.network.json.CwGameStatusContent;
import gone.lib.network.json.CwLoginContent;
import gone.lib.network.json.CwLoginOkContent;
import gone.lib.network.json.CwReadyContent;
import gone.lib.network.json.CwStartContent;
import gone.lib.network.serialization.IncomingMessagesInterpreter;
import gone.lib.network.serialization.OutgoingMessagesSerializer;
import gone.lib.network.tcp.TcpClientModule;
import gone.lib.network.tcp.TcpServerModule;
import org.junit.Test;
import java.util.LinkedList;
import java.util.concurrent.Callable;
import javax.naming.OperationNotSupportedException;
/**
* Created by winfr on 30.09.2016.
*/
public class ComplexMessageOverNetworkTest {
class ServerCallback implements IServerCallback {
private String sessionId;
private IServer server;
public void setServer(IServer server) {
this.server = server;
}
@Override
public void receiveLoginTelegram(CwLoginContent telegram, String sessionId) {
this.sessionId = sessionId;
this.receivedLogin = true;
CwLoginOkContent loginOkContent = new CwLoginOkContent();
loginOkContent.lineLength = 100;
loginOkContent.coreSize = 500;
loginOkContent.clientMustWait = false;
loginOkContent.standard = RcStandard.ICWS94;
this.server.sendLoginOkTelegram(loginOkContent, this.sessionId);
}
@Override
public void receiveReadyTelegram(CwReadyContent telegram, String sessionId) {
assertThat(telegram.warriorRedcode.size(), is(3));
assertThat(telegram.warriorRedcode.get(0), is("MOV #4 4"));
assertThat(telegram.warriorRedcode.get(1), is("ADD 1 1"));
assertThat(telegram.warriorRedcode.get(2), is("SUB 0 0"));
receivedReady = true;
}
public boolean receivedLogin;
public boolean receivedReady;
}
class ClientCallback implements IClientCallback {
private IClient client;
public void setClient(IClient client) {
this.client = client;
}
@Override
public void receiveLoginOkTelegram(CwLoginOkContent telegram) {
assertThat(telegram.lineLength, is(100));
assertThat(telegram.coreSize, is(500));
assertThat(telegram.clientMustWait, is(false));
assertThat(telegram.standard, is(RcStandard.ICWS94));
receivedLoginOk = true;
CwReadyContent content = new CwReadyContent();
content.warriorRedcode = new LinkedList<>();
content.warriorRedcode.add("MOV #4 4");
content.warriorRedcode.add("ADD 1 1");
content.warriorRedcode.add("SUB 0 0");
client.sendReadyTelegram(content);
}
@Override
public void receiveStartTelegram(CwStartContent telegram) {
}
@Override
public void receiveGameStatusTelegram(CwGameStatusContent telegram) {
}
public boolean receivedLoginOk;
}
@Test
public void testComplexScenario() throws InterruptedException, OperationNotSupportedException {
ComplexMessageOverNetworkTest.ServerCallback serverCallback
= new ComplexMessageOverNetworkTest.ServerCallback();
ComplexMessageOverNetworkTest.ClientCallback clientCallback
= new ComplexMessageOverNetworkTest.ClientCallback();
CwServer server = new CwServer(serverCallback);
CwClient client = new CwClient(clientCallback);
serverCallback.setServer(server);
clientCallback.setClient(client);
server.start(5557);
client.connect("localhost", 5557);
CwLoginContent login = new CwLoginContent();
login.role = ClientRole.Player;
login.name = "name";
client.sendLoginTelegram(login);
Callable<Boolean> hasReceivedLogin = fieldIn(serverCallback)
.ofType(boolean.class).andWithName("receivedReady");
await().atMost(1, SECONDS).until(hasReceivedLogin);
assertThat(serverCallback.receivedLogin, is(true));
assertThat(clientCallback.receivedLoginOk, is(true));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment