Skip to content

Commit

Permalink
add params config path
Browse files Browse the repository at this point in the history
  • Loading branch information
dylenfu committed May 11, 2020
1 parent c510207 commit f7bc03a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 17 deletions.
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ BUILD_NODE_PAR = -ldflags "-X main.Version=1.0.0"
ARCH=$(shell uname -m)
SRC_FILES = $(shell git ls-files | grep -e .go$ | grep -v _test.go)

ont-tool: $(SRC_FILES)
dht-tool: $(SRC_FILES)
$(GC) $(BUILD_NODE_PAR) -o dht-tool main.go

dht-tool-cross: dht-tool-windows dht-tool-linux dht-tool-darwin

dht-tool-windows:
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GC) $(BUILD_NODE_PAR) -o dht-tool-windows-amd64.exe main.go
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GC) $(BUILD_NODE_PAR) -o dht-tool-windows.exe main.go

dht-tool-linux:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GC) $(BUILD_NODE_PAR) -o dht-tool-linux-amd64 main.go
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GC) $(BUILD_NODE_PAR) -o dht-tool-linux main.go

dht-tool-darwin:
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GC) $(BUILD_NODE_PAR) -o dht-tool-darwin-amd64 main.go
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GC) $(BUILD_NODE_PAR) -o dht-tool-darwin main.go

tools-cross: tools-windows tools-linux tools-darwin

Expand All @@ -32,3 +32,7 @@ clean:

restart:
make clean && make dht-tool && ./dht-tool

docker:
make clean && make
docker build -t dht:latest .
12 changes: 8 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main

import (
"flag"
methods2 "github.com/ontio/ontology-tool/methods"
"math/rand"
"strings"
"time"
Expand All @@ -30,22 +31,25 @@ import (
)

var (
Version string
Config string //config file
LogConfig string //Log config file
Methods string //Methods list in cmdline
Version string
Config string //config file
LogConfig string //Log config file
ParamsConfig string // params file dir
Methods string //Methods list in cmdline
)

func init() {
flag.StringVar(&Config, "cfg", "./config.json", "Config of ontology-tool")
flag.StringVar(&LogConfig, "lfg", "./log4go.xml", "Log config of ontology-tool")
flag.StringVar(&ParamsConfig, "pfg", "./params", "Test params")
flag.StringVar(&Methods, "t", "handshake", "methods to run. use ',' to split methods")
flag.Parse()
}

func main() {
rand.Seed(time.Now().UnixNano())
log4.LoadConfiguration(LogConfig)
methods2.SetParamsDir(ParamsConfig)
defer time.Sleep(time.Second)

err := config.DefConfig.Init(Config)
Expand Down
1 change: 1 addition & 0 deletions methods/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ func init() {
core.OntTool.RegMethod("heartbeat", Heartbeat)
core.OntTool.RegMethod("heartbeatInterruptPing", HeartbeatInterruptPing)
core.OntTool.RegMethod("heartbeatInterruptPong", HeartbeatInterruptPong)
core.OntTool.RegMethod("dhtCapture", DHTCapture)
}
22 changes: 14 additions & 8 deletions methods/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func Handshake() bool {
Remote string
TestCase uint8
}
if err := getParamsFromJsonFile("./params/Handshake.json", &params); err != nil {
if err := getParamsFromJsonFile("Handshake.json", &params); err != nil {
_ = log4.Error("%s", err)
return false
}
Expand Down Expand Up @@ -96,7 +96,7 @@ func HandshakeWrongMsg() bool {
Remote string
WrongMsg bool
}
if err := getParamsFromJsonFile("./params/HandshakeWrongMsg.json", &params); err != nil {
if err := getParamsFromJsonFile("HandshakeWrongMsg.json", &params); err != nil {
_ = log4.Error("%s", err)
return false
}
Expand All @@ -118,9 +118,9 @@ func HandshakeTimeout() bool {
var params struct {
Remote string
BlockTime int
Retry int
Retry int
}
if err := getParamsFromJsonFile("./params/HandshakeTimeout.json", &params); err != nil {
if err := getParamsFromJsonFile("HandshakeTimeout.json", &params); err != nil {
_ = log4.Error("%s", err)
return false
}
Expand All @@ -136,7 +136,7 @@ func HandshakeTimeout() bool {
return true
}

for i:=0; i< params.Retry; i++ {
for i := 0; i < params.Retry; i++ {
log4.Debug("connecting retry cnt %d", i)
common.SetHandshakeTimeout(0)
if err := ns.Connect(params.Remote); err != nil {
Expand All @@ -156,7 +156,7 @@ func Heartbeat() bool {
InitBlockHeight uint64
DispatchTime int
}
if err := getParamsFromJsonFile("./params/Heartbeat.json", &params); err != nil {
if err := getParamsFromJsonFile("Heartbeat.json", &params); err != nil {
_ = log4.Error("%s", err)
return false
}
Expand Down Expand Up @@ -184,7 +184,7 @@ func HeartbeatInterruptPing() bool {
InterruptLastTime int64
DispatchTime int
}
if err := getParamsFromJsonFile("./params/HeartbeatInterruptPing.json", &params); err != nil {
if err := getParamsFromJsonFile("HeartbeatInterruptPing.json", &params); err != nil {
_ = log4.Error("%s", err)
return false
}
Expand Down Expand Up @@ -215,7 +215,7 @@ func HeartbeatInterruptPong() bool {
InterruptLastTime int64
DispatchTime int
}
if err := getParamsFromJsonFile("./params/HeartbeatInterruptPong.json", &params); err != nil {
if err := getParamsFromJsonFile("HeartbeatInterruptPong.json", &params); err != nil {
_ = log4.Error("%s", err)
return false
}
Expand All @@ -237,3 +237,9 @@ func HeartbeatInterruptPong() bool {
log4.Info("heartbeat end!")
return true
}

func DHTCapture() bool {

log4.Info("dht capture success")
return true
}
10 changes: 9 additions & 1 deletion methods/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@ package methods
import (
"encoding/json"
"io/ioutil"
"os"
"time"
)

var paramsFileDir string

func SetParamsDir(path string) {
paramsFileDir = path
}

func getParamsFromJsonFile(fileName string, data interface{}) error {
bz, err := ioutil.ReadFile(fileName)
fullPath := paramsFileDir + string(os.PathSeparator) + fileName
bz, err := ioutil.ReadFile(fullPath)
if err != nil {
return err
}
Expand Down
5 changes: 5 additions & 0 deletions params/DHTCapture.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Remote": "172.168.3.158:20338",
"InitBlockHeight": 4962,
"DispatchTime": 10
}

0 comments on commit f7bc03a

Please sign in to comment.