This document provides a concise summary of KAI's peer-to-peer networking implementation, focusing on the practical aspects of using and extending the system.
KAI's peer-to-peer networking is built around these key components:
- Node: The fundamental network entity that can both listen for connections and connect to other nodes
- PeerDiscovery: Provides automatic discovery of other nodes on the network
- ConnectionManager: Handles connection states, timeouts, and tracking
- NetworkSerializer: Serializes KAI objects for transmission over the network
- NetworkPeer: Application that demonstrates peer-to-peer command execution
The peer-to-peer architecture is implemented as follows:
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Node 1 │◄────► Node 2 │◄────► Node 3 │
└─────────────┘ └─────────────┘ └─────────────┘
▲ ▲
│ │
└───────────────────────────────────────┘
Direct P2P Communication
Each node can:
- Listen for incoming connections
- Connect to other nodes directly
- Send objects and commands to other nodes
- Execute remote commands and return results
- No client/server distinction - all nodes are equal peers
- Any peer can connect to any other peer
- Bidirectional communication between peers
- Dynamic peer discovery and connection management
@peer commandsyntax for remote command execution- Result retrieval to the initiating peer
- Command aliasing through configuration
- Extensible command processor system
- External configuration files for peers
- Connection settings
- Predefined commands and aliases
- Runtime behavior controls
- Remote execution of mathematical expressions
- Result retrieval and display
- Demonstrated with 1+2=3 calculation example
The 1+2 calculation example demonstrates the full command execution flow:
-
Configuration:
- Peer1 has a predefined command:
"add": "calc 1+2" - Peer2 connects to Peer1
- Peer1 has a predefined command:
-
Command Execution:
- Peer2 sends
@0 addto execute the "add" command on Peer1 - Peer1 maps "add" to "calc 1+2"
- Peer1 processes "calc 1+2" using the calculation function
- Peer1 computes the result: "3"
- Peer1 sends the result back to Peer2
- Peer2 displays: "Result from [peer]: 3"
- Peer2 sends
This demonstrates true peer-to-peer execution with SSH-like semantics.
The peer discovery process works as follows:
- A node starts discovery by broadcasting ping messages
- Other nodes respond with pong messages containing their addresses
- The discovering node collects responses and builds a list of available peers
- The node can then connect to any of the discovered peers
# Start a listening peer
./Bin/NetworkPeer config/peer1_config.json
# In another terminal, start a connecting peer
./Bin/NetworkPeer config/peer2_config.json# List connected peers
peers
# Execute a calculation on remote peer 0
@0 add
# Execute a custom command on peer 1
@1 hello
Peer configuration is handled through JSON files:
{
"listenPort": 14595,
"autoListen": true,
"interactive": true,
"peers": [
{
"host": "127.0.0.1",
"port": 14595
}
],
"commands": {
"add": "calc 1+2",
"hello": "echo Hello from peer"
}
}The network functionality can be tested using:
- Manual testing: Follow the instructions in
Scripts/network/run_peers.sh - Automated testing: Run
Scripts/network/automated_demo.sh
The p2p_test.sh script demonstrates the peer-to-peer functionality by:
- Creating configuration files for two peers
- Starting the first peer as a listener
- Starting the second peer that connects to the first
- Sending a command from the second peer to the first
- Verifying that the result (3) is correctly returned
| Component | File Location | Description |
|---|---|---|
| Node | Include/KAI/Network/Node.h |
Main network entity implementation |
| PeerDiscovery | Include/KAI/Network/PeerDiscovery.h |
Handles discovery of peers on the network |
| ConnectionManager | Include/KAI/Network/ConnectionManager.h |
Manages connection states and tracking |
| NetworkTest | Source/App/NetworkTest/ |
Basic network test application |
| NetworkPeer | Source/App/NetworkPeer/ |
Advanced peer-to-peer application |
| Test Scripts | Scripts/network/ |
Scripts for testing the network functionality |
| Config Files | config/ |
Configuration files for different peer setups |
- Port Selection: Use ports above 1024 to avoid permission issues
- Command Design: Keep commands simple and deterministic
- Error Handling: Implement proper error handling for remote commands
- Timeouts: Set appropriate timeouts for connections and operations
- Security: Avoid executing potentially dangerous commands remotely
Planned improvements include:
- Authentication and security
- More sophisticated command processing
- Data streaming between peers
- Integration with the Rho language
- Peer discovery with improved broadcast/multicast
- NAT traversal capabilities
- Enhanced reconnection handling
- Performance optimizations
- Networking: Main networking documentation
- NetworkTest README: Network test application details
- Configuration README: Configuration file format details
- Test Scripts README: Network test scripts documentation