-
Notifications
You must be signed in to change notification settings - Fork 86
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
Showing
5 changed files
with
108 additions
and
20 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
39 changes: 37 additions & 2 deletions
39
cluster2/src/main/java/io/scalecube/cluster2/gossip/GossipCodec.java
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 |
---|---|---|
@@ -1,20 +1,55 @@ | ||
package io.scalecube.cluster2.gossip; | ||
|
||
import static io.scalecube.cluster2.UUIDCodec.uuid; | ||
|
||
import io.scalecube.cluster2.AbstractCodec; | ||
import io.scalecube.cluster2.UUIDCodec; | ||
import io.scalecube.cluster2.sbe.GossipDecoder; | ||
import io.scalecube.cluster2.sbe.GossipEncoder; | ||
import java.util.UUID; | ||
import java.util.function.Consumer; | ||
import org.agrona.MutableDirectBuffer; | ||
import org.agrona.concurrent.UnsafeBuffer; | ||
|
||
public class GossipCodec extends AbstractCodec { | ||
|
||
private final GossipEncoder gossipEncoder = new GossipEncoder(); | ||
private final GossipDecoder gossipDecoder = new GossipDecoder(); | ||
|
||
public GossipCodec() {} | ||
|
||
// Encode | ||
|
||
public MutableDirectBuffer encode(Gossip gossip) { | ||
encodedLength = 0; | ||
|
||
gossipEncoder.wrapAndApplyHeader(encodedBuffer, 0, headerEncoder); | ||
UUIDCodec.encode(gossip.gossiperId(), gossipEncoder.gossiperId()); | ||
gossipEncoder.sequenceId(gossip.sequenceId()); | ||
gossipEncoder.putMessage(gossip.message(), 0, gossip.message().length); | ||
|
||
encodedLength = headerEncoder.encodedLength() + gossipEncoder.encodedLength(); | ||
return encodedBuffer; | ||
} | ||
|
||
// Decode | ||
|
||
public Gossip gossip(Consumer<UnsafeBuffer> consumer) { | ||
consumer.accept(unsafeBuffer); | ||
|
||
// TODO | ||
gossipDecoder.wrapAndApplyHeader(unsafeBuffer, 0, headerDecoder); | ||
|
||
final UUID gossiperId = uuid(gossipDecoder.gossiperId()); | ||
if (gossiperId == null) { | ||
return null; | ||
} | ||
|
||
final long sequenceId = gossipDecoder.sequenceId(); | ||
|
||
final int messageLength = gossipDecoder.messageLength(); | ||
final byte[] message = new byte[messageLength]; | ||
gossipDecoder.getMessage(message, 0, messageLength); | ||
|
||
return new Gossip(null, null, 1); | ||
return new Gossip(gossiperId, sequenceId, message); | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
cluster2/src/main/java/io/scalecube/cluster2/gossip/GossipRequestCodec.java
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,26 @@ | ||
package io.scalecube.cluster2.gossip; | ||
|
||
import io.scalecube.cluster2.AbstractCodec; | ||
import io.scalecube.cluster2.UUIDCodec; | ||
import io.scalecube.cluster2.sbe.GossipRequestEncoder; | ||
import java.util.UUID; | ||
import org.agrona.MutableDirectBuffer; | ||
|
||
public class GossipRequestCodec extends AbstractCodec { | ||
|
||
private final GossipRequestEncoder gossipRequestEncoder = new GossipRequestEncoder(); | ||
private final GossipCodec gossipCodec = new GossipCodec(); | ||
|
||
public GossipRequestCodec() {} | ||
|
||
public MutableDirectBuffer encode(UUID from, Gossip gossip) { | ||
encodedLength = 0; | ||
|
||
gossipRequestEncoder.wrapAndApplyHeader(encodedBuffer, 0, headerEncoder); | ||
UUIDCodec.encode(from, gossipRequestEncoder.from()); | ||
gossipRequestEncoder.putGossip(gossipCodec.encode(gossip), 0, gossipCodec.encodedLength()); | ||
|
||
encodedLength = headerEncoder.encodedLength() + gossipRequestEncoder.encodedLength(); | ||
return encodedBuffer; | ||
} | ||
} |
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