提交 faec2a32 作者: Jeromy

cleanup dht cmd output and fix unrecognized events

License: MIT
Signed-off-by: 's avatarJeromy <jeromyj@gmail.com>
上级 a99ad8a4
...@@ -103,29 +103,7 @@ var queryDhtCmd = &cmds.Command{ ...@@ -103,29 +103,7 @@ var queryDhtCmd = &cmds.Command{
verbose, _, _ := res.Request().Option("v").Bool() verbose, _, _ := res.Request().Option("v").Bool()
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
if verbose { printEvent(obj, buf, verbose, nil)
fmt.Fprintf(buf, "%s: ", time.Now().Format("15:04:05.000"))
}
switch obj.Type {
case notif.FinalPeer:
fmt.Fprintf(buf, "%s\n", obj.ID)
case notif.PeerResponse:
if verbose {
fmt.Fprintf(buf, "* %s says use ", obj.ID)
for _, p := range obj.Responses {
fmt.Fprintf(buf, "%s ", p.ID)
}
fmt.Fprintln(buf)
}
case notif.SendingQuery:
if verbose {
fmt.Fprintf(buf, "* querying %s\n", obj.ID)
}
case notif.QueryError:
fmt.Fprintf(buf, "error: %s\n", obj.Extra)
default:
fmt.Fprintf(buf, "unrecognized event type: %d\n", obj.Type)
}
return buf, nil return buf, nil
} }
...@@ -201,50 +179,34 @@ FindProviders will return a list of peers who are able to provide the value requ ...@@ -201,50 +179,34 @@ FindProviders will return a list of peers who are able to provide the value requ
} }
verbose, _, _ := res.Request().Option("v").Bool() verbose, _, _ := res.Request().Option("v").Bool()
pfm := pfuncMap{
marshal := func(v interface{}) (io.Reader, error) { notif.FinalPeer: func(obj *notif.QueryEvent, out io.Writer, verbose bool) {
obj, ok := v.(*notif.QueryEvent)
if !ok {
return nil, u.ErrCast()
}
buf := new(bytes.Buffer)
if verbose {
fmt.Fprintf(buf, "%s: ", time.Now().Format("15:04:05.000"))
}
switch obj.Type {
case notif.FinalPeer:
if verbose { if verbose {
fmt.Fprintf(buf, "* closest peer %s\n", obj.ID) fmt.Fprintf(out, "* closest peer %s\n", obj.ID)
} }
case notif.Provider: },
notif.Provider: func(obj *notif.QueryEvent, out io.Writer, verbose bool) {
prov := obj.Responses[0] prov := obj.Responses[0]
if verbose { if verbose {
fmt.Fprintf(buf, "provider: ") fmt.Fprintf(out, "provider: ")
} }
fmt.Fprintf(buf, "%s\n", prov.ID.Pretty()) fmt.Fprintf(out, "%s\n", prov.ID.Pretty())
if verbose { if verbose {
for _, a := range prov.Addrs { for _, a := range prov.Addrs {
fmt.Fprintf(buf, "\t%s\n", a) fmt.Fprintf(out, "\t%s\n", a)
} }
} }
case notif.PeerResponse: },
if verbose { }
fmt.Fprintf(buf, "* %s says use ", obj.ID)
for _, p := range obj.Responses { marshal := func(v interface{}) (io.Reader, error) {
fmt.Fprintf(buf, "%s ", p.ID) obj, ok := v.(*notif.QueryEvent)
} if !ok {
fmt.Fprintln(buf) return nil, u.ErrCast()
}
case notif.SendingQuery:
if verbose {
fmt.Fprintf(buf, "* querying %s\n", obj.ID)
}
case notif.QueryError:
fmt.Fprintf(buf, "error: %s\n", obj.Extra)
default:
fmt.Fprintf(buf, "unrecognized event type: %d\n", obj.Type)
} }
buf := new(bytes.Buffer)
printEvent(obj, buf, verbose, pfm)
return buf, nil return buf, nil
} }
...@@ -323,6 +285,15 @@ var findPeerDhtCmd = &cmds.Command{ ...@@ -323,6 +285,15 @@ var findPeerDhtCmd = &cmds.Command{
return nil, u.ErrCast() return nil, u.ErrCast()
} }
pfm := pfuncMap{
notif.FinalPeer: func(obj *notif.QueryEvent, out io.Writer, verbose bool) {
pi := obj.Responses[0]
fmt.Fprintf(out, "%s\n", pi.ID)
for _, a := range pi.Addrs {
fmt.Fprintf(out, "\t%s\n", a)
}
},
}
marshal := func(v interface{}) (io.Reader, error) { marshal := func(v interface{}) (io.Reader, error) {
obj, ok := v.(*notif.QueryEvent) obj, ok := v.(*notif.QueryEvent)
if !ok { if !ok {
...@@ -330,27 +301,7 @@ var findPeerDhtCmd = &cmds.Command{ ...@@ -330,27 +301,7 @@ var findPeerDhtCmd = &cmds.Command{
} }
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
fmt.Fprintf(buf, "%s: ", time.Now().Format("15:04:05.000")) printEvent(obj, buf, true, pfm)
switch obj.Type {
case notif.FinalPeer:
pi := obj.Responses[0]
fmt.Fprintf(buf, "%s\n", pi.ID)
for _, a := range pi.Addrs {
fmt.Fprintf(buf, "\t%s\n", a)
}
case notif.PeerResponse:
fmt.Fprintf(buf, "* %s says use ", obj.ID)
for _, p := range obj.Responses {
fmt.Fprintf(buf, "%s ", p.ID)
}
fmt.Fprintln(buf)
case notif.SendingQuery:
fmt.Fprintf(buf, "* querying %s\n", obj.ID)
case notif.QueryError:
fmt.Fprintf(buf, "error: %s\n", obj.Extra)
default:
fmt.Fprintf(buf, "unrecognized event type: %d\n", obj.Type)
}
return buf, nil return buf, nil
} }
...@@ -435,6 +386,15 @@ GetValue will return the value stored in the dht at the given key. ...@@ -435,6 +386,15 @@ GetValue will return the value stored in the dht at the given key.
verbose, _, _ := res.Request().Option("v").Bool() verbose, _, _ := res.Request().Option("v").Bool()
pfm := pfuncMap{
notif.Value: func(obj *notif.QueryEvent, out io.Writer, verbose bool) {
if verbose {
fmt.Fprintf(out, "got value: '%s'\n", obj.Extra)
} else {
fmt.Fprintln(out, obj.Extra)
}
},
}
marshal := func(v interface{}) (io.Reader, error) { marshal := func(v interface{}) (io.Reader, error) {
obj, ok := v.(*notif.QueryEvent) obj, ok := v.(*notif.QueryEvent)
if !ok { if !ok {
...@@ -442,33 +402,9 @@ GetValue will return the value stored in the dht at the given key. ...@@ -442,33 +402,9 @@ GetValue will return the value stored in the dht at the given key.
} }
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
if verbose {
fmt.Fprintf(buf, "%s: ", time.Now().Format("15:04:05.000")) printEvent(obj, buf, verbose, pfm)
}
switch obj.Type {
case notif.PeerResponse:
if verbose {
fmt.Fprintf(buf, "* %s says use ", obj.ID)
for _, p := range obj.Responses {
fmt.Fprintf(buf, "%s ", p.ID)
}
fmt.Fprintln(buf)
}
case notif.SendingQuery:
if verbose {
fmt.Fprintf(buf, "* querying %s\n", obj.ID)
}
case notif.Value:
if verbose {
fmt.Fprintf(buf, "got value: '%s'\n", obj.Extra)
} else {
buf.WriteString(obj.Extra)
}
case notif.QueryError:
fmt.Fprintf(buf, "error: %s\n", obj.Extra)
default:
fmt.Fprintf(buf, "unrecognized event type: %d\n", obj.Type)
}
return buf, nil return buf, nil
} }
...@@ -550,6 +486,16 @@ PutValue will store the given key value pair in the dht. ...@@ -550,6 +486,16 @@ PutValue will store the given key value pair in the dht.
} }
verbose, _, _ := res.Request().Option("v").Bool() verbose, _, _ := res.Request().Option("v").Bool()
pfm := pfuncMap{
notif.FinalPeer: func(obj *notif.QueryEvent, out io.Writer, verbose bool) {
if verbose {
fmt.Fprintf(out, "* closest peer %s\n", obj.ID)
}
},
notif.Value: func(obj *notif.QueryEvent, out io.Writer, verbose bool) {
fmt.Fprintf(out, "storing value at %s\n", obj.ID)
},
}
marshal := func(v interface{}) (io.Reader, error) { marshal := func(v interface{}) (io.Reader, error) {
obj, ok := v.(*notif.QueryEvent) obj, ok := v.(*notif.QueryEvent)
...@@ -558,33 +504,8 @@ PutValue will store the given key value pair in the dht. ...@@ -558,33 +504,8 @@ PutValue will store the given key value pair in the dht.
} }
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
if verbose { printEvent(obj, buf, verbose, pfm)
fmt.Fprintf(buf, "%s: ", time.Now().Format("15:04:05.000"))
}
switch obj.Type {
case notif.FinalPeer:
if verbose {
fmt.Fprintf(buf, "* closest peer %s\n", obj.ID)
}
case notif.PeerResponse:
if verbose {
fmt.Fprintf(buf, "* %s says use ", obj.ID)
for _, p := range obj.Responses {
fmt.Fprintf(buf, "%s ", p.ID)
}
fmt.Fprintln(buf)
}
case notif.SendingQuery:
if verbose {
fmt.Fprintf(buf, "* querying %s\n", obj.ID)
}
case notif.QueryError:
fmt.Fprintf(buf, "error: %s\n", obj.Extra)
case notif.Value:
fmt.Fprintf(buf, "storing value at %s\n", obj.ID)
default:
fmt.Fprintf(buf, "unrecognized event type: %d\n", obj.Type)
}
return buf, nil return buf, nil
} }
...@@ -598,6 +519,53 @@ PutValue will store the given key value pair in the dht. ...@@ -598,6 +519,53 @@ PutValue will store the given key value pair in the dht.
Type: notif.QueryEvent{}, Type: notif.QueryEvent{},
} }
type printFunc func(obj *notif.QueryEvent, out io.Writer, verbose bool)
type pfuncMap map[notif.QueryEventType]printFunc
func printEvent(obj *notif.QueryEvent, out io.Writer, verbose bool, override pfuncMap) {
if verbose {
fmt.Fprintf(out, "%s: ", time.Now().Format("15:04:05.000"))
}
if override != nil {
if pf, ok := override[obj.Type]; ok {
pf(obj, out, verbose)
return
}
}
switch obj.Type {
case notif.SendingQuery:
if verbose {
fmt.Fprintf(out, "* querying %s\n", obj.ID)
}
case notif.Value:
if verbose {
fmt.Fprintf(out, "got value: '%s'\n", obj.Extra)
} else {
fmt.Fprint(out, obj.Extra)
}
case notif.PeerResponse:
fmt.Fprintf(out, "* %s says use ", obj.ID)
for _, p := range obj.Responses {
fmt.Fprintf(out, "%s ", p.ID)
}
fmt.Fprintln(out)
case notif.QueryError:
fmt.Fprintf(out, "error: %s\n", obj.Extra)
case notif.DialingPeer:
if verbose {
fmt.Fprintf(out, "dialing peer: %s\n", obj.ID)
}
case notif.AddingPeer:
if verbose {
fmt.Fprintf(out, "adding peer to query: %s\n", obj.ID)
}
default:
fmt.Fprintf(out, "unrecognized event type: %d\n", obj.Type)
}
}
func escapeDhtKey(s string) (key.Key, error) { func escapeDhtKey(s string) (key.Key, error) {
parts := path.SplitList(s) parts := path.SplitList(s)
switch len(parts) { switch len(parts) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论