-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a831c7
commit 0d890ff
Showing
6 changed files
with
95 additions
and
110 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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,49 @@ | ||
package stream | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/libp2p/go-libp2p-net" | ||
"github.com/ubclaunchpad/cumulus/message" | ||
) | ||
|
||
// Stream is a synchronized container for net.Stream and a mapping of listener | ||
// ids to response channels | ||
type Stream struct { | ||
net.Stream | ||
listeners map[string]chan *message.Response | ||
lock *sync.RWMutex | ||
} | ||
|
||
// New returns a new Stream containing the given net.Stream | ||
func New(s net.Stream) *Stream { | ||
return &Stream{ | ||
s, | ||
make(map[string]chan *message.Response), | ||
&sync.RWMutex{}, | ||
} | ||
} | ||
|
||
// NewListener synchronously adds a listener to this peer's listeners map | ||
func (s *Stream) NewListener(id string) chan *message.Response { | ||
s.lock.Lock() | ||
s.listeners[id] = make(chan *message.Response) | ||
s.lock.Unlock() | ||
return s.listeners[id] | ||
} | ||
|
||
// RemoveListener synchronously removes a listener from this peer's listeners map | ||
func (s *Stream) RemoveListener(id string) { | ||
s.lock.Lock() | ||
delete(s.listeners, id) | ||
s.lock.Unlock() | ||
} | ||
|
||
// Listener synchronously retrieves the channel the listener with the given | ||
// request/response id is waiting on | ||
func (s *Stream) Listener(id string) chan *message.Response { | ||
s.lock.RLock() | ||
lchan := s.listeners[id] | ||
s.lock.RUnlock() | ||
return lchan | ||
} |
Oops, something went wrong.