Skip to content

Commit

Permalink
Refactor a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Oct 17, 2019
1 parent 8edf438 commit 608cc38
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 25 deletions.
21 changes: 18 additions & 3 deletions pkg/app2/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,43 @@ package app2
import (
"fmt"

"github.com/skycoin/skywire/pkg/app2/appserver"

"github.com/skycoin/skycoin/src/util/logging"

"github.com/skycoin/skywire/pkg/app2/appserver"
)

// App defines a skywire application. It encapsulates
// running process and the RPC server to communicate with
// the app.
type App struct {
key Key
config Config
log *logging.Logger
proc *Proc
rpcS *appserver.Server
}

// New constructs `App`.
func New(log *logging.Logger, c Config, args []string) *App {
p := NewProc(c, args)
k := GenerateAppKey()

p := NewProc(c, args, k)

return &App{
key: k,
config: c,
log: log,
proc: p,
}
}

// PID returns app PID.
func (a *App) PID() ProcID {
return a.proc.ID()
}

// Run sets the app running. It starts app process
// and sets up the RPC server.
func (a *App) Run() error {
appKey := GenerateAppKey()

Expand All @@ -54,18 +65,22 @@ func (a *App) Run() error {
return nil
}

// Stop stops application (process and the RPC server).
func (a *App) Stop() error {
a.closeRPCServer()

return a.proc.Stop()
}

// Wait waits for the app to exit. Shuts down the
// RPC server.
func (a *App) Wait() error {
err := a.proc.Wait()
a.closeRPCServer()
return err
}

// closeRPCServer closes RPC server and logs error if any.
func (a *App) closeRPCServer() {
if err := a.rpcS.Close(); err != nil {
a.log.WithError(err).Error("error closing RPC server")
Expand Down
13 changes: 12 additions & 1 deletion pkg/app2/app_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@ import (
)

var (
ErrAppExists = errors.New("app with such pid already exists")
// ErrAppExists is returned when trying to add
// app to the manager which already exists.
ErrAppExists = errors.New("app with such name already exists")
)

// AppManager allows to store and retrieve skywire apps.
type AppManager struct {
apps map[string]*App
mx sync.RWMutex
}

// NewAppManager constructs `AppManager`.
func NewAppManager() *AppManager {
return &AppManager{}
}

// Add adds app `a` to the manager instance.
func (m *AppManager) Add(a *App) error {
m.mx.Lock()
defer m.mx.Unlock()
Expand All @@ -32,6 +37,8 @@ func (m *AppManager) Add(a *App) error {
return nil
}

// App gets the app from the manager if it exists. Returns bool
// flag to indicate operation success.
func (m *AppManager) App(name string) (*App, bool) {
m.mx.RLock()
defer m.mx.RUnlock()
Expand All @@ -41,6 +48,7 @@ func (m *AppManager) App(name string) (*App, bool) {
return a, ok
}

// Exists checks whether app exists in the manager instance.
func (m *AppManager) Exists(name string) bool {
m.mx.RLock()
defer m.mx.RUnlock()
Expand All @@ -50,13 +58,16 @@ func (m *AppManager) Exists(name string) bool {
return ok
}

// Remove removes app with the name `name` from the manager instance.
func (m *AppManager) Remove(name string) {
m.mx.Lock()
defer m.mx.Unlock()

delete(m.apps, name)
}

// Range allows to iterate over stored apps. Calls `next` on each iteration.
// Stops execution once `next` returns false.
func (m *AppManager) Range(next func(name string, app *App) bool) {
m.mx.RLock()
defer m.mx.RUnlock()
Expand Down
2 changes: 2 additions & 0 deletions pkg/app2/appnet/addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
)

var (
// ErrUnknownAddrType is returned when trying to convert the
// unknown addr type.
ErrUnknownAddrType = errors.New("addr type is unknown")
)

Expand Down
1 change: 0 additions & 1 deletion pkg/app2/appnet/skywire_networker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"sync/atomic"

"github.com/pkg/errors"

"github.com/skycoin/dmsg/netutil"
"github.com/skycoin/skycoin/src/util/logging"

Expand Down
3 changes: 1 addition & 2 deletions pkg/app2/appserver/rpc_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"net"

"github.com/pkg/errors"
"github.com/skycoin/skywire/pkg/app2/idmanager"

"github.com/skycoin/skycoin/src/util/logging"

"github.com/skycoin/skywire/pkg/app2/appnet"
"github.com/skycoin/skywire/pkg/app2/idmanager"
"github.com/skycoin/skywire/pkg/routing"
)

Expand Down
6 changes: 4 additions & 2 deletions pkg/app2/appserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
"sync"

"github.com/pkg/errors"
"github.com/skycoin/skycoin/src/util/logging"

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

"github.com/skycoin/skycoin/src/util/logging"
)

// Server is a server for app/visor communication.
Expand Down Expand Up @@ -60,12 +59,15 @@ func (s *Server) ListenAndServe() error {
}
}

// Close closes the server.
func (s *Server) Close() error {
err := s.lis.Close()
close(s.stopCh)
s.done.Wait()
return err
}

// serveConn serves RPC on a single connection.
func (s *Server) serveConn(conn net.Conn) {
go s.rpcS.ServeConn(conn)
<-s.stopCh
Expand Down
4 changes: 1 addition & 3 deletions pkg/app2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import (
"net/rpc"

"github.com/pkg/errors"

"github.com/skycoin/skywire/pkg/app2/idmanager"

"github.com/skycoin/dmsg/cipher"
"github.com/skycoin/skycoin/src/util/logging"

"github.com/skycoin/skywire/pkg/app2/appnet"
"github.com/skycoin/skywire/pkg/app2/idmanager"
"github.com/skycoin/skywire/pkg/routing"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/app2/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app2

// Config defines configuration parameters for App
// Config defines configuration parameters for `App`.
type Config struct {
Name string `json:"name"`
Version string `json:"version"`
Expand Down
6 changes: 0 additions & 6 deletions pkg/app2/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import (
"errors"
)

var (
// ErrPortAlreadyBound is being returned when trying to bind to the port
// which is already bound to.
ErrPortAlreadyBound = errors.New("port is already bound")
)

var (
// errMethodNotImplemented serves as a return value for non-implemented funcs (stubs).
errMethodNotImplemented = errors.New("method not implemented")
Expand Down
4 changes: 2 additions & 2 deletions pkg/app2/idmanager/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net"
)

// assertListener asserts that `v` is of type `net.Listener`.
// AssertListener asserts that `v` is of type `net.Listener`.
func AssertListener(v interface{}) (net.Listener, error) {
lis, ok := v.(net.Listener)
if !ok {
Expand All @@ -15,7 +15,7 @@ func AssertListener(v interface{}) (net.Listener, error) {
return lis, nil
}

// assertConn asserts that `v` is of type `net.Conn`.
// AssertConn asserts that `v` is of type `net.Conn`.
func AssertConn(v interface{}) (net.Conn, error) {
conn, ok := v.(net.Conn)
if !ok {
Expand Down
3 changes: 3 additions & 0 deletions pkg/app2/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package app2

import "github.com/skycoin/dmsg/cipher"

// Key is an app key to authenticate within the
// app server.
type Key string

// GenerateAppKey generates new app key.
func GenerateAppKey() Key {
raw, _ := cipher.GenerateKeyPair()
return Key(raw.Hex())
Expand Down
4 changes: 2 additions & 2 deletions pkg/app2/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"net"
"sync"

"github.com/skycoin/skywire/pkg/app2/idmanager"

"github.com/skycoin/skycoin/src/util/logging"

"github.com/skycoin/skywire/pkg/app2/appnet"
"github.com/skycoin/skywire/pkg/app2/idmanager"
)

// Listener is a listener for app server connections.
Expand Down
6 changes: 6 additions & 0 deletions pkg/app2/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"sync"
)

// Proc is a wrapper for skywire app process.
type Proc struct {
id ProcID
cmd *exec.Cmd
mx sync.RWMutex
}

// NewProc constructs `Proc`.
func NewProc(c Config, args []string, key Key) *Proc {
binaryPath := getBinaryPath(c.BinaryDir, c.Name, c.Version)

Expand All @@ -35,13 +37,15 @@ func NewProc(c Config, args []string, key Key) *Proc {
}
}

// ID returns pid of the app.
func (p *Proc) ID() ProcID {
p.mx.RLock()
id := p.id
p.mx.RUnlock()
return id
}

// Run runs the app process.
func (p *Proc) Run() error {
if err := p.cmd.Run(); err != nil {
return err
Expand All @@ -54,10 +58,12 @@ func (p *Proc) Run() error {
return nil
}

// Stop stops the app process.
func (p *Proc) Stop() error {
return p.cmd.Process.Kill()
}

// Wait waits for the app process to exit.
func (p *Proc) Wait() error {
return p.cmd.Wait()
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/app2/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"fmt"
"net/rpc"

"github.com/skycoin/skywire/pkg/app2/appserver"

"github.com/skycoin/skywire/pkg/app2/appnet"
"github.com/skycoin/skywire/pkg/app2/appserver"
"github.com/skycoin/skywire/pkg/routing"
)

Expand Down

0 comments on commit 608cc38

Please sign in to comment.