|
| 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 | +} |
0 commit comments