diff options
| author | Paul Buetow <paul@buetow.org> | 2023-10-25 00:03:09 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-10-25 00:03:09 +0300 |
| commit | 4df9dfc1e91ca64f553b5d5a76619155531aabd0 (patch) | |
| tree | 593ae3db472ef71a7b8b8d283bffec7c8651b1cc | |
| parent | 8650dd57474e991515c93169151e6187205c9ff3 (diff) | |
test config works again after refactor
| -rw-r--r-- | internal/client/client.go | 10 | ||||
| -rw-r--r-- | internal/client/tcpclient.go | 8 | ||||
| -rw-r--r-- | internal/config/config.go | 10 | ||||
| -rw-r--r-- | test/gorum-earth.json | 27 | ||||
| -rw-r--r-- | test/gorum-mars.json | 27 | ||||
| -rw-r--r-- | test/gorum-uranus.json | 27 |
6 files changed, 81 insertions, 28 deletions
diff --git a/internal/client/client.go b/internal/client/client.go index f05fcba..0906386 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -15,7 +15,7 @@ func Start(ctx context.Context, conf config.Config, myVoteCh <-chan vote.Vote) { nodeNum := 0 for _, node := range conf.Nodes { - fanOut[nodeNum] = startConnection(ctx, node.Hostname) + fanOut[nodeNum] = startConnection(ctx, node.Address()) nodeNum++ } @@ -46,14 +46,14 @@ func Start(ctx context.Context, conf config.Config, myVoteCh <-chan vote.Vote) { }() } -func startConnection(ctx context.Context, node string) chan vote.Vote { +func startConnection(ctx context.Context, address string) chan vote.Vote { ch := make(chan vote.Vote, 1) go func() { for { - log.Println("client: starting connection", node) - if err := tcpClientRun(ctx, node, ch); err != nil { - log.Println("client: not connected to node", node, "anymore:", err) + log.Println("client: starting connection", address) + if err := tcpClientRun(ctx, address, ch); err != nil { + log.Println("client: not connected to address", address, "anymore:", err) } select { diff --git a/internal/client/tcpclient.go b/internal/client/tcpclient.go index db4334f..a94bf89 100644 --- a/internal/client/tcpclient.go +++ b/internal/client/tcpclient.go @@ -10,8 +10,8 @@ import ( "codeberg.org/snonux/gorum/internal/vote" ) -func tcpClientRun(ctx context.Context, node string, ch <-chan vote.Vote) error { - conn, err := net.Dial("tcp", node) +func tcpClientRun(ctx context.Context, address string, ch <-chan vote.Vote) error { + conn, err := net.Dial("tcp", address) if err != nil { return err } @@ -28,7 +28,7 @@ func tcpClientRun(ctx context.Context, node string, ch <-chan vote.Vote) error { return err } - log.Println("tcpclient: sending", message, "to node", node) + log.Println("tcpclient: sending", message, "to address", address) if err := iorw.WriteStr(conn, message); err != nil { return err } @@ -38,6 +38,6 @@ func tcpClientRun(ctx context.Context, node string, ch <-chan vote.Vote) error { return err } - log.Println("tcpclient: received", response, "from node", node) + log.Println("tcpclient: received", response, "from address", address) } } diff --git a/internal/config/config.go b/internal/config/config.go index b5030ee..f04aa57 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -19,6 +19,10 @@ type Node struct { originalId string } +func (n Node) Address() string { + return fmt.Sprintf("%s:%d", n.Hostname, n.Port) +} + type Config struct { StateDir string ScoreFile string @@ -57,6 +61,10 @@ func NewFromConfigFile(configFile string) (Config, error) { for id, node := range conf.Nodes { log.Printf("adding node %s: %v", id, node) node.originalId = id + if node.Alias != "" { + newNodes[node.Alias] = node + continue + } newNodes[node.Hostname] = node } conf.Nodes = newNodes @@ -181,5 +189,5 @@ func (conf Config) findNode(hostname string) (Node, error) { } } - return Node{}, fmt.Errorf("node %s not found", hostname) + return Node{}, fmt.Errorf("node %s not found in %v", hostname, conf.Nodes) } diff --git a/test/gorum-earth.json b/test/gorum-earth.json index 08a749d..d0c1660 100644 --- a/test/gorum-earth.json +++ b/test/gorum-earth.json @@ -4,10 +4,25 @@ "WinnerFile": "is_winner.earth", "MyID": "earth", "RelaxedMode": true, - "Address": ":1234", - "Nodes": [ - "localhost:1234:earth", - "localhost:2341:mars", - "localhost:3412:uranus" - ] + "Address": "localhost:1234", + "Nodes": { + "first": { + "Hostname": "localhost", + "Alias": "earth", + "Port": 1234, + "Priority": 3 + }, + "second": { + "Hostname": "localhost", + "Alias": "mars", + "Port": 1235, + "Priority": 2 + }, + "third": { + "Hostname": "localhost", + "Alias": "uranus", + "Port": 1236, + "Priority": 1 + } + } } diff --git a/test/gorum-mars.json b/test/gorum-mars.json index adecedf..2430eb3 100644 --- a/test/gorum-mars.json +++ b/test/gorum-mars.json @@ -4,10 +4,25 @@ "WinnerFile": "is_winner.mars", "MyID": "mars", "RelaxedMode": true, - "Address": ":2341", - "Nodes": [ - "localhost:1234", - "localhost:2341", - "localhost:3412" - ] + "Address": ":1235", + "Nodes": { + "first": { + "Hostname": "localhost", + "Alias": "earth", + "Port": 1234, + "Priority": 3 + }, + "second": { + "Hostname": "localhost", + "Alias": "mars", + "Port": 1235, + "Priority": 2 + }, + "third": { + "Hostname": "localhost", + "Alias": "uranus", + "Port": 1236, + "Priority": 1 + } + } } diff --git a/test/gorum-uranus.json b/test/gorum-uranus.json index bc00a7f..a95f5a2 100644 --- a/test/gorum-uranus.json +++ b/test/gorum-uranus.json @@ -4,10 +4,25 @@ "WinnerFile": "is_winner.uranus", "MyID": "uranus", "RelaxedMode": true, - "Address": ":3412", - "Nodes": [ - "localhost:1234", - "localhost:2341", - "localhost:3412" - ] + "Address": ":1236", + "Nodes": { + "first": { + "Hostname": "localhost", + "Alias": "earth", + "Port": 1234, + "Priority": 3 + }, + "second": { + "Hostname": "localhost", + "Alias": "mars", + "Port": 1235, + "Priority": 2 + }, + "third": { + "Hostname": "localhost", + "Alias": "uranus", + "Port": 1236, + "Priority": 1 + } + } } |
