Skip to content

Commit

Permalink
protofsm: add returned error for MsgEndpoint methods
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeTsagk committed Sep 17, 2024
1 parent fb66bd2 commit 0bb673f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
16 changes: 12 additions & 4 deletions protofsm/msg_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ type MsgEndpoint interface {

// CanHandle returns true if the target message can be routed to this
// endpoint.
CanHandle(msg PeerMsg) bool
CanHandle(msg PeerMsg) (bool, error)

// SendMessage handles the target message, and returns true if the
// message was able being processed.
SendMessage(msg PeerMsg) bool
SendMessage(msg PeerMsg) (bool, error)
}

// MsgRouter is an interface that represents a message router, which is generic
Expand Down Expand Up @@ -274,12 +274,20 @@ func (p *MultiMsgRouter) msgRouter() {
// to those that can handle it the message.
var couldSend bool
for _, endpoint := range endpoints {
if endpoint.CanHandle(msg) {
canHandle, err := endpoint.CanHandle(msg)
if err != nil {
msgQuery.SendError(err)
}

if canHandle {
log.Debugf("MsgRouter: sending msg %T "+
"to endpoint %s", msg.Message,
endpoint.Name())

sent := endpoint.SendMessage(msg)
sent, err := endpoint.SendMessage(msg)
if err != nil {
msgQuery.SendError(err)
}
couldSend = couldSend || sent
}
}
Expand Down
8 changes: 4 additions & 4 deletions protofsm/msg_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ func (m *mockEndpoint) Name() string {
return args.String(0)
}

func (m *mockEndpoint) CanHandle(msg PeerMsg) bool {
func (m *mockEndpoint) CanHandle(msg PeerMsg) (bool, error) {
args := m.Called(msg)

return args.Bool(0)
return args.Bool(0), nil
}

func (m *mockEndpoint) SendMessage(msg PeerMsg) bool {
func (m *mockEndpoint) SendMessage(msg PeerMsg) (bool, error) {
args := m.Called(msg)

return args.Bool(0)
return args.Bool(0), nil
}

// TestMessageRouterOperation tests the basic operation of the message router:
Expand Down

0 comments on commit 0bb673f

Please sign in to comment.