Ingredients
Tofu ingredients:
- Ampe — the Async Message Passing Engine (we call it the engine).
- ChannelGroup
- Message
- Errors and Statuses
- Addresses
- Test Helpers
Each ingredient depends on the others, so it’s hard to explain one without understanding the rest.
Because of that, the short descriptions below give only the basic idea.
The examples later will help you understand how tofu really works.
Separation of Concerns
Logical Separation
The Engine owns all resources in Tofu software.
It allocates and destroys Message(s) and ChannelGroup(s).
The ChannelGroup handles async, two-way exchange of Messages.
The Message does two things:
- Holds business data and metadata.
- Works as a command for Tofu.
Physical Separation
The Engine name shows the real work it does.
Every engine runs one internal thread with a poll loop.
This loop handles all socket operations.
The Ampe interface is implemented by the Reactor structure.
All ChannelGroups share one internal socket to talk to the engine thread.
Each ChannelGroup uses an internal queue for messages (from engine or application).
The ChannelGroup is a thin layer.
It forwards messages between application and engine thread.
I hope Errors and Statuses don't need explanation.
Addresses are helpers for adding network information(ports, addresses, etc.) to Message.
Test Helpers create network information for safe testing.
Tofu-based Communication Flow
The steps below show how communication works between network participants, called peers:
- Initialization: The peer creates a Reactor to get the Ampe interface.
- Channel Setup: The peer creates a ChannelGroup to manage channels (connections to other peers).
- Core Loop: In the main application loop, the peer:
- Sends: Gets Messages from Ampe, fills data, enqueues via ChannelGroup.
- Receives: Gets and processes incoming messages from other peers.
This is a simple overview.
Later sections show full logic and message lifecycle.