-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support connectivity check callback (#128)
- Loading branch information
Showing
15 changed files
with
389 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package dae | ||
|
||
import ( | ||
"container/list" | ||
"context" | ||
|
||
"github.com/daeuniverse/dae/control" | ||
) | ||
|
||
type DaeMsgProducer struct { | ||
chMsg <-chan *control.Msg | ||
// resetChMsg is used to re-assign chMsg. | ||
resetChMsg chan struct{} | ||
daeSubscriber chan *daeSubscriber | ||
} | ||
|
||
func NewDaeMsgProducer(chMsg <-chan *control.Msg) *DaeMsgProducer { | ||
r := &DaeMsgProducer{ | ||
chMsg: chMsg, | ||
resetChMsg: make(chan struct{}), | ||
daeSubscriber: make(chan *daeSubscriber), | ||
} | ||
return r | ||
} | ||
|
||
func (r *DaeMsgProducer) ReassignChMsg(chMsg <-chan *control.Msg) { | ||
r.chMsg = chMsg | ||
r.resetChMsg <- struct{}{} | ||
} | ||
|
||
func (r *DaeMsgProducer) Run() { | ||
subs := list.New().Init() | ||
for { | ||
select { | ||
// New subscriber. | ||
case sub := <-r.daeSubscriber: | ||
subs.PushBack(sub) | ||
// Reset pointer to r.chMsg. | ||
case <-r.resetChMsg: | ||
|
||
// Producer msg. | ||
case msg := <-r.chMsg: | ||
for node := subs.Front(); node != nil; node = node.Next() { | ||
sub := node.Value.(*daeSubscriber) | ||
select { | ||
case <-sub.stop: | ||
subs.Remove(node) | ||
case sub.msgs <- msg: | ||
default: | ||
// Busy. | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
type daeSubscriber struct { | ||
msgs chan<- *control.Msg | ||
stop <-chan struct{} | ||
} | ||
|
||
func (r *DaeMsgProducer) Subscribe(ctx context.Context) <-chan *control.Msg { | ||
c := make(chan *control.Msg, 10) | ||
r.daeSubscriber <- &daeSubscriber{msgs: c, stop: ctx.Done()} | ||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.