Skip to content

Commit c17974e

Browse files
committed
add serialization for FailedConnectionAttempt list
1 parent 1e57581 commit c17974e

3 files changed

Lines changed: 103 additions & 6 deletions

File tree

src/main/java/net/sharksystem/hub/BasicHubConnectionManager.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,7 @@ protected void syncLists() {
6868
}
6969
}
7070
if(recordedOldAttempt != null) this.failedConnectionAttempts.remove(recordedOldAttempt);
71-
this.failedConnectionAttempts.add(new HubConnectionManager.FailedConnectionAttempt() {
72-
@Override
73-
public HubConnectorDescription getHubConnectorDescription() { return failedConnection; }
74-
@Override
75-
public long getTimeStamp() { return lastConnectionAttempt; }
76-
});
71+
this.failedConnectionAttempts.add(new FailedConnectionAttemptImpl(failedConnection, lastConnectionAttempt));
7772
}
7873
}
7974

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package net.sharksystem.hub;
2+
3+
import net.sharksystem.asap.ASAPException;
4+
import net.sharksystem.asap.utils.ASAPSerialization;
5+
import net.sharksystem.hub.peerside.HubConnectorDescription;
6+
import net.sharksystem.hub.peerside.TCPHubConnectorDescriptionImpl;
7+
8+
import java.io.ByteArrayInputStream;
9+
import java.io.ByteArrayOutputStream;
10+
import java.io.IOException;
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
public class FailedConnectionAttemptImpl implements HubConnectionManager.FailedConnectionAttempt {
15+
16+
private final HubConnectorDescription hubConnectorDescription;
17+
private final long timestamp;
18+
19+
public FailedConnectionAttemptImpl(HubConnectorDescription hubConnectorDescription, long timestamp){
20+
this.hubConnectorDescription = hubConnectorDescription;
21+
this.timestamp = timestamp;
22+
}
23+
24+
@Override
25+
public HubConnectorDescription getHubConnectorDescription() {
26+
return hubConnectorDescription;
27+
}
28+
29+
@Override
30+
public long getTimeStamp() {
31+
return timestamp;
32+
}
33+
34+
public static byte[] serializeFailedConnectionAttempts(List<HubConnectionManager.FailedConnectionAttempt> failedConnectionAttempts) throws IOException {
35+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
36+
if(failedConnectionAttempts == null || failedConnectionAttempts.isEmpty()) {
37+
// no hcd
38+
ASAPSerialization.writeIntegerParameter(0, baos);
39+
}else {
40+
// write number of descriptions
41+
ASAPSerialization.writeIntegerParameter(failedConnectionAttempts.size(), baos);
42+
for (HubConnectionManager.FailedConnectionAttempt failedConnectionAttempt : failedConnectionAttempts) {
43+
// HubConnectorDescription
44+
baos.write(failedConnectionAttempt.getHubConnectorDescription().serialize());
45+
// timestamp
46+
ASAPSerialization.writeLongParameter(failedConnectionAttempt.getTimeStamp(), baos);
47+
}
48+
}
49+
return baos.toByteArray();
50+
}
51+
52+
public static List<HubConnectionManager.FailedConnectionAttempt> deserializeFailedConnectionAttempts(byte[] serializedConnectionAttempts) throws IOException, ASAPException {
53+
List<HubConnectionManager.FailedConnectionAttempt> connectionAttempts = new ArrayList<>();
54+
ByteArrayInputStream is = new ByteArrayInputStream(serializedConnectionAttempts);
55+
int number = ASAPSerialization.readIntegerParameter(is);
56+
while(number-- > 0) {
57+
HubConnectorDescription hcd = TCPHubConnectorDescriptionImpl.readDescription(is);
58+
long timestamp = ASAPSerialization.readLongParameter(is);
59+
connectionAttempts.add(new FailedConnectionAttemptImpl(hcd, timestamp));
60+
}
61+
return connectionAttempts;
62+
}
63+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package net.sharksystem.hub;
2+
3+
import net.sharksystem.asap.ASAPException;
4+
import net.sharksystem.hub.peerside.HubConnectorDescription;
5+
import net.sharksystem.hub.peerside.TCPHubConnectorDescriptionImpl;
6+
import org.junit.Test;
7+
8+
import java.io.IOException;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
import static org.junit.Assert.*;
13+
14+
public class FailedConnectionAttemptImplTest {
15+
16+
@Test
17+
public void serializeFailedConnectionAttempts() throws IOException, ASAPException {
18+
HubConnectorDescription hubConnectorDescription1 = new TCPHubConnectorDescriptionImpl("localhost", 6000);
19+
HubConnectorDescription hubConnectorDescription2 = new TCPHubConnectorDescriptionImpl("localhost", 6600);
20+
long timestamp1 = 10;
21+
long timestamp2 = 10;
22+
List<HubConnectionManager.FailedConnectionAttempt> failedConnectionAttempts = new ArrayList<>();
23+
failedConnectionAttempts.add(new FailedConnectionAttemptImpl(hubConnectorDescription1, timestamp1));
24+
failedConnectionAttempts.add(new FailedConnectionAttemptImpl(hubConnectorDescription2, timestamp2));
25+
26+
byte[] serializedAttempts = FailedConnectionAttemptImpl.serializeFailedConnectionAttempts(failedConnectionAttempts);
27+
28+
List<HubConnectionManager.FailedConnectionAttempt> deserializedAttempts = FailedConnectionAttemptImpl.deserializeFailedConnectionAttempts(serializedAttempts);
29+
30+
// list should contain two items
31+
assertEquals(2, deserializedAttempts.size());
32+
// verify first FailedConnectionAttempt
33+
assertTrue(deserializedAttempts.get(0).getHubConnectorDescription().isSame(hubConnectorDescription1));
34+
assertEquals(timestamp1, deserializedAttempts.get(0).getTimeStamp());
35+
// verify second FailedConnectionAttempt
36+
assertTrue(deserializedAttempts.get(1).getHubConnectorDescription().isSame(hubConnectorDescription2));
37+
assertEquals(timestamp2, deserializedAttempts.get(1).getTimeStamp());
38+
}
39+
}

0 commit comments

Comments
 (0)