blob: 78b5b491a94ca5cf16b7e6c7691d17140b65a77b [file] [log] [blame]
Suharsh Sivakumara4ac50a2015-03-13 16:13:50 -07001// This file was auto-generated by the vanadium vdl tool.
Jiri Simsa37cdfc02014-10-09 18:00:43 -07002// Source: service.vdl
3
Robin Thellend320af422015-03-09 17:18:15 -07004// Package rps is an example of vanadium service for playing the game of
Jiri Simsa37cdfc02014-10-09 18:00:43 -07005// Rock-Paper-Scissors. (http://en.wikipedia.org/wiki/Rock-paper-scissors)
6//
7// There are three different roles in the game:
8//
9// 1. Judge: A judge enforces the rules of the game and decides who
10// the winner is. At the end of the game, the judge reports the
11// final score to all the score keepers.
12//
13// 2. Player: A player can ask a judge to start a new game, it can
14// challenge another player, and it can play a game.
15//
16// 3. ScoreKeeper: A score keeper receives the final score for a game
17// after it ended.
18package rps
19
20import (
Todd Wangd1779a52015-02-05 11:46:12 -080021 // VDL system imports
22 "io"
Jiri Simsa386ce3e2015-02-23 16:14:20 -080023 "v.io/v23"
24 "v.io/v23/context"
Matt Rosencrantz94502cf2015-03-18 09:43:44 -070025 "v.io/v23/rpc"
Jiri Simsa386ce3e2015-02-23 16:14:20 -080026 "v.io/v23/vdl"
Asim Shankar4794c932014-11-24 17:45:56 -080027
Todd Wangd1779a52015-02-05 11:46:12 -080028 // VDL user imports
Todd Wang07f13462015-03-13 15:54:15 -070029 "time"
Jiri Simsa386ce3e2015-02-23 16:14:20 -080030 "v.io/v23/services/security/access"
Todd Wang07f13462015-03-13 15:54:15 -070031 _ "v.io/v23/vdlroot/time"
Jiri Simsa37cdfc02014-10-09 18:00:43 -070032)
33
Benjamin Prosnitz2be28dc2015-03-11 13:33:22 -070034// A GameId is used to uniquely identify a game within one Judge.
35type GameId struct {
36 Id string
Jiri Simsa37cdfc02014-10-09 18:00:43 -070037}
38
Benjamin Prosnitz2be28dc2015-03-11 13:33:22 -070039func (GameId) __VDLReflect(struct {
40 Name string "v.io/x/ref/examples/rps.GameId"
Todd Wang24983c72014-11-26 15:07:25 -080041}) {
42}
43
Jiri Simsa37cdfc02014-10-09 18:00:43 -070044// GameOptions specifies the parameters of a game.
45type GameOptions struct {
46 NumRounds int32 // The number of rounds that a player must win to win the game.
47 GameType GameTypeTag // The type of game to play: Classic or LizardSpock.
48}
49
Todd Wang24983c72014-11-26 15:07:25 -080050func (GameOptions) __VDLReflect(struct {
Jiri Simsa3b913022015-02-27 15:19:25 -080051 Name string "v.io/x/ref/examples/rps.GameOptions"
Todd Wang24983c72014-11-26 15:07:25 -080052}) {
53}
54
Jiri Simsa37cdfc02014-10-09 18:00:43 -070055type GameTypeTag byte
56
Todd Wang24983c72014-11-26 15:07:25 -080057func (GameTypeTag) __VDLReflect(struct {
Jiri Simsa3b913022015-02-27 15:19:25 -080058 Name string "v.io/x/ref/examples/rps.GameTypeTag"
Todd Wang24983c72014-11-26 15:07:25 -080059}) {
60}
61
Robin Thellend636b3602015-02-11 14:57:44 -080062type (
63 // PlayerAction represents any single field of the PlayerAction union type.
64 PlayerAction interface {
65 // Index returns the field index.
66 Index() int
67 // Interface returns the field value as an interface.
68 Interface() interface{}
69 // Name returns the field name.
70 Name() string
71 // __VDLReflect describes the PlayerAction union type.
72 __VDLReflect(__PlayerActionReflect)
73 }
74 // PlayerActionMove represents field Move of the PlayerAction union type.
75 PlayerActionMove struct{ Value string } // The move that the player wants to make.
76 // PlayerActionQuit represents field Quit of the PlayerAction union type.
77 PlayerActionQuit struct{ Value unused } // Indicates that the player is quitting the game.
78 // __PlayerActionReflect describes the PlayerAction union type.
79 __PlayerActionReflect struct {
Jiri Simsa3b913022015-02-27 15:19:25 -080080 Name string "v.io/x/ref/examples/rps.PlayerAction"
Robin Thellend636b3602015-02-11 14:57:44 -080081 Type PlayerAction
82 Union struct {
83 Move PlayerActionMove
84 Quit PlayerActionQuit
85 }
86 }
87)
88
89func (x PlayerActionMove) Index() int { return 0 }
90func (x PlayerActionMove) Interface() interface{} { return x.Value }
91func (x PlayerActionMove) Name() string { return "Move" }
92func (x PlayerActionMove) __VDLReflect(__PlayerActionReflect) {}
93
94func (x PlayerActionQuit) Index() int { return 1 }
95func (x PlayerActionQuit) Interface() interface{} { return x.Value }
96func (x PlayerActionQuit) Name() string { return "Quit" }
97func (x PlayerActionQuit) __VDLReflect(__PlayerActionReflect) {}
98
99type unused struct {
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700100}
101
Robin Thellend636b3602015-02-11 14:57:44 -0800102func (unused) __VDLReflect(struct {
Jiri Simsa3b913022015-02-27 15:19:25 -0800103 Name string "v.io/x/ref/examples/rps.unused"
Todd Wang24983c72014-11-26 15:07:25 -0800104}) {
105}
106
Robin Thellend636b3602015-02-11 14:57:44 -0800107type (
108 // JudgeAction represents any single field of the JudgeAction union type.
109 JudgeAction interface {
110 // Index returns the field index.
111 Index() int
112 // Interface returns the field value as an interface.
113 Interface() interface{}
114 // Name returns the field name.
115 Name() string
116 // __VDLReflect describes the JudgeAction union type.
117 __VDLReflect(__JudgeActionReflect)
118 }
119 // JudgeActionPlayerNum represents field PlayerNum of the JudgeAction union type.
120 JudgeActionPlayerNum struct{ Value int32 } // The player's number.
121 // JudgeActionOpponentName represents field OpponentName of the JudgeAction union type.
122 JudgeActionOpponentName struct{ Value string } // The name of the opponent.
123 // JudgeActionMoveOptions represents field MoveOptions of the JudgeAction union type.
124 JudgeActionMoveOptions struct{ Value []string } // A list of allowed moves that the player must choose from.
125 // JudgeActionRoundResult represents field RoundResult of the JudgeAction union type.
126 JudgeActionRoundResult struct{ Value Round } // The result of the previous round.
127 // JudgeActionScore represents field Score of the JudgeAction union type.
128 JudgeActionScore struct{ Value ScoreCard } // The result of the game.
129 // __JudgeActionReflect describes the JudgeAction union type.
130 __JudgeActionReflect struct {
Jiri Simsa3b913022015-02-27 15:19:25 -0800131 Name string "v.io/x/ref/examples/rps.JudgeAction"
Robin Thellend636b3602015-02-11 14:57:44 -0800132 Type JudgeAction
133 Union struct {
134 PlayerNum JudgeActionPlayerNum
135 OpponentName JudgeActionOpponentName
136 MoveOptions JudgeActionMoveOptions
137 RoundResult JudgeActionRoundResult
138 Score JudgeActionScore
139 }
140 }
141)
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700142
Robin Thellend636b3602015-02-11 14:57:44 -0800143func (x JudgeActionPlayerNum) Index() int { return 0 }
144func (x JudgeActionPlayerNum) Interface() interface{} { return x.Value }
145func (x JudgeActionPlayerNum) Name() string { return "PlayerNum" }
146func (x JudgeActionPlayerNum) __VDLReflect(__JudgeActionReflect) {}
147
148func (x JudgeActionOpponentName) Index() int { return 1 }
149func (x JudgeActionOpponentName) Interface() interface{} { return x.Value }
150func (x JudgeActionOpponentName) Name() string { return "OpponentName" }
151func (x JudgeActionOpponentName) __VDLReflect(__JudgeActionReflect) {}
152
153func (x JudgeActionMoveOptions) Index() int { return 2 }
154func (x JudgeActionMoveOptions) Interface() interface{} { return x.Value }
155func (x JudgeActionMoveOptions) Name() string { return "MoveOptions" }
156func (x JudgeActionMoveOptions) __VDLReflect(__JudgeActionReflect) {}
157
158func (x JudgeActionRoundResult) Index() int { return 3 }
159func (x JudgeActionRoundResult) Interface() interface{} { return x.Value }
160func (x JudgeActionRoundResult) Name() string { return "RoundResult" }
161func (x JudgeActionRoundResult) __VDLReflect(__JudgeActionReflect) {}
162
163func (x JudgeActionScore) Index() int { return 4 }
164func (x JudgeActionScore) Interface() interface{} { return x.Value }
165func (x JudgeActionScore) Name() string { return "Score" }
166func (x JudgeActionScore) __VDLReflect(__JudgeActionReflect) {}
Todd Wang24983c72014-11-26 15:07:25 -0800167
Sergey Rogulenko2f64f982015-02-05 14:14:46 -0800168type PlayersMoves [2]string
169
170func (PlayersMoves) __VDLReflect(struct {
Jiri Simsa3b913022015-02-27 15:19:25 -0800171 Name string "v.io/x/ref/examples/rps.PlayersMoves"
Sergey Rogulenko2f64f982015-02-05 14:14:46 -0800172}) {
173}
174
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700175// Round represents the state of a round.
176type Round struct {
Todd Wang07f13462015-03-13 15:54:15 -0700177 Moves PlayersMoves // Each player's move.
178 Comment string // A text comment from judge about the round.
179 Winner WinnerTag // Who won the round.
180 StartTime time.Time // The time at which the round started.
181 EndTime time.Time // The time at which the round ended.
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700182}
183
Todd Wang24983c72014-11-26 15:07:25 -0800184func (Round) __VDLReflect(struct {
Jiri Simsa3b913022015-02-27 15:19:25 -0800185 Name string "v.io/x/ref/examples/rps.Round"
Todd Wang24983c72014-11-26 15:07:25 -0800186}) {
187}
188
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700189// WinnerTag is a type used to indicate whether a round or a game was a draw,
190// was won by player 1 or was won by player 2.
191type WinnerTag byte
192
Todd Wang24983c72014-11-26 15:07:25 -0800193func (WinnerTag) __VDLReflect(struct {
Jiri Simsa3b913022015-02-27 15:19:25 -0800194 Name string "v.io/x/ref/examples/rps.WinnerTag"
Todd Wang24983c72014-11-26 15:07:25 -0800195}) {
196}
197
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700198// PlayResult is the value returned by the Play method. It indicates the outcome of the game.
199type PlayResult struct {
200 YouWon bool // True if the player receiving the result won the game.
201}
202
Todd Wang24983c72014-11-26 15:07:25 -0800203func (PlayResult) __VDLReflect(struct {
Jiri Simsa3b913022015-02-27 15:19:25 -0800204 Name string "v.io/x/ref/examples/rps.PlayResult"
Todd Wang24983c72014-11-26 15:07:25 -0800205}) {
206}
207
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700208type ScoreCard struct {
Todd Wang07f13462015-03-13 15:54:15 -0700209 Opts GameOptions // The game options.
210 Judge string // The name of the judge.
211 Players []string // The name of the players.
212 Rounds []Round // The outcome of each round.
213 StartTime time.Time // The time at which the game started.
214 EndTime time.Time // The time at which the game ended.
215 Winner WinnerTag // Who won the game.
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700216}
217
Todd Wang24983c72014-11-26 15:07:25 -0800218func (ScoreCard) __VDLReflect(struct {
Jiri Simsa3b913022015-02-27 15:19:25 -0800219 Name string "v.io/x/ref/examples/rps.ScoreCard"
Todd Wang24983c72014-11-26 15:07:25 -0800220}) {
221}
222
223func init() {
Benjamin Prosnitz2be28dc2015-03-11 13:33:22 -0700224 vdl.Register((*GameId)(nil))
Todd Wang83f63ba2015-02-09 13:30:43 -0800225 vdl.Register((*GameOptions)(nil))
226 vdl.Register((*GameTypeTag)(nil))
227 vdl.Register((*PlayerAction)(nil))
Robin Thellend636b3602015-02-11 14:57:44 -0800228 vdl.Register((*unused)(nil))
Todd Wang83f63ba2015-02-09 13:30:43 -0800229 vdl.Register((*JudgeAction)(nil))
230 vdl.Register((*PlayersMoves)(nil))
231 vdl.Register((*Round)(nil))
232 vdl.Register((*WinnerTag)(nil))
233 vdl.Register((*PlayResult)(nil))
234 vdl.Register((*ScoreCard)(nil))
Todd Wang24983c72014-11-26 15:07:25 -0800235}
236
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700237const Classic = GameTypeTag(0) // Rock-Paper-Scissors
238
239const LizardSpock = GameTypeTag(1) // Rock-Paper-Scissors-Lizard-Spock
240
241const Draw = WinnerTag(0)
242
243const Player1 = WinnerTag(1)
244
245const Player2 = WinnerTag(2)
246
Todd Wangd8cb55b2014-11-07 00:58:32 -0800247// JudgeClientMethods is the client interface
248// containing Judge methods.
249type JudgeClientMethods interface {
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700250 // CreateGame creates a new game with the given game options and returns a game
251 // identifier that can be used by the players to join the game.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700252 CreateGame(ctx *context.T, Opts GameOptions, opts ...rpc.CallOpt) (GameId, error)
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700253 // Play lets a player join an existing game and play.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700254 Play(ctx *context.T, Id GameId, opts ...rpc.CallOpt) (JudgePlayClientCall, error)
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700255}
256
Todd Wangd8cb55b2014-11-07 00:58:32 -0800257// JudgeClientStub adds universal methods to JudgeClientMethods.
258type JudgeClientStub interface {
259 JudgeClientMethods
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700260 rpc.UniversalServiceMethods
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700261}
262
Todd Wangd8cb55b2014-11-07 00:58:32 -0800263// JudgeClient returns a client stub for Judge.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700264func JudgeClient(name string, opts ...rpc.BindOpt) JudgeClientStub {
265 var client rpc.Client
Todd Wangd8cb55b2014-11-07 00:58:32 -0800266 for _, opt := range opts {
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700267 if clientOpt, ok := opt.(rpc.Client); ok {
Todd Wangd8cb55b2014-11-07 00:58:32 -0800268 client = clientOpt
269 }
270 }
271 return implJudgeClientStub{name, client}
272}
273
274type implJudgeClientStub struct {
275 name string
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700276 client rpc.Client
Todd Wangd8cb55b2014-11-07 00:58:32 -0800277}
278
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700279func (c implJudgeClientStub) c(ctx *context.T) rpc.Client {
Todd Wangd8cb55b2014-11-07 00:58:32 -0800280 if c.client != nil {
281 return c.client
282 }
Jiri Simsa386ce3e2015-02-23 16:14:20 -0800283 return v23.GetClient(ctx)
Todd Wangd8cb55b2014-11-07 00:58:32 -0800284}
285
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700286func (c implJudgeClientStub) CreateGame(ctx *context.T, i0 GameOptions, opts ...rpc.CallOpt) (o0 GameId, err error) {
287 var call rpc.ClientCall
Todd Wangd8cb55b2014-11-07 00:58:32 -0800288 if call, err = c.c(ctx).StartCall(ctx, c.name, "CreateGame", []interface{}{i0}, opts...); err != nil {
289 return
290 }
Todd Wang8f9f7f12015-02-18 13:21:09 -0800291 err = call.Finish(&o0)
Todd Wangd8cb55b2014-11-07 00:58:32 -0800292 return
293}
294
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700295func (c implJudgeClientStub) Play(ctx *context.T, i0 GameId, opts ...rpc.CallOpt) (ocall JudgePlayClientCall, err error) {
296 var call rpc.ClientCall
Todd Wangd8cb55b2014-11-07 00:58:32 -0800297 if call, err = c.c(ctx).StartCall(ctx, c.name, "Play", []interface{}{i0}, opts...); err != nil {
298 return
299 }
Matt Rosencrantz72a15612015-02-27 22:22:41 -0800300 ocall = &implJudgePlayClientCall{ClientCall: call}
Todd Wangd8cb55b2014-11-07 00:58:32 -0800301 return
302}
303
Todd Wangd8cb55b2014-11-07 00:58:32 -0800304// JudgePlayClientStream is the client stream for Judge.Play.
305type JudgePlayClientStream interface {
Todd Wang5cd27352014-11-11 20:27:39 -0800306 // RecvStream returns the receiver side of the Judge.Play client stream.
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700307 RecvStream() interface {
Todd Wangd8cb55b2014-11-07 00:58:32 -0800308 // Advance stages an item so that it may be retrieved via Value. Returns
309 // true iff there is an item to retrieve. Advance must be called before
310 // Value is called. May block if an item is not available.
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700311 Advance() bool
Todd Wangd8cb55b2014-11-07 00:58:32 -0800312 // Value returns the item that was staged by Advance. May panic if Advance
313 // returned false or was not called. Never blocks.
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700314 Value() JudgeAction
Todd Wangd8cb55b2014-11-07 00:58:32 -0800315 // Err returns any error encountered by Advance. Never blocks.
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700316 Err() error
317 }
Todd Wang5cd27352014-11-11 20:27:39 -0800318 // SendStream returns the send side of the Judge.Play client stream.
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700319 SendStream() interface {
Todd Wang04f97dd2014-12-18 18:52:07 -0800320 // Send places the item onto the output stream. Returns errors
321 // encountered while sending, or if Send is called after Close or
322 // the stream has been canceled. Blocks if there is no buffer
323 // space; will unblock when buffer space is available or after
324 // the stream has been canceled.
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700325 Send(item PlayerAction) error
Todd Wang04f97dd2014-12-18 18:52:07 -0800326 // Close indicates to the server that no more items will be sent;
327 // server Recv calls will receive io.EOF after all sent items.
328 // This is an optional call - e.g. a client might call Close if it
329 // needs to continue receiving items from the server after it's
330 // done sending. Returns errors encountered while closing, or if
331 // Close is called after the stream has been canceled. Like Send,
332 // blocks if there is no buffer space available.
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700333 Close() error
334 }
Todd Wangd8cb55b2014-11-07 00:58:32 -0800335}
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700336
Matt Rosencrantz72a15612015-02-27 22:22:41 -0800337// JudgePlayClientCall represents the call returned from Judge.Play.
338type JudgePlayClientCall interface {
Todd Wangd8cb55b2014-11-07 00:58:32 -0800339 JudgePlayClientStream
340 // Finish performs the equivalent of SendStream().Close, then blocks until
341 // the server is done, and returns the positional return values for the call.
342 //
Todd Wang04f97dd2014-12-18 18:52:07 -0800343 // Finish returns immediately if the call has been canceled; depending on the
Todd Wangd8cb55b2014-11-07 00:58:32 -0800344 // timing the output could either be an error signaling cancelation, or the
345 // valid positional return values from the server.
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700346 //
Todd Wang04f97dd2014-12-18 18:52:07 -0800347 // Calling Finish is mandatory for releasing stream resources, unless the call
348 // has been canceled or any of the other methods return an error. Finish should
Todd Wangd8cb55b2014-11-07 00:58:32 -0800349 // be called at most once.
350 Finish() (PlayResult, error)
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700351}
352
Matt Rosencrantz72a15612015-02-27 22:22:41 -0800353type implJudgePlayClientCall struct {
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700354 rpc.ClientCall
Todd Wang5cd27352014-11-11 20:27:39 -0800355 valRecv JudgeAction
356 errRecv error
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700357}
358
Matt Rosencrantz72a15612015-02-27 22:22:41 -0800359func (c *implJudgePlayClientCall) RecvStream() interface {
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700360 Advance() bool
361 Value() JudgeAction
362 Err() error
363} {
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800364 return implJudgePlayClientCallRecv{c}
Todd Wang5cd27352014-11-11 20:27:39 -0800365}
366
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800367type implJudgePlayClientCallRecv struct {
Matt Rosencrantz72a15612015-02-27 22:22:41 -0800368 c *implJudgePlayClientCall
Todd Wang5cd27352014-11-11 20:27:39 -0800369}
370
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800371func (c implJudgePlayClientCallRecv) Advance() bool {
Todd Wang5cd27352014-11-11 20:27:39 -0800372 c.c.errRecv = c.c.Recv(&c.c.valRecv)
373 return c.c.errRecv == nil
374}
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800375func (c implJudgePlayClientCallRecv) Value() JudgeAction {
Todd Wang5cd27352014-11-11 20:27:39 -0800376 return c.c.valRecv
377}
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800378func (c implJudgePlayClientCallRecv) Err() error {
Todd Wangd1779a52015-02-05 11:46:12 -0800379 if c.c.errRecv == io.EOF {
Todd Wang5cd27352014-11-11 20:27:39 -0800380 return nil
381 }
382 return c.c.errRecv
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700383}
Matt Rosencrantz72a15612015-02-27 22:22:41 -0800384func (c *implJudgePlayClientCall) SendStream() interface {
Todd Wangd8cb55b2014-11-07 00:58:32 -0800385 Send(item PlayerAction) error
386 Close() error
387} {
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800388 return implJudgePlayClientCallSend{c}
Todd Wang5cd27352014-11-11 20:27:39 -0800389}
390
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800391type implJudgePlayClientCallSend struct {
Matt Rosencrantz72a15612015-02-27 22:22:41 -0800392 c *implJudgePlayClientCall
Todd Wang5cd27352014-11-11 20:27:39 -0800393}
394
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800395func (c implJudgePlayClientCallSend) Send(item PlayerAction) error {
Todd Wang5cd27352014-11-11 20:27:39 -0800396 return c.c.Send(item)
397}
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800398func (c implJudgePlayClientCallSend) Close() error {
Todd Wang5cd27352014-11-11 20:27:39 -0800399 return c.c.CloseSend()
Todd Wangd8cb55b2014-11-07 00:58:32 -0800400}
Matt Rosencrantz72a15612015-02-27 22:22:41 -0800401func (c *implJudgePlayClientCall) Finish() (o0 PlayResult, err error) {
402 err = c.ClientCall.Finish(&o0)
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700403 return
404}
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700405
Todd Wangd8cb55b2014-11-07 00:58:32 -0800406// JudgeServerMethods is the interface a server writer
407// implements for Judge.
408type JudgeServerMethods interface {
409 // CreateGame creates a new game with the given game options and returns a game
410 // identifier that can be used by the players to join the game.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700411 CreateGame(call rpc.ServerCall, Opts GameOptions) (GameId, error)
Todd Wangd8cb55b2014-11-07 00:58:32 -0800412 // Play lets a player join an existing game and play.
Benjamin Prosnitz2be28dc2015-03-11 13:33:22 -0700413 Play(call JudgePlayServerCall, Id GameId) (PlayResult, error)
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700414}
415
Todd Wangd8cb55b2014-11-07 00:58:32 -0800416// JudgeServerStubMethods is the server interface containing
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700417// Judge methods, as expected by rpc.Server.
Todd Wang5cd27352014-11-11 20:27:39 -0800418// The only difference between this interface and JudgeServerMethods
419// is the streaming methods.
Todd Wangd8cb55b2014-11-07 00:58:32 -0800420type JudgeServerStubMethods interface {
421 // CreateGame creates a new game with the given game options and returns a game
422 // identifier that can be used by the players to join the game.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700423 CreateGame(call rpc.ServerCall, Opts GameOptions) (GameId, error)
Todd Wangd8cb55b2014-11-07 00:58:32 -0800424 // Play lets a player join an existing game and play.
Benjamin Prosnitz2be28dc2015-03-11 13:33:22 -0700425 Play(call *JudgePlayServerCallStub, Id GameId) (PlayResult, error)
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700426}
427
Todd Wangd8cb55b2014-11-07 00:58:32 -0800428// JudgeServerStub adds universal methods to JudgeServerStubMethods.
429type JudgeServerStub interface {
430 JudgeServerStubMethods
Todd Wangdddc1fb2014-11-16 21:39:02 -0800431 // Describe the Judge interfaces.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700432 Describe__() []rpc.InterfaceDesc
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700433}
434
Todd Wangd8cb55b2014-11-07 00:58:32 -0800435// JudgeServer returns a server stub for Judge.
436// It converts an implementation of JudgeServerMethods into
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700437// an object that may be used by rpc.Server.
Todd Wangd8cb55b2014-11-07 00:58:32 -0800438func JudgeServer(impl JudgeServerMethods) JudgeServerStub {
439 stub := implJudgeServerStub{
440 impl: impl,
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700441 }
Todd Wangd8cb55b2014-11-07 00:58:32 -0800442 // Initialize GlobState; always check the stub itself first, to handle the
443 // case where the user has the Glob method defined in their VDL source.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700444 if gs := rpc.NewGlobState(stub); gs != nil {
Todd Wangd8cb55b2014-11-07 00:58:32 -0800445 stub.gs = gs
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700446 } else if gs := rpc.NewGlobState(impl); gs != nil {
Todd Wangd8cb55b2014-11-07 00:58:32 -0800447 stub.gs = gs
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700448 }
Robin Thellenda7548452014-11-05 18:10:24 -0800449 return stub
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700450}
451
Todd Wangd8cb55b2014-11-07 00:58:32 -0800452type implJudgeServerStub struct {
453 impl JudgeServerMethods
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700454 gs *rpc.GlobState
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700455}
456
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700457func (s implJudgeServerStub) CreateGame(call rpc.ServerCall, i0 GameOptions) (GameId, error) {
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800458 return s.impl.CreateGame(call, i0)
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700459}
460
Benjamin Prosnitz2be28dc2015-03-11 13:33:22 -0700461func (s implJudgeServerStub) Play(call *JudgePlayServerCallStub, i0 GameId) (PlayResult, error) {
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800462 return s.impl.Play(call, i0)
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700463}
464
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700465func (s implJudgeServerStub) Globber() *rpc.GlobState {
Todd Wangd8cb55b2014-11-07 00:58:32 -0800466 return s.gs
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700467}
468
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700469func (s implJudgeServerStub) Describe__() []rpc.InterfaceDesc {
470 return []rpc.InterfaceDesc{JudgeDesc}
Todd Wangdddc1fb2014-11-16 21:39:02 -0800471}
472
473// JudgeDesc describes the Judge interface.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700474var JudgeDesc rpc.InterfaceDesc = descJudge
Todd Wangdddc1fb2014-11-16 21:39:02 -0800475
476// descJudge hides the desc to keep godoc clean.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700477var descJudge = rpc.InterfaceDesc{
Todd Wangdddc1fb2014-11-16 21:39:02 -0800478 Name: "Judge",
Jiri Simsa3b913022015-02-27 15:19:25 -0800479 PkgPath: "v.io/x/ref/examples/rps",
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700480 Methods: []rpc.MethodDesc{
Todd Wangdddc1fb2014-11-16 21:39:02 -0800481 {
482 Name: "CreateGame",
483 Doc: "// CreateGame creates a new game with the given game options and returns a game\n// identifier that can be used by the players to join the game.",
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700484 InArgs: []rpc.ArgDesc{
Todd Wangdddc1fb2014-11-16 21:39:02 -0800485 {"Opts", ``}, // GameOptions
486 },
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700487 OutArgs: []rpc.ArgDesc{
Benjamin Prosnitz2be28dc2015-03-11 13:33:22 -0700488 {"", ``}, // GameId
Todd Wangdddc1fb2014-11-16 21:39:02 -0800489 },
Todd Wangcab990d2015-02-19 19:54:06 -0800490 Tags: []*vdl.Value{vdl.ValueOf(access.Tag("Write"))},
Todd Wangdddc1fb2014-11-16 21:39:02 -0800491 },
492 {
493 Name: "Play",
494 Doc: "// Play lets a player join an existing game and play.",
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700495 InArgs: []rpc.ArgDesc{
Benjamin Prosnitz2be28dc2015-03-11 13:33:22 -0700496 {"Id", ``}, // GameId
Todd Wangdddc1fb2014-11-16 21:39:02 -0800497 },
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700498 OutArgs: []rpc.ArgDesc{
Todd Wangdddc1fb2014-11-16 21:39:02 -0800499 {"", ``}, // PlayResult
Todd Wangdddc1fb2014-11-16 21:39:02 -0800500 },
Todd Wangcab990d2015-02-19 19:54:06 -0800501 Tags: []*vdl.Value{vdl.ValueOf(access.Tag("Write"))},
Todd Wangdddc1fb2014-11-16 21:39:02 -0800502 },
503 },
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700504}
505
Todd Wangd8cb55b2014-11-07 00:58:32 -0800506// JudgePlayServerStream is the server stream for Judge.Play.
507type JudgePlayServerStream interface {
Todd Wang5cd27352014-11-11 20:27:39 -0800508 // RecvStream returns the receiver side of the Judge.Play server stream.
Todd Wangd8cb55b2014-11-07 00:58:32 -0800509 RecvStream() interface {
510 // Advance stages an item so that it may be retrieved via Value. Returns
511 // true iff there is an item to retrieve. Advance must be called before
512 // Value is called. May block if an item is not available.
513 Advance() bool
514 // Value returns the item that was staged by Advance. May panic if Advance
515 // returned false or was not called. Never blocks.
516 Value() PlayerAction
517 // Err returns any error encountered by Advance. Never blocks.
518 Err() error
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700519 }
Todd Wang5cd27352014-11-11 20:27:39 -0800520 // SendStream returns the send side of the Judge.Play server stream.
Todd Wangd8cb55b2014-11-07 00:58:32 -0800521 SendStream() interface {
522 // Send places the item onto the output stream. Returns errors encountered
523 // while sending. Blocks if there is no buffer space; will unblock when
524 // buffer space is available.
525 Send(item JudgeAction) error
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700526 }
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700527}
528
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800529// JudgePlayServerCall represents the context passed to Judge.Play.
530type JudgePlayServerCall interface {
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700531 rpc.ServerCall
Todd Wangd8cb55b2014-11-07 00:58:32 -0800532 JudgePlayServerStream
Robin Thellenda7548452014-11-05 18:10:24 -0800533}
534
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700535// JudgePlayServerCallStub is a wrapper that converts rpc.StreamServerCall into
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800536// a typesafe stub that implements JudgePlayServerCall.
537type JudgePlayServerCallStub struct {
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700538 rpc.StreamServerCall
Todd Wang5cd27352014-11-11 20:27:39 -0800539 valRecv PlayerAction
540 errRecv error
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700541}
542
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700543// Init initializes JudgePlayServerCallStub from rpc.StreamServerCall.
544func (s *JudgePlayServerCallStub) Init(call rpc.StreamServerCall) {
Matt Rosencrantz9ef0f2d2015-02-27 11:04:32 -0800545 s.StreamServerCall = call
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700546}
547
Todd Wang5cd27352014-11-11 20:27:39 -0800548// RecvStream returns the receiver side of the Judge.Play server stream.
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800549func (s *JudgePlayServerCallStub) RecvStream() interface {
Todd Wangd8cb55b2014-11-07 00:58:32 -0800550 Advance() bool
551 Value() PlayerAction
552 Err() error
553} {
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800554 return implJudgePlayServerCallRecv{s}
Todd Wangd8cb55b2014-11-07 00:58:32 -0800555}
Todd Wang5cd27352014-11-11 20:27:39 -0800556
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800557type implJudgePlayServerCallRecv struct {
558 s *JudgePlayServerCallStub
Todd Wang5cd27352014-11-11 20:27:39 -0800559}
560
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800561func (s implJudgePlayServerCallRecv) Advance() bool {
Todd Wang5cd27352014-11-11 20:27:39 -0800562 s.s.errRecv = s.s.Recv(&s.s.valRecv)
563 return s.s.errRecv == nil
564}
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800565func (s implJudgePlayServerCallRecv) Value() PlayerAction {
Todd Wang5cd27352014-11-11 20:27:39 -0800566 return s.s.valRecv
567}
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800568func (s implJudgePlayServerCallRecv) Err() error {
Todd Wangd1779a52015-02-05 11:46:12 -0800569 if s.s.errRecv == io.EOF {
Todd Wang5cd27352014-11-11 20:27:39 -0800570 return nil
571 }
572 return s.s.errRecv
573}
574
575// SendStream returns the send side of the Judge.Play server stream.
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800576func (s *JudgePlayServerCallStub) SendStream() interface {
Todd Wangd8cb55b2014-11-07 00:58:32 -0800577 Send(item JudgeAction) error
578} {
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800579 return implJudgePlayServerCallSend{s}
Todd Wang5cd27352014-11-11 20:27:39 -0800580}
581
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800582type implJudgePlayServerCallSend struct {
583 s *JudgePlayServerCallStub
Todd Wang5cd27352014-11-11 20:27:39 -0800584}
585
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800586func (s implJudgePlayServerCallSend) Send(item JudgeAction) error {
Todd Wang5cd27352014-11-11 20:27:39 -0800587 return s.s.Send(item)
Todd Wangd8cb55b2014-11-07 00:58:32 -0800588}
589
590// PlayerClientMethods is the client interface
591// containing Player methods.
592//
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700593// Player can receive challenges from other players.
Todd Wangd8cb55b2014-11-07 00:58:32 -0800594type PlayerClientMethods interface {
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700595 // Challenge is used by other players to challenge this player to a game. If
596 // the challenge is accepted, the method returns nil.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700597 Challenge(ctx *context.T, Address string, Id GameId, Opts GameOptions, opts ...rpc.CallOpt) error
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700598}
599
Todd Wangd8cb55b2014-11-07 00:58:32 -0800600// PlayerClientStub adds universal methods to PlayerClientMethods.
601type PlayerClientStub interface {
602 PlayerClientMethods
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700603 rpc.UniversalServiceMethods
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700604}
605
Todd Wangd8cb55b2014-11-07 00:58:32 -0800606// PlayerClient returns a client stub for Player.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700607func PlayerClient(name string, opts ...rpc.BindOpt) PlayerClientStub {
608 var client rpc.Client
Todd Wangd8cb55b2014-11-07 00:58:32 -0800609 for _, opt := range opts {
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700610 if clientOpt, ok := opt.(rpc.Client); ok {
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700611 client = clientOpt
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700612 }
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700613 }
Todd Wangd8cb55b2014-11-07 00:58:32 -0800614 return implPlayerClientStub{name, client}
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700615}
616
Todd Wangd8cb55b2014-11-07 00:58:32 -0800617type implPlayerClientStub struct {
618 name string
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700619 client rpc.Client
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700620}
621
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700622func (c implPlayerClientStub) c(ctx *context.T) rpc.Client {
Todd Wangd8cb55b2014-11-07 00:58:32 -0800623 if c.client != nil {
624 return c.client
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700625 }
Jiri Simsa386ce3e2015-02-23 16:14:20 -0800626 return v23.GetClient(ctx)
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700627}
628
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700629func (c implPlayerClientStub) Challenge(ctx *context.T, i0 string, i1 GameId, i2 GameOptions, opts ...rpc.CallOpt) (err error) {
630 var call rpc.ClientCall
Todd Wangd8cb55b2014-11-07 00:58:32 -0800631 if call, err = c.c(ctx).StartCall(ctx, c.name, "Challenge", []interface{}{i0, i1, i2}, opts...); err != nil {
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700632 return
633 }
Todd Wang8f9f7f12015-02-18 13:21:09 -0800634 err = call.Finish()
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700635 return
636}
637
Todd Wangd8cb55b2014-11-07 00:58:32 -0800638// PlayerServerMethods is the interface a server writer
639// implements for Player.
640//
641// Player can receive challenges from other players.
642type PlayerServerMethods interface {
643 // Challenge is used by other players to challenge this player to a game. If
644 // the challenge is accepted, the method returns nil.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700645 Challenge(call rpc.ServerCall, Address string, Id GameId, Opts GameOptions) error
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700646}
647
Todd Wangd8cb55b2014-11-07 00:58:32 -0800648// PlayerServerStubMethods is the server interface containing
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700649// Player methods, as expected by rpc.Server.
Todd Wang5cd27352014-11-11 20:27:39 -0800650// There is no difference between this interface and PlayerServerMethods
651// since there are no streaming methods.
652type PlayerServerStubMethods PlayerServerMethods
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700653
Todd Wangd8cb55b2014-11-07 00:58:32 -0800654// PlayerServerStub adds universal methods to PlayerServerStubMethods.
655type PlayerServerStub interface {
656 PlayerServerStubMethods
Todd Wangdddc1fb2014-11-16 21:39:02 -0800657 // Describe the Player interfaces.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700658 Describe__() []rpc.InterfaceDesc
Todd Wangd8cb55b2014-11-07 00:58:32 -0800659}
660
661// PlayerServer returns a server stub for Player.
662// It converts an implementation of PlayerServerMethods into
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700663// an object that may be used by rpc.Server.
Todd Wangd8cb55b2014-11-07 00:58:32 -0800664func PlayerServer(impl PlayerServerMethods) PlayerServerStub {
665 stub := implPlayerServerStub{
666 impl: impl,
667 }
668 // Initialize GlobState; always check the stub itself first, to handle the
669 // case where the user has the Glob method defined in their VDL source.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700670 if gs := rpc.NewGlobState(stub); gs != nil {
Todd Wangd8cb55b2014-11-07 00:58:32 -0800671 stub.gs = gs
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700672 } else if gs := rpc.NewGlobState(impl); gs != nil {
Todd Wangd8cb55b2014-11-07 00:58:32 -0800673 stub.gs = gs
674 }
675 return stub
676}
677
678type implPlayerServerStub struct {
679 impl PlayerServerMethods
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700680 gs *rpc.GlobState
Todd Wangd8cb55b2014-11-07 00:58:32 -0800681}
682
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700683func (s implPlayerServerStub) Challenge(call rpc.ServerCall, i0 string, i1 GameId, i2 GameOptions) error {
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800684 return s.impl.Challenge(call, i0, i1, i2)
Todd Wangd8cb55b2014-11-07 00:58:32 -0800685}
686
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700687func (s implPlayerServerStub) Globber() *rpc.GlobState {
Todd Wangd8cb55b2014-11-07 00:58:32 -0800688 return s.gs
689}
690
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700691func (s implPlayerServerStub) Describe__() []rpc.InterfaceDesc {
692 return []rpc.InterfaceDesc{PlayerDesc}
Todd Wangdddc1fb2014-11-16 21:39:02 -0800693}
694
695// PlayerDesc describes the Player interface.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700696var PlayerDesc rpc.InterfaceDesc = descPlayer
Todd Wangdddc1fb2014-11-16 21:39:02 -0800697
698// descPlayer hides the desc to keep godoc clean.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700699var descPlayer = rpc.InterfaceDesc{
Todd Wangdddc1fb2014-11-16 21:39:02 -0800700 Name: "Player",
Jiri Simsa3b913022015-02-27 15:19:25 -0800701 PkgPath: "v.io/x/ref/examples/rps",
Todd Wangdddc1fb2014-11-16 21:39:02 -0800702 Doc: "// Player can receive challenges from other players.",
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700703 Methods: []rpc.MethodDesc{
Todd Wangdddc1fb2014-11-16 21:39:02 -0800704 {
705 Name: "Challenge",
706 Doc: "// Challenge is used by other players to challenge this player to a game. If\n// the challenge is accepted, the method returns nil.",
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700707 InArgs: []rpc.ArgDesc{
Todd Wangdddc1fb2014-11-16 21:39:02 -0800708 {"Address", ``}, // string
Benjamin Prosnitz2be28dc2015-03-11 13:33:22 -0700709 {"Id", ``}, // GameId
Todd Wangdddc1fb2014-11-16 21:39:02 -0800710 {"Opts", ``}, // GameOptions
711 },
Todd Wangcab990d2015-02-19 19:54:06 -0800712 Tags: []*vdl.Value{vdl.ValueOf(access.Tag("Write"))},
Todd Wangdddc1fb2014-11-16 21:39:02 -0800713 },
714 },
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700715}
716
Todd Wangd8cb55b2014-11-07 00:58:32 -0800717// ScoreKeeperClientMethods is the client interface
718// containing ScoreKeeper methods.
719//
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700720// ScoreKeeper receives the outcome of games from Judges.
Todd Wangd8cb55b2014-11-07 00:58:32 -0800721type ScoreKeeperClientMethods interface {
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700722 Record(ctx *context.T, Score ScoreCard, opts ...rpc.CallOpt) error
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700723}
724
Todd Wangd8cb55b2014-11-07 00:58:32 -0800725// ScoreKeeperClientStub adds universal methods to ScoreKeeperClientMethods.
726type ScoreKeeperClientStub interface {
727 ScoreKeeperClientMethods
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700728 rpc.UniversalServiceMethods
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700729}
730
Todd Wangd8cb55b2014-11-07 00:58:32 -0800731// ScoreKeeperClient returns a client stub for ScoreKeeper.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700732func ScoreKeeperClient(name string, opts ...rpc.BindOpt) ScoreKeeperClientStub {
733 var client rpc.Client
Todd Wangd8cb55b2014-11-07 00:58:32 -0800734 for _, opt := range opts {
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700735 if clientOpt, ok := opt.(rpc.Client); ok {
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700736 client = clientOpt
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700737 }
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700738 }
Todd Wangd8cb55b2014-11-07 00:58:32 -0800739 return implScoreKeeperClientStub{name, client}
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700740}
741
Todd Wangd8cb55b2014-11-07 00:58:32 -0800742type implScoreKeeperClientStub struct {
743 name string
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700744 client rpc.Client
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700745}
746
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700747func (c implScoreKeeperClientStub) c(ctx *context.T) rpc.Client {
Todd Wangd8cb55b2014-11-07 00:58:32 -0800748 if c.client != nil {
749 return c.client
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700750 }
Jiri Simsa386ce3e2015-02-23 16:14:20 -0800751 return v23.GetClient(ctx)
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700752}
753
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700754func (c implScoreKeeperClientStub) Record(ctx *context.T, i0 ScoreCard, opts ...rpc.CallOpt) (err error) {
755 var call rpc.ClientCall
Todd Wangd8cb55b2014-11-07 00:58:32 -0800756 if call, err = c.c(ctx).StartCall(ctx, c.name, "Record", []interface{}{i0}, opts...); err != nil {
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700757 return
758 }
Todd Wang8f9f7f12015-02-18 13:21:09 -0800759 err = call.Finish()
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700760 return
761}
762
Todd Wangd8cb55b2014-11-07 00:58:32 -0800763// ScoreKeeperServerMethods is the interface a server writer
764// implements for ScoreKeeper.
765//
766// ScoreKeeper receives the outcome of games from Judges.
767type ScoreKeeperServerMethods interface {
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700768 Record(call rpc.ServerCall, Score ScoreCard) error
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700769}
770
Todd Wangd8cb55b2014-11-07 00:58:32 -0800771// ScoreKeeperServerStubMethods is the server interface containing
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700772// ScoreKeeper methods, as expected by rpc.Server.
Todd Wang5cd27352014-11-11 20:27:39 -0800773// There is no difference between this interface and ScoreKeeperServerMethods
774// since there are no streaming methods.
775type ScoreKeeperServerStubMethods ScoreKeeperServerMethods
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700776
Todd Wangd8cb55b2014-11-07 00:58:32 -0800777// ScoreKeeperServerStub adds universal methods to ScoreKeeperServerStubMethods.
778type ScoreKeeperServerStub interface {
779 ScoreKeeperServerStubMethods
Todd Wangdddc1fb2014-11-16 21:39:02 -0800780 // Describe the ScoreKeeper interfaces.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700781 Describe__() []rpc.InterfaceDesc
Todd Wangd8cb55b2014-11-07 00:58:32 -0800782}
783
784// ScoreKeeperServer returns a server stub for ScoreKeeper.
785// It converts an implementation of ScoreKeeperServerMethods into
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700786// an object that may be used by rpc.Server.
Todd Wangd8cb55b2014-11-07 00:58:32 -0800787func ScoreKeeperServer(impl ScoreKeeperServerMethods) ScoreKeeperServerStub {
788 stub := implScoreKeeperServerStub{
789 impl: impl,
790 }
791 // Initialize GlobState; always check the stub itself first, to handle the
792 // case where the user has the Glob method defined in their VDL source.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700793 if gs := rpc.NewGlobState(stub); gs != nil {
Todd Wangd8cb55b2014-11-07 00:58:32 -0800794 stub.gs = gs
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700795 } else if gs := rpc.NewGlobState(impl); gs != nil {
Todd Wangd8cb55b2014-11-07 00:58:32 -0800796 stub.gs = gs
797 }
798 return stub
799}
800
801type implScoreKeeperServerStub struct {
802 impl ScoreKeeperServerMethods
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700803 gs *rpc.GlobState
Todd Wangd8cb55b2014-11-07 00:58:32 -0800804}
805
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700806func (s implScoreKeeperServerStub) Record(call rpc.ServerCall, i0 ScoreCard) error {
Suharsh Sivakumar31f49852015-03-03 16:13:20 -0800807 return s.impl.Record(call, i0)
Todd Wangd8cb55b2014-11-07 00:58:32 -0800808}
809
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700810func (s implScoreKeeperServerStub) Globber() *rpc.GlobState {
Todd Wangd8cb55b2014-11-07 00:58:32 -0800811 return s.gs
812}
813
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700814func (s implScoreKeeperServerStub) Describe__() []rpc.InterfaceDesc {
815 return []rpc.InterfaceDesc{ScoreKeeperDesc}
Todd Wangdddc1fb2014-11-16 21:39:02 -0800816}
817
818// ScoreKeeperDesc describes the ScoreKeeper interface.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700819var ScoreKeeperDesc rpc.InterfaceDesc = descScoreKeeper
Todd Wangdddc1fb2014-11-16 21:39:02 -0800820
821// descScoreKeeper hides the desc to keep godoc clean.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700822var descScoreKeeper = rpc.InterfaceDesc{
Todd Wangdddc1fb2014-11-16 21:39:02 -0800823 Name: "ScoreKeeper",
Jiri Simsa3b913022015-02-27 15:19:25 -0800824 PkgPath: "v.io/x/ref/examples/rps",
Todd Wangdddc1fb2014-11-16 21:39:02 -0800825 Doc: "// ScoreKeeper receives the outcome of games from Judges.",
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700826 Methods: []rpc.MethodDesc{
Todd Wangdddc1fb2014-11-16 21:39:02 -0800827 {
828 Name: "Record",
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700829 InArgs: []rpc.ArgDesc{
Todd Wangdddc1fb2014-11-16 21:39:02 -0800830 {"Score", ``}, // ScoreCard
831 },
Todd Wangcab990d2015-02-19 19:54:06 -0800832 Tags: []*vdl.Value{vdl.ValueOf(access.Tag("Write"))},
Todd Wangdddc1fb2014-11-16 21:39:02 -0800833 },
834 },
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700835}
836
Todd Wangd8cb55b2014-11-07 00:58:32 -0800837// RockPaperScissorsClientMethods is the client interface
838// containing RockPaperScissors methods.
839type RockPaperScissorsClientMethods interface {
840 JudgeClientMethods
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700841 // Player can receive challenges from other players.
Todd Wangd8cb55b2014-11-07 00:58:32 -0800842 PlayerClientMethods
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700843 // ScoreKeeper receives the outcome of games from Judges.
Todd Wangd8cb55b2014-11-07 00:58:32 -0800844 ScoreKeeperClientMethods
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700845}
846
Todd Wangd8cb55b2014-11-07 00:58:32 -0800847// RockPaperScissorsClientStub adds universal methods to RockPaperScissorsClientMethods.
848type RockPaperScissorsClientStub interface {
849 RockPaperScissorsClientMethods
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700850 rpc.UniversalServiceMethods
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700851}
852
Todd Wangd8cb55b2014-11-07 00:58:32 -0800853// RockPaperScissorsClient returns a client stub for RockPaperScissors.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700854func RockPaperScissorsClient(name string, opts ...rpc.BindOpt) RockPaperScissorsClientStub {
855 var client rpc.Client
Todd Wangd8cb55b2014-11-07 00:58:32 -0800856 for _, opt := range opts {
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700857 if clientOpt, ok := opt.(rpc.Client); ok {
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700858 client = clientOpt
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700859 }
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700860 }
Todd Wangd8cb55b2014-11-07 00:58:32 -0800861 return implRockPaperScissorsClientStub{name, client, JudgeClient(name, client), PlayerClient(name, client), ScoreKeeperClient(name, client)}
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700862}
863
Todd Wangd8cb55b2014-11-07 00:58:32 -0800864type implRockPaperScissorsClientStub struct {
865 name string
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700866 client rpc.Client
Todd Wangd8cb55b2014-11-07 00:58:32 -0800867
868 JudgeClientStub
869 PlayerClientStub
870 ScoreKeeperClientStub
871}
872
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700873func (c implRockPaperScissorsClientStub) c(ctx *context.T) rpc.Client {
Todd Wangd8cb55b2014-11-07 00:58:32 -0800874 if c.client != nil {
875 return c.client
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700876 }
Jiri Simsa386ce3e2015-02-23 16:14:20 -0800877 return v23.GetClient(ctx)
Todd Wangd8cb55b2014-11-07 00:58:32 -0800878}
879
Todd Wangd8cb55b2014-11-07 00:58:32 -0800880// RockPaperScissorsServerMethods is the interface a server writer
881// implements for RockPaperScissors.
882type RockPaperScissorsServerMethods interface {
883 JudgeServerMethods
884 // Player can receive challenges from other players.
885 PlayerServerMethods
886 // ScoreKeeper receives the outcome of games from Judges.
887 ScoreKeeperServerMethods
888}
889
890// RockPaperScissorsServerStubMethods is the server interface containing
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700891// RockPaperScissors methods, as expected by rpc.Server.
Todd Wang5cd27352014-11-11 20:27:39 -0800892// The only difference between this interface and RockPaperScissorsServerMethods
893// is the streaming methods.
Todd Wangd8cb55b2014-11-07 00:58:32 -0800894type RockPaperScissorsServerStubMethods interface {
895 JudgeServerStubMethods
896 // Player can receive challenges from other players.
897 PlayerServerStubMethods
898 // ScoreKeeper receives the outcome of games from Judges.
899 ScoreKeeperServerStubMethods
900}
901
902// RockPaperScissorsServerStub adds universal methods to RockPaperScissorsServerStubMethods.
903type RockPaperScissorsServerStub interface {
904 RockPaperScissorsServerStubMethods
Todd Wangdddc1fb2014-11-16 21:39:02 -0800905 // Describe the RockPaperScissors interfaces.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700906 Describe__() []rpc.InterfaceDesc
Todd Wangd8cb55b2014-11-07 00:58:32 -0800907}
908
909// RockPaperScissorsServer returns a server stub for RockPaperScissors.
910// It converts an implementation of RockPaperScissorsServerMethods into
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700911// an object that may be used by rpc.Server.
Todd Wangd8cb55b2014-11-07 00:58:32 -0800912func RockPaperScissorsServer(impl RockPaperScissorsServerMethods) RockPaperScissorsServerStub {
913 stub := implRockPaperScissorsServerStub{
914 impl: impl,
915 JudgeServerStub: JudgeServer(impl),
916 PlayerServerStub: PlayerServer(impl),
917 ScoreKeeperServerStub: ScoreKeeperServer(impl),
918 }
919 // Initialize GlobState; always check the stub itself first, to handle the
920 // case where the user has the Glob method defined in their VDL source.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700921 if gs := rpc.NewGlobState(stub); gs != nil {
Todd Wangd8cb55b2014-11-07 00:58:32 -0800922 stub.gs = gs
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700923 } else if gs := rpc.NewGlobState(impl); gs != nil {
Todd Wangd8cb55b2014-11-07 00:58:32 -0800924 stub.gs = gs
925 }
Robin Thellenda7548452014-11-05 18:10:24 -0800926 return stub
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700927}
928
Todd Wangd8cb55b2014-11-07 00:58:32 -0800929type implRockPaperScissorsServerStub struct {
930 impl RockPaperScissorsServerMethods
Todd Wangd8cb55b2014-11-07 00:58:32 -0800931 JudgeServerStub
932 PlayerServerStub
933 ScoreKeeperServerStub
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700934 gs *rpc.GlobState
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700935}
936
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700937func (s implRockPaperScissorsServerStub) Globber() *rpc.GlobState {
Todd Wangd8cb55b2014-11-07 00:58:32 -0800938 return s.gs
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700939}
940
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700941func (s implRockPaperScissorsServerStub) Describe__() []rpc.InterfaceDesc {
942 return []rpc.InterfaceDesc{RockPaperScissorsDesc, JudgeDesc, PlayerDesc, ScoreKeeperDesc}
Todd Wangdddc1fb2014-11-16 21:39:02 -0800943}
944
945// RockPaperScissorsDesc describes the RockPaperScissors interface.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700946var RockPaperScissorsDesc rpc.InterfaceDesc = descRockPaperScissors
Todd Wangdddc1fb2014-11-16 21:39:02 -0800947
948// descRockPaperScissors hides the desc to keep godoc clean.
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700949var descRockPaperScissors = rpc.InterfaceDesc{
Todd Wangdddc1fb2014-11-16 21:39:02 -0800950 Name: "RockPaperScissors",
Jiri Simsa3b913022015-02-27 15:19:25 -0800951 PkgPath: "v.io/x/ref/examples/rps",
Matt Rosencrantz94502cf2015-03-18 09:43:44 -0700952 Embeds: []rpc.EmbedDesc{
Jiri Simsa3b913022015-02-27 15:19:25 -0800953 {"Judge", "v.io/x/ref/examples/rps", ``},
954 {"Player", "v.io/x/ref/examples/rps", "// Player can receive challenges from other players."},
955 {"ScoreKeeper", "v.io/x/ref/examples/rps", "// ScoreKeeper receives the outcome of games from Judges."},
Todd Wangdddc1fb2014-11-16 21:39:02 -0800956 },
Jiri Simsa37cdfc02014-10-09 18:00:43 -0700957}