1 package main
2
3 import (
4 "flag"
5 "fmt"
6 au "golang.conradwood.net/apis/auth"
7 "golang.conradwood.net/apis/common"
8 "golang.conradwood.net/apis/helloworld"
9 "golang.conradwood.net/go-easyops/auth"
10 "golang.conradwood.net/go-easyops/authremote"
11 "golang.conradwood.net/go-easyops/client"
12 cm "golang.conradwood.net/go-easyops/common"
13 "golang.conradwood.net/go-easyops/utils"
14 "os"
15 "time"
16 )
17
18 var (
19 uo = flag.Bool("users_only", false, "only print user accounts")
20 )
21
22 func main() {
23 flag.Parse()
24 fal, err := client.ConnectNoBalance("registry.Registry")
25 utils.Bail("no registry", err)
26 fmt.Printf("Got fancyaddresslist for %s\n", fal.ServiceName())
27 fmt.Printf("go-easyops test client\n")
28 u, s := authremote.GetLocalUsers()
29 fmt.Printf("Local User account : %s\n", user2string(u))
30 fmt.Printf("Local Service account: %s\n", user2string(s))
31 if *uo {
32 os.Exit(0)
33 }
34 pingAs("7")
35 pingLoop()
36 pingLookup()
37 pingStream()
38 }
39 func pingAs(userid string) {
40 fmt.Printf("Pinging with user \"%s\"...\n", userid)
41 ctx, err := authremote.ContextForUserID(userid)
42 utils.Bail("failed to get context", err)
43 started := time.Now()
44 con := client.Connect("helloworld.HelloWorld")
45 c := helloworld.NewHelloWorldClient(con)
46 r, err := c.Ping(ctx, &common.Void{})
47 utils.Bail("failed to ping", err)
48 fmt.Printf("Pinged (%0.2fs), User=%s, Service=%s, Creator=%s\n", time.Since(started).Seconds(), auth.UserIDString(r.CallingUser), auth.Description(r.CallingService), auth.Description(r.CreatingService))
49 reu := r.CallingUser
50 if reu == nil || reu.ID != userid {
51 fmt.Printf("Creatd context for user \"%s\", but server reported user \"%s\"\n", userid, auth.Description(reu))
52 os.Exit(10)
53 }
54 }
55
56 func pingLoop() {
57 ctx := authremote.Context()
58 if ctx == nil {
59 fmt.Printf("ERROR: authremote.Context() created no context\n")
60 os.Exit(10)
61 }
62 fmt.Printf("Pinging with default client...\n")
63 started := time.Now()
64 res, err := helloworld.GetHelloWorldClient().PingLoop(ctx, &helloworld.PingRequest{Loops: 5})
65 utils.Bail("failed to ping", err)
66 fmt.Printf("Pinged (%0.2fs)\n", time.Since(started).Seconds())
67 t := &utils.Table{}
68 t.AddHeaders("#", "User", "Service", "Creator")
69 for i, r := range res.Responses {
70 t.AddInt(i + 1)
71 t.AddString(auth.Description(r.CallingUser))
72 t.AddString(auth.Description(r.CallingService))
73 t.AddString(auth.Description(r.CallingService))
74 t.NewRow()
75 }
76 fmt.Println(t.ToPrettyString())
77 }
78 func pingLookup() {
79 fmt.Printf("Pinging with lookup...\n")
80 ctx := authremote.Context()
81 started := time.Now()
82 con := client.Connect("helloworld.HelloWorld")
83 c := helloworld.NewHelloWorldClient(con)
84 r, err := c.Ping(ctx, &common.Void{})
85 utils.Bail("failed to ping", err)
86 fmt.Printf("Pinged (%0.2fs), User=%s, Service=%s, Creator=%s\n", time.Since(started).Seconds(), auth.Description(r.CallingUser), auth.Description(r.CallingService), auth.Description(r.CreatingService))
87 }
88
89 func pingStream() {
90 fmt.Printf("Pinging stream...\n")
91 ctx := authremote.Context()
92 psreq := &helloworld.PingStreamRequest{DelayInMillis: 500}
93 started := time.Now()
94 srv, err := helloworld.GetHelloWorldClient().PingStream(ctx, psreq)
95 utils.Bail("failed to set up pingstream", err)
96 pings := 0
97 var user, service, cservice *au.User
98 for {
99 pr, err := srv.Recv()
100 if err != nil {
101 fmt.Printf("error received: %s\n", err)
102 break
103 }
104 pings++
105 fmt.Printf("Received Sequence %d (Stream %d)\n", pr.SequenceNumber, pr.StreamID)
106 print := false
107 if pr.User != nil && pr.User != user {
108 user = pr.User
109 print = true
110 }
111 if pr.CallingService != nil && pr.CallingService != service {
112 service = pr.CallingService
113 print = true
114 }
115 if pr.CreatorService != nil && pr.CreatorService != cservice {
116 cservice = pr.CreatorService
117 print = true
118 }
119 if print {
120 fmt.Printf("User:%s, Service:%s, Creator:%s\n", auth.Description(user), auth.Description(service), auth.Description(cservice))
121 }
122 }
123 dur := time.Since(started)
124 fmt.Printf("Stream ended after %0.2fs and %d pings\n", dur.Seconds(), pings)
125 fmt.Printf("Sleeping a few seconds after stream end...\n")
126 for i := 5; i > 0; i-- {
127 fmt.Printf(" %d\r", i)
128 time.Sleep(time.Duration(1) * time.Second)
129 }
130
131 }
132 func user2string(u *au.SignedUser) string {
133 uu := cm.VerifySignedUser(u)
134 return auth.Description(uu)
135 }
136
View as plain text