swarm/network/stream: add SubscribeErrorMsg and UnsubscribeMsg

This commit is contained in:
Janos Guljas 2018-01-31 15:45:53 +01:00
parent 679867dccb
commit a14e4e7280
3 changed files with 54 additions and 6 deletions

View file

@ -66,7 +66,17 @@ type SubscribeMsg struct {
Priority uint8 // delivered on priority channel Priority uint8 // delivered on priority channel
} }
func (p *Peer) handleSubscribeMsg(req *SubscribeMsg) error { func (p *Peer) handleSubscribeMsg(req *SubscribeMsg) (err error) {
defer func() {
if err != nil {
if e := p.Send(SubscribeErrorMsg{
Error: err.Error(),
}); e != nil {
log.Error("send stream subscribe error message", "err", err)
}
}
}()
f, err := p.streamer.GetServerFunc(req.Stream) f, err := p.streamer.GetServerFunc(req.Stream)
if err != nil { if err != nil {
return err return err
@ -77,7 +87,7 @@ func (p *Peer) handleSubscribeMsg(req *SubscribeMsg) error {
} }
os, err := p.setServer(req.Stream, req.Key, s, req.Priority) os, err := p.setServer(req.Stream, req.Key, s, req.Priority)
if err != nil { if err != nil {
return nil return err
} }
log.Debug("received subscription", "peer", p.ID(), "stream", req.Stream, "Key", req.Key, "from", req.From, "to", req.To) log.Debug("received subscription", "peer", p.ID(), "stream", req.Stream, "Key", req.Key, "from", req.From, "to", req.To)
go func() { go func() {
@ -88,6 +98,24 @@ func (p *Peer) handleSubscribeMsg(req *SubscribeMsg) error {
return nil return nil
} }
type SubscribeErrorMsg struct {
Error string
}
func (p *Peer) handleSubscribeErrorMsg(req *SubscribeErrorMsg) (err error) {
return fmt.Errorf("subscribe to peer %s: %v", p.ID(), req.Error)
}
type UnsubscribeMsg struct {
Stream string
Key []byte
}
func (p *Peer) handleUnsubscribeMsg(req *UnsubscribeMsg) error {
p.removeServer(req.Stream, req.Key)
return nil
}
// OfferedHashesMsg is the protocol msg for offering to hand over a // OfferedHashesMsg is the protocol msg for offering to hand over a
// stream section // stream section
type OfferedHashesMsg struct { type OfferedHashesMsg struct {
@ -247,5 +275,3 @@ func (p *Peer) handleTakeoverProofMsg(req *TakeoverProofMsg) error {
// store the strongest takeoverproof for the stream in streamer // store the strongest takeoverproof for the stream in streamer
return nil return nil
} }
type UnsubscribeMsg struct{}

View file

@ -18,6 +18,7 @@ package stream
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"sync" "sync"
"time" "time"
@ -30,6 +31,8 @@ import (
var sendTimeout = 5 * time.Second var sendTimeout = 5 * time.Second
var errServerNotFound = errors.New("server not found")
// Peer is the Peer extention for the streaming protocol // Peer is the Peer extention for the streaming protocol
type Peer struct { type Peer struct {
*protocols.Peer *protocols.Peer
@ -145,6 +148,20 @@ func (p *Peer) setServer(s string, key []byte, o Server, priority uint8) (*serve
return os, nil return os, nil
} }
func (p *Peer) removeServer(s string, key []byte) error {
p.serverMu.Lock()
defer p.serverMu.Unlock()
sk := s + keyToString(key)
server, ok := p.servers[sk]
if !ok {
return errServerNotFound
}
server.Close()
delete(p.servers, sk)
return nil
}
func (p *Peer) setClient(s string, key []byte, i Client, priority uint8, live bool) error { func (p *Peer) setClient(s string, key []byte, i Client, priority uint8, live bool) error {
p.clientMu.Lock() p.clientMu.Lock()
defer p.clientMu.Unlock() defer p.clientMu.Unlock()

View file

@ -151,8 +151,7 @@ func (r *Registry) Subscribe(peerId discover.NodeID, s string, t []byte, from, t
} }
log.Debug("Subscribe ", "peer", peerId, "stream", s, "key", t, "from", from, "to", to) log.Debug("Subscribe ", "peer", peerId, "stream", s, "key", t, "from", from, "to", to)
peer.SendPriority(msg, priority) return peer.SendPriority(msg, priority)
return nil
} }
func (r *Registry) Retrieve(chunk *storage.Chunk) error { func (r *Registry) Retrieve(chunk *storage.Chunk) error {
@ -218,6 +217,12 @@ func (p *Peer) HandleMsg(msg interface{}) error {
case *SubscribeMsg: case *SubscribeMsg:
return p.handleSubscribeMsg(msg) return p.handleSubscribeMsg(msg)
case *SubscribeErrorMsg:
return p.handleSubscribeErrorMsg(msg)
case *UnsubscribeMsg:
return p.handleUnsubscribeMsg(msg)
case *OfferedHashesMsg: case *OfferedHashesMsg:
return p.handleOfferedHashesMsg(msg) return p.handleOfferedHashesMsg(msg)