Cosmos Nicolaou | 9ca249d | 2014-09-18 15:07:12 -0700 | [diff] [blame] | 1 | package core |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "os" |
| 7 | |
| 8 | "veyron.io/veyron/veyron2/ipc" |
| 9 | "veyron.io/veyron/veyron2/naming" |
| 10 | "veyron.io/veyron/veyron2/rt" |
Cosmos Nicolaou | 9c9918d | 2014-09-23 08:45:56 -0700 | [diff] [blame] | 11 | "veyron.io/veyron/veyron2/security" |
Cosmos Nicolaou | 9ca249d | 2014-09-18 15:07:12 -0700 | [diff] [blame] | 12 | |
| 13 | "veyron.io/veyron/veyron/lib/modules" |
| 14 | ) |
| 15 | |
| 16 | func init() { |
Cosmos Nicolaou | 1e78ccc | 2014-10-09 08:10:26 -0700 | [diff] [blame] | 17 | modules.RegisterChild(EchoServerCommand, `<name> <message>... |
| 18 | invokes name.Echo(message)`, echoServer) |
| 19 | modules.RegisterChild(EchoClientCommand, `<name> <text> |
| 20 | runs an Echo server mounted at <name> and configured to return <text>: as a prefix in its response`, echoClient) |
Cosmos Nicolaou | 9ca249d | 2014-09-18 15:07:12 -0700 | [diff] [blame] | 21 | } |
| 22 | |
Cosmos Nicolaou | 9c9918d | 2014-09-23 08:45:56 -0700 | [diff] [blame] | 23 | type treeDispatcher struct{ id string } |
| 24 | |
Cosmos Nicolaou | 1ee5e1a | 2014-11-02 10:20:30 -0800 | [diff] [blame] | 25 | func (d treeDispatcher) Lookup(suffix, method string) (interface{}, security.Authorizer, error) { |
Cosmos Nicolaou | 9c9918d | 2014-09-23 08:45:56 -0700 | [diff] [blame] | 26 | return ipc.ReflectInvoker(&echoServerObject{d.id, suffix}), nil, nil |
| 27 | } |
| 28 | |
Cosmos Nicolaou | 9ca249d | 2014-09-18 15:07:12 -0700 | [diff] [blame] | 29 | type echoServerObject struct { |
Cosmos Nicolaou | 9c9918d | 2014-09-23 08:45:56 -0700 | [diff] [blame] | 30 | id, suffix string |
Cosmos Nicolaou | 9ca249d | 2014-09-18 15:07:12 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | func (es *echoServerObject) Echo(call ipc.ServerCall, m string) (string, error) { |
Cosmos Nicolaou | 9c9918d | 2014-09-23 08:45:56 -0700 | [diff] [blame] | 34 | if len(es.suffix) > 0 { |
| 35 | return fmt.Sprintf("%s.%s: %s\n", es.id, es.suffix, m), nil |
| 36 | } |
| 37 | return fmt.Sprintf("%s: %s\n", es.id, m), nil |
Cosmos Nicolaou | 9ca249d | 2014-09-18 15:07:12 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | func echoServer(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 41 | fl, args, err := parseListenFlags(args) |
Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 42 | if err != nil { |
Adam Sadovsky | d21d95a | 2014-10-29 09:56:59 -0700 | [diff] [blame] | 43 | return fmt.Errorf("failed to parse args: %s", err) |
Cosmos Nicolaou | 9ca249d | 2014-09-18 15:07:12 -0700 | [diff] [blame] | 44 | } |
Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 45 | if err := checkArgs(args, 2, "<message> <name>"); err != nil { |
| 46 | return err |
| 47 | } |
| 48 | id, mp := args[0], args[1] |
Cosmos Nicolaou | 9c9918d | 2014-09-23 08:45:56 -0700 | [diff] [blame] | 49 | disp := &treeDispatcher{id: id} |
Cosmos Nicolaou | 9ca249d | 2014-09-18 15:07:12 -0700 | [diff] [blame] | 50 | server, err := rt.R().NewServer() |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | defer server.Stop() |
Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 55 | ep, err := server.Listen(initListenSpec(fl)) |
Cosmos Nicolaou | 9ca249d | 2014-09-18 15:07:12 -0700 | [diff] [blame] | 56 | if err != nil { |
| 57 | return err |
| 58 | } |
Cosmos Nicolaou | 9c9918d | 2014-09-23 08:45:56 -0700 | [diff] [blame] | 59 | if err := server.Serve(mp, disp); err != nil { |
Cosmos Nicolaou | 9ca249d | 2014-09-18 15:07:12 -0700 | [diff] [blame] | 60 | return err |
| 61 | } |
| 62 | fmt.Fprintf(stdout, "NAME=%s\n", naming.MakeTerminal(naming.JoinAddressName(ep.String(), ""))) |
| 63 | fmt.Fprintf(stdout, "ADDR=%s\n", ep.String()) |
| 64 | fmt.Fprintf(stdout, "PID=%d\n", os.Getpid()) |
| 65 | modules.WaitForEOF(stdin) |
| 66 | return nil |
| 67 | } |
| 68 | |
| 69 | func echoClient(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 70 | args = args[1:] |
Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 71 | name := args[0] |
| 72 | args = args[1:] |
Cosmos Nicolaou | 9ca249d | 2014-09-18 15:07:12 -0700 | [diff] [blame] | 73 | client := rt.R().Client() |
| 74 | for _, a := range args { |
| 75 | ctxt := rt.R().NewContext() |
| 76 | h, err := client.StartCall(ctxt, name, "Echo", []interface{}{a}) |
| 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | var r string |
| 81 | var apperr error |
| 82 | if err := h.Finish(&r, &apperr); err != nil { |
| 83 | return err |
| 84 | } else { |
| 85 | if apperr != nil { |
| 86 | return apperr |
| 87 | } |
| 88 | } |
| 89 | fmt.Fprintf(stdout, r) |
| 90 | } |
| 91 | return nil |
| 92 | } |