Skip to content

Commit

Permalink
node manager
Browse files Browse the repository at this point in the history
  • Loading branch information
skycoin committed Sep 24, 2016
1 parent 756febb commit 9b806f2
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 48 deletions.
137 changes: 109 additions & 28 deletions cmd/mesh/mesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,32 @@ package main

import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"reflect"
"time"

"github.com/skycoin/skycoin/src/mesh/domain"
"github.com/skycoin/skycoin/src/mesh/gui"
mesh "github.com/skycoin/skycoin/src/mesh/node"
"github.com/skycoin/skycoin/src/mesh/node/connection"
"github.com/skycoin/skycoin/src/mesh/nodemanager"
"github.com/skycoin/skycoin/src/util"

"gopkg.in/op/go-logging.v1"
)

var ()
var (
logger = logging.MustGetLogger("skywire.main")
logFormat = "[%{module}:%{level}] %{message}"
logModules = []string{
"skywire.main",
}
)

type Config struct {
// Remote web interface
Expand All @@ -29,6 +44,12 @@ type Config struct {
DataDirectory string
// GUI directory contains assets for the html gui
GUIDirectory string

// Logging
LogLevel logging.Level
ColorLog bool
// This is the value registered with flag, it is converted to LogLevel after parsing
logLevel string
}

func (c *Config) Parse() {
Expand All @@ -38,17 +59,8 @@ func (c *Config) Parse() {
}

func (c *Config) register() {
flag.BoolVar(&c.DisablePEX, "disable-pex", c.DisablePEX,
"disable PEX peer discovery")
flag.BoolVar(&c.DisableOutgoingConnections, "disable-outgoing",
c.DisableOutgoingConnections, "Don't make outgoing connections")
flag.BoolVar(&c.DisableIncomingConnections, "disable-incoming",
c.DisableIncomingConnections, "Don't make incoming connections")
flag.BoolVar(&c.DisableNetworking, "disable-networking",
c.DisableNetworking, "Disable all network activity")
flag.StringVar(&c.Address, "address", c.Address,
"IP Address to run application on. Leave empty to default to a public interface")
flag.IntVar(&c.Port, "port", c.Port, "Port to run application on")

//flag.IntVar(&c.Port, "port", c.Port, "Port to run application on")
flag.BoolVar(&c.WebInterface, "web-interface", c.WebInterface,
"enable the web interface")
flag.IntVar(&c.WebInterfacePort, "web-interface-port",
Expand All @@ -72,10 +84,13 @@ func (c *Config) register() {
flag.StringVar(&c.GUIDirectory, "gui-dir", c.GUIDirectory,
"static content directory for the html gui")

flag.StringVar(&c.logLevel, "log-level", c.logLevel,
"Choices are: debug, info, notice, warning, error, critical")
flag.BoolVar(&c.ColorLog, "color-log", c.ColorLog,
"Add terminal colors to log output")
}

func (c *Config) postProcess() {
var err error

c.DataDirectory = util.InitDataDir(c.DataDirectory)
if c.WebInterfaceCert == "" {
Expand All @@ -85,12 +100,18 @@ func (c *Config) postProcess() {
c.WebInterfaceKey = filepath.Join(c.DataDirectory, "key.pem")
}

//ll, err := logging.LogLevel(c.logLevel)
//panicIfError(err, "Invalid -log-level %s", c.logLevel)
//c.LogLevel = ll
ll, err := logging.LogLevel(c.logLevel)
panicIfError(err, "Invalid -log-level %s", c.logLevel)
c.LogLevel = ll

}

func panicIfError(err error, msg string, args ...interface{}) {
if err != nil {
log.Panicf(msg+": %v", append(args, err)...)
}
}

var devConfig Config = Config{
// Remote web interface
WebInterface: true,
Expand All @@ -106,8 +127,14 @@ var devConfig Config = Config{
DataDirectory: ".skywire",
// Web GUI static resources
GUIDirectory: "./src/mesh/gui/static/",

// Logging
LogLevel: logging.DEBUG,
ColorLog: true,
logLevel: "DEBUG",
}

//move to util
func catchInterrupt(quit chan<- int) {
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, os.Interrupt)
Expand All @@ -116,6 +143,18 @@ func catchInterrupt(quit chan<- int) {
quit <- 1
}

//move to util
func initLogging(level logging.Level, color bool) {
format := logging.MustStringFormatter(logFormat)
logging.SetFormatter(format)
for _, s := range logModules {
logging.SetLevel(level, s)
}
stdout := logging.NewLogBackend(os.Stdout, "", 0)
stdout.Color = color
logging.SetBackend(stdout)
}

func Run(c *Config) {

c.GUIDirectory = util.ResolveResourceDirectory(c.GUIDirectory)
Expand All @@ -124,23 +163,42 @@ func Run(c *Config) {
quit := make(chan int)
go catchInterrupt(quit)

scheme := "http"
host := fmt.Sprintf("%s:%d", c.WebInterfaceAddr, c.WebInterfacePort)
fullAddress := fmt.Sprintf("%s://%s", scheme, host)
logger.Critical("Full address: %s", fullAddress)

initLogging(c.LogLevel, c.ColorLog)

//state node_manager
fmt.Fprintln(os.Stdout, "Starting Node Manager Service...")
var nm_config = nodemanager.NodeManagerConfig{}
var nm *nodemanager.NodeManager = nodemanager.NewNodeManager(&nm_config)

if c.WebInterface {
var err error
if c.WebInterfaceHTTPS {
// Verify cert/key parameters, and if neither exist, create them
errs := gui.CreateCertIfNotExists(host, c.WebInterfaceCert, c.WebInterfaceKey)
if len(errs) != 0 {
for _, err := range errs {
logger.Error(err.Error())

/*
if c.WebInterfaceHTTPS {
// Verify cert/key parameters, and if neither exist, create them
errs := util.CreateCertIfNotExists(host, c.WebInterfaceCert, c.WebInterfaceKey, "Skywired")
if len(errs) != 0 {
for _, err := range errs {
logger.Error(err.Error())
}
logger.Error("gui.CreateCertIfNotExists failure")
os.Exit(1)
}
logger.Error("gui.CreateCertIfNotExists failure")
os.Exit(1)
err = gui.LaunchWebInterfaceHTTPS(host, c.GUIDirectory, d, c.WebInterfaceCert, c.WebInterfaceKey)
} else {
err = gui.LaunchWebInterface(host, c.GUIDirectory, d)
}
*/

err = gui.LaunchWebInterfaceHTTPS(host, c.GUIDirectory, d, c.WebInterfaceCert, c.WebInterfaceKey)
} else {
err = gui.LaunchWebInterface(host, c.GUIDirectory, d)
}
//pass the node manager instance to the http server
//no https
err = gui.LaunchWebInterface(host, c.GUIDirectory, nm)

if err != nil {
logger.Error(err.Error())
Expand All @@ -159,10 +217,32 @@ func Run(c *Config) {
}
}()
}

}

stopDaemon := make(chan int)
//start the node manager
go nm.Start(stopDaemon)

<-quit
stopDaemon <- 1

logger.Info("Shutting down")
nm.Shutdown()
logger.Info("Goodbye")

}

func main() {
devConfig.Parse()
Run(&devConfig)
}

//
// OLD
//

func main_old() {
fmt.Fprintln(os.Stdout, "Starting Node Manager Service...")

config := nodemanager.ServerConfig
Expand All @@ -189,6 +269,7 @@ func main() {
}
}
}

}

func filterMessages(msg mesh.MeshMessage) bool {
Expand Down
3 changes: 1 addition & 2 deletions cmd/skycoin/skycoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ type Config struct {
// Logging
LogLevel logging.Level
ColorLog bool
// This is the value registered with flag, it is converted to LogLevel
// after parsing
// This is the value registered with flag, it is converted to LogLevel after parsing
logLevel string

// Wallets
Expand Down
2 changes: 1 addition & 1 deletion src/gui/http.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gui

import (
"crypto/tls"
//"crypto/tls"
"fmt"
"io/ioutil"
"log"
Expand Down
35 changes: 18 additions & 17 deletions src/mesh/gui/http.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package gui

import (
"crypto/tls"
//"crypto/tls"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
//"os"
"path/filepath"
"strings"
//"strings"

"gopkg.in/op/go-logging.v1"

"github.com/skycoin/skycoin/src/mesh/nodemanager"
"github.com/skycoin/skycoin/src/util"

wh "github.com/skycoin/skycoin/src/util/http" //http,json helpers
)

var (
Expand All @@ -28,7 +31,7 @@ const (

// Begins listening on http://$host, for enabling remote web access
// Does NOT use HTTPS
func LaunchWebInterface(host, staticDir string, nodemanager *nodemanager.NodeManager) error {
func LaunchWebInterface(host, staticDir string, nm *nodemanager.NodeManager) error {
logger.Info("Starting web interface on http://%s", host)
logger.Warning("HTTPS not in use!")
logger.Info("Web resources directory: %s", staticDir)
Expand All @@ -44,7 +47,7 @@ func LaunchWebInterface(host, staticDir string, nodemanager *nodemanager.NodeMan
}

// Runs http.Serve() in a goroutine
serve(listener, NewGUIMux(appLoc, daemon))
serve(listener, NewGUIMux(appLoc, nm))
return nil
}

Expand All @@ -65,7 +68,7 @@ func serve(listener net.Listener, mux *http.ServeMux) {
//move to util

// Creates an http.ServeMux with handlers registered
func NewGUIMux(appLoc string, daemon *nodemanager.NodeManager) *http.ServeMux {
func NewGUIMux(appLoc string, nm *nodemanager.NodeManager) *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/", newIndexHandler(appLoc))

Expand All @@ -78,16 +81,14 @@ func NewGUIMux(appLoc string, daemon *nodemanager.NodeManager) *http.ServeMux {
mux.Handle(route, http.FileServer(http.Dir(appLoc)))
}

// Wallet interface
RegisterWalletHandlers(mux, daemon.Gateway)
// Blockchain interface
RegisterBlockchainHandlers(mux, daemon.Gateway)
// Network stats interface
RegisterNetworkHandlers(mux, daemon.Gateway)
// Network API handler
RegisterApiHandlers(mux, daemon.Gateway)
// Transaction interface
RegisterTxHandlers(mux, daemon.Gateway)
/*
// Wallet interface
RegisterWalletHandlers(mux, daemon.Gateway)
*/

//register handlers
RegisterNodeManagerHandlers(mux, nm)

return mux
}

Expand All @@ -100,7 +101,7 @@ func newIndexHandler(appLoc string) http.HandlerFunc {
if r.URL.Path == "/" {
http.ServeFile(w, r, page)
} else {
Error404(w)
wh.Error404(w)
}
}
}
21 changes: 21 additions & 0 deletions src/mesh/nodemanager/nodemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ type NodeManager struct {
Routes map[RouteKey]Route
}

//configuration in here
type NodeManagerConfig struct {
}

//add config eventually
func NewNodeManager(config *NodeManagerConfig) *NodeManager {
nm := NodeManager{}
return &nm
}

//run node manager, in goroutine
//write into channel to stop
func (self *NodeManager) Start(channel chan int) {

}

//called to trigger shutdown
func (self *NodeManager) Shutdown() {

}

type RouteKey struct {
SourceNode cipher.PubKey
TargetNode cipher.PubKey
Expand Down

0 comments on commit 9b806f2

Please sign in to comment.