Node class is the core component of the P2P file sharing system. It manages TCP connections, implements the messaging protocol, and coordinates piece downloads and uploads.
Constructor
Creates a new P2P node instance.TCP port for listening to incoming peer connections
Path to the file. For seeders, this should be an existing file. For leechers, this is the destination path where the file will be downloaded.
Properties
Unique peer identifier (16 hexadecimal characters generated from 8 random bytes)
TCP port for listening to connections
Path to the file being shared or downloaded
Name of the file (extracted from filePath or received from peer metadata)
Total file size in bytes
SHA-1 hash of the file content (used as content identifier)
Size of each piece in bytes (64 KiB by default). Automatically adjusted if file is smaller.
Total number of pieces the file is divided into
Indicates whether this node has the complete file
Set of piece indices that this node possesses
Set of piece indices that are still needed for download
Set of piece indices that have been requested but not yet received
Map of known peers (key: peer ID, value: peer information object)
File manager instance for reading/writing pieces
Connection Methods
startListening()
Starts the TCP server to accept incoming peer connections.- Creates a TCP server on the specified port
- Accepts incoming connections from other peers
- Sets up socket handlers for each connection
- Logs the node ID and listening port
connectToPeer(host, port)
Establishes an outgoing connection to another peer.IP address or hostname of the peer
TCP port of the peer
- Creates an outgoing TCP connection
- Sends a handshake message upon connection
- Sets up socket handlers
- Handles connection errors
Protocol Methods
_sendHandshake(socket)
Sends a handshake message containing file metadata.TCP socket to send the handshake through
_sendBitfield(socket)
Sends a bitfield message indicating which pieces this node possesses.TCP socket to send the bitfield through
_sendRequest(socket, index)
Sends a request for a specific piece to a peer.TCP socket of the peer to request from
Zero-based index of the piece to request
Message Handling Methods
_handleMessage(socket, message)
Routes incoming messages to appropriate handlers based on message type.TCP socket that received the message
Parsed JSON message object
handshake- Initial peer identification and file metadatabitfield- List of pieces a peer possessesrequest- Request for a specific piecepiece- Piece data deliveryhave- Notification that a peer acquired a new piecepeers- List of additional known peers
_handleHandshake(socket, message)
Processes incoming handshake messages.- Validates peer ID (rejects if same as own ID)
- Verifies file hash matches (disconnects if mismatch)
- Adopts file metadata if this is a leecher
- Exchanges peer lists for peer discovery
- Sends bitfield if this node has pieces
_handleBitfield(socket, message)
Processes bitfield messages containing peer’s available pieces._handleRequest(socket, message)
Processes piece request messages from peers._handlePiece(socket, message)
Processes received piece data and writes it to the file.- Verifies file integrity when download completes
- Transitions node to seed mode after completion
- Schedules next piece requests
_handleHave(socket, message)
Processes notifications that a peer acquired a new piece._handlePeers(socket, message)
Processes peer list messages for peer discovery.Internal Methods
_setupSocketHandlers(socket)
Configures event handlers for a peer connection socket.TCP socket to configure
data- Buffers and parses JSON messages (newline-delimited)close- Cleans up peer state and reschedules requestserror- Logs connection errors
_scheduleRequests()
Assigns piece download requests to available peers._shouldInitiateConnection(otherPeerId)
Determines whether this node should initiate a connection to avoid duplicates.ID of the other peer
true if this node’s ID is greater (lexicographically) than the other peer’s ID.
_startProgressInterval()
Starts a periodic timer to display download progress.- Download percentage
- Bytes downloaded / total bytes
- Download speed in KB/s