From f7bc03a9915ea580c83a21e0b5e126964bf04a93 Mon Sep 17 00:00:00 2001 From: dylenfu Date: Mon, 11 May 2020 17:18:50 +0800 Subject: [PATCH] add params config path --- Makefile | 12 ++++++++---- main.go | 12 ++++++++---- methods/endpoint.go | 1 + methods/methods.go | 22 ++++++++++++++-------- methods/utils.go | 10 +++++++++- params/DHTCapture.json | 5 +++++ 6 files changed, 45 insertions(+), 17 deletions(-) create mode 100644 params/DHTCapture.json diff --git a/Makefile b/Makefile index 34c9c16..748a0f4 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -32,3 +32,7 @@ clean: restart: make clean && make dht-tool && ./dht-tool + +docker: + make clean && make + docker build -t dht:latest . diff --git a/main.go b/main.go index e88a332..1978750 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ package main import ( "flag" + methods2 "github.com/ontio/ontology-tool/methods" "math/rand" "strings" "time" @@ -30,15 +31,17 @@ 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() } @@ -46,6 +49,7 @@ func init() { func main() { rand.Seed(time.Now().UnixNano()) log4.LoadConfiguration(LogConfig) + methods2.SetParamsDir(ParamsConfig) defer time.Sleep(time.Second) err := config.DefConfig.Init(Config) diff --git a/methods/endpoint.go b/methods/endpoint.go index 25b52a2..d599732 100644 --- a/methods/endpoint.go +++ b/methods/endpoint.go @@ -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) } diff --git a/methods/methods.go b/methods/methods.go index 272cadd..5c620ca 100644 --- a/methods/methods.go +++ b/methods/methods.go @@ -67,7 +67,7 @@ func Handshake() bool { Remote string TestCase uint8 } - if err := getParamsFromJsonFile("./params/Handshake.json", ¶ms); err != nil { + if err := getParamsFromJsonFile("Handshake.json", ¶ms); err != nil { _ = log4.Error("%s", err) return false } @@ -96,7 +96,7 @@ func HandshakeWrongMsg() bool { Remote string WrongMsg bool } - if err := getParamsFromJsonFile("./params/HandshakeWrongMsg.json", ¶ms); err != nil { + if err := getParamsFromJsonFile("HandshakeWrongMsg.json", ¶ms); err != nil { _ = log4.Error("%s", err) return false } @@ -118,9 +118,9 @@ func HandshakeTimeout() bool { var params struct { Remote string BlockTime int - Retry int + Retry int } - if err := getParamsFromJsonFile("./params/HandshakeTimeout.json", ¶ms); err != nil { + if err := getParamsFromJsonFile("HandshakeTimeout.json", ¶ms); err != nil { _ = log4.Error("%s", err) return false } @@ -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 { @@ -156,7 +156,7 @@ func Heartbeat() bool { InitBlockHeight uint64 DispatchTime int } - if err := getParamsFromJsonFile("./params/Heartbeat.json", ¶ms); err != nil { + if err := getParamsFromJsonFile("Heartbeat.json", ¶ms); err != nil { _ = log4.Error("%s", err) return false } @@ -184,7 +184,7 @@ func HeartbeatInterruptPing() bool { InterruptLastTime int64 DispatchTime int } - if err := getParamsFromJsonFile("./params/HeartbeatInterruptPing.json", ¶ms); err != nil { + if err := getParamsFromJsonFile("HeartbeatInterruptPing.json", ¶ms); err != nil { _ = log4.Error("%s", err) return false } @@ -215,7 +215,7 @@ func HeartbeatInterruptPong() bool { InterruptLastTime int64 DispatchTime int } - if err := getParamsFromJsonFile("./params/HeartbeatInterruptPong.json", ¶ms); err != nil { + if err := getParamsFromJsonFile("HeartbeatInterruptPong.json", ¶ms); err != nil { _ = log4.Error("%s", err) return false } @@ -237,3 +237,9 @@ func HeartbeatInterruptPong() bool { log4.Info("heartbeat end!") return true } + +func DHTCapture() bool { + + log4.Info("dht capture success") + return true +} diff --git a/methods/utils.go b/methods/utils.go index 12530ef..9c4bba9 100644 --- a/methods/utils.go +++ b/methods/utils.go @@ -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 } diff --git a/params/DHTCapture.json b/params/DHTCapture.json new file mode 100644 index 0000000..9953b8c --- /dev/null +++ b/params/DHTCapture.json @@ -0,0 +1,5 @@ +{ + "Remote": "172.168.3.158:20338", + "InitBlockHeight": 4962, + "DispatchTime": 10 +}