Permalink
Browse files

Fixes #2: Fixed serialization issues with missing no-arg constructor

  • Loading branch information...
klaus7 committed Sep 13, 2018
1 parent 8fefc29 commit e42c534e146b4bdd8d06bbfa62a9cf850f589473
@@ -42,6 +42,9 @@
@Setter(value = AccessLevel.PROTECTED)
private static long roundTripTime = 0;

/** no-arg constructor required for serialization. */
private ClientTimerSyncMessage() {}

@java.beans.ConstructorProperties({"retrievedOffsetToHost", "retrievedRoundTripTime"})
public ClientTimerSyncMessage(long retrievedOffsetToHost, long retrievedRoundTripTime) {
this.retrievedOffsetToHost = retrievedOffsetToHost;
@@ -40,6 +40,9 @@
@Getter
int clientId;

/** no-arg constructor required for serialization. */
private ConnectResponse() {}

public ConnectResponse(long connectRequestMsgId, int clientId) {
this.connectRequestMsgId = connectRequestMsgId;
this.clientId = clientId;
@@ -22,6 +22,9 @@

public Object object;

/** no-arg constructor required for serialization. */
public GenericMessage() {}

public GenericMessage(Object object) {
this.object = object;
}
@@ -55,6 +55,9 @@
/** The payload of the message part. */
byte[] bytes;

/** no-arg constructor required for serialization. */
private MessagePart() {}

private MessagePart(long id, int partNumber, byte[] bytes) {
this.id = id;
this.partNumber = partNumber;
@@ -35,6 +35,9 @@
/** List of sequence message ids we require. */
private List<Long> missingIds = new ArrayList<>();

/** no-arg constructor required for serialization. */
private RequestSeqIdsMessage() {}

public RequestSeqIdsMessage(List<Long> missingIds, int receiverId) {
this.missingIds = missingIds;
setReceiverId(receiverId);
@@ -24,6 +24,9 @@
@Getter
private long lastReceivedId;

/** no-arg constructor required for serialization. */
private StackAckMessage() {}

public StackAckMessage(long lastReceivedId) {
this.lastReceivedId = lastReceivedId;
}
@@ -26,6 +26,9 @@
@Getter
List<Message> messages;

/** no-arg constructor required for serialization. */
private StackedMessage() {}

public StackedMessage(List<Message> messages) {
this.messages = messages;
}
@@ -55,6 +55,9 @@
@Getter
private MessageFeatures features = new MessageFeatures();

/** no-arg constructor required for serialization. */
private TimerSyncMessage() {}

public TimerSyncMessage(int clientId) {
this.clientId = clientId;
getFeatures().add(new TimestampFeature());
@@ -76,7 +76,7 @@ public void stop() {
if (receiveThread != null && receiveThread.isAlive()) {
receiveThread.join(3000);
if (receiveThread.isAlive()) {
log.error("Receiver thread should be destroyed by now.");
log.warn("Receiver thread should be destroyed by now.");
}
}
receiveThread = null;
@@ -118,7 +118,7 @@ public boolean send(Message message) {

private void socketSend(DatagramPacket datagramPacket) {
if (socket == null || socket.isClosed()) {
log.error("Couldn't send message: Socket is closed!");
log.warn("Couldn't send message: Socket is closed!");
return;
}
try {
@@ -183,14 +183,14 @@ public void run() {
final DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
while(true) {
try {
if (socket.isClosed()) {
if (socket == null || socket.isClosed()) {
log.info("Receiving socket closed.");
break;
}
socket.receive(receivePacket);
receive(receivePacket);
} catch (Exception e) {
if (!socket.isClosed()) {
if (socket != null && !socket.isClosed()) {
log.warn("Error receiving packet.", e);
} else {
log.warn("Error receiving packet. Socket is already closed!");
@@ -33,6 +33,6 @@

Message deserialiseWithStream(InputStream _is);

/** Setted payload is a requirement. */
/** Set payload is a requirement. */
CRC32 getChecksum(Message message, byte[] salt);
}
@@ -21,17 +21,24 @@
import com.jfastnet.Server;
import com.jfastnet.messages.GenericMessage;

import java.util.concurrent.atomic.AtomicInteger;

/** @author Klaus Pfeiffer - klaus@allpiper.com */
public class HelloWorld {

public static int received = 0;
private static final AtomicInteger received = new AtomicInteger(0);

public static class PrintMessage extends GenericMessage {
public PrintMessage(Object object) { super(object); }

/** no-arg constructor required for serialization. */
private PrintMessage() {}

PrintMessage(Object object) { super(object); }

@Override
public void process(Object context) {
System.out.println(object);
received++;
received.incrementAndGet();
}
}

@@ -46,7 +53,7 @@ public static void main(String[] args) throws InterruptedException {
server.send(new PrintMessage("Hello Client!"));
client.send(new PrintMessage("Hello Server!"));

while (received < 2) Thread.sleep(100);
while (received.get() < 2) Thread.sleep(100);

client.stop();
server.stop();

0 comments on commit e42c534

Please sign in to comment.