Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update readme, refactor code for more flexibility, allow configuratio…
…n of the signaling channel
- Loading branch information
1 parent
3edac96
commit 16961e7
Showing
8 changed files
with
167 additions
and
61 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,55 @@ | ||
package channels | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"sync" | ||
) | ||
|
||
// A Channel facilitates signaling. | ||
type Channel interface { | ||
Send(key, data string) error | ||
Recv(key string) (data string, err error) | ||
} | ||
|
||
// A Factory returns a Channel from an address | ||
type Factory = func(addr string) (Channel, error) | ||
|
||
var channelFactories = struct { | ||
sync.Mutex | ||
m map[string]Factory | ||
}{ | ||
m: make(map[string]Factory), | ||
} | ||
|
||
// RegisterFactory registers a new Factory | ||
func RegisterFactory(scheme string, factory Factory) { | ||
channelFactories.Lock() | ||
channelFactories.m[scheme] = factory | ||
channelFactories.Unlock() | ||
} | ||
|
||
// Get returns a channel for the given address | ||
func Get(addr string) (Channel, error) { | ||
u, err := url.Parse(addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
channelFactories.Lock() | ||
factory, ok := channelFactories.m[u.Scheme] | ||
channelFactories.Unlock() | ||
if !ok { | ||
return nil, fmt.Errorf("no channel factory registered for %s", u.Scheme) | ||
} | ||
|
||
return factory(addr) | ||
} | ||
|
||
// Must panics if there's an error | ||
func Must(ch Channel, err error) Channel { | ||
if err != nil { | ||
panic(err) | ||
} | ||
return ch | ||
} |
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
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.