Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove unused ls command #76

Merged
merged 1 commit into from
Dec 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,18 +284,6 @@ func simOpenSelectClient(protos []string, rwc io.ReadWriteCloser) (string, error
return selectProtosOrFail(protos, rwc)
}

func handshake(rw io.ReadWriter) error {
errCh := make(chan error, 1)
go func() {
errCh <- delimWriteBuffered(rw, []byte(ProtocolID))
}()

if err := readMultistreamHeader(rw); err != nil {
return err
}
return <-errCh
}

func readMultistreamHeader(r io.Reader) error {
tok, err := ReadNextToken(r)
if err != nil {
Expand Down
131 changes: 23 additions & 108 deletions multistream.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package multistream

import (
"bufio"
"bytes"
"errors"
"fmt"

Expand Down Expand Up @@ -110,39 +109,6 @@ func delimWrite(w io.Writer, mes []byte) error {
return nil
}

// Ls is a Multistream muxer command which returns the list of handler names
// available on a muxer.
func Ls(rw io.ReadWriter) ([]string, error) {
err := handshake(rw)
if err != nil {
return nil, err
}
err = delimWriteBuffered(rw, []byte("ls"))
if err != nil {
return nil, err
}

response, err := lpReadBuf(rw)
if err != nil {
return nil, err
}

r := bytes.NewReader(response)

var out []string
for {
val, err := lpReadBuf(r)
switch err {
default:
return nil, err
case io.EOF:
return out, nil
case nil:
out = append(out, string(val))
}
}
}

func fulltextMatch(s string) func(string) bool {
return func(a string) bool {
return a == s
Expand Down Expand Up @@ -270,42 +236,27 @@ loop:
return nil, "", nil, err
}

switch tok {
case "ls":
protos, err := msm.encodeLocalProtocols()
if err != nil {
rwc.Close()
return nil, "", nil, err
}
h := msm.findHandler(tok)
if h == nil {
select {
case pval <- string(protos):
case pval <- "na":
case err := <-writeErr:
rwc.Close()
return nil, "", nil, err
}
default:
h := msm.findHandler(tok)
if h == nil {
select {
case pval <- "na":
case err := <-writeErr:
rwc.Close()
return nil, "", nil, err
}
continue loop
}

select {
case pval <- tok:
case <-writeErr:
// explicitly ignore this error. It will be returned to any
// writers and if we don't plan on writing anything, we still
// want to complete the handshake
}
continue loop
}

// hand off processing to the sub-protocol handler
return lzc, tok, h.Handle, nil
select {
case pval <- tok:
case <-writeErr:
// explicitly ignore this error. It will be returned to any
// writers and if we don't plan on writing anything, we still
// want to complete the handshake
}

// hand off processing to the sub-protocol handler
return lzc, tok, h.Handle, nil
}
}

Expand Down Expand Up @@ -336,58 +287,22 @@ loop:
return "", nil, err
}

switch tok {
case "ls":
err := msm.Ls(rwc)
if err != nil {
h := msm.findHandler(tok)
if h == nil {
if err := delimWriteBuffered(rwc, []byte("na")); err != nil {
return "", nil, err
}
default:
h := msm.findHandler(tok)
if h == nil {
err := delimWriteBuffered(rwc, []byte("na"))
if err != nil {
return "", nil, err
}
continue loop
}

err := delimWriteBuffered(rwc, []byte(tok))
if err != nil {
return "", nil, err
}

// hand off processing to the sub-protocol handler
return tok, h.Handle, nil
continue loop
}
}

}
if err := delimWriteBuffered(rwc, []byte(tok)); err != nil {
return "", nil, err
}

// Ls implements the "ls" command which writes the list of
// supported protocols to the given Writer.
func (msm *MultistreamMuxer) Ls(w io.Writer) error {
protos, err := msm.encodeLocalProtocols()
if err != nil {
return err
// hand off processing to the sub-protocol handler
return tok, h.Handle, nil
}
return delimWrite(w, protos)
}

// encodeLocalProtocols encodes the protocols this multistream-select router
// handles, packed in a list of varint-delimited strings.
func (msm *MultistreamMuxer) encodeLocalProtocols() ([]byte, error) {
buf := new(bytes.Buffer)
msm.handlerlock.RLock()
for _, h := range msm.handlers {
err := delimWrite(buf, []byte(h.AddName))
if err != nil {
msm.handlerlock.RUnlock()
return nil, err
}
}
msm.handlerlock.RUnlock()
return buf.Bytes(), nil
}

// Handle performs protocol negotiation on a ReadWriteCloser
Expand Down
63 changes: 0 additions & 63 deletions multistream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,69 +635,6 @@ func TestLargeMessageNegotiate(t *testing.T) {
}
}

func TestLs(t *testing.T) {
t.Run("none-eager", subtestLs(nil, false))
t.Run("one-eager", subtestLs([]string{"a"}, false))
t.Run("many-eager", subtestLs([]string{"a", "b", "c", "d", "e"}, false))
t.Run("empty-eager", subtestLs([]string{"", "a"}, false))

// lazy variants
t.Run("none-lazy", subtestLs(nil, true))
t.Run("one-lazy", subtestLs([]string{"a"}, true))
t.Run("many-lazy", subtestLs([]string{"a", "b", "c", "d", "e"}, true))
t.Run("empty-lazy", subtestLs([]string{"", "a"}, true))
}

func subtestLs(protos []string, lazy bool) func(*testing.T) {
return func(t *testing.T) {
mr := NewMultistreamMuxer()
mset := make(map[string]bool)
for _, p := range protos {
mr.AddHandler(p, nil)
mset[p] = true
}

c1, c2 := net.Pipe()
done := make(chan struct{})
go func() {
defer close(done)

var proto string
var err error
if lazy {
_, proto, _, err = mr.NegotiateLazy(c2)
} else {
proto, _, err = mr.Negotiate(c2)
}

c2.Close()
if err != io.EOF {
t.Error(err)
}
if proto != "" {
t.Errorf("expected no proto, got %s", proto)
}
}()
defer func() { <-done }()

items, err := Ls(c1)
if err != nil {
t.Fatal(err)
}
c1.Close()

if len(items) != len(protos) {
t.Fatal("got wrong number of protocols")
}

for _, tok := range items {
if !mset[tok] {
t.Fatalf("wasnt expecting protocol %s", tok)
}
}
}
}

type readonlyBuffer struct {
buf io.Reader
}
Expand Down