Skip to content

Commit

Permalink
Prevent port from having two connections (#138)
Browse files Browse the repository at this point in the history
* Prevent port from having two connections

* regenerate mock files
  • Loading branch information
syifan authored Sep 5, 2024
1 parent 1312d4f commit 2b70cea
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 2 deletions.
14 changes: 14 additions & 0 deletions mem/idealmemcontroller/mock_sim_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sim/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func NewSendError() *SendError {

// A Connection is responsible for delivering messages to its destination.
type Connection interface {
Named
Hookable

// PlugIn connects a port to the connection. The connection should reserve
Expand Down
6 changes: 5 additions & 1 deletion sim/directconnection/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ func (b Builder) WithFreq(f sim.Freq) Builder {
func (b Builder) Build(name string) *Comp {
c := &Comp{}
c.TickingComponent = sim.NewSecondaryTickingComponent(name, b.engine, b.freq, c)
middleware := &middleware{Comp: c}

middleware := &middleware{
Comp: c,
}
c.AddMiddleware(middleware)

return c
}
4 changes: 3 additions & 1 deletion sim/directconnection/directconnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ func (m *middleware) forwardMany(
break
}

err := head.Meta().Dst.Deliver(head)
dst := head.Meta().Dst

err := dst.Deliver(head)
if err != nil {
break
}
Expand Down
14 changes: 14 additions & 0 deletions sim/directconnection/mock_sim_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions sim/mock_sim_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions sim/port.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sim

import (
"fmt"
"sync"
)

Expand Down Expand Up @@ -51,6 +52,16 @@ var HookPosPortMsgRetrieve = &HookPos{Name: "Port Msg Retrieve"}

// SetConnection sets which connection plugged in to this port.
func (p *LimitNumMsgPort) SetConnection(conn Connection) {
if p.conn != nil {
connName := p.conn.Name()
newConnName := conn.Name()
panicMsg := fmt.Sprintf(
"connection already set to %s, now connecting to %s",
connName, newConnName,
)
panic(panicMsg)
}

p.conn = conn
}

Expand Down

0 comments on commit 2b70cea

Please sign in to comment.