diff --git a/main.go b/main.go index d42492b..856eeb2 100644 --- a/main.go +++ b/main.go @@ -207,9 +207,24 @@ func cliLeave(ctx context.Context, cmd *cli.Command) error { return cl.SayBye(ctx, chord.Address(n)) } -func cliLookup(ctx context.Context, cmd *cli.Command) error { return errors.New("not implemented") } -func cliPut(ctx context.Context, cmd *cli.Command) error { return errors.New("not implemented") } -func cliGet(ctx context.Context, cmd *cli.Command) error { return errors.New("not implemented") } +func cliLookup(ctx context.Context, cmd *cli.Command) error { + n, err := netip.ParseAddrPort(cmd.String("n")) + if err != nil { + return err + } + k := chord.Key(cmd.String("k")) + cl := &httpnode.Client{HTTP: http.Client{Timeout: 5 * time.Second}} + p, err := cl.FindSuccessor(ctx, chord.Address(n), k) + if err != nil { + return err + } + _, addr := p.Values() + fmt.Println(addr) + return nil +} + +func cliPut(ctx context.Context, cmd *cli.Command) error { return errors.New("not implemented") } +func cliGet(ctx context.Context, cmd *cli.Command) error { return errors.New("not implemented") } func main() { ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) diff --git a/test.bash b/test.bash index 30021b8..cfbc866 100755 --- a/test.bash +++ b/test.bash @@ -17,6 +17,15 @@ THIRD=$! sleep 5 # Each node logs its predecessor and successors. At this point, we see the ring. +# Test lookup. +# First check that the lookup is independent of the node we ask. +./chord-node lookup -k key1 -n 127.0.0.1:3000 +./chord-node lookup -k key1 -n 127.0.0.1:3001 +./chord-node lookup -k key1 -n 127.0.0.1:3002 +# Now check that we get some different nodes for different keys. +./chord-node lookup -k key2 -n 127.0.0.1:3000 +./chord-node lookup -k key3 -n 127.0.0.1:3000 + # Test leaving. ./chord-node leave -n 127.0.0.1:3000