Babel 2 Distributed Protocol Framework API Documentation
Babel 2 Distributed Protocol Framework
Welcome to the unified API documentation for Babel 2, a high-performance, event-driven framework for developing and evaluating distributed protocols in Java 17+.
Architecture Overview
Babel 2 decouples distributed protocol logic from network transports and execution models, providing compile-time type safety, zero-reflection serialization, and cryptographic security.
| Subsystem / Module | Description & Key Packages |
|---|---|
| Babel 2 Core |
Runtime orchestration, protocol lifecycle, event dispatching, execution threading models,
and protocol extensions.
|
| Babel 2 IP Network Layer |
High-performance Netty-based network channels, binary serialization, and secure transport mechanisms.
|
Quick Start Example
Building a Babel 2 application involves three main steps: defining messages, implementing a protocol with declarative event annotations, and bootstrapping the runtime with an execution model and transport channels.
1. Define a Network Message
Annotate a record with @BabelIPMessage and assign a unique numeric identifier.
The annotation processor generates zero-reflection binary serializers automatically at compile time.
import pt.unl.fct.di.novasys.babel2.channels.BabelIPMessage;
import pt.unl.fct.di.novasys.babel2.channels.BabelMessage;
@BabelIPMessage(id = 100)
public record PingMessage(String content) implements BabelMessage {}
2. Implement a Protocol
Implement BabelProtocol and annotate handlers with @UponMessageIn.
Messages are sent to peers via the assigned ComposedBabelChannel.
import pt.unl.fct.di.novasys.babel2.annotations.UponMessageIn;
import pt.unl.fct.di.novasys.babel2.channels.ComposedBabelChannel;
import pt.unl.fct.di.novasys.babel2.core.Babel;
import pt.unl.fct.di.novasys.babel2.core.BabelNode;
import pt.unl.fct.di.novasys.babel2.core.BabelProtocol;
public class PingPongProtocol implements BabelProtocol {
private ComposedBabelChannel channel;
@Override
public void init(Babel runtime, ComposedBabelChannel channel) {
this.channel = channel;
}
@UponMessageIn
public void onPing(PingMessage msg, BabelNode sender) {
System.out.printf("Received '%s' from %s%n", msg.content(), sender);
// Reply back to sender across the composed channel
channel.send(sender, new PingMessage("pong"));
}
}
3. Bootstrap and Initialize the Runtime
Configure node cryptographic identity, Netty event loops, execution holder factories,
channel builders, and register protocols before starting the runtime with init():
import java.net.InetSocketAddress;
import java.security.KeyPair;
import io.netty.channel.EventLoopGroup;
import pt.unl.fct.di.novasys.babel2.channels.DefaultNodeStore;
import pt.unl.fct.di.novasys.babel2.channels.PublicKeyBabelNode;
import pt.unl.fct.di.novasys.babel2.channels.tcp_tls.AbstractTCPBabelChannel.TCPBabelChannelAuthMode;
import pt.unl.fct.di.novasys.babel2.channels.tcp_tls.TCPBabelChannel;
import pt.unl.fct.di.novasys.babel2.channels.utils.BabelIpUtils;
import pt.unl.fct.di.novasys.babel2.core.Babel;
import pt.unl.fct.di.novasys.babel2.core.NettyExecutorProtocolHolderFactory;
import pt.unl.fct.di.novasys.babel2.utils.CryptoUtils;
public class Application {
public static void main(String[] args) {
// 1. Generate identity cryptographic key pair & node representation
KeyPair keyPair = CryptoUtils.generateKeyPair();
PublicKeyBabelNode myself = new PublicKeyBabelNode(keyPair.getPublic());
// 2. Configure event loop and protocol execution policy
EventLoopGroup eventLoop = BabelIpUtils.createLoopGroup(4);
var nettyHolder = new NettyExecutorProtocolHolderFactory(4096, eventLoop);
// 3. Configure network transport channel builder
var tcpChannel = TCPBabelChannel.builder()
.setBindingAddress(new InetSocketAddress(10000))
.setEventLoopGroup(eventLoop)
.setAuthMode(TCPBabelChannelAuthMode.FULL_AUTH)
.verifyIdentity();
// 4. Instantiate protocol
var pingPong = new PingPongProtocol();
// 5. Bootstrap runtime, register protocol with holder and channel, and initialize
Babel runtime = Babel.builder()
.setMyself(myself)
.setKeyPair(keyPair)
.setNodeStore(new DefaultNodeStore(10, 1))
.registerProtocol(pingPong, nettyHolder, tcpChannel)
.build()
.init();
}
}