Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Construct Infra Messenger from Configs only #2506

Merged
merged 2 commits into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions go/integration/cert_req/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ func (c client) run() int {
}
log.Debug("Send on", "local", c.conn.LocalAddr())
c.msgr = messenger.New(
integration.Local.IA,
disp.New(
transport.NewPacketTransport(c.conn),
messenger.DefaultAdapter,
log.Root(),
), nil, log.Root(), nil,
&messenger.Config{
IA: integration.Local.IA,
Dispatcher: disp.New(
transport.NewPacketTransport(c.conn),
messenger.DefaultAdapter,
log.Root(),
),
},
)
if err = getRemote(); err != nil {
integration.LogFatal("Error finding remote address", err)
Expand Down
18 changes: 9 additions & 9 deletions go/lib/infra/infraenv/infraenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ func InitMessengerWithSciond(ia addr.IA, public, bind *snet.Addr, svc addr.HostS
return nil, err
}
msger := messenger.New(
ia,
disp.New(
transport.NewPacketTransport(conn),
messenger.DefaultAdapter,
log.Root(),
),
store,
log.Root(),
nil,
&messenger.Config{
IA: ia,
Dispatcher: disp.New(
transport.NewPacketTransport(conn),
messenger.DefaultAdapter,
log.Root(),
),
TrustStore: store,
},
)
store.SetMessenger(msger)
return msger, nil
Expand Down
30 changes: 20 additions & 10 deletions go/lib/infra/messenger/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ const (

// Config can be used to customize the behavior of the Messenger.
type Config struct {
// IA is the local ISD-AS number.
IA addr.IA
// Dispatcher to use for associating requests with replies.
Dispatcher *disp.Dispatcher
// TrustStore stores and retrieves certificate chains and TRCs.
TrustStore infra.TrustStore
// HandlerTimeout is the amount of time allocated to the processing of a
// received message. This includes the time needed to verify the signature
// and the execution of a registered handler (if one exists). If the
Expand All @@ -115,12 +121,18 @@ type Config struct {
// If WaitForAcks is set to true, notifications wait for Ack messages to be
// received before returning.
WaitForAcks bool
// Logger is used for internal Messenger logging. If it is nil, the default
// root logger is used.
Logger log.Logger
}

func (c *Config) loadDefaults() {
func (c *Config) InitDefaults() {
if c.HandlerTimeout == 0 {
c.HandlerTimeout = DefaultHandlerTimeout
}
if c.Logger == nil {
c.Logger = log.Root()
}
}

var _ infra.Messenger = (*Messenger)(nil)
Expand Down Expand Up @@ -156,34 +168,32 @@ type Messenger struct {
log log.Logger
}

// New creates a new Messenger that uses dispatcher for sending and receiving
// messages, and trustStore as crypto information database.
func New(ia addr.IA, dispatcher *disp.Dispatcher, store infra.TrustStore, logger log.Logger,
config *Config) *Messenger {
// New creates a new Messenger based on config.
func New(config *Config) *Messenger {

initMetrics()
if config == nil {
config = &Config{}
}
config.loadDefaults()
config.InitDefaults()
// XXX(scrye): A trustStore object is passed to the Messenger as it is required
// to verify top-level signatures. This is never used right now since only
// unsigned messages are supported. The content of received messages is
// processed in the relevant handlers which have their own reference to the
// trustStore.
ctx, cancelF := context.WithCancel(context.Background())
return &Messenger{
ia: ia,
ia: config.IA,
config: config,
dispatcher: dispatcher,
dispatcher: config.Dispatcher,
signer: infra.NullSigner,
verifier: infra.NullSigVerifier,
trustStore: store,
trustStore: config.TrustStore,
handlers: make(map[infra.MessageType]infra.Handler),
closeChan: make(chan struct{}),
ctx: ctx,
cancelF: cancelF,
log: logger,
log: config.Logger,
}
}

Expand Down
18 changes: 11 additions & 7 deletions go/lib/infra/messenger/messenger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,17 @@ func TestTRCExchange(t *testing.T) {
}

func setupMessenger(ia addr.IA, conn net.PacketConn, name string) *Messenger {
dispatcher := disp.New(
transport.NewPacketTransport(conn),
DefaultAdapter,
log.New("name", name),
)
config := &Config{DisableSignatureVerification: true}
return New(ia, dispatcher, nil, log.Root().New("name", name), config)
config := &Config{
IA: ia,
DisableSignatureVerification: true,
Dispatcher: disp.New(
transport.NewPacketTransport(conn),
DefaultAdapter,
log.New("name", name),
),
Logger: log.Root().New("name", name),
}
return New(config)
}

func TestMain(m *testing.M) {
Expand Down
19 changes: 12 additions & 7 deletions go/lib/infra/modules/trust/trust_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,13 +690,18 @@ func TestChainReqHandler(t *testing.T) {
}

func setupMessenger(ia addr.IA, conn net.PacketConn, store *Store, name string) infra.Messenger {
dispatcher := disp.New(
transport.NewPacketTransport(conn),
messenger.DefaultAdapter,
log.New("name", name),
)
config := &messenger.Config{DisableSignatureVerification: true}
return messenger.New(ia, dispatcher, store, log.Root().New("name", name), config)
config := &messenger.Config{
IA: ia,
Dispatcher: disp.New(
transport.NewPacketTransport(conn),
messenger.DefaultAdapter,
log.New("name", name),
),
TrustStore: store,
DisableSignatureVerification: true,
Logger: log.Root().New("name", name),
}
return messenger.New(config)
}

func loadCrypto(t *testing.T, isds []addr.ISD,
Expand Down