use of io.atomix.messaging.Endpoint in project atomix by atomix.
the class TestMessagingService method getHandler.
/**
* Returns the given handler for the given endpoint.
*/
private BiFunction<Endpoint, byte[], CompletableFuture<byte[]>> getHandler(Endpoint endpoint, String type) {
TestMessagingService service = getService(endpoint);
if (service == null) {
return (e, p) -> Futures.exceptionalFuture(new NoRemoteHandler());
}
BiFunction<Endpoint, byte[], CompletableFuture<byte[]>> handler = service.handlers.get(checkNotNull(type));
if (handler == null) {
return (e, p) -> Futures.exceptionalFuture(new NoRemoteHandler());
}
return handler;
}
use of io.atomix.messaging.Endpoint in project atomix by atomix.
the class RaftPerformanceTest method nextNode.
/**
* Returns the next unique member identifier.
*
* @return The next unique member identifier.
*/
private Node nextNode(Node.Type type) {
Endpoint endpoint = Endpoint.from("localhost", ++port);
Node node = Node.builder(NodeId.from(String.valueOf(++nextId))).withType(type).withEndpoint(endpoint).build();
endpointMap.put(node.id(), endpoint);
return node;
}
use of io.atomix.messaging.Endpoint in project atomix by atomix.
the class MessageDecoder method decode.
@Override
// suppress switch fall through warning
@SuppressWarnings("squid:S128")
protected void decode(ChannelHandlerContext context, ByteBuf buffer, List<Object> out) throws Exception {
switch(currentState) {
case READ_SENDER_IP:
if (buffer.readableBytes() < BYTE_SIZE) {
return;
}
buffer.markReaderIndex();
int octetsLength = buffer.readByte();
if (buffer.readableBytes() < octetsLength) {
buffer.resetReaderIndex();
return;
}
byte[] octets = new byte[octetsLength];
buffer.readBytes(octets);
senderIp = InetAddress.getByAddress(octets);
currentState = DecoderState.READ_SENDER_PORT;
case READ_SENDER_PORT:
if (buffer.readableBytes() < INT_SIZE) {
return;
}
senderPort = buffer.readInt();
currentState = DecoderState.READ_TYPE;
case READ_TYPE:
if (buffer.readableBytes() < BYTE_SIZE) {
return;
}
type = InternalMessage.Type.forId(buffer.readByte());
currentState = DecoderState.READ_PREAMBLE;
case READ_PREAMBLE:
if (buffer.readableBytes() < INT_SIZE) {
return;
}
preamble = buffer.readInt();
currentState = DecoderState.READ_MESSAGE_ID;
case READ_MESSAGE_ID:
if (buffer.readableBytes() < LONG_SIZE) {
return;
}
messageId = buffer.readLong();
currentState = DecoderState.READ_CONTENT_LENGTH;
case READ_CONTENT_LENGTH:
if (buffer.readableBytes() < INT_SIZE) {
return;
}
contentLength = buffer.readInt();
currentState = DecoderState.READ_CONTENT;
case READ_CONTENT:
if (buffer.readableBytes() < contentLength) {
return;
}
if (contentLength > 0) {
// TODO: Perform a sanity check on the size before allocating
content = new byte[contentLength];
buffer.readBytes(content);
} else {
content = EMPTY_PAYLOAD;
}
switch(type) {
case REQUEST:
currentState = DecoderState.READ_SUBJECT_LENGTH;
break;
case REPLY:
currentState = DecoderState.READ_STATUS;
break;
default:
checkState(false, "Must not be here");
}
break;
default:
break;
}
switch(type) {
case REQUEST:
switch(currentState) {
case READ_SUBJECT_LENGTH:
if (buffer.readableBytes() < SHORT_SIZE) {
return;
}
subjectLength = buffer.readShort();
currentState = DecoderState.READ_SUBJECT;
case READ_SUBJECT:
if (buffer.readableBytes() < subjectLength) {
return;
}
final String subject = readString(buffer, subjectLength, UTF_8);
InternalRequest message = new InternalRequest(preamble, messageId, new Endpoint(senderIp, senderPort), subject, content);
out.add(message);
currentState = DecoderState.READ_TYPE;
break;
default:
break;
}
break;
case REPLY:
switch(currentState) {
case READ_STATUS:
if (buffer.readableBytes() < BYTE_SIZE) {
return;
}
InternalReply.Status status = InternalReply.Status.forId(buffer.readByte());
InternalReply message = new InternalReply(preamble, messageId, content, status);
out.add(message);
currentState = DecoderState.READ_TYPE;
break;
default:
break;
}
break;
default:
checkState(false, "Must not be here");
}
}
use of io.atomix.messaging.Endpoint in project atomix by atomix.
the class NettyMessagingServiceTest method setUp.
@Before
public void setUp() throws Exception {
ep1 = new Endpoint(InetAddress.getByName("127.0.0.1"), findAvailablePort(5001));
netty1 = (ManagedMessagingService) NettyMessagingService.builder().withEndpoint(ep1).build().start().join();
ep2 = new Endpoint(InetAddress.getByName("127.0.0.1"), findAvailablePort(5003));
netty2 = (ManagedMessagingService) NettyMessagingService.builder().withEndpoint(ep2).build().start().join();
invalidEndPoint = new Endpoint(InetAddress.getByName(IP_STRING), 5003);
}
use of io.atomix.messaging.Endpoint in project atomix by atomix.
the class RaftFuzzTest method createServer.
/**
* Creates a Raft server.
*/
private RaftServer createServer(RaftMember member) {
RaftServerProtocol protocol;
if (USE_NETTY) {
try {
Endpoint endpoint = new Endpoint(InetAddress.getLocalHost(), ++port);
MessagingService messagingManager = NettyMessagingService.builder().withEndpoint(endpoint).build().start().join();
messagingServices.add(messagingManager);
endpointMap.put(member.nodeId(), endpoint);
protocol = new RaftServerMessagingProtocol(messagingManager, protocolSerializer, endpointMap::get);
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
} else {
protocol = protocolFactory.newServerProtocol(member.nodeId());
}
RaftServer.Builder builder = RaftServer.builder(member.nodeId()).withProtocol(protocol).withStorage(RaftStorage.builder().withStorageLevel(StorageLevel.DISK).withDirectory(new File(String.format("target/fuzz-logs/%s", member.nodeId()))).withSerializer(storageSerializer).withMaxSegmentSize(1024 * 1024).build()).addPrimitiveType(TestPrimitiveType.INSTANCE);
RaftServer server = builder.build();
servers.add(server);
return server;
}
Aggregations