-
Notifications
You must be signed in to change notification settings - Fork 45
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
Showing
17 changed files
with
1,122 additions
and
576 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
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,28 @@ | ||
package network | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/skycoin/dmsg/cipher" | ||
"github.com/skycoin/skywire/pkg/routing" | ||
) | ||
|
||
// Addr implements net.Addr for network addresses. | ||
type Addr struct { | ||
Net Type | ||
PubKey cipher.PubKey | ||
Port routing.Port | ||
} | ||
|
||
// Network returns "dmsg" | ||
func (a Addr) Network() string { | ||
return string(a.Net) | ||
} | ||
|
||
// String returns public key and port of node split by colon. | ||
func (a Addr) String() string { | ||
if a.Port == 0 { | ||
return fmt.Sprintf("%s:~", a.PubKey) | ||
} | ||
return fmt.Sprintf("%s:%d", a.PubKey, a.Port) | ||
} |
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,84 @@ | ||
package network | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net" | ||
"sync" | ||
) | ||
|
||
var ( | ||
// ErrNoSuchNetworker is being returned when there's no suitable networker. | ||
ErrNoSuchNetworker = errors.New("no such networker") | ||
// ErrNetworkerAlreadyExists is being returned when there's already one with such Network type. | ||
ErrNetworkerAlreadyExists = errors.New("networker already exists") | ||
) | ||
|
||
var ( | ||
networkers = map[Type]Networker{} | ||
networkersMx sync.RWMutex | ||
) | ||
|
||
// AddNetworker associated Networker with the `network`. | ||
func AddNetworker(t Type, n Networker) error { | ||
networkersMx.Lock() | ||
defer networkersMx.Unlock() | ||
|
||
if _, ok := networkers[t]; ok { | ||
return ErrNetworkerAlreadyExists | ||
} | ||
|
||
networkers[t] = n | ||
|
||
return nil | ||
} | ||
|
||
// ResolveNetworker resolves Networker by `network`. | ||
func ResolveNetworker(t Type) (Networker, error) { | ||
networkersMx.RLock() | ||
n, ok := networkers[t] | ||
if !ok { | ||
networkersMx.RUnlock() | ||
return nil, ErrNoSuchNetworker | ||
} | ||
networkersMx.RUnlock() | ||
return n, nil | ||
} | ||
|
||
// Networker defines basic network operations, such as Dial/Listen. | ||
type Networker interface { | ||
Dial(addr Addr) (net.Conn, error) | ||
DialContext(ctx context.Context, addr Addr) (net.Conn, error) | ||
Listen(addr Addr) (net.Listener, error) | ||
ListenContext(ctx context.Context, addr Addr) (net.Listener, error) | ||
} | ||
|
||
// Dial dials the remote `addr` of the specified `network`. | ||
func Dial(t Type, addr Addr) (net.Conn, error) { | ||
return DialContext(context.Background(), t, addr) | ||
} | ||
|
||
// DialContext dials the remote `Addr` of the specified `network` with the context. | ||
func DialContext(ctx context.Context, t Type, addr Addr) (net.Conn, error) { | ||
n, err := ResolveNetworker(t) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return n.DialContext(ctx, addr) | ||
} | ||
|
||
// Listen starts listening on the local `addr` of the specified `network`. | ||
func Listen(t Type, addr Addr) (net.Listener, error) { | ||
return ListenContext(context.Background(), t, addr) | ||
} | ||
|
||
// ListenContext starts listening on the local `addr` of the specified `network` with the context. | ||
func ListenContext(ctx context.Context, t Type, addr Addr) (net.Listener, error) { | ||
networker, err := ResolveNetworker(t) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return networker.ListenContext(ctx, addr) | ||
} |
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,21 @@ | ||
package network | ||
|
||
// Type represents the network type. | ||
type Type string | ||
|
||
const ( | ||
// TypeDMSG is a network type for DMSG communication. | ||
TypeDMSG = "dmsg" | ||
) | ||
|
||
// IsValid checks whether the network contains valid value for the type. | ||
func (n Type) IsValid() bool { | ||
_, ok := validNetworks[n] | ||
return ok | ||
} | ||
|
||
var ( | ||
validNetworks = map[Type]struct{}{ | ||
TypeDMSG: {}, | ||
} | ||
) |
Oops, something went wrong.