Skip to content

Commit

Permalink
Add networker stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Sep 20, 2019
1 parent 946a3dc commit f5986d5
Show file tree
Hide file tree
Showing 17 changed files with 1,122 additions and 576 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ require (
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7
golang.org/x/net v0.0.0-20190916140828-c8589233b77d
)

replace github.com/skycoin/dmsg => ../dmsg
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxv
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
Expand Down Expand Up @@ -101,14 +104,18 @@ github.com/prometheus/procfs v0.0.3 h1:CTwfnzjQ+8dS6MhHHu4YswVAD99sL2wjPqP+VkURm
github.com/prometheus/procfs v0.0.3/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDaekg4FpcdQ=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/skycoin/dmsg v0.0.0-20190805065636-70f4c32a994f h1:WWjaxOXoj6oYelm67MNtJbg51HQALjKAyhs2WAHgpZs=
github.com/skycoin/dmsg v0.0.0-20190805065636-70f4c32a994f/go.mod h1:obZYZp8eKR7Xqz+KNhJdUE6Gvp6rEXbDO8YTlW2YXgU=
github.com/skycoin/skycoin v0.25.1/go.mod h1:78nHjQzd8KG0jJJVL/j0xMmrihXi70ti63fh8vXScJw=
github.com/skycoin/skycoin v0.26.0 h1:xDxe2r8AclMntZ550Y/vUQgwgLtwrf9Wu5UYiYcN5/o=
github.com/skycoin/skycoin v0.26.0/go.mod h1:78nHjQzd8KG0jJJVL/j0xMmrihXi70ti63fh8vXScJw=
github.com/skycoin/skywire v0.1.1/go.mod h1:jDuUgTG20jhiBI6Trpayj0my6xhdS+ejEO9gTSM+C/E=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s=
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
Expand All @@ -124,16 +131,19 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/bbolt v1.3.3 h1:MUGmc65QhB3pIlaQ5bB4LwqSj6GIonVJXpZiaKNyaKk=
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7 h1:0hQKqeLdqlt5iIwVOBErRisrHJAN57yOiPRQItI20fU=
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190313220215-9f648a60d977/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down
1 change: 1 addition & 0 deletions pkg/app2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (c *Client) Dial(remote routing.Addr) (net.Conn, error) {
if err != nil {
return nil, err
}
net.Dial()

connID, err := c.rpc.Dial(remote)
if err != nil {
Expand Down
28 changes: 28 additions & 0 deletions pkg/app2/network/addr.go
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)
}
84 changes: 84 additions & 0 deletions pkg/app2/network/networker.go
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)
}
21 changes: 21 additions & 0 deletions pkg/app2/network/type.go
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: {},
}
)
Loading

0 comments on commit f5986d5

Please sign in to comment.