Skip to content
Snippets Groups Projects
Commit e337f5b5 authored by Luca Moser's avatar Luca Moser
Browse files

switches to hive.go parameter and events package

parent c2d1b2f9
No related branches found
No related tags found
No related merge requests found
Showing
with 202 additions and 139 deletions
{
"node": {
"logLevel": 3,
"disablePlugins": [],
"enablePlugins": []
},
"database": {
"directory": "mainnetdb"
},
"analysis": {
"serverPort": 0,
"serverAddress": "159.69.158.51:188"
},
"gossip": {
"port": 14666
},
"zeromq": {
"port": 5556
},
"autopeering": {
"address": "0.0.0.0",
"port": 14626,
"entryNodes": [
"7f7a876a4236091257e650da8dcf195fbe3cb625@159.69.158.51:14626"
],
"acceptRequests": true,
"sendRequests": true
}
}
\ No newline at end of file
...@@ -4,22 +4,25 @@ go 1.13 ...@@ -4,22 +4,25 @@ go 1.13
require ( require (
github.com/AndreasBriese/bbloom v0.0.0-20190825152654-46b345b51c96 // indirect github.com/AndreasBriese/bbloom v0.0.0-20190825152654-46b345b51c96 // indirect
github.com/apsdehal/go-logger v0.0.0-20190506062552-f85330a4b532 // indirect
github.com/dgraph-io/badger v1.6.0 github.com/dgraph-io/badger v1.6.0
github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/ethereum/go-ethereum v1.9.3 github.com/ethereum/go-ethereum v1.9.3
github.com/gdamore/tcell v1.2.0 github.com/gdamore/tcell v1.2.0
github.com/go-zeromq/zmq4 v0.5.0 github.com/go-zeromq/zmq4 v0.5.0
github.com/golang/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.3.2 // indirect
github.com/google/open-location-code/go v0.0.0-20190723034300-2c7115db77a3 github.com/google/open-location-code/go v0.0.0-20190903173953-119bc96a3a51
github.com/gorilla/websocket v1.4.1 github.com/gorilla/websocket v1.4.1
github.com/iotaledger/iota.go v1.0.0-beta.7 github.com/iotaledger/hive.go v0.0.0-20191113184748-b545de9170d9
github.com/kr/pretty v0.1.0 // indirect github.com/iotaledger/iota.go v1.0.0-beta.9
github.com/labstack/echo v3.3.10+incompatible github.com/labstack/echo v3.3.10+incompatible
github.com/labstack/gommon v0.3.0 // indirect github.com/labstack/gommon v0.3.0 // indirect
github.com/magiconair/properties v1.8.1 github.com/magiconair/properties v1.8.1
github.com/pkg/errors v0.8.1 github.com/pkg/errors v0.8.1
github.com/rivo/tview v0.0.0-20190829161255-f8bc69b90341 github.com/rivo/tview v0.0.0-20190829161255-f8bc69b90341
github.com/rivo/uniseg v0.1.0 // indirect github.com/rivo/uniseg v0.1.0 // indirect
github.com/spf13/pflag v1.0.5
github.com/stretchr/objx v0.2.0 // indirect
golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472 golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd // indirect golang.org/x/sys v0.0.0-20190904154756-749cb33beabd // indirect
......
This diff is collapsed.
package daemon package daemon
import ( import (
"github.com/iotaledger/goshimmer/packages/events" "github.com/iotaledger/hive.go/events"
) )
var Events = struct { var Events = struct {
......
package database package database
import ( import (
"github.com/iotaledger/hive.go/parameter"
"os" "os"
"sync" "sync"
...@@ -37,7 +38,7 @@ func checkDir(dir string) error { ...@@ -37,7 +38,7 @@ func checkDir(dir string) error {
} }
func createDB() (*badger.DB, error) { func createDB() (*badger.DB, error) {
directory := *DIRECTORY.Value directory := parameter.NodeConfig.GetString(CFG_DIRECTORY)
if err := checkDir(directory); err != nil { if err := checkDir(directory); err != nil {
return nil, errors.Wrap(err, "Could not check directory") return nil, errors.Wrap(err, "Could not check directory")
} }
......
package database package database
import ( import (
"github.com/iotaledger/goshimmer/packages/parameter" flag "github.com/spf13/pflag"
) )
var DIRECTORY = parameter.AddString("DATABASE/DIRECTORY", "mainnetdb", "path to the database folder") const (
CFG_DIRECTORY = "database.directory"
)
func init() {
flag.String(CFG_DIRECTORY, "mainnetdb", "path to the database folder")
}
package events
import "reflect"
type Closure struct {
Id uintptr
Fnc interface{}
}
func NewClosure(f interface{}) *Closure {
closure := &Closure{
Fnc: f,
}
closure.Id = reflect.ValueOf(closure).Pointer()
return closure
}
package events
import "sync"
type Event struct {
triggerFunc func(handler interface{}, params ...interface{})
callbacks map[uintptr]interface{}
mutex sync.RWMutex
}
func (this *Event) Attach(closure *Closure) {
this.mutex.Lock()
this.callbacks[closure.Id] = closure.Fnc
this.mutex.Unlock()
}
func (this *Event) Detach(closure *Closure) {
this.mutex.Lock()
delete(this.callbacks, closure.Id)
this.mutex.Unlock()
}
func (this *Event) Trigger(params ...interface{}) {
this.mutex.RLock()
for _, handler := range this.callbacks {
this.triggerFunc(handler, params...)
}
this.mutex.RUnlock()
}
func NewEvent(triggerFunc func(handler interface{}, params ...interface{})) *Event {
return &Event{
triggerFunc: triggerFunc,
callbacks: make(map[uintptr]interface{}),
}
}
package events
import (
"fmt"
"strconv"
"testing"
)
func BenchmarkEvent_Trigger(b *testing.B) {
event := NewEvent(intStringCaller)
event.Attach(NewClosure(func(param1 int, param2 string) {
// do nothing just get called
}))
b.ResetTimer()
for i := 0; i < b.N; i++ {
event.Trigger(4, "test")
}
}
// define how the event converts the generic parameters to the typed params - ugly but go has no generics :(
func intStringCaller(handler interface{}, params ...interface{}) {
handler.(func(int, string))(params[0].(int), params[1].(string))
}
func ExampleEvent() {
// create event object (usually exposed through a public struct that holds all the different event types)
event := NewEvent(intStringCaller)
// we have to wrap a function in a closure to make it identifiable
closure1 := NewClosure(func(param1 int, param2 string) {
fmt.Println("#1 " + param2 + ": " + strconv.Itoa(param1))
})
// multiple subscribers can attach to an event (closures can be inlined)
event.Attach(closure1)
event.Attach(NewClosure(func(param1 int, param2 string) {
fmt.Println("#2 " + param2 + ": " + strconv.Itoa(param1))
}))
// trigger the event
event.Trigger(1, "Hello World")
// unsubscribe the first closure and trigger again
event.Detach(closure1)
event.Trigger(1, "Hello World")
// Unordered output: #1 Hello World: 1
// #2 Hello World: 1
// #2 Hello World: 1
}
package events
func CallbackCaller(handler interface{}, params ...interface{}) { handler.(func())() }
func ErrorCaller(handler interface{}, params ...interface{}) { handler.(func(error))(params[0].(error)) }
package network package network
import ( import (
"github.com/iotaledger/goshimmer/packages/events" "github.com/iotaledger/hive.go/events"
) )
type BufferedConnectionEvents struct { type BufferedConnectionEvents struct {
......
...@@ -6,7 +6,7 @@ import ( ...@@ -6,7 +6,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/iotaledger/goshimmer/packages/events" "github.com/iotaledger/hive.go/events"
) )
type ManagedConnection struct { type ManagedConnection struct {
......
...@@ -5,8 +5,8 @@ import ( ...@@ -5,8 +5,8 @@ import (
"strconv" "strconv"
"sync" "sync"
"github.com/iotaledger/goshimmer/packages/events"
"github.com/iotaledger/goshimmer/packages/network" "github.com/iotaledger/goshimmer/packages/network"
"github.com/iotaledger/hive.go/events"
) )
type Server struct { type Server struct {
......
package tcp package tcp
import ( import (
"github.com/iotaledger/goshimmer/packages/events"
"github.com/iotaledger/goshimmer/packages/network" "github.com/iotaledger/goshimmer/packages/network"
"github.com/iotaledger/hive.go/events"
) )
type serverEvents struct { type serverEvents struct {
......
...@@ -3,7 +3,7 @@ package udp ...@@ -3,7 +3,7 @@ package udp
import ( import (
"net" "net"
"github.com/iotaledger/goshimmer/packages/events" "github.com/iotaledger/hive.go/events"
) )
type serverEvents struct { type serverEvents struct {
......
...@@ -5,7 +5,7 @@ import ( ...@@ -5,7 +5,7 @@ import (
"strconv" "strconv"
"sync" "sync"
"github.com/iotaledger/goshimmer/packages/events" "github.com/iotaledger/hive.go/events"
) )
type Server struct { type Server struct {
......
package node package node
import ( import (
"github.com/iotaledger/goshimmer/packages/events" "github.com/iotaledger/hive.go/events"
) )
type pluginEvents struct { type pluginEvents struct {
......
package node package node
import ( import (
"github.com/iotaledger/hive.go/parameter"
"sync" "sync"
"github.com/iotaledger/goshimmer/packages/daemon" "github.com/iotaledger/goshimmer/packages/daemon"
...@@ -53,7 +54,7 @@ func (node *Node) AddLogger(logger *Logger) { ...@@ -53,7 +54,7 @@ func (node *Node) AddLogger(logger *Logger) {
} }
func (node *Node) LogSuccess(pluginName string, message string) { func (node *Node) LogSuccess(pluginName string, message string) {
if *LOG_LEVEL.Value >= LOG_LEVEL_SUCCESS { if parameter.NodeConfig.GetInt(CFG_LOG_LEVEL) >= LOG_LEVEL_SUCCESS {
for _, logger := range node.loggers { for _, logger := range node.loggers {
if logger.GetEnabled() { if logger.GetEnabled() {
logger.LogSuccess(pluginName, message) logger.LogSuccess(pluginName, message)
...@@ -63,7 +64,7 @@ func (node *Node) LogSuccess(pluginName string, message string) { ...@@ -63,7 +64,7 @@ func (node *Node) LogSuccess(pluginName string, message string) {
} }
func (node *Node) LogInfo(pluginName string, message string) { func (node *Node) LogInfo(pluginName string, message string) {
if *LOG_LEVEL.Value >= LOG_LEVEL_INFO { if parameter.NodeConfig.GetInt(CFG_LOG_LEVEL) >= LOG_LEVEL_INFO {
for _, logger := range node.loggers { for _, logger := range node.loggers {
if logger.GetEnabled() { if logger.GetEnabled() {
logger.LogInfo(pluginName, message) logger.LogInfo(pluginName, message)
...@@ -73,7 +74,7 @@ func (node *Node) LogInfo(pluginName string, message string) { ...@@ -73,7 +74,7 @@ func (node *Node) LogInfo(pluginName string, message string) {
} }
func (node *Node) LogDebug(pluginName string, message string) { func (node *Node) LogDebug(pluginName string, message string) {
if *LOG_LEVEL.Value >= LOG_LEVEL_DEBUG { if parameter.NodeConfig.GetInt(CFG_LOG_LEVEL) >= LOG_LEVEL_DEBUG {
for _, logger := range node.loggers { for _, logger := range node.loggers {
if logger.GetEnabled() { if logger.GetEnabled() {
logger.LogDebug(pluginName, message) logger.LogDebug(pluginName, message)
...@@ -83,7 +84,7 @@ func (node *Node) LogDebug(pluginName string, message string) { ...@@ -83,7 +84,7 @@ func (node *Node) LogDebug(pluginName string, message string) {
} }
func (node *Node) LogWarning(pluginName string, message string) { func (node *Node) LogWarning(pluginName string, message string) {
if *LOG_LEVEL.Value >= LOG_LEVEL_WARNING { if parameter.NodeConfig.GetInt(CFG_LOG_LEVEL) >= LOG_LEVEL_WARNING {
for _, logger := range node.loggers { for _, logger := range node.loggers {
if logger.GetEnabled() { if logger.GetEnabled() {
logger.LogWarning(pluginName, message) logger.LogWarning(pluginName, message)
...@@ -93,7 +94,7 @@ func (node *Node) LogWarning(pluginName string, message string) { ...@@ -93,7 +94,7 @@ func (node *Node) LogWarning(pluginName string, message string) {
} }
func (node *Node) LogFailure(pluginName string, message string) { func (node *Node) LogFailure(pluginName string, message string) {
if *LOG_LEVEL.Value >= LOG_LEVEL_FAILURE { if parameter.NodeConfig.GetInt(CFG_LOG_LEVEL) >= LOG_LEVEL_FAILURE {
for _, logger := range node.loggers { for _, logger := range node.loggers {
if logger.GetEnabled() { if logger.GetEnabled() {
logger.LogFailure(pluginName, message) logger.LogFailure(pluginName, message)
......
package node package node
import "github.com/iotaledger/goshimmer/packages/parameter" import (
"flag"
var ( )
LOG_LEVEL = parameter.AddInt("NODE/LOG_LEVEL", LOG_LEVEL_INFO, "controls the log types that are shown")
DISABLE_PLUGINS = parameter.AddString("NODE/DISABLE_PLUGINS", "", "a list of plugins that shall be disabled") const (
ENABLE_PLUGINS = parameter.AddString("NODE/ENABLE_PLUGINS", "", "a list of plugins that shall be enabled") CFG_LOG_LEVEL = "node.logLevel"
CFG_DISABLE_PLUGINS = "node.disablePlugins"
CFG_ENABLE_PLGUINS = "node.enablePlugins"
) )
func init() {
flag.Int(CFG_LOG_LEVEL, LOG_LEVEL_INFO, "controls the log types that are shown")
flag.String(CFG_DISABLE_PLUGINS, "", "a list of plugins that shall be disabled")
flag.String(CFG_ENABLE_PLGUINS, "", "a list of plugins that shall be enabled")
}
...@@ -4,8 +4,8 @@ import ( ...@@ -4,8 +4,8 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/iotaledger/goshimmer/packages/events" "github.com/iotaledger/hive.go/events"
"github.com/iotaledger/goshimmer/packages/parameter" "github.com/iotaledger/hive.go/parameter"
) )
const ( const (
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment