From d7a753624e5e1319cbff7534480407bef1cd078a Mon Sep 17 00:00:00 2001 From: Viktor Posloncec Date: Thu, 25 Jan 2024 13:59:01 +0100 Subject: [PATCH] Add reliabilitylevel_string autogen code --- base/node.go | 26 +++++++++++++++++--------- base/reliability.go | 1 + base/reliabilitylevel_string.go | 27 +++++++++++++++++++++++++++ cmd/run.go | 3 +-- export/node.go | 3 +-- orchestration/orchestrator.go | 10 +++++++++- 6 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 base/reliabilitylevel_string.go diff --git a/base/node.go b/base/node.go index defa8ef..fbd377e 100644 --- a/base/node.go +++ b/base/node.go @@ -2,25 +2,32 @@ package base import ( "go.uber.org/zap" + "strconv" "time" ) type NodeID int + +func (n NodeID) String() string { + return strconv.Itoa(int(n)) +} + type PacketWithSender struct { Packet *Packet Sender *Node } type Node struct { - ID NodeID - Log *zap.SugaredLogger - MessageQueue chan *PacketWithSender - Subscribers []*Node - PacketHistory map[PacketUUID]*PacketLog - Reliability ReliabilityLevel - CpuScore int - packagesSent int - packagesDropped int + ID NodeID + Log *zap.SugaredLogger + MessageQueue chan *PacketWithSender + Subscribers []*Node + PacketHistory map[PacketUUID]*PacketLog + Reliability ReliabilityLevel + CpuScore int + PackagesReceived int + packagesSent int + packagesDropped int } type PacketLog struct { @@ -60,6 +67,7 @@ func (n *Node) RecvPacket(callerNode *Node, p *Packet) { n.MessageQueue <- &PacketWithSender{ Sender: callerNode, Packet: p} + n.PackagesReceived++ } func (n *Node) AlreadyReceived(id PacketUUID) bool { diff --git a/base/reliability.go b/base/reliability.go index a0a6605..f95416b 100644 --- a/base/reliability.go +++ b/base/reliability.go @@ -4,6 +4,7 @@ import ( "math/rand" ) +//go:generate stringer -type=ReliabilityLevel type ReliabilityLevel int const ( diff --git a/base/reliabilitylevel_string.go b/base/reliabilitylevel_string.go new file mode 100644 index 0000000..781d3c1 --- /dev/null +++ b/base/reliabilitylevel_string.go @@ -0,0 +1,27 @@ +// Code generated by "stringer -type=ReliabilityLevel"; DO NOT EDIT. + +package base + +import "strconv" + +func _() { + // An "invalid array index" compiler error signifies that the constant values have changed. + // Re-run the stringer command to generate them again. + var x [1]struct{} + _ = x[Super-0] + _ = x[Reliable-1] + _ = x[Common-2] + _ = x[Occasional-3] + _ = x[Erratic-4] +} + +const _ReliabilityLevel_name = "SuperReliableCommonOccasionalErratic" + +var _ReliabilityLevel_index = [...]uint8{0, 5, 13, 19, 29, 36} + +func (i ReliabilityLevel) String() string { + if i < 0 || i >= ReliabilityLevel(len(_ReliabilityLevel_index)-1) { + return "ReliabilityLevel(" + strconv.FormatInt(int64(i), 10) + ")" + } + return _ReliabilityLevel_name[_ReliabilityLevel_index[i]:_ReliabilityLevel_index[i+1]] +} diff --git a/cmd/run.go b/cmd/run.go index 8744b41..3335344 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -4,9 +4,8 @@ Copyright © 2024 NAME HERE package cmd import ( - "github.com/vposloncec/go-ssip/orchestration" - "github.com/spf13/cobra" + "github.com/vposloncec/go-ssip/orchestration" ) // runCmd represents the run command diff --git a/export/node.go b/export/node.go index 386f622..77ea602 100644 --- a/export/node.go +++ b/export/node.go @@ -3,7 +3,6 @@ package export import ( "bytes" "encoding/csv" - "fmt" "github.com/vposloncec/go-ssip/base" "strconv" ) @@ -28,7 +27,7 @@ func nodeToRow(node *base.Node) []string { row := make([]string, len(nodeHeaders)) row[0] = node.ID.String() row[1] = strconv.Itoa(node.PackagesReceived) - row[2] = fmt.Sprint(node.Reliability) + row[2] = node.Reliability.String() return row } diff --git a/orchestration/orchestrator.go b/orchestration/orchestrator.go index 0610dac..f84f1c5 100644 --- a/orchestration/orchestrator.go +++ b/orchestration/orchestrator.go @@ -1,10 +1,13 @@ package orchestration import ( + "fmt" "github.com/spf13/viper" "github.com/vposloncec/go-ssip/base" + "github.com/vposloncec/go-ssip/export" "go.uber.org/zap" "go.uber.org/zap/zapcore" + "os" "time" ) @@ -18,9 +21,12 @@ func StartFromInput(nodeNum int, connections []base.ConnectionPair) { graph.Nodes[0].SendPacket(p) time.Sleep(3 * time.Second) graph.CalcPacketReach(p.ID) + export.EdgesToCSV(graph.Connections).WriteTo(os.Stdout) + fmt.Println("==============================") + export.NodesToCSV(graph.Nodes).WriteTo(os.Stdout) } -func StartRandom(nodeNum int, connections int) { +func StartRandom(nodeNum int, connections int) *base.Graph { log := getLogger() maxId := nodeNum - 1 @@ -33,6 +39,8 @@ func StartRandom(nodeNum int, connections int) { graph.Nodes[0].SendPacket(p) time.Sleep(3 * time.Second) graph.CalcPacketReach(p.ID) + + return graph } func getLogger() *zap.SugaredLogger {