blob: 8cc7b33290e9e808ecd6077acd01010c326309d7 [file] [log] [blame]
Cosmos Nicolaou9ca249d2014-09-18 15:07:12 -07001package core
2
3import (
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 Nicolaou9c9918d2014-09-23 08:45:56 -070011 "veyron.io/veyron/veyron2/security"
Cosmos Nicolaou9ca249d2014-09-18 15:07:12 -070012
13 "veyron.io/veyron/veyron/lib/modules"
14)
15
16func init() {
Cosmos Nicolaou1e78ccc2014-10-09 08:10:26 -070017 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 Nicolaou9ca249d2014-09-18 15:07:12 -070021}
22
Cosmos Nicolaou9c9918d2014-09-23 08:45:56 -070023type treeDispatcher struct{ id string }
24
Cosmos Nicolaou1ee5e1a2014-11-02 10:20:30 -080025func (d treeDispatcher) Lookup(suffix, method string) (interface{}, security.Authorizer, error) {
Cosmos Nicolaou9c9918d2014-09-23 08:45:56 -070026 return ipc.ReflectInvoker(&echoServerObject{d.id, suffix}), nil, nil
27}
28
Cosmos Nicolaou9ca249d2014-09-18 15:07:12 -070029type echoServerObject struct {
Cosmos Nicolaou9c9918d2014-09-23 08:45:56 -070030 id, suffix string
Cosmos Nicolaou9ca249d2014-09-18 15:07:12 -070031}
32
33func (es *echoServerObject) Echo(call ipc.ServerCall, m string) (string, error) {
Cosmos Nicolaou9c9918d2014-09-23 08:45:56 -070034 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 Nicolaou9ca249d2014-09-18 15:07:12 -070038}
39
40func echoServer(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
Cosmos Nicolaoud811b072014-10-28 17:46:27 -070041 fl, args, err := parseListenFlags(args)
Cosmos Nicolaou4e213d72014-10-26 22:21:52 -070042 if err != nil {
Adam Sadovskyd21d95a2014-10-29 09:56:59 -070043 return fmt.Errorf("failed to parse args: %s", err)
Cosmos Nicolaou9ca249d2014-09-18 15:07:12 -070044 }
Cosmos Nicolaou4e213d72014-10-26 22:21:52 -070045 if err := checkArgs(args, 2, "<message> <name>"); err != nil {
46 return err
47 }
48 id, mp := args[0], args[1]
Cosmos Nicolaou9c9918d2014-09-23 08:45:56 -070049 disp := &treeDispatcher{id: id}
Cosmos Nicolaou9ca249d2014-09-18 15:07:12 -070050 server, err := rt.R().NewServer()
51 if err != nil {
52 return err
53 }
54 defer server.Stop()
Cosmos Nicolaou4e213d72014-10-26 22:21:52 -070055 ep, err := server.Listen(initListenSpec(fl))
Cosmos Nicolaou9ca249d2014-09-18 15:07:12 -070056 if err != nil {
57 return err
58 }
Cosmos Nicolaou9c9918d2014-09-23 08:45:56 -070059 if err := server.Serve(mp, disp); err != nil {
Cosmos Nicolaou9ca249d2014-09-18 15:07:12 -070060 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
69func echoClient(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
Cosmos Nicolaoud811b072014-10-28 17:46:27 -070070 args = args[1:]
Cosmos Nicolaou4e213d72014-10-26 22:21:52 -070071 name := args[0]
72 args = args[1:]
Cosmos Nicolaou9ca249d2014-09-18 15:07:12 -070073 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}