Skip to content

Latest commit

 

History

History
186 lines (141 loc) · 6.26 KB

File metadata and controls

186 lines (141 loc) · 6.26 KB

KAI Peer-to-Peer Networking Summary

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.

Key Components

KAI's peer-to-peer networking is built around these key components:

  1. Node: The fundamental network entity that can both listen for connections and connect to other nodes
  2. PeerDiscovery: Provides automatic discovery of other nodes on the network
  3. ConnectionManager: Handles connection states, timeouts, and tracking
  4. NetworkSerializer: Serializes KAI objects for transmission over the network
  5. NetworkPeer: Application that demonstrates peer-to-peer command execution

Implementation Architecture

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

Key Features

1. True Peer-to-Peer Architecture

  • 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

2. SSH-like Command Execution

  • @peer command syntax for remote command execution
  • Result retrieval to the initiating peer
  • Command aliasing through configuration
  • Extensible command processor system

3. JSON Configuration

  • External configuration files for peers
  • Connection settings
  • Predefined commands and aliases
  • Runtime behavior controls

4. Remote Calculation

  • Remote execution of mathematical expressions
  • Result retrieval and display
  • Demonstrated with 1+2=3 calculation example

Remote Command Execution Flow

The 1+2 calculation example demonstrates the full command execution flow:

  1. Configuration:

    • Peer1 has a predefined command: "add": "calc 1+2"
    • Peer2 connects to Peer1
  2. Command Execution:

    • Peer2 sends @0 add to 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"

This demonstrates true peer-to-peer execution with SSH-like semantics.

Peer Discovery Process

The peer discovery process works as follows:

  1. A node starts discovery by broadcasting ping messages
  2. Other nodes respond with pong messages containing their addresses
  3. The discovering node collects responses and builds a list of available peers
  4. The node can then connect to any of the discovered peers

Usage Examples

Basic Peer Setup

# Start a listening peer
./Bin/NetworkPeer config/peer1_config.json

# In another terminal, start a connecting peer
./Bin/NetworkPeer config/peer2_config.json

Interactive Commands

# List connected peers
peers

# Execute a calculation on remote peer 0
@0 add

# Execute a custom command on peer 1
@1 hello

Configuration

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"
    }
}

Testing

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:

  1. Creating configuration files for two peers
  2. Starting the first peer as a listener
  3. Starting the second peer that connects to the first
  4. Sending a command from the second peer to the first
  5. Verifying that the result (3) is correctly returned

Core Components and File Locations

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

Best Practices

  1. Port Selection: Use ports above 1024 to avoid permission issues
  2. Command Design: Keep commands simple and deterministic
  3. Error Handling: Implement proper error handling for remote commands
  4. Timeouts: Set appropriate timeouts for connections and operations
  5. Security: Avoid executing potentially dangerous commands remotely

Future Enhancements

Planned improvements include:

  1. Authentication and security
  2. More sophisticated command processing
  3. Data streaming between peers
  4. Integration with the Rho language
  5. Peer discovery with improved broadcast/multicast
  6. NAT traversal capabilities
  7. Enhanced reconnection handling
  8. Performance optimizations

Related Documentation