Skip to content
/ p2p Public
forked from lynn9388/p2p

A simple P2P network implementation.

License

Notifications You must be signed in to change notification settings

suhasagg/p2p

 
 

Repository files navigation

P2P

GoDoc Build Status

A simple P2P (peer-to-peer) network implementation.

Introduction

Features

  1. Peer discovery and auto management.
  2. Scalability of services.
  3. Broadcast message (flood protocol).

Network

In the P2P network, each node will choose some nodes as their neighbors.

             +------+
    +------->+ node +<------+
    |        +------+       |
    |                       |
    v                       v
+---+--+                 +--+---+
| node |                 | node |
+---+--+                 +--+---+
    ^                       ^
    |                       |
    |        +------+       |
    +------->+ node +<------+
             +------+

Node & Peer

The neighbor "nodes" are looked as "peers" for a node. A node will management the connections for neighbor peers and provide some services for other nodes. We use gRPC for communication between the node and neighbor peers.

                                gRPC
                                  |
                                  v       +----+
                       +-----connection-->+peer|
                       |                  +----+
                       |
                       |
                       |
                   +---+--+               +----+
+----Service---+-->+ node +--connection-->+peer|
|PeerManager   |   +---+--+               +----+
|MessageManager|       |
+--------------+       |
                       |
                       |                  +----+
                       +-----connection-->+peer|
                                          +----+

Install

Fist, use go get to install the latest version of the library:

go get -u github.com/lynn9388/p2p

Next, include this package in your application:

import "github.com/lynn9388/p2p"

Example

The code below shows how to create and launch a new node.

func main() {
	port := flag.Int("port", 9388, "port for server")
	flag.Parse()

	node := p2p.NewNode("localhost:" + strconv.Itoa(*port))
	node.StartServer()
	defer node.StopServer()
	node.PeerManager.StartDiscoverPeers("localhost:9388")
	defer node.PeerManager.StopDiscoverPeers()
	node.Wait()
}

Try to run several examples with different port.

For more information you can check the GoDoc

About

A simple P2P network implementation.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%