Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ htmlcov/
.cache
nosetests.xml
coverage.xml
.profraw
*.profraw

# Environment and secret files
.env
Expand Down
8 changes: 2 additions & 6 deletions crates/gateway/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,8 @@ impl SwarmDriver<Behaviour> for NetworkDriver {
tracing::info!(address=%address, "New listen address");
self.process_new_listen_addr(&listener_id).await;
}
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Received { peer_id, info, .. })) => {
// Add known addresses of peers to the Kademlia routing table
tracing::debug!(peer_id=%peer_id, info=?info, "Adding address to Kademlia routing table");
for addr in info.listen_addrs {
self.swarm.behaviour_mut().kademlia.add_address(&peer_id, addr);
}
SwarmEvent::Behaviour(BehaviourEvent::Identify(event)) => {
self.process_identify_event(event);
}
SwarmEvent::Behaviour(BehaviourEvent::Kademlia(kad::Event::OutboundQueryProgressed {id, result, step, ..})) => {
self.process_kademlia_query_result(id, result, step).await;
Expand Down
96 changes: 96 additions & 0 deletions crates/network/examples/basic_networking/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Basic Networking Example

This example demonstrates the fundamental networking capabilities of the hypha-network crate, focusing on:

- **Error handling** with `HyphaError` types
- **Connection establishment** using dial and listen interfaces
- **Basic network setup** without complex protocols
- **Driver pattern** for managing network events

## Features Demonstrated

### Error Handling
- `HyphaError::DialError` - Connection failures
- `HyphaError::SwarmError` - Swarm initialization issues
- Error trait implementations (Display, Debug, Error)

### Networking Traits
- `DialInterface` - Initiating connections to remote peers
- `ListenInterface` - Accepting incoming connections
- `DialDriver` - Processing dial events and connection state
- `ListenDriver` - Processing listen events and address management
- `SwarmDriver` - Core event loop and swarm management

### Basic Usage Patterns
- Creating network interfaces and drivers
- Setting up asynchronous event processing
- Managing connection lifecycle
- Handling network events and errors

## Running the Example

### Demo Mode (Recommended)
```bash
cargo run --example basic_networking demo
```

This creates two in-memory networks, connects them, and demonstrates error handling.

### Server Mode
```bash
cargo run --example basic_networking server --listen-addr "/memory/test_server"
```

### Client Mode
```bash
cargo run --example basic_networking client --server-addr "/memory/test_server"
```

## Key Concepts

### Driver Pattern
The example shows the driver pattern used throughout hypha-network:
- **Interface**: High-level API for applications (`DialInterface`, `ListenInterface`)
- **Driver**: Event processing and state management (`DialDriver`, `ListenDriver`)
- **Action**: Commands sent from interface to driver (`DialAction`, `ListenAction`)

### Error Handling
All networking operations return `Result` types with appropriate error information:
```rust
match network.dial(address).await {
Ok(peer_id) => println!("Connected to {}", peer_id),
Err(e) => eprintln!("Connection failed: {}", e),
}
```

### Async Event Processing
The driver runs an event loop processing both libp2p swarm events and application actions:
```rust
tokio::select! {
event = self.swarm.select_next_some() => {
// Handle libp2p events
}
Some(action) = self.action_rx.recv() => {
// Handle application actions
}
}
```

## Testing

The example includes unit tests demonstrating:
- Basic connection setup
- Error type behavior
- Memory transport usage for testing

Run tests with:
```bash
cargo test --example basic_networking
```

## See Also

- `request_response` example - Higher-level request-response protocol
- `crates/network/src/dial.rs` - Dial trait implementation
- `crates/network/src/listen.rs` - Listen trait implementation
- `crates/network/src/error.rs` - Error type definitions
Loading
Loading