Byebye hostv1

pull/392/head
Eugene Kim 6 years ago
parent 4b41338ea2
commit 15d3693b49
  1. 119
      p2p/host/hostv1/hostv1.go
  2. 11
      p2p/p2pimpl/p2pimpl.go

@ -1,119 +0,0 @@
package hostv1
import (
"fmt"
"io"
"net"
"time"
"github.com/ethereum/go-ethereum/log"
"github.com/harmony-one/harmony/p2p"
p2p_host "github.com/libp2p/go-libp2p-host"
peer "github.com/libp2p/go-libp2p-peer"
)
// HostV1 is the version 1 p2p host, using direct socket call.
type HostV1 struct {
self p2p.Peer
listener net.Listener
quit chan struct{}
}
// AddPeer do nothing
func (host *HostV1) AddPeer(p *p2p.Peer) error {
return nil
}
// New creates a HostV1
func New(self *p2p.Peer) *HostV1 {
h := &HostV1{
self: *self,
quit: make(chan struct{}, 1),
}
return h
}
// GetSelfPeer gets self peer
func (host *HostV1) GetSelfPeer() p2p.Peer {
return host.self
}
// GetID return ID
func (host *HostV1) GetID() peer.ID {
return peer.ID(fmt.Sprintf("%s:%s", host.self.IP, host.self.Port))
}
// BindHandlerAndServe Version 0 p2p. Going to be deprecated.
func (host *HostV1) BindHandlerAndServe(handler p2p.StreamHandler) {
port := host.self.Port
addr := net.JoinHostPort("", port)
var err error
host.listener, err = net.Listen("tcp4", addr)
if err != nil {
log.Error("Socket listen port failed", "addr", addr, "err", err)
return
}
if host.listener == nil {
log.Error("Listen returned nil", "addr", addr)
return
}
backoff := p2p.NewExpBackoff(250*time.Millisecond, 15*time.Second, 2.0)
for { // Keep listening
conn, err := host.listener.Accept()
select {
case <-host.quit:
// If we've already received quit signal, simply ignore the error and return
log.Info("Quit host", "addr", net.JoinHostPort(host.self.IP, host.self.Port))
return
default:
{
if err != nil {
log.Error("Error listening on port.", "port", port,
"err", err)
backoff.Sleep()
continue
}
// log.Debug("Received New connection", "local", conn.LocalAddr(), "remote", conn.RemoteAddr())
go handler(conn)
}
}
}
}
// SendMessage sends message to peer
func (host *HostV1) SendMessage(peer p2p.Peer, message []byte) error {
logger := log.New("from", host.self, "to", peer, "PeerID", peer.PeerID)
addr := net.JoinHostPort(peer.IP, peer.Port)
conn, err := net.Dial("tcp", addr)
if err != nil {
logger.Warn("Dial() failed", "address", addr, "error", err)
return fmt.Errorf("Dial(%s) failed: %v", addr, err)
}
defer func() {
if err := conn.Close(); err != nil {
logger.Warn("Close() failed", "error", err)
}
}()
if nw, err := conn.Write(message); err != nil {
logger.Warn("Write() failed", "error", err)
return fmt.Errorf("Write() failed: %v", err)
} else if nw < len(message) {
logger.Warn("Short Write()", "actual", nw, "expected", len(message))
return io.ErrShortWrite
}
// No ack (reply) message from the receiver for now.
return nil
}
// Close closes the host
func (host *HostV1) Close() error {
host.quit <- struct{}{}
return host.listener.Close()
}
// GetP2PHost returns nothing
func (host *HostV1) GetP2PHost() p2p_host.Host {
return nil
}

@ -4,7 +4,6 @@ import (
"net"
"github.com/harmony-one/harmony/p2p"
"github.com/harmony-one/harmony/p2p/host/hostv1"
"github.com/harmony-one/harmony/p2p/host/hostv2"
"github.com/harmony-one/harmony/internal/utils"
@ -12,20 +11,10 @@ import (
p2p_config "github.com/libp2p/go-libp2p/config"
)
// Version The version number of p2p library
// 1 - Direct socket connection
// 2 - libp2p
const Version = 2
// NewHost starts the host for p2p
// for hostv2, it generates multiaddress, keypair and add PeerID to peer, add priKey to host
// TODO (leo) The peerstore has to be persisted on disk.
func NewHost(self *p2p.Peer, key p2p_crypto.PrivKey, opts ...p2p_config.Option) (p2p.Host, error) {
if Version == 1 {
h := hostv1.New(self)
return h, nil
}
h := hostv2.New(self, key, opts...)
utils.GetLogInstance().Info("NewHost", "self", net.JoinHostPort(self.IP, self.Port), "PeerID", self.PeerID)

Loading…
Cancel
Save