# tarun-kavipurapu/p2p-file-transfer architecture

> Architecture diagram of the GitHub repository tarun-kavipurapu/p2p-file-transfer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-10-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tarun-kavipurapu/p2p-file-transfer
- Repository: https://github.com/tarun-kavipurapu/p2p-file-transfer

## Overview

##### Project Type & Purpose
- This is a hybrid peer-to-peer file-transfer system in Go, with:
- A central server (peer discovery + metadata management)
- Multiple peer servers (serve and request file chunks)
- A CLI front end (`cmd/`) to drive operations.

##### File Structure
- Top-level directories:
- `central-server/` – central registry service code
- `peer/` – peer node logic (chunking, storage, worker pool)
- `pkg/` – shared transport, messaging, encoding, utilities
- `cmd/` – CLI subcommands: `cserver`, `peer`, `peer1`, `peer2`
- Support files:
- `files/` – sample assets & architecture image
- `TestingFiles/` – test artifacts
- Config/build:
- `go.mod`, `Makefile`

##### README Insights
- Key features: peer discovery, chunk-based transfer, concurrent downloads, integrity checks
- CLI commands: central-server, peer-server, register-file, request-file

##### Main Components
a. CLI Layer (cmd/):
- User invokes commands → dispatches to central-server or peer logic
b. Central Server (central-server/cserver.go):
- HTTP/TCP API for peer registration & metadata storage
c. Peer Server (peer/logic.go, peer/peer.go):
- Listens for chunk requests
- Worker pool (`workerpool.go`) for concurrent chunk handling
- Local chunk store (`store.go`)
d. Shared Transport & Protocol (pkg/):
- Message types (`message.go`, `commStructures.go`)
- Encoding/decoding (`encoding.go`)
- TCP transport abstraction (`tcp_transport.go`, `transport.go`)
e. File Storage:
- Local filesystem or memory chunk store
- Metadata hashing & verification (`metadata.go`)
f. External Interfaces:
- CLI → TCP/HTTP calls
- Peers ↔ Peers over TCP

##### Relationships & Data Flows
- register-file (send file metadata, peer address)
- peer discovery list, heartbeat
- request-file (file ID, peer address)
- concurrent TCP connections via worker pool

##### Architectural Patterns & Principles
- Hybrid P2P with central registry (single point for discovery + metadata)
- Chunk-based data partitioning for parallelism and resumability
- Concurrency via worker pools
- Clear separation of concerns: CLI, business logic, transport, storage

##### Technologies & Libraries
- Go modules (`go.mod`)
- net/TCP for peer communications
- Custom encoding (JSON/gob) in `pkg/encoding.go`
- Logging and error handling in each layer

##### Final Checklist
- All major components (CLI, Central Server, Peer Servers, Storage, Transport) are present

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% CLI Layer
    subgraph "CLI Layer"
        direction TB
        CServerCLI["central-server CLI"]:::cli
        PeerCLI["peer-server CLI"]:::cli
        Peer1CLI["peer1 CLI"]:::cli
        Peer2CLI["peer2 CLI"]:::cli
    end

    %% Central Server
    Central["Central Server"]:::central

    %% Peer Servers
    subgraph "Peer Servers"
        direction TB
        PeerServer["Peer Node"]:::peer
        subgraph "Peer Components"
            direction TB
            Logic["Logic & Chunk Manager"]:::peer
            Worker["Worker Pool"]:::peer
            Store["Local Chunk Store"]:::store
        end
    end

    %% Shared Transport & Protocol
    subgraph "Transport & Protocol" 
        direction TB
        Transport["transport.go"]:::shared
        TCP["tcp_transport.go"]:::shared
        Comm["commStructures.go"]:::shared
        Message["message.go"]:::shared
        Encoding["encoding.go"]:::shared
    end

    %% Metadata & Utilities
    Metadata["metadata.go"]:::store
    Utils["utils.go"]:::shared

    %% Configuration
    GoMod["go.mod"]:::config
    Makefile["Makefile"]:::config

    %% Connections
    CServerCLI -->|"RegisterFile (HTTP/TCP)"| Central
    PeerCLI -->|"RequestFile"| PeerServer
    Peer1CLI -->|"RequestFile"| PeerServer
    Peer2CLI -->|"RequestFile"| PeerServer

    Central -->|"GetPeers / Heartbeat"| PeerServer
    PeerServer -->|"Peer Discovery"| Central

    PeerServer -->|"ChunkRequest (TCP)"| PeerServer
    PeerServer -->|"Read/Write Chunks"| Store
    Store -->|"Validate Hash"| Metadata

    Central -- Relay --> Transport
    PeerServer -- Use --> Transport
    Transport --> TCP
    Transport --> Comm
    Comm --> Message
    Message --> Encoding
    PeerServer -- Use --> Utils

    %% Click Events
    click CServerCLI "https://github.com/tarun-kavipurapu/p2p-file-transfer/blob/main/cmd/cserver/main.go"
    click PeerCLI "https://github.com/tarun-kavipurapu/p2p-file-transfer/blob/main/cmd/peer/main.go"
    click Peer1CLI "https://github.com/tarun-kavipurapu/p2p-file-transfer/blob/main/cmd/peer1/main.go"
    click Peer2CLI "https://github.com/tarun-kavipurapu/p2p-file-transfer/blob/main/cmd/peer2/main.go"
    click Central "https://github.com/tarun-kavipurapu/p2p-file-transfer/blob/main/central-server/cserver.go"
    click PeerServer "https://github.com/tarun-kavipurapu/p2p-file-transfer/blob/main/peer/peer.go"
    click Logic "https://github.com/tarun-kavipurapu/p2p-file-transfer/blob/main/peer/logic.go"
    click Worker "https://github.com/tarun-kavipurapu/p2p-file-transfer/blob/main/peer/workerpool.go"
    click Store "https://github.com/tarun-kavipurapu/p2p-file-transfer/blob/main/peer/store.go"
    click Transport "https://github.com/tarun-kavipurapu/p2p-file-transfer/blob/main/pkg/transport.go"
    click TCP "https://github.com/tarun-kavipurapu/p2p-file-transfer/blob/main/pkg/tcp_transport.go"
    click Comm "https://github.com/tarun-kavipurapu/p2p-file-transfer/blob/main/pkg/commStructures.go"
    click Message "https://github.com/tarun-kavipurapu/p2p-file-transfer/blob/main/pkg/message.go"
    click Encoding "https://github.com/tarun-kavipurapu/p2p-file-transfer/blob/main/pkg/encoding.go"
    click Metadata "https://github.com/tarun-kavipurapu/p2p-file-transfer/blob/main/pkg/metadata.go"
    click Utils "https://github.com/tarun-kavipurapu/p2p-file-transfer/blob/main/pkg/utils.go"
    click GoMod "https://github.com/tarun-kavipurapu/p2p-file-transfer/blob/main/go.mod"
    click Makefile "https://github.com/tarun-kavipurapu/p2p-file-transfer/tree/main/Makefile"

    %% Styles
    classDef cli fill:#FFA500,stroke:#333,stroke-width:1px
    classDef central fill:#87CEFA,stroke:#333,stroke-width:1px
    classDef peer fill:#90EE90,stroke:#333,stroke-width:1px
    classDef store fill:#D3D3D3,stroke:#333,stroke-width:1px
    classDef shared fill:#D8BFD8,stroke:#333,stroke-width:1px
    classDef config fill:#FFFACD,stroke:#333,stroke-width:1px
```
