Skip to content

A reliable remote file backup system with GBN (Go-Back-N) protocol implementation. Computer Network course project featuring TCP/UDP socket programming, Docker containerization, and comprehensive testing.

License

Notifications You must be signed in to change notification settings

FrankieLiu04/Computer_Network_lab

Computer Network Lab β€” Reliable Remote Backup System

δΈ­ζ–‡ CI C++17 Docker License: MIT

A production-style remote file backup system built from scratch using C++17, demonstrating network programming, systems design, and cloud-native practices.


🎯 Project Motivation

This project originated from a university network programming assignment. Rather than submitting a minimal solution, I treated it as an opportunity to learn and practice real-world backend engineering skills:

Original Assignment What I Built
Basic UDP file transfer Dual-channel architecture: UDP for commands + TCP for reliable data transfer
Console-only interaction RESTful HTTP API + Web management dashboard
Manual testing Automated CI/CD with GitHub Actions + unit/integration tests
Local execution only Docker containerization with one-command deployment
No observability Structured logging + real-time metrics (request counts, latency percentiles, throughput)

πŸ—οΈ Architecture Overview

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                              Client                                         β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚   CLI       β”‚    β”‚  Commands   β”‚    β”‚  File Data (TCP)                β”‚  β”‚
β”‚  β”‚  Interface  │───▢│   (UDP)     │───▢│  - Explicit handshake           β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚  - Timeout & retry              β”‚  β”‚
β”‚                                        β”‚  - Progress tracking            β”‚  β”‚
β”‚                                        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                    β”‚
                                    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                              Server                                         β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚  UDP Cmd    β”‚    β”‚   Command   β”‚    β”‚  HTTP Server (cpp-httplib)      β”‚  β”‚
β”‚  β”‚  Listener   │───▢│   Handler   │◀──▢│  - REST API (/api/files, etc.)  β”‚  β”‚
β”‚  β”‚  :35887     β”‚    β”‚             β”‚    β”‚  - Web UI (static/index.html)   β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚  - Health check (/healthz)      β”‚  β”‚
β”‚                            β”‚           β”‚  - Metrics (/api/metrics)       β”‚  β”‚
β”‚                            β–Ό           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚                     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                         β”‚
β”‚                     β”‚  Metrics    │◀── Request counts, latency (p50/p95),   β”‚
β”‚                     β”‚  Collector  β”‚    bytes transferred, error rates       β”‚
β”‚                     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                         β”‚
β”‚                            β”‚                                                β”‚
β”‚                            β–Ό                                                β”‚
β”‚                     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                         β”‚
β”‚                     β”‚  Backup     β”‚    Validated filenames, atomic writes   β”‚
β”‚                     β”‚  Storage    β”‚                                         β”‚
β”‚                     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Design Decisions

Decision Rationale
UDP for commands, TCP for data Commands are small and latency-sensitive; file transfers need reliability and flow control
Explicit handshake before transfer Prevents race conditions in rename/remove operations (a bug I fixed from the original lab)
Singleton Metrics collector Thread-safe, lock-free counters for high-frequency operations; mutex-protected for percentile sampling
Filename validation layer Defense against path traversal (../), reserved names, and injection attacks
Separation of HTTP and UDP interfaces HTTP for human/dashboard access; UDP for programmatic client access

πŸ› οΈ Technical Highlights

1. Network Programming (C++ Socket API)

  • Dual-protocol design: UDP command channel + dynamic TCP data channel
  • Non-trivial protocol: Custom binary message format (CmdMsg, DataMsg) with explicit field packing
  • Error handling: Timeout, retry, and graceful degradation
// Example: Explicit handshake to prevent race conditions
// Server waits for client ACK before proceeding with rename
sendto(sock, &ackMsg, sizeof(ackMsg), 0, clientAddr, addrLen);
recvfrom(sock, &response, sizeof(response), 0, ...);  // Wait for client confirmation

2. Observability & Metrics

  • Structured logging with spdlog (file + console, configurable levels)
  • Real-time metrics collection:
    • Request counts by command type
    • Error counts by error code
    • Latency tracking with percentile calculation (p50, p95, max)
    • Throughput (bytes received/sent)
// RAII-based latency measurement
auto timer = Metrics::instance().startTimer("http_upload");
// ... operation executes ...
// Timer automatically records latency on scope exit

3. Cloud-Native Practices

  • Docker multi-stage build: Minimized image size, reproducible builds
  • Docker Compose: One-command deployment with health checks
  • GitHub Actions CI: Automated build + test on Linux and Windows
# Health check ensures container is truly ready
healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost:8080/healthz"]
  interval: 10s
  timeout: 5s
  retries: 3

4. Security Considerations

Threat Mitigation
Path traversal (../../../etc/passwd) Strict filename validation, reject any path separators
Reserved filename injection (Windows) Block CON, PRN, NUL, etc.
Oversized uploads Configurable maxUploadSize limit
Malformed requests Input validation at protocol and HTTP layers

πŸš€ Quick Start

Option 1: Docker (Recommended)

cd reliable-remote-backup-system
docker compose up -d

# Open Web UI
# http://localhost:8080

# Test the API
curl http://localhost:8080/healthz
curl http://localhost:8080/api/files

Option 2: Build from Source

cd reliable-remote-backup-system
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
cmake --build . --parallel

# Run tests
ctest --output-on-failure

# Start server
./backup-server -port 35887 -http 8080

πŸ“Š API Reference

Endpoint Method Description
/healthz GET Health check (for container orchestration)
/api/files GET List all backed-up files
/api/upload POST Upload file (multipart/form-data)
/api/files/{name} DELETE Delete a file
/api/rename POST Rename a file ({"oldName": "a", "newName": "b"})
/api/metrics GET Real-time performance metrics
Example: Metrics Response
{
  "uptime_seconds": 3600,
  "requests": {
    "ls": 42,
    "send": 15,
    "http_upload": 8
  },
  "errors": {
    "invalid_filename": 2,
    "file_not_found": 1
  },
  "transfer": {
    "bytes_received": 10485760,
    "send_sessions": 15,
    "send_failures": 0
  },
  "latency": {
    "http_upload": {
      "avg_ms": 125.3,
      "max_ms": 450.0,
      "p95_ms": 280.0
    }
  }
}

πŸ§ͺ Testing Strategy

Layer Tool Coverage
Unit tests GoogleTest Filename validation, error codes, metrics logic
Integration tests Bash + curl HTTP API end-to-end flows
CI GitHub Actions Linux (Ubuntu 22.04) + Windows builds
# Run integration tests
docker compose up -d
bash scripts/integration_test.sh

πŸ“ˆ Development Roadmap

βœ… Phase 1: Engineering Foundation

  • C++17 + CMake build system
  • Structured logging (spdlog)
  • Unit testing (GoogleTest)
  • GitHub Actions CI/CD
  • Security: filename validation

βœ… Phase 2: Web Dashboard + Observability

  • HTTP REST API
  • Web file management UI
  • Metrics collection (requests, errors, latency, throughput)
  • Docker Compose deployment
  • Integration test suite

πŸ”œ Phase 3: Production Hardening (Planned)

  • Prometheus metrics export (/metrics)
  • Request ID tracing for log correlation
  • Connection pooling / thread pool for concurrent transfers
  • Chaos testing with tc netem (network delay/loss simulation)

πŸ“ Project Structure

reliable-remote-backup-system/
β”œβ”€β”€ include/                    # Header files
β”‚   β”œβ”€β”€ client/                 # Client interfaces
β”‚   β”œβ”€β”€ common/                 # Shared utilities (metrics, logging, file ops)
β”‚   β”œβ”€β”€ protocol/               # Wire protocol definitions
β”‚   └── server/                 # Server components (HTTP, command handler)
β”œβ”€β”€ src/                        # Implementation
β”œβ”€β”€ static/                     # Web UI (index.html)
β”œβ”€β”€ scripts/                    # Build and test scripts
β”œβ”€β”€ tests/                      # Unit tests
β”œβ”€β”€ .github/workflows/          # CI configuration
β”œβ”€β”€ Dockerfile                  # Server container
β”œβ”€β”€ docker-compose.yml          # Orchestration config
└── docs/                       # Additional documentation
    β”œβ”€β”€ DOCKER_QUICKSTART.md
    └── LOCAL_RUN_GUIDE.md

πŸ”§ What I Learned

Through this project, I gained hands-on experience with:

  1. Low-level network programming: Socket APIs, protocol design, handling partial reads/writes
  2. Systems thinking: Trade-offs between UDP/TCP, designing for failure modes
  3. Observability: Why metrics matter, how to instrument code without overhead
  4. DevOps practices: Docker, CI/CD, infrastructure-as-code mindset
  5. Security awareness: Input validation, defense in depth

πŸ“„ License

MIT License β€” feel free to use this as a reference or starting point.


Author: Frankie Liu
Repository: Computer_Network_lab

About

A reliable remote file backup system with GBN (Go-Back-N) protocol implementation. Computer Network course project featuring TCP/UDP socket programming, Docker containerization, and comprehensive testing.

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

No packages published