forked from septemhill/dpos-pbft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer.go
62 lines (53 loc) · 1.21 KB
/
peer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"context"
"encoding/gob"
"fmt"
"log"
"net"
"strconv"
)
//Peer p
type Peer struct {
ID int64
NodeID int64
Conn net.Conn
ConnEncoder *gob.Encoder
}
//NewPeer create a new peer
func NewPeer(ctx context.Context, peerID, port int64, node *Node) *Peer {
conn, err := net.Dial("tcp", ":"+strconv.FormatInt(port, 10))
if err != nil {
log.Println("Dial Failed", err)
}
peer := &Peer{
ID: peerID,
NodeID: node.ID,
Conn: conn,
ConnEncoder: gob.NewEncoder(conn),
}
go handleConnection(ctx, conn, gob.NewDecoder(conn), node)
fmt.Println("Node ", node.ID, " connect to peer ", peerID)
SendMessage(InitMessage(node.ID), peer.ConnEncoder, node.ID)
return peer
}
//NewPeer create a new peer
//func NewPeer(peerID, nodeID, port int64) *Peer {
// conn, err := net.Dial("tcp", ":"+strconv.FormatInt(port, 10))
//
// if err != nil {
// log.Println("Dial Failed")
// }
//
// peer := &Peer{
// ID: peerID,
// NodeID: nodeID,
// Conn: conn,
// ConnEncoder: gob.NewEncoder(conn),
// }
//
// fmt.Println("Node ", nodeID, " connect to peer ", peerID)
// SendMessage(InitMessage(nodeID), peer.ConnEncoder, nodeID)
//
// return peer
//}