Skip to content

Commit

Permalink
Introduce type Port to be used instead of uint16
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Jul 7, 2019
1 parent bc74dbc commit 57b9f25
Show file tree
Hide file tree
Showing 22 changed files with 97 additions and 88 deletions.
2 changes: 1 addition & 1 deletion cmd/apps/therealproxy-client/therealproxy-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func main() {

var conn net.Conn
err = r.Do(func() error {
conn, err = socksApp.Dial(&routing.Addr{PubKey: pk, Port: uint16(socksPort)})
conn, err = socksApp.Dial(&routing.Addr{PubKey: pk, Port: routing.Port(socksPort)})
return err
})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/skywire-cli/commands/node/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ var addRuleCmd = &cobra.Command{
var (
routeID = routing.RouteID(parseUint("route-id", args[1], 32))
remotePK = internal.ParsePK("remote-pk", args[2])
remotePort = uint16(parseUint("remote-port", args[3], 16))
localPort = uint16(parseUint("local-port", args[4], 16))
remotePort = routing.Port(parseUint("remote-port", args[3], 16))
localPort = routing.Port(parseUint("local-port", args[4], 16))
)
rule = routing.AppRule(time.Now().Add(expire), routeID, remotePK, remotePort, localPort)
case "fwd":
Expand Down
12 changes: 6 additions & 6 deletions pkg/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ func TestAppDial(t *testing.T) {

addr := &LoopAddr{}
require.NoError(t, json.Unmarshal(<-dataCh, addr))
assert.Equal(t, uint16(2), addr.Port)
assert.Equal(t, routing.Port(2), addr.Port)
assert.Equal(t, rpk, addr.Remote.PubKey)
assert.Equal(t, uint16(3), addr.Remote.Port)
assert.Equal(t, routing.Port(3), addr.Remote.Port)

app.mu.Lock()
require.Len(t, app.conns, 0)
Expand Down Expand Up @@ -150,8 +150,8 @@ func TestAppWrite(t *testing.T) {
packet := &Packet{}
require.NoError(t, json.Unmarshal(<-dataCh, packet))
assert.Equal(t, rpk, packet.Addr.Remote.PubKey)
assert.Equal(t, uint16(3), packet.Addr.Remote.Port)
assert.Equal(t, uint16(2), packet.Addr.Port)
assert.Equal(t, routing.Port(3), packet.Addr.Remote.Port)
assert.Equal(t, routing.Port(2), packet.Addr.Port)
assert.Equal(t, []byte("foo"), packet.Payload)

require.NoError(t, proto.Close())
Expand Down Expand Up @@ -261,9 +261,9 @@ func TestAppClose(t *testing.T) {

addr := &LoopAddr{}
require.NoError(t, json.Unmarshal(<-dataCh, addr))
assert.Equal(t, uint16(2), addr.Port)
assert.Equal(t, routing.Port(2), addr.Port)
assert.Equal(t, pk, addr.Remote.PubKey)
assert.Equal(t, uint16(3), addr.Remote.Port)
assert.Equal(t, routing.Port(3), addr.Remote.Port)
}

func TestAppCommand(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// LoopAddr stores addressing parameters of a loop packets.
type LoopAddr struct {
Port uint16 `json:"port"`
Port routing.Port `json:"port"`
Remote routing.Addr `json:"remote"`
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ type ManagerConfig struct {

// AppConfig defines app startup parameters.
type AppConfig struct {
Version string `json:"version"`
App string `json:"app"`
AutoStart bool `json:"auto_start"`
Port uint16 `json:"port"`
Args []string `json:"args"`
Version string `json:"version"`
App string `json:"app"`
AutoStart bool `json:"auto_start"`
Port routing.Port `json:"port"`
Args []string `json:"args"`
}

// InterfaceConfig defines listening interfaces for skywire Node.
Expand Down
6 changes: 4 additions & 2 deletions pkg/node/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"testing"
"time"

"github.com/skycoin/skywire/pkg/routing"

"github.com/skycoin/dmsg/cipher"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -99,13 +101,13 @@ func TestAppsConfig(t *testing.T) {
app1 := appsConf[0]
assert.Equal(t, "foo", app1.App)
assert.Equal(t, "1.1", app1.Version)
assert.Equal(t, uint16(1), app1.Port)
assert.Equal(t, routing.Port(1), app1.Port)
assert.False(t, app1.AutoStart)

app2 := appsConf[1]
assert.Equal(t, "bar", app2.App)
assert.Equal(t, "1.0", app2.Version)
assert.Equal(t, uint16(2), app2.Port)
assert.Equal(t, routing.Port(2), app2.Port)
assert.True(t, app2.AutoStart)
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ const Version = "0.0.1"

const supportedProtocolVersion = "0.0.1"

var reservedPorts = map[uint16]string{0: "router", 1: "skychat", 2: "SSH", 3: "socksproxy"}
var reservedPorts = map[routing.Port]string{0: "router", 1: "skychat", 2: "SSH", 3: "socksproxy"}

// AppState defines state parameters for a registered App.
type AppState struct {
Name string `json:"name"`
AutoStart bool `json:"autostart"`
Port uint16 `json:"port"`
Status AppStatus `json:"status"`
Name string `json:"name"`
AutoStart bool `json:"autostart"`
Port routing.Port `json:"port"`
Status AppStatus `json:"status"`
}

type appExecuter interface {
Expand All @@ -76,7 +76,7 @@ type appBind struct {
type PacketRouter interface {
io.Closer
Serve(ctx context.Context) error
ServeApp(conn net.Conn, port uint16, appConf *app.Config) error
ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) error
IsSetupTransport(tr *transport.ManagedTransport) bool
}

Expand Down
12 changes: 7 additions & 5 deletions pkg/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"testing"
"time"

"github.com/skycoin/skywire/pkg/routing"

"github.com/skycoin/dmsg/cipher"
"github.com/skycoin/dmsg/disc"
"github.com/skycoin/skycoin/src/util/logging"
Expand Down Expand Up @@ -136,7 +138,7 @@ func TestNodeSpawnApp(t *testing.T) {

ports := r.Ports()
require.Len(t, ports, 1)
assert.Equal(t, uint16(10), ports[0])
assert.Equal(t, routing.Port(10), ports[0])

require.NoError(t, node.StopApp("skychat"))
}
Expand Down Expand Up @@ -225,15 +227,15 @@ func (exc *MockExecuter) Wait(cmd *exec.Cmd) error {
type mockRouter struct {
sync.Mutex

ports []uint16
ports []routing.Port

didStart bool
didClose bool

errChan chan error
}

func (r *mockRouter) Ports() []uint16 {
func (r *mockRouter) Ports() []routing.Port {
r.Lock()
p := r.ports
r.Unlock()
Expand All @@ -245,10 +247,10 @@ func (r *mockRouter) Serve(_ context.Context) error {
return nil
}

func (r *mockRouter) ServeApp(conn net.Conn, port uint16, appConf *app.Config) error {
func (r *mockRouter) ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) error {
r.Lock()
if r.ports == nil {
r.ports = []uint16{}
r.ports = []routing.Port{}
}

r.ports = append(r.ports, port)
Expand Down
4 changes: 2 additions & 2 deletions pkg/node/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ func NewMockRPCClient(r *rand.Rand, maxTps int, maxRules int) (cipher.PubKey, RP
var lpRaw, rpRaw [2]byte
r.Read(lpRaw[:])
r.Read(rpRaw[:])
lp := binary.BigEndian.Uint16(lpRaw[:])
rp := binary.BigEndian.Uint16(rpRaw[:])
lp := routing.Port(binary.BigEndian.Uint16(lpRaw[:]))
rp := routing.Port(binary.BigEndian.Uint16(rpRaw[:]))
fwdRule := routing.ForwardRule(ruleExp, routing.RouteID(r.Uint32()), uuid.New())
fwdRID, err := rt.AddRule(fwdRule)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/node/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ func TestListApps(t *testing.T) {
app1 := reply[0]
assert.Equal(t, "foo", app1.Name)
assert.False(t, app1.AutoStart)
assert.Equal(t, uint16(10), app1.Port)
assert.Equal(t, routing.Port(10), app1.Port)
assert.Equal(t, AppStatusStopped, app1.Status)

app2 := reply[1]
assert.Equal(t, "bar", app2.Name)
assert.True(t, app2.AutoStart)
assert.Equal(t, uint16(11), app2.Port)
assert.Equal(t, routing.Port(11), app2.Port)
assert.Equal(t, AppStatusRunning, app2.Status)
}

Expand Down
20 changes: 11 additions & 9 deletions pkg/router/port_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"math"
"sync"

"github.com/skycoin/skywire/pkg/routing"

"github.com/skycoin/skywire/pkg/app"
)

Expand All @@ -16,15 +18,15 @@ type portList struct {
sync.Mutex

minPort uint16
ports map[uint16]*portBind
ports map[routing.Port]*portBind
}

func newPortList(minPort uint16) *portList {
return &portList{minPort: minPort, ports: make(map[uint16]*portBind, minPort)}
return &portList{minPort: minPort, ports: make(map[routing.Port]*portBind, minPort)}
}

func (pl *portList) all() map[uint16]*portBind {
r := make(map[uint16]*portBind)
func (pl *portList) all() map[routing.Port]*portBind {
r := make(map[routing.Port]*portBind)
pl.Lock()
for port, bind := range pl.ports {
r[port] = bind
Expand All @@ -34,11 +36,11 @@ func (pl *portList) all() map[uint16]*portBind {
return r
}

func (pl *portList) add(b *portBind) uint16 {
func (pl *portList) add(b *portBind) routing.Port {
pl.Lock()
defer pl.Unlock()

for i := uint16(pl.minPort); i < math.MaxUint16; i++ {
for i := routing.Port(pl.minPort); i < math.MaxUint16; i++ {
if pl.ports[i] == nil {
pl.ports[i] = b
return i
Expand All @@ -48,21 +50,21 @@ func (pl *portList) add(b *portBind) uint16 {
panic("no free ports")
}

func (pl *portList) set(port uint16, b *portBind) {
func (pl *portList) set(port routing.Port, b *portBind) {
pl.Lock()
pl.ports[port] = b
pl.Unlock()
}

func (pl *portList) get(port uint16) *portBind {
func (pl *portList) get(port routing.Port) *portBind {
pl.Lock()
l := pl.ports[port]
pl.Unlock()

return l
}

func (pl *portList) remove(port uint16) *portBind {
func (pl *portList) remove(port routing.Port) *portBind {
pl.Lock()
b := pl.ports[port]
delete(pl.ports, port)
Expand Down
18 changes: 9 additions & 9 deletions pkg/router/port_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ func newPortManager(minPort uint16) *portManager {
return &portManager{newPortList(minPort)}
}

func (pm *portManager) Alloc(conn *app.Protocol) uint16 {
func (pm *portManager) Alloc(conn *app.Protocol) routing.Port {
b := &portBind{conn, newLoopList()}
return pm.ports.add(b)
}

func (pm *portManager) Open(port uint16, proto *app.Protocol) error {
func (pm *portManager) Open(port routing.Port, proto *app.Protocol) error {
if pm.ports.get(port) != nil {
return fmt.Errorf("port %d is already bound", port)
}
Expand All @@ -30,7 +30,7 @@ func (pm *portManager) Open(port uint16, proto *app.Protocol) error {
return nil
}

func (pm *portManager) SetLoop(port uint16, raddr *routing.Addr, l *loop) error {
func (pm *portManager) SetLoop(port routing.Port, raddr *routing.Addr, l *loop) error {
b := pm.ports.get(port)
if b == nil {
return errors.New("port is not bound")
Expand All @@ -52,8 +52,8 @@ func (pm *portManager) AppConns() []*app.Protocol {
return res
}

func (pm *portManager) AppPorts(appConn *app.Protocol) []uint16 {
res := []uint16{}
func (pm *portManager) AppPorts(appConn *app.Protocol) []routing.Port {
res := []routing.Port{}
for port, bind := range pm.ports.all() {
if bind.conn == appConn {
res = append(res, port)
Expand All @@ -62,7 +62,7 @@ func (pm *portManager) AppPorts(appConn *app.Protocol) []uint16 {
return res
}

func (pm *portManager) Close(port uint16) []routing.Addr {
func (pm *portManager) Close(port routing.Port) []routing.Addr {
if pm == nil {
return nil
}
Expand All @@ -75,7 +75,7 @@ func (pm *portManager) Close(port uint16) []routing.Addr {
return b.loops.dropAll()
}

func (pm *portManager) RemoveLoop(port uint16, raddr *routing.Addr) error {
func (pm *portManager) RemoveLoop(port routing.Port, raddr *routing.Addr) error {
b, err := pm.Get(port)
if err != nil {
return err
Expand All @@ -85,7 +85,7 @@ func (pm *portManager) RemoveLoop(port uint16, raddr *routing.Addr) error {
return nil
}

func (pm *portManager) Get(port uint16) (*portBind, error) {
func (pm *portManager) Get(port routing.Port) (*portBind, error) {
b := pm.ports.get(port)
if b == nil {
return nil, errors.New("port is not bound")
Expand All @@ -94,7 +94,7 @@ func (pm *portManager) Get(port uint16) (*portBind, error) {
return b, nil
}

func (pm *portManager) GetLoop(localPort uint16, remoteAddr *routing.Addr) (*loop, error) {
func (pm *portManager) GetLoop(localPort routing.Port, remoteAddr *routing.Addr) (*loop, error) {
b, err := pm.Get(localPort)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/router/port_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestPortManager(t *testing.T) {
proto := app.NewProtocol(in)

p1 := pm.Alloc(proto)
assert.Equal(t, uint16(10), p1)
assert.Equal(t, routing.Port(10), p1)

require.Error(t, pm.Open(10, proto))
require.NoError(t, pm.Open(8, proto))
Expand All @@ -35,7 +35,7 @@ func TestPortManager(t *testing.T) {

ports := pm.AppPorts(proto)
sort.Slice(ports, func(i, j int) bool { return ports[i] < ports[j] })
assert.Equal(t, []uint16{8, 10}, ports)
assert.Equal(t, []routing.Port{8, 10}, ports)

b, err := pm.Get(10)
require.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/router/route_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ func TestRouteManagerConfirmLoop(t *testing.T) {
err := setup.ConfirmLoop(proto, ld)
require.NoError(t, err)
assert.Equal(t, rule, inRule)
assert.Equal(t, uint16(2), inAddr.Port)
assert.Equal(t, uint16(3), inAddr.Remote.Port)
assert.Equal(t, routing.Port(2), inAddr.Port)
assert.Equal(t, routing.Port(3), inAddr.Remote.Port)
assert.Equal(t, pk, inAddr.Remote.PubKey)

require.NoError(t, in.Close())
Expand Down Expand Up @@ -197,8 +197,8 @@ func TestRouteManagerLoopClosed(t *testing.T) {
RouteID: 1,
}
require.NoError(t, setup.LoopClosed(proto, ld))
assert.Equal(t, uint16(2), inAddr.Port)
assert.Equal(t, uint16(3), inAddr.Remote.Port)
assert.Equal(t, routing.Port(2), inAddr.Port)
assert.Equal(t, routing.Port(3), inAddr.Remote.Port)
assert.Equal(t, pk, inAddr.Remote.PubKey)

require.NoError(t, in.Close())
Expand Down
Loading

0 comments on commit 57b9f25

Please sign in to comment.