-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7eb9a1
commit c27760f
Showing
9 changed files
with
134 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package rs.iggy.message; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import rs.iggy.clients.blocking.tcp.BytesSerializer; | ||
import java.math.BigInteger; | ||
|
||
public class BigIntegerMessageId implements MessageId { | ||
|
||
private final BigInteger value; | ||
|
||
public BigIntegerMessageId(BigInteger value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public BigInteger toBigInteger() { | ||
return value; | ||
} | ||
|
||
public ByteBuf toBytes() { | ||
return BytesSerializer.toBytesAsU128(value); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return value.toString(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package rs.iggy.message; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import java.math.BigInteger; | ||
import java.util.Arrays; | ||
|
||
public class BytesMessageId implements MessageId { | ||
|
||
private final byte[] value; | ||
|
||
public BytesMessageId(byte[] value) { | ||
if (value.length != 16) { | ||
throw new IllegalArgumentException("Message id must have 16 bytes"); | ||
} | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public BigInteger toBigInteger() { | ||
return new BigInteger(1, value); | ||
} | ||
|
||
@Override | ||
public ByteBuf toBytes() { | ||
return Unpooled.wrappedBuffer(value); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return Arrays.toString(value); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package rs.iggy.message; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonValue; | ||
import io.netty.buffer.ByteBuf; | ||
import java.math.BigInteger; | ||
|
||
public interface MessageId { | ||
|
||
@JsonCreator | ||
static MessageId from(BigInteger bigInteger) { | ||
return new BigIntegerMessageId(bigInteger); | ||
} | ||
|
||
@JsonValue | ||
BigInteger toBigInteger(); | ||
|
||
ByteBuf toBytes(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package rs.iggy.message; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import java.math.BigInteger; | ||
import java.nio.ByteBuffer; | ||
import java.util.UUID; | ||
|
||
public class UuidMessageId implements MessageId { | ||
|
||
private final UUID value; | ||
|
||
public UuidMessageId(UUID value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public BigInteger toBigInteger() { | ||
ByteBuffer buffer = ByteBuffer.wrap(new byte[16]); | ||
buffer.putLong(value.getMostSignificantBits()); | ||
buffer.putLong(value.getLeastSignificantBits()); | ||
return new BigInteger(1, buffer.array()); | ||
} | ||
|
||
public ByteBuf toBytes() { | ||
var buffer = Unpooled.buffer(16, 16); | ||
buffer.writeLongLE(value.getLeastSignificantBits()); | ||
buffer.writeLongLE(value.getMostSignificantBits()); | ||
return buffer; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return value.toString(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters