Skip to content

Commit

Permalink
Cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
Teiva Harsanyi committed Feb 27, 2023
1 parent 73dc0ac commit bd7deca
Showing 1 changed file with 1 addition and 58 deletions.
59 changes: 1 addition & 58 deletions challenge-3d-broadcast/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"math/rand"
"os"
"strconv"
"sync"
Expand All @@ -29,7 +28,6 @@ func main() {

n.Handle("init", s.initHandler)
n.Handle("broadcast", s.broadcastHandler)
n.Handle("forward", s.forwardHandler)
n.Handle("read", s.readHandler)
n.Handle("topology", s.topologyHandler)

Expand Down Expand Up @@ -60,27 +58,6 @@ func (s *server) initHandler(_ maelstrom.Message) error {
return nil
}

func (s *server) forwardHandler(msg maelstrom.Message) error {
var body map[string]any
if err := json.Unmarshal(msg.Body, &body); err != nil {
return err
}

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_, err := s.n.SyncRPC(ctx, body["to"].(string), map[string]any{
"type": "broadcast",
"message": int(body["message"].(float64)),
})
if err != nil {
return err
}

return s.n.Reply(msg, map[string]any{
"type": "forward_ok",
})
}

func (s *server) broadcastHandler(msg maelstrom.Message) error {
var body map[string]any
if err := json.Unmarshal(msg.Body, &body); err != nil {
Expand Down Expand Up @@ -146,12 +123,6 @@ func (s *server) broadcast(src string, body map[string]any) error {
return nil
}

func random(max int) int {
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)
return r1.Int() % max
}

func (s *server) rpc(dst string, body map[string]any) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
Expand Down Expand Up @@ -181,8 +152,7 @@ func (s *server) getAllIDs() []int {

func (s *server) topologyHandler(msg maelstrom.Message) error {
tree := btree.NewWithIntComparator(len(s.n.NodeIDs()))
// TODO Use number of nodes
for i := 0; i < 25; i++ {
for i := 0; i < len(s.n.NodeIDs()); i++ {
tree.Put(i, fmt.Sprintf("n%d", i))
}

Expand All @@ -195,33 +165,6 @@ func (s *server) topologyHandler(msg maelstrom.Message) error {
})
}

func (s *server) isComingFromUpwards(src string) (bool, error) {
srcID, err := id(src)
if err != nil {
return false, err
}

cur, err := id(s.nodeID)
if err != nil {
return false, err
}

return srcID < cur, nil
}

func (s *server) isComingFromSameLevel(src string) (bool, error) {
srcID, err := id(src)
if err != nil {
return false, err
}
cur, err := id(src)
if err != nil {
return false, err
}

return srcID%3 != cur%3, nil
}

func id(s string) (int, error) {
i, err := strconv.Atoi(s[1:])
if err != nil {
Expand Down

0 comments on commit bd7deca

Please sign in to comment.