From eb42bca02d0a9485789d4a8e977a8d07059807e2 Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Sat, 21 Sep 2019 20:36:35 +0300 Subject: [PATCH] Refactor a bit --- go.mod | 1 + go.sum | 3 ++ pkg/app2/client.go | 11 +++-- pkg/app2/client_test.go | 40 ++++++++--------- pkg/app2/conn.go | 2 +- pkg/app2/conn_test.go | 6 +-- pkg/app2/listener.go | 2 +- pkg/app2/listener_test.go | 20 +++++---- ...erver_rpc_client.go => mock_rpc_client.go} | 24 ++++++----- pkg/app2/network/addr.go | 3 +- pkg/app2/network/networker.go | 8 ++-- .../{server_rpc_client.go => rpc_client.go} | 30 ++++++------- pkg/app2/{server_rpc.go => rpc_gateway.go} | 43 +++++++++---------- vendor/github.com/skycoin/dmsg/go.mod | 4 +- vendor/github.com/skycoin/dmsg/go.sum | 6 --- 15 files changed, 101 insertions(+), 102 deletions(-) rename pkg/app2/{mock_server_rpc_client.go => mock_rpc_client.go} (77%) rename pkg/app2/{server_rpc_client.go => rpc_client.go} (64%) rename pkg/app2/{server_rpc.go => rpc_gateway.go} (77%) diff --git a/go.mod b/go.mod index c0cfa4c314..ec315f5700 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,7 @@ require ( github.com/skycoin/skycoin v0.26.0 github.com/spf13/cobra v0.0.5 github.com/stretchr/testify v1.4.0 + github.com/vektra/mockery v0.0.0-20181123154057-e78b021dcbb5 // indirect go.etcd.io/bbolt v1.3.3 golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7 golang.org/x/net v0.0.0-20190916140828-c8589233b77d diff --git a/go.sum b/go.sum index b9ca83793d..ba051af7ca 100644 --- a/go.sum +++ b/go.sum @@ -130,6 +130,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= 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/vektra/mockery v0.0.0-20181123154057-e78b021dcbb5 h1:Xim2mBRFdXzXmKRO8DJg/FJtn/8Fj9NOEpO6+WuMPmk= +github.com/vektra/mockery v0.0.0-20181123154057-e78b021dcbb5/go.mod h1:ppEjwdhyy7Y31EnHRDm1JkChoC7LXIJ7Ex0VYLWtZtQ= 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= @@ -165,6 +167,7 @@ golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20181112210238-4b1f3b6b1646/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190627182818-9947fec5c3ab/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= diff --git a/pkg/app2/client.go b/pkg/app2/client.go index 743fadc433..bd9e203905 100644 --- a/pkg/app2/client.go +++ b/pkg/app2/client.go @@ -4,10 +4,10 @@ import ( "context" "net" - "github.com/skycoin/skywire/pkg/app2/network" - "github.com/skycoin/dmsg/cipher" "github.com/skycoin/dmsg/netutil" + + "github.com/skycoin/skywire/pkg/app2/network" "github.com/skycoin/skywire/pkg/routing" ) @@ -15,21 +15,20 @@ import ( type Client struct { pk cipher.PubKey pid ProcID - rpc ServerRPCClient + rpc RPCClient porter *netutil.Porter } // NewClient creates a new `Client`. The `Client` needs to be provided with: -// - log: Logger instance. // - localPK: The local public key of the parent skywire visor. // - pid: The procID assigned for the process that Client is being used by. // - rpc: RPC client to communicate with the server. -func NewClient(localPK cipher.PubKey, pid ProcID, rpc ServerRPCClient, porter *netutil.Porter) *Client { +func NewClient(localPK cipher.PubKey, pid ProcID, rpc RPCClient) *Client { return &Client{ pk: localPK, pid: pid, rpc: rpc, - porter: porter, + porter: netutil.NewPorter(netutil.PorterMinEphemeral), } } diff --git a/pkg/app2/client_test.go b/pkg/app2/client_test.go index 1c49d5c14f..6bc19e2a8d 100644 --- a/pkg/app2/client_test.go +++ b/pkg/app2/client_test.go @@ -3,16 +3,12 @@ package app2 import ( "testing" - "github.com/skycoin/skywire/pkg/app2/network" - "github.com/pkg/errors" + "github.com/skycoin/dmsg/cipher" "github.com/stretchr/testify/require" + "github.com/skycoin/skywire/pkg/app2/network" "github.com/skycoin/skywire/pkg/routing" - - "github.com/skycoin/dmsg/cipher" - - "github.com/skycoin/dmsg/netutil" ) func TestClient_Dial(t *testing.T) { @@ -30,10 +26,10 @@ func TestClient_Dial(t *testing.T) { dialConnID := uint16(1) var dialErr error - rpc := &MockServerRPCClient{} + rpc := &MockRPCClient{} rpc.On("Dial", remote).Return(dialConnID, dialErr) - cl := NewClient(localPK, pid, rpc, netutil.NewPorter(netutil.PorterMinEphemeral)) + cl := NewClient(localPK, pid, rpc) wantConn := &Conn{ id: dialConnID, @@ -62,10 +58,10 @@ func TestClient_Dial(t *testing.T) { t.Run("dial error", func(t *testing.T) { dialErr := errors.New("dial error") - rpc := &MockServerRPCClient{} + rpc := &MockRPCClient{} rpc.On("Dial", remote).Return(uint16(0), dialErr) - cl := NewClient(localPK, pid, rpc, netutil.NewPorter(netutil.PorterMinEphemeral)) + cl := NewClient(localPK, pid, rpc) conn, err := cl.Dial(remote) require.Equal(t, dialErr, err) @@ -79,6 +75,7 @@ func TestClient_Listen(t *testing.T) { port := routing.Port(1) local := network.Addr{ + Net: network.TypeDMSG, PubKey: localPK, Port: port, } @@ -87,10 +84,10 @@ func TestClient_Listen(t *testing.T) { listenLisID := uint16(1) var listenErr error - rpc := &MockServerRPCClient{} + rpc := &MockRPCClient{} rpc.On("Listen", local).Return(listenLisID, listenErr) - cl := NewClient(localPK, pid, rpc, netutil.NewPorter(netutil.PorterMinEphemeral)) + cl := NewClient(localPK, pid, rpc) wantListener := &Listener{ id: listenLisID, @@ -98,7 +95,7 @@ func TestClient_Listen(t *testing.T) { addr: local, } - listener, err := cl.Listen(port) + listener, err := cl.Listen(network.TypeDMSG, port) require.Nil(t, err) appListener, ok := listener.(*Listener) require.True(t, ok) @@ -112,17 +109,16 @@ func TestClient_Listen(t *testing.T) { }) t.Run("port is already bound", func(t *testing.T) { - porter := netutil.NewPorter(netutil.PorterMinEphemeral) - ok, _ := porter.Reserve(uint16(port), nil) - require.True(t, ok) + rpc := &MockRPCClient{} - rpc := &MockServerRPCClient{} + cl := NewClient(localPK, pid, rpc) - cl := NewClient(localPK, pid, rpc, porter) + ok, _ := cl.porter.Reserve(uint16(port), nil) + require.True(t, ok) wantErr := ErrPortAlreadyBound - listener, err := cl.Listen(port) + listener, err := cl.Listen(network.TypeDMSG, port) require.Equal(t, wantErr, err) require.Nil(t, listener) }) @@ -130,12 +126,12 @@ func TestClient_Listen(t *testing.T) { t.Run("listen error", func(t *testing.T) { listenErr := errors.New("listen error") - rpc := &MockServerRPCClient{} + rpc := &MockRPCClient{} rpc.On("Listen", local).Return(uint16(0), listenErr) - cl := NewClient(localPK, pid, rpc, netutil.NewPorter(netutil.PorterMinEphemeral)) + cl := NewClient(localPK, pid, rpc) - listener, err := cl.Listen(port) + listener, err := cl.Listen(network.TypeDMSG, port) require.Equal(t, listenErr, err) require.Nil(t, listener) _, ok := cl.porter.PortValue(uint16(port)) diff --git a/pkg/app2/conn.go b/pkg/app2/conn.go index 632fe0d464..d201f22a62 100644 --- a/pkg/app2/conn.go +++ b/pkg/app2/conn.go @@ -11,7 +11,7 @@ import ( // Implements `net.Conn`. type Conn struct { id uint16 - rpc ServerRPCClient + rpc RPCClient local network.Addr remote network.Addr freeLocalPort func() diff --git a/pkg/app2/conn_test.go b/pkg/app2/conn_test.go index d25de2716b..ef860a6983 100644 --- a/pkg/app2/conn_test.go +++ b/pkg/app2/conn_test.go @@ -35,7 +35,7 @@ func TestConn_Read(t *testing.T) { for _, tc := range tt { t.Run(tc.name, func(t *testing.T) { - rpc := &MockServerRPCClient{} + rpc := &MockRPCClient{} rpc.On("Read", connID, tc.readBuff).Return(tc.readN, tc.readBytes, tc.readErr) conn := &Conn{ @@ -74,7 +74,7 @@ func TestConn_Write(t *testing.T) { for _, tc := range tt { t.Run(tc.name, func(t *testing.T) { - rpc := &MockServerRPCClient{} + rpc := &MockRPCClient{} rpc.On("Write", connID, tc.writeBuff).Return(tc.writeN, tc.writeErr) conn := &Conn{ @@ -107,7 +107,7 @@ func TestConn_Close(t *testing.T) { for _, tc := range tt { t.Run(tc.name, func(t *testing.T) { - rpc := &MockServerRPCClient{} + rpc := &MockRPCClient{} rpc.On("CloseConn", connID).Return(tc.closeErr) conn := &Conn{ diff --git a/pkg/app2/listener.go b/pkg/app2/listener.go index bb38364a7d..56e52160e3 100644 --- a/pkg/app2/listener.go +++ b/pkg/app2/listener.go @@ -10,7 +10,7 @@ import ( // Implements `net.Listener`. type Listener struct { id uint16 - rpc ServerRPCClient + rpc RPCClient addr network.Addr freePort func() } diff --git a/pkg/app2/listener_test.go b/pkg/app2/listener_test.go index e43b384f86..058b070657 100644 --- a/pkg/app2/listener_test.go +++ b/pkg/app2/listener_test.go @@ -4,16 +4,18 @@ import ( "testing" "github.com/pkg/errors" + "github.com/skycoin/dmsg/cipher" "github.com/stretchr/testify/require" - "github.com/skycoin/dmsg/cipher" + "github.com/skycoin/skywire/pkg/app2/network" "github.com/skycoin/skywire/pkg/routing" ) func TestListener_Accept(t *testing.T) { lisID := uint16(1) localPK, _ := cipher.GenerateKeyPair() - local := routing.Addr{ + local := network.Addr{ + Net: network.TypeDMSG, PubKey: localPK, Port: routing.Port(100), } @@ -21,13 +23,14 @@ func TestListener_Accept(t *testing.T) { t.Run("ok", func(t *testing.T) { acceptConnID := uint16(1) remotePK, _ := cipher.GenerateKeyPair() - acceptRemote := routing.Addr{ + acceptRemote := network.Addr{ + Net: network.TypeDMSG, PubKey: remotePK, Port: routing.Port(100), } var acceptErr error - rpc := &MockServerRPCClient{} + rpc := &MockRPCClient{} rpc.On("Accept", acceptConnID).Return(acceptConnID, acceptRemote, acceptErr) lis := &Listener{ @@ -50,10 +53,10 @@ func TestListener_Accept(t *testing.T) { t.Run("accept error", func(t *testing.T) { acceptConnID := uint16(0) - acceptRemote := routing.Addr{} + acceptRemote := network.Addr{} acceptErr := errors.New("accept error") - rpc := &MockServerRPCClient{} + rpc := &MockRPCClient{} rpc.On("Accept", lisID).Return(acceptConnID, acceptRemote, acceptErr) lis := &Listener{ @@ -71,7 +74,8 @@ func TestListener_Accept(t *testing.T) { func TestListener_Close(t *testing.T) { lisID := uint16(1) localPK, _ := cipher.GenerateKeyPair() - local := routing.Addr{ + local := network.Addr{ + Net: network.TypeDMSG, PubKey: localPK, Port: routing.Port(100), } @@ -91,7 +95,7 @@ func TestListener_Close(t *testing.T) { for _, tc := range tt { t.Run(tc.name, func(t *testing.T) { - rpc := &MockServerRPCClient{} + rpc := &MockRPCClient{} rpc.On("CloseListener", lisID).Return(tc.closeErr) lis := &Listener{ diff --git a/pkg/app2/mock_server_rpc_client.go b/pkg/app2/mock_rpc_client.go similarity index 77% rename from pkg/app2/mock_server_rpc_client.go rename to pkg/app2/mock_rpc_client.go index 58e88cab76..af8f62fb6c 100644 --- a/pkg/app2/mock_server_rpc_client.go +++ b/pkg/app2/mock_rpc_client.go @@ -2,16 +2,18 @@ package app2 -import mock "github.com/stretchr/testify/mock" -import network "github.com/skycoin/skywire/pkg/app2/network" +import ( + network "github.com/skycoin/skywire/pkg/app2/network" + mock "github.com/stretchr/testify/mock" +) -// MockServerRPCClient is an autogenerated mock type for the ServerRPCClient type -type MockServerRPCClient struct { +// MockRPCClient is an autogenerated mock type for the RPCClient type +type MockRPCClient struct { mock.Mock } // Accept provides a mock function with given fields: lisID -func (_m *MockServerRPCClient) Accept(lisID uint16) (uint16, network.Addr, error) { +func (_m *MockRPCClient) Accept(lisID uint16) (uint16, network.Addr, error) { ret := _m.Called(lisID) var r0 uint16 @@ -39,7 +41,7 @@ func (_m *MockServerRPCClient) Accept(lisID uint16) (uint16, network.Addr, error } // CloseConn provides a mock function with given fields: id -func (_m *MockServerRPCClient) CloseConn(id uint16) error { +func (_m *MockRPCClient) CloseConn(id uint16) error { ret := _m.Called(id) var r0 error @@ -53,7 +55,7 @@ func (_m *MockServerRPCClient) CloseConn(id uint16) error { } // CloseListener provides a mock function with given fields: id -func (_m *MockServerRPCClient) CloseListener(id uint16) error { +func (_m *MockRPCClient) CloseListener(id uint16) error { ret := _m.Called(id) var r0 error @@ -67,7 +69,7 @@ func (_m *MockServerRPCClient) CloseListener(id uint16) error { } // Dial provides a mock function with given fields: remote -func (_m *MockServerRPCClient) Dial(remote network.Addr) (uint16, error) { +func (_m *MockRPCClient) Dial(remote network.Addr) (uint16, error) { ret := _m.Called(remote) var r0 uint16 @@ -88,7 +90,7 @@ func (_m *MockServerRPCClient) Dial(remote network.Addr) (uint16, error) { } // Listen provides a mock function with given fields: local -func (_m *MockServerRPCClient) Listen(local network.Addr) (uint16, error) { +func (_m *MockRPCClient) Listen(local network.Addr) (uint16, error) { ret := _m.Called(local) var r0 uint16 @@ -109,7 +111,7 @@ func (_m *MockServerRPCClient) Listen(local network.Addr) (uint16, error) { } // Read provides a mock function with given fields: connID, b -func (_m *MockServerRPCClient) Read(connID uint16, b []byte) (int, []byte, error) { +func (_m *MockRPCClient) Read(connID uint16, b []byte) (int, []byte, error) { ret := _m.Called(connID, b) var r0 int @@ -139,7 +141,7 @@ func (_m *MockServerRPCClient) Read(connID uint16, b []byte) (int, []byte, error } // Write provides a mock function with given fields: connID, b -func (_m *MockServerRPCClient) Write(connID uint16, b []byte) (int, error) { +func (_m *MockRPCClient) Write(connID uint16, b []byte) (int, error) { ret := _m.Called(connID, b) var r0 int diff --git a/pkg/app2/network/addr.go b/pkg/app2/network/addr.go index 36fec8306d..2db24213e5 100644 --- a/pkg/app2/network/addr.go +++ b/pkg/app2/network/addr.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/skycoin/dmsg/cipher" + "github.com/skycoin/skywire/pkg/routing" ) @@ -14,7 +15,7 @@ type Addr struct { Port routing.Port } -// Network returns "dmsg" +// Network returns network type. func (a Addr) Network() string { return string(a.Net) } diff --git a/pkg/app2/network/networker.go b/pkg/app2/network/networker.go index ed6277c88e..708b707e78 100644 --- a/pkg/app2/network/networker.go +++ b/pkg/app2/network/networker.go @@ -53,12 +53,12 @@ type Networker interface { ListenContext(ctx context.Context, addr Addr) (net.Listener, error) } -// Dial dials the remote `addr` of the specified `network`. +// Dial dials the remote `addr`. func Dial(addr Addr) (net.Conn, error) { return DialContext(context.Background(), addr) } -// DialContext dials the remote `Addr` of the specified `network` with the context. +// DialContext dials the remote `addr` with the context. func DialContext(ctx context.Context, addr Addr) (net.Conn, error) { n, err := ResolveNetworker(addr.Net) if err != nil { @@ -68,12 +68,12 @@ func DialContext(ctx context.Context, addr Addr) (net.Conn, error) { return n.DialContext(ctx, addr) } -// Listen starts listening on the local `addr` of the specified `network`. +// Listen starts listening on the local `addr`. func Listen(addr Addr) (net.Listener, error) { return ListenContext(context.Background(), addr) } -// ListenContext starts listening on the local `addr` of the specified `network` with the context. +// ListenContext starts listening on the local `addr` with the context. func ListenContext(ctx context.Context, addr Addr) (net.Listener, error) { networker, err := ResolveNetworker(addr.Net) if err != nil { diff --git a/pkg/app2/server_rpc_client.go b/pkg/app2/rpc_client.go similarity index 64% rename from pkg/app2/server_rpc_client.go rename to pkg/app2/rpc_client.go index d3dde3056b..1ed5b93b4d 100644 --- a/pkg/app2/server_rpc_client.go +++ b/pkg/app2/rpc_client.go @@ -6,10 +6,10 @@ import ( "github.com/skycoin/skywire/pkg/app2/network" ) -//go:generate mockery -name ServerRPCClient -case underscore -inpkg +//go:generate mockery -name RPCClient -case underscore -inpkg -// ServerRPCClient describes RPC interface to communicate with the server. -type ServerRPCClient interface { +// RPCClient describes RPC interface to communicate with the server. +type RPCClient interface { Dial(remote network.Addr) (uint16, error) Listen(local network.Addr) (uint16, error) Accept(lisID uint16) (uint16, network.Addr, error) @@ -19,20 +19,20 @@ type ServerRPCClient interface { CloseListener(id uint16) error } -// serverRPCClient implements `ServerRPCClient`. -type serverRPCCLient struct { +// rpcClient implements `RPCClient`. +type rpcCLient struct { rpc *rpc.Client } -// NewServerRPCClient constructs new `serverRPCClient`. -func NewServerRPCClient(rpc *rpc.Client) ServerRPCClient { - return &serverRPCCLient{ +// NewRPCClient constructs new `rpcClient`. +func NewRPCClient(rpc *rpc.Client) RPCClient { + return &rpcCLient{ rpc: rpc, } } // Dial sends `Dial` command to the server. -func (c *serverRPCCLient) Dial(remote network.Addr) (uint16, error) { +func (c *rpcCLient) Dial(remote network.Addr) (uint16, error) { var connID uint16 if err := c.rpc.Call("Dial", &remote, &connID); err != nil { return 0, err @@ -42,7 +42,7 @@ func (c *serverRPCCLient) Dial(remote network.Addr) (uint16, error) { } // Listen sends `Listen` command to the server. -func (c *serverRPCCLient) Listen(local network.Addr) (uint16, error) { +func (c *rpcCLient) Listen(local network.Addr) (uint16, error) { var lisID uint16 if err := c.rpc.Call("Listen", &local, &lisID); err != nil { return 0, err @@ -52,7 +52,7 @@ func (c *serverRPCCLient) Listen(local network.Addr) (uint16, error) { } // Accept sends `Accept` command to the server. -func (c *serverRPCCLient) Accept(lisID uint16) (uint16, network.Addr, error) { +func (c *rpcCLient) Accept(lisID uint16) (uint16, network.Addr, error) { var acceptResp AcceptResp if err := c.rpc.Call("Accept", &lisID, &acceptResp); err != nil { return 0, network.Addr{}, err @@ -62,7 +62,7 @@ func (c *serverRPCCLient) Accept(lisID uint16) (uint16, network.Addr, error) { } // Write sends `Write` command to the server. -func (c *serverRPCCLient) Write(connID uint16, b []byte) (int, error) { +func (c *rpcCLient) Write(connID uint16, b []byte) (int, error) { req := WriteReq{ ConnID: connID, B: b, @@ -77,7 +77,7 @@ func (c *serverRPCCLient) Write(connID uint16, b []byte) (int, error) { } // Read sends `Read` command to the server. -func (c *serverRPCCLient) Read(connID uint16, b []byte) (int, []byte, error) { +func (c *rpcCLient) Read(connID uint16, b []byte) (int, []byte, error) { var resp ReadResp if err := c.rpc.Call("Read", &connID, &resp); err != nil { return 0, nil, err @@ -87,11 +87,11 @@ func (c *serverRPCCLient) Read(connID uint16, b []byte) (int, []byte, error) { } // CloseConn sends `CloseConn` command to the server. -func (c *serverRPCCLient) CloseConn(id uint16) error { +func (c *rpcCLient) CloseConn(id uint16) error { return c.rpc.Call("CloseConn", &id, nil) } // CloseListener sends `CloseListener` command to the server. -func (c *serverRPCCLient) CloseListener(id uint16) error { +func (c *rpcCLient) CloseListener(id uint16) error { return c.rpc.Call("CloseListener", &id, nil) } diff --git a/pkg/app2/server_rpc.go b/pkg/app2/rpc_gateway.go similarity index 77% rename from pkg/app2/server_rpc.go rename to pkg/app2/rpc_gateway.go index bc716e681a..e5e9c7ec0b 100644 --- a/pkg/app2/server_rpc.go +++ b/pkg/app2/rpc_gateway.go @@ -4,23 +4,22 @@ import ( "fmt" "net" - "github.com/skycoin/skywire/pkg/app2/network" - "github.com/pkg/errors" - "github.com/skycoin/dmsg" "github.com/skycoin/skycoin/src/util/logging" + + "github.com/skycoin/skywire/pkg/app2/network" ) -// ServerRPC is a RPC interface for the app server. -type ServerRPC struct { +// RPCGateway is a RPC interface for the app server. +type RPCGateway struct { lm *manager cm *manager log *logging.Logger } -// newServerRPC constructs new server RPC interface. -func newServerRPC(log *logging.Logger) *ServerRPC { - return &ServerRPC{ +// newRPCGateway constructs new server RPC interface. +func newRPCGateway(log *logging.Logger) *RPCGateway { + return &RPCGateway{ lm: newManager(), cm: newManager(), log: log, @@ -28,7 +27,7 @@ func newServerRPC(log *logging.Logger) *ServerRPC { } // Dial dials to the remote. -func (r *ServerRPC) Dial(remote *network.Addr, connID *uint16) error { +func (r *RPCGateway) Dial(remote *network.Addr, connID *uint16) error { connID, err := r.cm.nextKey() if err != nil { return err @@ -51,7 +50,7 @@ func (r *ServerRPC) Dial(remote *network.Addr, connID *uint16) error { } // Listen starts listening. -func (r *ServerRPC) Listen(local *network.Addr, lisID *uint16) error { +func (r *RPCGateway) Listen(local *network.Addr, lisID *uint16) error { lisID, err := r.lm.nextKey() if err != nil { return err @@ -80,7 +79,7 @@ type AcceptResp struct { } // Accept accepts connection from the listener specified by `lisID`. -func (r *ServerRPC) Accept(lisID *uint16, resp *AcceptResp) error { +func (r *RPCGateway) Accept(lisID *uint16, resp *AcceptResp) error { lis, err := r.getListener(*lisID) if err != nil { return err @@ -124,7 +123,7 @@ type WriteReq struct { } // Write writes to the connection. -func (r *ServerRPC) Write(req *WriteReq, n *int) error { +func (r *RPCGateway) Write(req *WriteReq, n *int) error { conn, err := r.getConn(req.ConnID) if err != nil { return err @@ -145,7 +144,7 @@ type ReadResp struct { } // Read reads data from connection specified by `connID`. -func (r *ServerRPC) Read(connID *uint16, resp *ReadResp) error { +func (r *RPCGateway) Read(connID *uint16, resp *ReadResp) error { conn, err := r.getConn(*connID) if err != nil { return err @@ -160,7 +159,7 @@ func (r *ServerRPC) Read(connID *uint16, resp *ReadResp) error { } // CloseConn closes connection specified by `connID`. -func (r *ServerRPC) CloseConn(connID *uint16, _ *struct{}) error { +func (r *RPCGateway) CloseConn(connID *uint16, _ *struct{}) error { conn, err := r.popConn(*connID) if err != nil { return err @@ -170,7 +169,7 @@ func (r *ServerRPC) CloseConn(connID *uint16, _ *struct{}) error { } // CloseListener closes listener specified by `lisID`. -func (r *ServerRPC) CloseListener(lisID *uint16, _ *struct{}) error { +func (r *RPCGateway) CloseListener(lisID *uint16, _ *struct{}) error { lis, err := r.popListener(*lisID) if err != nil { return err @@ -181,7 +180,7 @@ func (r *ServerRPC) CloseListener(lisID *uint16, _ *struct{}) error { // popListener gets listener from the manager by `lisID` and removes it. // Handles type assertion. -func (r *ServerRPC) popListener(lisID uint16) (*dmsg.Listener, error) { +func (r *RPCGateway) popListener(lisID uint16) (net.Listener, error) { lisIfc, err := r.lm.pop(lisID) if err != nil { return nil, err @@ -192,7 +191,7 @@ func (r *ServerRPC) popListener(lisID uint16) (*dmsg.Listener, error) { // popConn gets conn from the manager by `connID` and removes it. // Handles type assertion. -func (r *ServerRPC) popConn(connID uint16) (net.Conn, error) { +func (r *RPCGateway) popConn(connID uint16) (net.Conn, error) { connIfc, err := r.cm.pop(connID) if err != nil { return nil, err @@ -202,7 +201,7 @@ func (r *ServerRPC) popConn(connID uint16) (net.Conn, error) { } // getListener gets listener from the manager by `lisID`. Handles type assertion. -func (r *ServerRPC) getListener(lisID uint16) (*dmsg.Listener, error) { +func (r *RPCGateway) getListener(lisID uint16) (net.Listener, error) { lisIfc, ok := r.lm.get(lisID) if !ok { return nil, fmt.Errorf("no listener with key %d", lisID) @@ -212,7 +211,7 @@ func (r *ServerRPC) getListener(lisID uint16) (*dmsg.Listener, error) { } // getConn gets conn from the manager by `connID`. Handles type assertion. -func (r *ServerRPC) getConn(connID uint16) (net.Conn, error) { +func (r *RPCGateway) getConn(connID uint16) (net.Conn, error) { connIfc, ok := r.cm.get(connID) if !ok { return nil, fmt.Errorf("no conn with key %d", connID) @@ -222,8 +221,8 @@ func (r *ServerRPC) getConn(connID uint16) (net.Conn, error) { } // assertListener asserts that `v` is of type `net.Listener`. -func (r *ServerRPC) assertListener(v interface{}) (*dmsg.Listener, error) { - lis, ok := v.(*dmsg.Listener) +func (r *RPCGateway) assertListener(v interface{}) (net.Listener, error) { + lis, ok := v.(net.Listener) if !ok { return nil, errors.New("wrong type of value stored for listener") } @@ -232,7 +231,7 @@ func (r *ServerRPC) assertListener(v interface{}) (*dmsg.Listener, error) { } // assertConn asserts that `v` is of type `net.Conn`. -func (r *ServerRPC) assertConn(v interface{}) (net.Conn, error) { +func (r *RPCGateway) assertConn(v interface{}) (net.Conn, error) { conn, ok := v.(net.Conn) if !ok { return nil, errors.New("wrong type of value stored for conn") diff --git a/vendor/github.com/skycoin/dmsg/go.mod b/vendor/github.com/skycoin/dmsg/go.mod index a24455c1f5..1ef2c47f66 100644 --- a/vendor/github.com/skycoin/dmsg/go.mod +++ b/vendor/github.com/skycoin/dmsg/go.mod @@ -14,7 +14,7 @@ require ( golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4 // indirect golang.org/x/net v0.0.0-20190620200207-3b0461eec859 golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb // indirect - golang.org/x/text v0.3.2 // indirect - golang.org/x/tools v0.0.0-20190627182818-9947fec5c3ab // indirect gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect ) + +replace github.com/skycoin/dmsg => ../dmsg diff --git a/vendor/github.com/skycoin/dmsg/go.sum b/vendor/github.com/skycoin/dmsg/go.sum index c6a730a9e6..624818fed7 100644 --- a/vendor/github.com/skycoin/dmsg/go.sum +++ b/vendor/github.com/skycoin/dmsg/go.sum @@ -32,11 +32,9 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4 h1:ydJNl0ENAG67pFbB+9tfhiL2pYqLhfoaZFw/cjLhY4A= golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -45,9 +43,5 @@ golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb h1:fgwFCsaw9buMuxNd6+DQfAuSF golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190627182818-9947fec5c3ab h1:uOzhX2fm3C4BmBwW2a7lnJQD7qel2+4uhmTc8czKBCU= -golang.org/x/tools v0.0.0-20190627182818-9947fec5c3ab/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=