Skip to content

Commit

Permalink
feat(net:service, routing) remove error return value
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Tiger Chow committed Sep 24, 2014
1 parent 245dbd6 commit 623cb6c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 30 deletions.
16 changes: 8 additions & 8 deletions exchange/bitswap/network/net_message_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,32 @@ type impl struct {
// HandleMessage marshals and unmarshals net messages, forwarding them to the
// BitSwapMessage receiver
func (adapter *impl) HandleMessage(
ctx context.Context, incoming netmsg.NetMessage) (netmsg.NetMessage, error) {
ctx context.Context, incoming netmsg.NetMessage) netmsg.NetMessage {

if adapter.receiver == nil {
return nil, nil
return nil
}

received, err := bsmsg.FromNet(incoming)
if err != nil {
adapter.receiver.ReceiveError(err)
return nil, nil
go adapter.receiver.ReceiveError(err)
return nil
}

p, bsmsg := adapter.receiver.ReceiveMessage(ctx, incoming.Peer(), received)

// TODO(brian): put this in a helper function
if bsmsg == nil || p == nil {
return nil, nil
return nil
}

outgoing, err := bsmsg.ToNet(p)
if err != nil {
adapter.receiver.ReceiveError(err)
return nil, nil
go adapter.receiver.ReceiveError(err)
return nil
}

return outgoing, nil
return outgoing
}

func (adapter *impl) SendMessage(
Expand Down
8 changes: 2 additions & 6 deletions net/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Handler interface {

// HandleMessage receives an incoming message, and potentially returns
// a response message to send back.
HandleMessage(context.Context, msg.NetMessage) (msg.NetMessage, error)
HandleMessage(context.Context, msg.NetMessage) msg.NetMessage
}

// Service is a networking component that protocols can use to multiplex
Expand Down Expand Up @@ -181,11 +181,7 @@ func (s *Service) handleIncomingMessage(ctx context.Context, m msg.NetMessage) {
}

// should this be "go HandleMessage ... ?"
r1, err := s.Handler.HandleMessage(ctx, m2)
if err != nil {
u.PErr("handled message yielded error %v\n", err)
return
}
r1 := s.Handler.HandleMessage(ctx, m2)

// if handler gave us a response, send it back out!
if r1 != nil {
Expand Down
5 changes: 2 additions & 3 deletions net/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ import (
// ReverseHandler reverses all Data it receives and sends it back.
type ReverseHandler struct{}

func (t *ReverseHandler) HandleMessage(ctx context.Context, m msg.NetMessage) (
msg.NetMessage, error) {
func (t *ReverseHandler) HandleMessage(ctx context.Context, m msg.NetMessage) msg.NetMessage {

d := m.Data()
for i, j := 0, len(d)-1; i < j; i, j = i+1, j-1 {
d[i], d[j] = d[j], d[i]
}

return msg.New(m.Peer(), d), nil
return msg.New(m.Peer(), d)
}

func newPeer(t *testing.T, id string) *peer.Peer {
Expand Down
24 changes: 15 additions & 9 deletions routing/dht/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,26 @@ func (dht *IpfsDHT) Connect(ctx context.Context, npeer *peer.Peer) (*peer.Peer,
}

// HandleMessage implements the inet.Handler interface.
func (dht *IpfsDHT) HandleMessage(ctx context.Context, mes msg.NetMessage) (msg.NetMessage, error) {
func (dht *IpfsDHT) HandleMessage(ctx context.Context, mes msg.NetMessage) msg.NetMessage {

mData := mes.Data()
if mData == nil {
return nil, errors.New("message did not include Data")
// TODO handle/log err
return nil
}

mPeer := mes.Peer()
if mPeer == nil {
return nil, errors.New("message did not include a Peer")
// TODO handle/log err
return nil
}

// deserialize msg
pmes := new(Message)
err := proto.Unmarshal(mData, pmes)
if err != nil {
return nil, fmt.Errorf("Failed to decode protobuf message: %v\n", err)
// TODO handle/log err
return nil
}

// update the peer (on valid msgs only)
Expand All @@ -133,27 +136,30 @@ func (dht *IpfsDHT) HandleMessage(ctx context.Context, mes msg.NetMessage) (msg.
// get handler for this msg type.
handler := dht.handlerForMsgType(pmes.GetType())
if handler == nil {
return nil, errors.New("Recieved invalid message type")
// TODO handle/log err
return nil
}

// dispatch handler.
rpmes, err := handler(mPeer, pmes)
if err != nil {
return nil, err
// TODO handle/log err
return nil
}

// if nil response, return it before serializing
if rpmes == nil {
return nil, nil
return nil
}

// serialize response msg
rmes, err := msg.FromObject(mPeer, rpmes)
if err != nil {
return nil, fmt.Errorf("Failed to encode protobuf message: %v\n", err)
// TODO handle/log err
return nil
}

return rmes, nil
return rmes
}

// sendRequest sends out a request using dht.sender, but also makes sure to
Expand Down
5 changes: 1 addition & 4 deletions routing/dht/ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,7 @@ func TestGetFailures(t *testing.T) {
t.Error(err)
}

mes, err = d.HandleMessage(ctx, mes)
if err != nil {
t.Error(err)
}
mes = d.HandleMessage(ctx, mes)

pmes := new(Message)
err = proto.Unmarshal(mes.Data(), pmes)
Expand Down

0 comments on commit 623cb6c

Please sign in to comment.