A simple P2P (peer-to-peer) network implementation.
- Peer discovery and auto management.
- Scalability of services.
- Broadcast message (flood protocol).
In the P2P network, each node will choose some nodes as their neighbors.
+------+
+------->+ node +<------+
| +------+ |
| |
v v
+---+--+ +--+---+
| node | | node |
+---+--+ +--+---+
^ ^
| |
| +------+ |
+------->+ node +<------+
+------+
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|
+----+
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"
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