Fix 'add/delnode' type switch evaluation in server

* The cases for the 'addnode' command were previously
  stacked on top the new cases for the 'node' command.
  The intended behavior was to create a fall through and
  handle both commands. However, trying to use this
  syntax with a type switch caused the first case to be
  ignored.
* addnode' specific functions and structs in the server
  have been removed. Instead, the 'add' and 'del' subcommands
  are now proxied to the matching 'node' cmd functions.
This commit is contained in:
Olaoluwa Osuntokun 2015-04-22 11:50:49 -07:00
parent 99ac1f5667
commit ab2ed710cb
2 changed files with 3 additions and 42 deletions

View file

@ -364,11 +364,11 @@ func handleAddNode(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (in
var err error
switch c.SubCmd {
case "add":
err = s.server.AddAddr(addr, true)
err = s.server.ConnectNode(addr, true)
case "remove":
err = s.server.RemoveAddr(addr)
err = s.server.RemoveNodeByAddr(addr)
case "onetry":
err = s.server.AddAddr(addr, false)
err = s.server.ConnectNode(addr, false)
default:
return nil, &btcjson.RPCError{
Code: btcjson.ErrRPCInvalidParameter,

View file

@ -404,17 +404,6 @@ type getPeerInfoMsg struct {
reply chan []*btcjson.GetPeerInfoResult
}
type addNodeMsg struct {
addr string
permanent bool
reply chan error
}
type delNodeMsg struct {
cmp func(*peer) bool
reply chan error
}
type getAddedNodesMsg struct {
reply chan []*peer
}
@ -490,7 +479,6 @@ func (s *server) handleQuery(querymsg interface{}, state *peerState) {
})
msg.reply <- infos
case addNodeMsg:
case connectNodeMsg:
// XXX(oga) duplicate oneshots?
for e := state.persistentPeers.Front(); e != nil; e = e.Next() {
@ -512,8 +500,6 @@ func (s *server) handleQuery(querymsg interface{}, state *peerState) {
} else {
msg.reply <- errors.New("failed to add peer")
}
case delNodeMsg:
case removeNodeMsg:
found := disconnectPeer(state.persistentPeers, msg.cmp, func(p *peer) {
// Keep group counts ok since we remove from
@ -526,7 +512,6 @@ func (s *server) handleQuery(querymsg interface{}, state *peerState) {
} else {
msg.reply <- errors.New("peer not found")
}
// Request a list of the persistent (added) peers.
case getAddedNodesMsg:
// Respond with a slice of the relavent peers.
@ -880,30 +865,6 @@ func (s *server) PeerInfo() []*btcjson.GetPeerInfoResult {
return <-replyChan
}
// AddAddr adds `addr' as a new outbound peer. If permanent is true then the
// peer will be persistent and reconnect if the connection is lost.
// It is an error to call this with an already existing peer.
func (s *server) AddAddr(addr string, permanent bool) error {
replyChan := make(chan error)
s.query <- addNodeMsg{addr: addr, permanent: permanent, reply: replyChan}
return <-replyChan
}
// RemoveAddr removes `addr' from the list of persistent peers if present.
// An error will be returned if the peer was not found.
func (s *server) RemoveAddr(addr string) error {
replyChan := make(chan error)
s.query <- delNodeMsg{
cmp: func(p *peer) bool { return p.addr == addr },
reply: replyChan,
}
return <-replyChan
}
// DisconnectNodeByAddr disconnects a peer by target address. Both outbound and
// inbound nodes will be searched for the target node. An error message will
// be returned if the peer was not found.