From 1d9e0f856ccf8fb451c9a6d0fe466837eafe5e23 Mon Sep 17 00:00:00 2001 From: Ben Grewell Date: Wed, 10 Mar 2021 08:38:04 -0800 Subject: [PATCH] refactor for better usability --- .idea/workspace.xml | 2 +- client.go | 78 ++++++++++++++++++++++++--------------------- cmd/main.go | 74 +++++++++++++++++++++--------------------- server.go | 26 ++++++++------- shared.go | 8 ----- 5 files changed, 94 insertions(+), 94 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index c8d7f43..8193e62 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,7 +4,7 @@ - + diff --git a/client.go b/client.go index bb3b3c5..83b55da 100644 --- a/client.go +++ b/client.go @@ -21,9 +21,7 @@ func NewClient(host string) *Client { streams := 1 c := &Client{ options: &ClientOptions{ - SharedOptions: SharedOptions{ - JSON: &json, - }, + JSON: &json, Proto: &proto, TimeSec: &time, Length: &length, @@ -37,48 +35,56 @@ func NewClient(host string) *Client { } type ClientOptions struct { - SharedOptions - Host *string - Proto *Protocol - Bandwidth *string - TimeSec *int - Bytes *string - BlockCount *string - Length *string - Streams *int - Reverse *bool - Window *string - MSS *int - NoDelay *bool - Version4 *bool - Version6 *bool - TOS *int - ZeroCopy *bool - OmitSec *int - Prefix *string - IncludeServer *bool + Port *int `json:"port" yaml:"port" xml:"port"` + Format *rune `json:"format" yaml:"format" xml:"format"` + Interval *int `json:"interval" yaml:"interval" xml:"interval"` + JSON *bool `json:"json" yaml:"json" xml:"json"` + LogFile *string `json:"log_file" yaml:"log_file" xml:"log_file"` + Host *string `json:"host" yaml:"host" xml:"host"` + Proto *Protocol `json:"proto" yaml:"proto" xml:"proto"` + Bandwidth *string `json:"bandwidth" yaml:"bandwidth" xml:"bandwidth"` + TimeSec *int `json:"time_sec" yaml:"time_sec" xml:"time_sec"` + Bytes *string `json:"bytes" yaml:"bytes" xml:"bytes"` + BlockCount *string `json:"block_count" yaml:"block_count" xml:"block_count"` + Length *string `json:"length" yaml:"length" xml:"length"` + Streams *int `json:"streams" yaml:"streams" xml:"streams"` + Reverse *bool `json:"reverse" yaml:"reverse" xml:"reverse"` + Window *string `json:"window" yaml:"window" xml:"window"` + MSS *int `json:"mss" yaml:"mss" xml:"mss"` + NoDelay *bool `json:"no_delay" yaml:"no_delay" xml:"no_delay"` + Version4 *bool `json:"version_4" yaml:"version_4" xml:"version_4"` + Version6 *bool `json:"version_6" yaml:"version_6" xml:"version_6"` + TOS *int `json:"tos" yaml:"tos" xml:"tos"` + ZeroCopy *bool `json:"zero_copy" yaml:"zero_copy" xml:"zero_copy"` + OmitSec *int `json:"omit_sec" yaml:"omit_sec" xml:"omit_sec"` + Prefix *string `json:"prefix" yaml:"prefix" xml:"prefix"` + IncludeServer *bool `json:"include_server" yaml:"include_server" xml:"include_server"` } type Client struct { - Id string - Running bool - Done chan bool - options *ClientOptions - exitCode *int - report *TestReport - outputStream io.ReadCloser - errorStream io.ReadCloser - cancel context.CancelFunc - mode TestMode - live bool - reportingChan chan *StreamIntervalReport - reportingFile string + Id string `json:"id" yaml:"id" xml:"id"` + Running bool `json:"running" yaml:"running" xml:"running"` + Done chan bool `json:"done" yaml:"done" xml:"done"` + options *ClientOptions `json:"options" yaml:"options" xml:"options"` + exitCode *int `json:"exit_code" yaml:"exit_code" xml:"exit_code"` + report *TestReport `json:"report" yaml:"report" xml:"report"` + outputStream io.ReadCloser `json:"output_stream" yaml:"output_stream" xml:"output_stream"` + errorStream io.ReadCloser `json:"error_stream" yaml:"error_stream" xml:"error_stream"` + cancel context.CancelFunc `json:"cancel" yaml:"cancel" xml:"cancel"` + mode TestMode `json:"mode" yaml:"mode" xml:"mode"` + live bool `json:"live" yaml:"live" xml:"live"` + reportingChan chan *StreamIntervalReport `json:"reporting_chan" yaml:"reporting_chan" xml:"reporting_chan"` + reportingFile string `json:"reporting_file" yaml:"reporting_file" xml:"reporting_file"` } func (c *Client) LoadOptionsJSON(jsonStr string) (err error) { return json.Unmarshal([]byte(jsonStr), c.options) } +func (c *Client) LoadOptions(options *ClientOptions) { + c.options = options +} + func (c *Client) commandString() (cmd string, err error) { builder := strings.Builder{} if c.options.Host == nil || *c.options.Host == "" { diff --git a/cmd/main.go b/cmd/main.go index 0d86a3b..2643adb 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,46 +1,46 @@ package main import ( - "fmt" - "github.com/BGrewell/go-conversions" - "github.com/BGrewell/go-iperf" - "time" + //"fmt" + //"github.com/BGrewell/go-conversions" + //"github.com/BGrewell/go-iperf" + //"time" ) func main() { - s := iperf.NewServer() - c := iperf.NewClient("127.0.0.1") - includeServer := true - c.IncludeServer = &includeServer - fmt.Println(s.Id) - fmt.Println(c.Id) - - err := s.Start() - if err != nil { - fmt.Println(err) - } - - err = c.Start() - if err != nil { - fmt.Println(err) - } - - for c.Running { - time.Sleep(1 * time.Second) - } - - fmt.Println("stopping server") - s.Stop() - - fmt.Printf("Client exit code: %d\n", *c.ExitCode) - fmt.Printf("Server exit code: %d\n", *s.ExitCode) - iperf.Cleanup() - if c.Report.Error != "" { - fmt.Println(c.Report.Error) - } else { - fmt.Printf("Recv Rate: %s\n", conversions.IntBitRateToString(int64(c.Report.End.SumReceived.BitsPerSecond))) - fmt.Printf("Send Rate: %s\n", conversions.IntBitRateToString(int64(c.Report.End.SumSent.BitsPerSecond))) - } + //s := iperf.NewServer() + //c := iperf.NewClient("127.0.0.1") + //includeServer := true + //c.IncludeServer = &includeServer + //fmt.Println(s.Id) + //fmt.Println(c.Id) + // + //err := s.Start() + //if err != nil { + // fmt.Println(err) + //} + // + //err = c.Start() + //if err != nil { + // fmt.Println(err) + //} + // + //for c.Running { + // time.Sleep(1 * time.Second) + //} + // + //fmt.Println("stopping server") + //s.Stop() + // + //fmt.Printf("Client exit code: %d\n", *c.ExitCode) + //fmt.Printf("Server exit code: %d\n", *s.ExitCode) + //iperf.Cleanup() + //if c.Report.Error != "" { + // fmt.Println(c.Report.Error) + //} else { + // fmt.Printf("Recv Rate: %s\n", conversions.IntBitRateToString(int64(c.Report.End.SumReceived.BitsPerSecond))) + // fmt.Printf("Send Rate: %s\n", conversions.IntBitRateToString(int64(c.Report.End.SumSent.BitsPerSecond))) + //} } diff --git a/server.go b/server.go index 7b1ea83..5aacff9 100644 --- a/server.go +++ b/server.go @@ -12,24 +12,26 @@ func NewServer() *Server { defaultPort := 5201 defaultInterval := 1 s := &Server{ - SharedOptions: SharedOptions{ - Port: &defaultPort, - Interval: &defaultInterval, - }, + Port: &defaultPort, + Interval: &defaultInterval, } s.Id = uuid.New().String() return s } type Server struct { - SharedOptions - Id string - OneOff *bool - ExitCode *int - Running bool - outputStream io.ReadCloser - errorStream io.ReadCloser - cancel context.CancelFunc + Id string `json:"id" yaml:"id" xml:"id"` + OneOff *bool `json:"one_off" yaml:"one_off" xml:"one_off"` + ExitCode *int `json:"exit_code" yaml:"exit_code" xml:"exit_code"` + Port *int `json:"port" yaml:"port" xml:"port"` + Format *rune `json:"format" yaml:"format" xml:"format"` + Interval *int `json:"interval" yaml:"interval" xml:"interval"` + JSON *bool `json:"json" yaml:"json" xml:"json"` + LogFile *string `json:"log_file" yaml:"log_file" xml:"log_file"` + Running bool `json:"running" yaml:"running" xml:"running"` + outputStream io.ReadCloser `json:"output_stream" yaml:"output_stream" xml:"output_stream"` + errorStream io.ReadCloser `json:"error_stream" yaml:"error_stream" xml:"error_stream"` + cancel context.CancelFunc `json:"cancel" yaml:"cancel" xml:"cancel"` } func (s *Server) Start() (err error) { diff --git a/shared.go b/shared.go index ab6c701..6934e6d 100644 --- a/shared.go +++ b/shared.go @@ -13,14 +13,6 @@ const ( MODE_LIVE TestMode = "live" ) -type SharedOptions struct { - Port *int - Format *rune - Interval *int - JSON *bool - LogFile *string -} - type DebugScanner struct { Silent bool }