pair requests with responses

this is soooooo janky
master
Jordan Orelli 11 years ago
parent a06813846a
commit e90e1ec3ea

@ -1,14 +1,14 @@
package main package main
import ( import (
"bufio" // "bufio"
"flag" "flag"
"fmt" "fmt"
"io" // "io"
"math/rand" "math/rand"
"net" "net"
"os" "os"
"strings" // "strings"
) )
var options struct { var options struct {
@ -50,39 +50,56 @@ func main() {
fmt.Fprintf(conn, "*2\r\n$4\r\nauth\r\n$%d\r\n%s\r\n", len(options.password), options.password) fmt.Fprintf(conn, "*2\r\n$4\r\nauth\r\n$%d\r\n%s\r\n", len(options.password), options.password)
} }
f, err := os.Open(fname) infile, err := os.Open(fname)
if err != nil { if err != nil {
fmt.Printf("unable to open file %s: %v\n", fname, err) fmt.Printf("unable to open file %s: %v\n", fname, err)
os.Exit(1) os.Exit(1)
} }
defer f.Close() defer infile.Close()
c := make(chan statement) c, e := make(chan value), make(chan error)
go split(f, c) sent := make(chan value, 1)
go streamValues(infile, c, e)
s := randomString(32)
go func() { go func() {
for s := range c { for {
if err := s.write(conn); err != nil { select {
case v, ok := <-c:
if !ok {
return
}
v.Write(conn)
sent <- v
case err, ok := <-e:
if !ok {
return
}
fmt.Println(err) fmt.Println(err)
break
} }
} }
fmt.Fprintf(conn, "*2\r\n$4\r\necho\r\n$32\r\n%s\r\n", s)
}() }()
r := bufio.NewReader(conn) type pair struct {
request value
response value
}
cc, ee := make(chan value), make(chan error)
go streamValues(conn, cc, ee)
ReadResponses:
for { for {
line, err := r.ReadString('\n') select {
switch err { case response, ok := <-cc:
case nil: if !ok {
if strings.TrimSpace(line) == s { break ReadResponses
return
} }
case io.EOF: request := <-sent
return fmt.Println(pair{request, response})
default: case err, ok := <-ee:
fmt.Println(err) if !ok {
break ReadResponses
}
request := <-sent
fmt.Printf("fuck %v %v\n", request, err)
} }
} }
} }

@ -16,6 +16,7 @@ var (
) )
type value interface { type value interface {
Write(io.Writer) (int, error)
} }
func streamValues(r io.Reader, c chan value, e chan error) { func streamValues(r io.Reader, c chan value, e chan error) {
@ -36,6 +37,12 @@ func streamValues(r io.Reader, c chan value, e chan error) {
} }
} }
func writeValues(w io.Writer, c chan value) {
for v := range c {
v.Write(w)
}
}
func readValue(r io.Reader) (value, error) { func readValue(r io.Reader) (value, error) {
var br *bufio.Reader var br *bufio.Reader
switch t := r.(type) { switch t := r.(type) {
@ -89,6 +96,10 @@ func readString(b []byte) (value, error) {
return String(b), nil return String(b), nil
} }
func (s String) Write(w io.Writer) (int, error) {
return fmt.Fprintf(w, "+%s\r\n", s)
}
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
type Error string type Error string
@ -97,6 +108,10 @@ func readError(b []byte) (value, error) {
return Error(b), nil return Error(b), nil
} }
func (e Error) Write(w io.Writer) (int, error) {
return fmt.Fprintf(w, "-%s\r\n")
}
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
type Integer int64 type Integer int64
@ -109,6 +124,10 @@ func readInteger(b []byte) (value, error) {
return Integer(i), nil return Integer(i), nil
} }
func (i Integer) Write(w io.Writer) (int, error) {
return fmt.Fprintf(w, ":%d\r\n", i)
}
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
type BulkString string type BulkString string
@ -147,6 +166,10 @@ func readBulkString(prefix []byte, r io.Reader) (value, error) {
return BulkString(b[:len(b)-2]), nil return BulkString(b[:len(b)-2]), nil
} }
func (s BulkString) Write(w io.Writer) (int, error) {
return fmt.Fprintf(w, "$%d\r\n%s\r\n", len(s), s)
}
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
type Array []value type Array []value
@ -174,3 +197,23 @@ func readArray(prefix []byte, r *bufio.Reader) (value, error) {
} }
return a, nil return a, nil
} }
func (a Array) Write(w io.Writer) (int, error) {
n, err := fmt.Fprintf(w, "*%d\r\n", len(a))
if err != nil {
return n, err
}
var (
nn int
e error
)
for i := 0; i < len(a); i++ {
nn, e = a[i].Write(w)
n += nn
if e != nil {
return n, e
}
}
return n, nil
}

Loading…
Cancel
Save