aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/peer.go
blob: 34b6152a38438a9e428b258db40738d9e6a4e578 (plain) (blame)
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
63
64
65
66
package p2p

import (
    "fmt"
    "net"
    "strconv"
)

type Peer struct {
    Inbound          bool // inbound (via listener) or outbound (via dialout)
    Address          net.Addr
    Host             []byte
    Port             uint16
    Pubkey           []byte
    Id               string
    Caps             []string
    peerErrorChan    chan error
    messenger        *messenger
    peerErrorHandler *PeerErrorHandler
    server           *Server
}

func NewPeer(conn net.Conn, address net.Addr, inbound bool, server *Server) *Peer {
    peerErrorChan := NewPeerErrorChannel()
    host, port, _ := net.SplitHostPort(address.String())
    intport, _ := strconv.Atoi(port)
    peer := &Peer{
        Inbound:       inbound,
        Address:       address,
        Port:          uint16(intport),
        Host:          net.ParseIP(host),
        peerErrorChan: peerErrorChan,
        server:        server,
    }
    peer.messenger = newMessenger(peer, conn, peerErrorChan, server.Handlers())
    peer.peerErrorHandler = NewPeerErrorHandler(address, server.PeerDisconnect(), peerErrorChan)
    return peer
}

func (self *Peer) String() string {
    var kind string
    if self.Inbound {
        kind = "inbound"
    } else {
        kind = "outbound"
    }
    return fmt.Sprintf("%v:%v (%s) v%v %v", self.Host, self.Port, kind, self.Id, self.Caps)
}

func (self *Peer) Write(protocol string, msg Msg) error {
    return self.messenger.writeProtoMsg(protocol, msg)
}

func (self *Peer) Start() {
    self.peerErrorHandler.Start()
    self.messenger.Start()
}

func (self *Peer) Stop() {
    self.peerErrorHandler.Stop()
    self.messenger.Stop()
}

func (p *Peer) Encode() []interface{} {
    return []interface{}{p.Host, p.Port, p.Pubkey}
}