Suharsh Sivakumar | a4ac50a | 2015-03-13 16:13:50 -0700 | [diff] [blame] | 1 | // This file was auto-generated by the vanadium vdl tool. |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 2 | // Source: service.vdl |
| 3 | |
Robin Thellend | 320af42 | 2015-03-09 17:18:15 -0700 | [diff] [blame] | 4 | // Package rps is an example of vanadium service for playing the game of |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 5 | // 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. |
| 18 | package rps |
| 19 | |
| 20 | import ( |
Todd Wang | d1779a5 | 2015-02-05 11:46:12 -0800 | [diff] [blame] | 21 | // VDL system imports |
| 22 | "io" |
Jiri Simsa | 386ce3e | 2015-02-23 16:14:20 -0800 | [diff] [blame] | 23 | "v.io/v23" |
| 24 | "v.io/v23/context" |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 25 | "v.io/v23/rpc" |
Jiri Simsa | 386ce3e | 2015-02-23 16:14:20 -0800 | [diff] [blame] | 26 | "v.io/v23/vdl" |
Asim Shankar | 4794c93 | 2014-11-24 17:45:56 -0800 | [diff] [blame] | 27 | |
Todd Wang | d1779a5 | 2015-02-05 11:46:12 -0800 | [diff] [blame] | 28 | // VDL user imports |
Todd Wang | 07f1346 | 2015-03-13 15:54:15 -0700 | [diff] [blame] | 29 | "time" |
Jiri Simsa | 386ce3e | 2015-02-23 16:14:20 -0800 | [diff] [blame] | 30 | "v.io/v23/services/security/access" |
Todd Wang | 07f1346 | 2015-03-13 15:54:15 -0700 | [diff] [blame] | 31 | _ "v.io/v23/vdlroot/time" |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 32 | ) |
| 33 | |
Benjamin Prosnitz | 2be28dc | 2015-03-11 13:33:22 -0700 | [diff] [blame] | 34 | // A GameId is used to uniquely identify a game within one Judge. |
| 35 | type GameId struct { |
| 36 | Id string |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Benjamin Prosnitz | 2be28dc | 2015-03-11 13:33:22 -0700 | [diff] [blame] | 39 | func (GameId) __VDLReflect(struct { |
| 40 | Name string "v.io/x/ref/examples/rps.GameId" |
Todd Wang | 24983c7 | 2014-11-26 15:07:25 -0800 | [diff] [blame] | 41 | }) { |
| 42 | } |
| 43 | |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 44 | // GameOptions specifies the parameters of a game. |
| 45 | type 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 Wang | 24983c7 | 2014-11-26 15:07:25 -0800 | [diff] [blame] | 50 | func (GameOptions) __VDLReflect(struct { |
Jiri Simsa | 3b91302 | 2015-02-27 15:19:25 -0800 | [diff] [blame] | 51 | Name string "v.io/x/ref/examples/rps.GameOptions" |
Todd Wang | 24983c7 | 2014-11-26 15:07:25 -0800 | [diff] [blame] | 52 | }) { |
| 53 | } |
| 54 | |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 55 | type GameTypeTag byte |
| 56 | |
Todd Wang | 24983c7 | 2014-11-26 15:07:25 -0800 | [diff] [blame] | 57 | func (GameTypeTag) __VDLReflect(struct { |
Jiri Simsa | 3b91302 | 2015-02-27 15:19:25 -0800 | [diff] [blame] | 58 | Name string "v.io/x/ref/examples/rps.GameTypeTag" |
Todd Wang | 24983c7 | 2014-11-26 15:07:25 -0800 | [diff] [blame] | 59 | }) { |
| 60 | } |
| 61 | |
Robin Thellend | 636b360 | 2015-02-11 14:57:44 -0800 | [diff] [blame] | 62 | type ( |
| 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 Simsa | 3b91302 | 2015-02-27 15:19:25 -0800 | [diff] [blame] | 80 | Name string "v.io/x/ref/examples/rps.PlayerAction" |
Robin Thellend | 636b360 | 2015-02-11 14:57:44 -0800 | [diff] [blame] | 81 | Type PlayerAction |
| 82 | Union struct { |
| 83 | Move PlayerActionMove |
| 84 | Quit PlayerActionQuit |
| 85 | } |
| 86 | } |
| 87 | ) |
| 88 | |
| 89 | func (x PlayerActionMove) Index() int { return 0 } |
| 90 | func (x PlayerActionMove) Interface() interface{} { return x.Value } |
| 91 | func (x PlayerActionMove) Name() string { return "Move" } |
| 92 | func (x PlayerActionMove) __VDLReflect(__PlayerActionReflect) {} |
| 93 | |
| 94 | func (x PlayerActionQuit) Index() int { return 1 } |
| 95 | func (x PlayerActionQuit) Interface() interface{} { return x.Value } |
| 96 | func (x PlayerActionQuit) Name() string { return "Quit" } |
| 97 | func (x PlayerActionQuit) __VDLReflect(__PlayerActionReflect) {} |
| 98 | |
| 99 | type unused struct { |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Robin Thellend | 636b360 | 2015-02-11 14:57:44 -0800 | [diff] [blame] | 102 | func (unused) __VDLReflect(struct { |
Jiri Simsa | 3b91302 | 2015-02-27 15:19:25 -0800 | [diff] [blame] | 103 | Name string "v.io/x/ref/examples/rps.unused" |
Todd Wang | 24983c7 | 2014-11-26 15:07:25 -0800 | [diff] [blame] | 104 | }) { |
| 105 | } |
| 106 | |
Robin Thellend | 636b360 | 2015-02-11 14:57:44 -0800 | [diff] [blame] | 107 | type ( |
| 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 Simsa | 3b91302 | 2015-02-27 15:19:25 -0800 | [diff] [blame] | 131 | Name string "v.io/x/ref/examples/rps.JudgeAction" |
Robin Thellend | 636b360 | 2015-02-11 14:57:44 -0800 | [diff] [blame] | 132 | Type JudgeAction |
| 133 | Union struct { |
| 134 | PlayerNum JudgeActionPlayerNum |
| 135 | OpponentName JudgeActionOpponentName |
| 136 | MoveOptions JudgeActionMoveOptions |
| 137 | RoundResult JudgeActionRoundResult |
| 138 | Score JudgeActionScore |
| 139 | } |
| 140 | } |
| 141 | ) |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 142 | |
Robin Thellend | 636b360 | 2015-02-11 14:57:44 -0800 | [diff] [blame] | 143 | func (x JudgeActionPlayerNum) Index() int { return 0 } |
| 144 | func (x JudgeActionPlayerNum) Interface() interface{} { return x.Value } |
| 145 | func (x JudgeActionPlayerNum) Name() string { return "PlayerNum" } |
| 146 | func (x JudgeActionPlayerNum) __VDLReflect(__JudgeActionReflect) {} |
| 147 | |
| 148 | func (x JudgeActionOpponentName) Index() int { return 1 } |
| 149 | func (x JudgeActionOpponentName) Interface() interface{} { return x.Value } |
| 150 | func (x JudgeActionOpponentName) Name() string { return "OpponentName" } |
| 151 | func (x JudgeActionOpponentName) __VDLReflect(__JudgeActionReflect) {} |
| 152 | |
| 153 | func (x JudgeActionMoveOptions) Index() int { return 2 } |
| 154 | func (x JudgeActionMoveOptions) Interface() interface{} { return x.Value } |
| 155 | func (x JudgeActionMoveOptions) Name() string { return "MoveOptions" } |
| 156 | func (x JudgeActionMoveOptions) __VDLReflect(__JudgeActionReflect) {} |
| 157 | |
| 158 | func (x JudgeActionRoundResult) Index() int { return 3 } |
| 159 | func (x JudgeActionRoundResult) Interface() interface{} { return x.Value } |
| 160 | func (x JudgeActionRoundResult) Name() string { return "RoundResult" } |
| 161 | func (x JudgeActionRoundResult) __VDLReflect(__JudgeActionReflect) {} |
| 162 | |
| 163 | func (x JudgeActionScore) Index() int { return 4 } |
| 164 | func (x JudgeActionScore) Interface() interface{} { return x.Value } |
| 165 | func (x JudgeActionScore) Name() string { return "Score" } |
| 166 | func (x JudgeActionScore) __VDLReflect(__JudgeActionReflect) {} |
Todd Wang | 24983c7 | 2014-11-26 15:07:25 -0800 | [diff] [blame] | 167 | |
Sergey Rogulenko | 2f64f98 | 2015-02-05 14:14:46 -0800 | [diff] [blame] | 168 | type PlayersMoves [2]string |
| 169 | |
| 170 | func (PlayersMoves) __VDLReflect(struct { |
Jiri Simsa | 3b91302 | 2015-02-27 15:19:25 -0800 | [diff] [blame] | 171 | Name string "v.io/x/ref/examples/rps.PlayersMoves" |
Sergey Rogulenko | 2f64f98 | 2015-02-05 14:14:46 -0800 | [diff] [blame] | 172 | }) { |
| 173 | } |
| 174 | |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 175 | // Round represents the state of a round. |
| 176 | type Round struct { |
Todd Wang | 07f1346 | 2015-03-13 15:54:15 -0700 | [diff] [blame] | 177 | 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 Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Todd Wang | 24983c7 | 2014-11-26 15:07:25 -0800 | [diff] [blame] | 184 | func (Round) __VDLReflect(struct { |
Jiri Simsa | 3b91302 | 2015-02-27 15:19:25 -0800 | [diff] [blame] | 185 | Name string "v.io/x/ref/examples/rps.Round" |
Todd Wang | 24983c7 | 2014-11-26 15:07:25 -0800 | [diff] [blame] | 186 | }) { |
| 187 | } |
| 188 | |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 189 | // 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. |
| 191 | type WinnerTag byte |
| 192 | |
Todd Wang | 24983c7 | 2014-11-26 15:07:25 -0800 | [diff] [blame] | 193 | func (WinnerTag) __VDLReflect(struct { |
Jiri Simsa | 3b91302 | 2015-02-27 15:19:25 -0800 | [diff] [blame] | 194 | Name string "v.io/x/ref/examples/rps.WinnerTag" |
Todd Wang | 24983c7 | 2014-11-26 15:07:25 -0800 | [diff] [blame] | 195 | }) { |
| 196 | } |
| 197 | |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 198 | // PlayResult is the value returned by the Play method. It indicates the outcome of the game. |
| 199 | type PlayResult struct { |
| 200 | YouWon bool // True if the player receiving the result won the game. |
| 201 | } |
| 202 | |
Todd Wang | 24983c7 | 2014-11-26 15:07:25 -0800 | [diff] [blame] | 203 | func (PlayResult) __VDLReflect(struct { |
Jiri Simsa | 3b91302 | 2015-02-27 15:19:25 -0800 | [diff] [blame] | 204 | Name string "v.io/x/ref/examples/rps.PlayResult" |
Todd Wang | 24983c7 | 2014-11-26 15:07:25 -0800 | [diff] [blame] | 205 | }) { |
| 206 | } |
| 207 | |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 208 | type ScoreCard struct { |
Todd Wang | 07f1346 | 2015-03-13 15:54:15 -0700 | [diff] [blame] | 209 | 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 Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Todd Wang | 24983c7 | 2014-11-26 15:07:25 -0800 | [diff] [blame] | 218 | func (ScoreCard) __VDLReflect(struct { |
Jiri Simsa | 3b91302 | 2015-02-27 15:19:25 -0800 | [diff] [blame] | 219 | Name string "v.io/x/ref/examples/rps.ScoreCard" |
Todd Wang | 24983c7 | 2014-11-26 15:07:25 -0800 | [diff] [blame] | 220 | }) { |
| 221 | } |
| 222 | |
| 223 | func init() { |
Benjamin Prosnitz | 2be28dc | 2015-03-11 13:33:22 -0700 | [diff] [blame] | 224 | vdl.Register((*GameId)(nil)) |
Todd Wang | 83f63ba | 2015-02-09 13:30:43 -0800 | [diff] [blame] | 225 | vdl.Register((*GameOptions)(nil)) |
| 226 | vdl.Register((*GameTypeTag)(nil)) |
| 227 | vdl.Register((*PlayerAction)(nil)) |
Robin Thellend | 636b360 | 2015-02-11 14:57:44 -0800 | [diff] [blame] | 228 | vdl.Register((*unused)(nil)) |
Todd Wang | 83f63ba | 2015-02-09 13:30:43 -0800 | [diff] [blame] | 229 | 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 Wang | 24983c7 | 2014-11-26 15:07:25 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 237 | const Classic = GameTypeTag(0) // Rock-Paper-Scissors |
| 238 | |
| 239 | const LizardSpock = GameTypeTag(1) // Rock-Paper-Scissors-Lizard-Spock |
| 240 | |
| 241 | const Draw = WinnerTag(0) |
| 242 | |
| 243 | const Player1 = WinnerTag(1) |
| 244 | |
| 245 | const Player2 = WinnerTag(2) |
| 246 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 247 | // JudgeClientMethods is the client interface |
| 248 | // containing Judge methods. |
| 249 | type JudgeClientMethods interface { |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 250 | // 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 Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 252 | CreateGame(ctx *context.T, Opts GameOptions, opts ...rpc.CallOpt) (GameId, error) |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 253 | // Play lets a player join an existing game and play. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 254 | Play(ctx *context.T, Id GameId, opts ...rpc.CallOpt) (JudgePlayClientCall, error) |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 257 | // JudgeClientStub adds universal methods to JudgeClientMethods. |
| 258 | type JudgeClientStub interface { |
| 259 | JudgeClientMethods |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 260 | rpc.UniversalServiceMethods |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 263 | // JudgeClient returns a client stub for Judge. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 264 | func JudgeClient(name string, opts ...rpc.BindOpt) JudgeClientStub { |
| 265 | var client rpc.Client |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 266 | for _, opt := range opts { |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 267 | if clientOpt, ok := opt.(rpc.Client); ok { |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 268 | client = clientOpt |
| 269 | } |
| 270 | } |
| 271 | return implJudgeClientStub{name, client} |
| 272 | } |
| 273 | |
| 274 | type implJudgeClientStub struct { |
| 275 | name string |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 276 | client rpc.Client |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 279 | func (c implJudgeClientStub) c(ctx *context.T) rpc.Client { |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 280 | if c.client != nil { |
| 281 | return c.client |
| 282 | } |
Jiri Simsa | 386ce3e | 2015-02-23 16:14:20 -0800 | [diff] [blame] | 283 | return v23.GetClient(ctx) |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 286 | func (c implJudgeClientStub) CreateGame(ctx *context.T, i0 GameOptions, opts ...rpc.CallOpt) (o0 GameId, err error) { |
| 287 | var call rpc.ClientCall |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 288 | if call, err = c.c(ctx).StartCall(ctx, c.name, "CreateGame", []interface{}{i0}, opts...); err != nil { |
| 289 | return |
| 290 | } |
Todd Wang | 8f9f7f1 | 2015-02-18 13:21:09 -0800 | [diff] [blame] | 291 | err = call.Finish(&o0) |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 292 | return |
| 293 | } |
| 294 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 295 | func (c implJudgeClientStub) Play(ctx *context.T, i0 GameId, opts ...rpc.CallOpt) (ocall JudgePlayClientCall, err error) { |
| 296 | var call rpc.ClientCall |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 297 | if call, err = c.c(ctx).StartCall(ctx, c.name, "Play", []interface{}{i0}, opts...); err != nil { |
| 298 | return |
| 299 | } |
Matt Rosencrantz | 72a1561 | 2015-02-27 22:22:41 -0800 | [diff] [blame] | 300 | ocall = &implJudgePlayClientCall{ClientCall: call} |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 301 | return |
| 302 | } |
| 303 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 304 | // JudgePlayClientStream is the client stream for Judge.Play. |
| 305 | type JudgePlayClientStream interface { |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 306 | // RecvStream returns the receiver side of the Judge.Play client stream. |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 307 | RecvStream() interface { |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 308 | // 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 Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 311 | Advance() bool |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 312 | // Value returns the item that was staged by Advance. May panic if Advance |
| 313 | // returned false or was not called. Never blocks. |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 314 | Value() JudgeAction |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 315 | // Err returns any error encountered by Advance. Never blocks. |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 316 | Err() error |
| 317 | } |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 318 | // SendStream returns the send side of the Judge.Play client stream. |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 319 | SendStream() interface { |
Todd Wang | 04f97dd | 2014-12-18 18:52:07 -0800 | [diff] [blame] | 320 | // 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 Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 325 | Send(item PlayerAction) error |
Todd Wang | 04f97dd | 2014-12-18 18:52:07 -0800 | [diff] [blame] | 326 | // 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 Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 333 | Close() error |
| 334 | } |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 335 | } |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 336 | |
Matt Rosencrantz | 72a1561 | 2015-02-27 22:22:41 -0800 | [diff] [blame] | 337 | // JudgePlayClientCall represents the call returned from Judge.Play. |
| 338 | type JudgePlayClientCall interface { |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 339 | 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 Wang | 04f97dd | 2014-12-18 18:52:07 -0800 | [diff] [blame] | 343 | // Finish returns immediately if the call has been canceled; depending on the |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 344 | // timing the output could either be an error signaling cancelation, or the |
| 345 | // valid positional return values from the server. |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 346 | // |
Todd Wang | 04f97dd | 2014-12-18 18:52:07 -0800 | [diff] [blame] | 347 | // 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 Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 349 | // be called at most once. |
| 350 | Finish() (PlayResult, error) |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Matt Rosencrantz | 72a1561 | 2015-02-27 22:22:41 -0800 | [diff] [blame] | 353 | type implJudgePlayClientCall struct { |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 354 | rpc.ClientCall |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 355 | valRecv JudgeAction |
| 356 | errRecv error |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Matt Rosencrantz | 72a1561 | 2015-02-27 22:22:41 -0800 | [diff] [blame] | 359 | func (c *implJudgePlayClientCall) RecvStream() interface { |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 360 | Advance() bool |
| 361 | Value() JudgeAction |
| 362 | Err() error |
| 363 | } { |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 364 | return implJudgePlayClientCallRecv{c} |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 367 | type implJudgePlayClientCallRecv struct { |
Matt Rosencrantz | 72a1561 | 2015-02-27 22:22:41 -0800 | [diff] [blame] | 368 | c *implJudgePlayClientCall |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 369 | } |
| 370 | |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 371 | func (c implJudgePlayClientCallRecv) Advance() bool { |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 372 | c.c.errRecv = c.c.Recv(&c.c.valRecv) |
| 373 | return c.c.errRecv == nil |
| 374 | } |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 375 | func (c implJudgePlayClientCallRecv) Value() JudgeAction { |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 376 | return c.c.valRecv |
| 377 | } |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 378 | func (c implJudgePlayClientCallRecv) Err() error { |
Todd Wang | d1779a5 | 2015-02-05 11:46:12 -0800 | [diff] [blame] | 379 | if c.c.errRecv == io.EOF { |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 380 | return nil |
| 381 | } |
| 382 | return c.c.errRecv |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 383 | } |
Matt Rosencrantz | 72a1561 | 2015-02-27 22:22:41 -0800 | [diff] [blame] | 384 | func (c *implJudgePlayClientCall) SendStream() interface { |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 385 | Send(item PlayerAction) error |
| 386 | Close() error |
| 387 | } { |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 388 | return implJudgePlayClientCallSend{c} |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 389 | } |
| 390 | |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 391 | type implJudgePlayClientCallSend struct { |
Matt Rosencrantz | 72a1561 | 2015-02-27 22:22:41 -0800 | [diff] [blame] | 392 | c *implJudgePlayClientCall |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 395 | func (c implJudgePlayClientCallSend) Send(item PlayerAction) error { |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 396 | return c.c.Send(item) |
| 397 | } |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 398 | func (c implJudgePlayClientCallSend) Close() error { |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 399 | return c.c.CloseSend() |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 400 | } |
Matt Rosencrantz | 72a1561 | 2015-02-27 22:22:41 -0800 | [diff] [blame] | 401 | func (c *implJudgePlayClientCall) Finish() (o0 PlayResult, err error) { |
| 402 | err = c.ClientCall.Finish(&o0) |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 403 | return |
| 404 | } |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 405 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 406 | // JudgeServerMethods is the interface a server writer |
| 407 | // implements for Judge. |
| 408 | type 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 Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 411 | CreateGame(call rpc.ServerCall, Opts GameOptions) (GameId, error) |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 412 | // Play lets a player join an existing game and play. |
Benjamin Prosnitz | 2be28dc | 2015-03-11 13:33:22 -0700 | [diff] [blame] | 413 | Play(call JudgePlayServerCall, Id GameId) (PlayResult, error) |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 416 | // JudgeServerStubMethods is the server interface containing |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 417 | // Judge methods, as expected by rpc.Server. |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 418 | // The only difference between this interface and JudgeServerMethods |
| 419 | // is the streaming methods. |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 420 | type 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 Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 423 | CreateGame(call rpc.ServerCall, Opts GameOptions) (GameId, error) |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 424 | // Play lets a player join an existing game and play. |
Benjamin Prosnitz | 2be28dc | 2015-03-11 13:33:22 -0700 | [diff] [blame] | 425 | Play(call *JudgePlayServerCallStub, Id GameId) (PlayResult, error) |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 428 | // JudgeServerStub adds universal methods to JudgeServerStubMethods. |
| 429 | type JudgeServerStub interface { |
| 430 | JudgeServerStubMethods |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 431 | // Describe the Judge interfaces. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 432 | Describe__() []rpc.InterfaceDesc |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 435 | // JudgeServer returns a server stub for Judge. |
| 436 | // It converts an implementation of JudgeServerMethods into |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 437 | // an object that may be used by rpc.Server. |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 438 | func JudgeServer(impl JudgeServerMethods) JudgeServerStub { |
| 439 | stub := implJudgeServerStub{ |
| 440 | impl: impl, |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 441 | } |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 442 | // 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 Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 444 | if gs := rpc.NewGlobState(stub); gs != nil { |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 445 | stub.gs = gs |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 446 | } else if gs := rpc.NewGlobState(impl); gs != nil { |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 447 | stub.gs = gs |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 448 | } |
Robin Thellend | a754845 | 2014-11-05 18:10:24 -0800 | [diff] [blame] | 449 | return stub |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 452 | type implJudgeServerStub struct { |
| 453 | impl JudgeServerMethods |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 454 | gs *rpc.GlobState |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 457 | func (s implJudgeServerStub) CreateGame(call rpc.ServerCall, i0 GameOptions) (GameId, error) { |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 458 | return s.impl.CreateGame(call, i0) |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Benjamin Prosnitz | 2be28dc | 2015-03-11 13:33:22 -0700 | [diff] [blame] | 461 | func (s implJudgeServerStub) Play(call *JudgePlayServerCallStub, i0 GameId) (PlayResult, error) { |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 462 | return s.impl.Play(call, i0) |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 465 | func (s implJudgeServerStub) Globber() *rpc.GlobState { |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 466 | return s.gs |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 469 | func (s implJudgeServerStub) Describe__() []rpc.InterfaceDesc { |
| 470 | return []rpc.InterfaceDesc{JudgeDesc} |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | // JudgeDesc describes the Judge interface. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 474 | var JudgeDesc rpc.InterfaceDesc = descJudge |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 475 | |
| 476 | // descJudge hides the desc to keep godoc clean. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 477 | var descJudge = rpc.InterfaceDesc{ |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 478 | Name: "Judge", |
Jiri Simsa | 3b91302 | 2015-02-27 15:19:25 -0800 | [diff] [blame] | 479 | PkgPath: "v.io/x/ref/examples/rps", |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 480 | Methods: []rpc.MethodDesc{ |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 481 | { |
| 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 Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 484 | InArgs: []rpc.ArgDesc{ |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 485 | {"Opts", ``}, // GameOptions |
| 486 | }, |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 487 | OutArgs: []rpc.ArgDesc{ |
Benjamin Prosnitz | 2be28dc | 2015-03-11 13:33:22 -0700 | [diff] [blame] | 488 | {"", ``}, // GameId |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 489 | }, |
Todd Wang | cab990d | 2015-02-19 19:54:06 -0800 | [diff] [blame] | 490 | Tags: []*vdl.Value{vdl.ValueOf(access.Tag("Write"))}, |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 491 | }, |
| 492 | { |
| 493 | Name: "Play", |
| 494 | Doc: "// Play lets a player join an existing game and play.", |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 495 | InArgs: []rpc.ArgDesc{ |
Benjamin Prosnitz | 2be28dc | 2015-03-11 13:33:22 -0700 | [diff] [blame] | 496 | {"Id", ``}, // GameId |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 497 | }, |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 498 | OutArgs: []rpc.ArgDesc{ |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 499 | {"", ``}, // PlayResult |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 500 | }, |
Todd Wang | cab990d | 2015-02-19 19:54:06 -0800 | [diff] [blame] | 501 | Tags: []*vdl.Value{vdl.ValueOf(access.Tag("Write"))}, |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 502 | }, |
| 503 | }, |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 506 | // JudgePlayServerStream is the server stream for Judge.Play. |
| 507 | type JudgePlayServerStream interface { |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 508 | // RecvStream returns the receiver side of the Judge.Play server stream. |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 509 | 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 Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 519 | } |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 520 | // SendStream returns the send side of the Judge.Play server stream. |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 521 | 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 Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 526 | } |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 529 | // JudgePlayServerCall represents the context passed to Judge.Play. |
| 530 | type JudgePlayServerCall interface { |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 531 | rpc.ServerCall |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 532 | JudgePlayServerStream |
Robin Thellend | a754845 | 2014-11-05 18:10:24 -0800 | [diff] [blame] | 533 | } |
| 534 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 535 | // JudgePlayServerCallStub is a wrapper that converts rpc.StreamServerCall into |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 536 | // a typesafe stub that implements JudgePlayServerCall. |
| 537 | type JudgePlayServerCallStub struct { |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 538 | rpc.StreamServerCall |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 539 | valRecv PlayerAction |
| 540 | errRecv error |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 543 | // Init initializes JudgePlayServerCallStub from rpc.StreamServerCall. |
| 544 | func (s *JudgePlayServerCallStub) Init(call rpc.StreamServerCall) { |
Matt Rosencrantz | 9ef0f2d | 2015-02-27 11:04:32 -0800 | [diff] [blame] | 545 | s.StreamServerCall = call |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 548 | // RecvStream returns the receiver side of the Judge.Play server stream. |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 549 | func (s *JudgePlayServerCallStub) RecvStream() interface { |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 550 | Advance() bool |
| 551 | Value() PlayerAction |
| 552 | Err() error |
| 553 | } { |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 554 | return implJudgePlayServerCallRecv{s} |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 555 | } |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 556 | |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 557 | type implJudgePlayServerCallRecv struct { |
| 558 | s *JudgePlayServerCallStub |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 559 | } |
| 560 | |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 561 | func (s implJudgePlayServerCallRecv) Advance() bool { |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 562 | s.s.errRecv = s.s.Recv(&s.s.valRecv) |
| 563 | return s.s.errRecv == nil |
| 564 | } |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 565 | func (s implJudgePlayServerCallRecv) Value() PlayerAction { |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 566 | return s.s.valRecv |
| 567 | } |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 568 | func (s implJudgePlayServerCallRecv) Err() error { |
Todd Wang | d1779a5 | 2015-02-05 11:46:12 -0800 | [diff] [blame] | 569 | if s.s.errRecv == io.EOF { |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 570 | return nil |
| 571 | } |
| 572 | return s.s.errRecv |
| 573 | } |
| 574 | |
| 575 | // SendStream returns the send side of the Judge.Play server stream. |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 576 | func (s *JudgePlayServerCallStub) SendStream() interface { |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 577 | Send(item JudgeAction) error |
| 578 | } { |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 579 | return implJudgePlayServerCallSend{s} |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 580 | } |
| 581 | |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 582 | type implJudgePlayServerCallSend struct { |
| 583 | s *JudgePlayServerCallStub |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 584 | } |
| 585 | |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 586 | func (s implJudgePlayServerCallSend) Send(item JudgeAction) error { |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 587 | return s.s.Send(item) |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | // PlayerClientMethods is the client interface |
| 591 | // containing Player methods. |
| 592 | // |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 593 | // Player can receive challenges from other players. |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 594 | type PlayerClientMethods interface { |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 595 | // Challenge is used by other players to challenge this player to a game. If |
| 596 | // the challenge is accepted, the method returns nil. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 597 | Challenge(ctx *context.T, Address string, Id GameId, Opts GameOptions, opts ...rpc.CallOpt) error |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 600 | // PlayerClientStub adds universal methods to PlayerClientMethods. |
| 601 | type PlayerClientStub interface { |
| 602 | PlayerClientMethods |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 603 | rpc.UniversalServiceMethods |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 606 | // PlayerClient returns a client stub for Player. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 607 | func PlayerClient(name string, opts ...rpc.BindOpt) PlayerClientStub { |
| 608 | var client rpc.Client |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 609 | for _, opt := range opts { |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 610 | if clientOpt, ok := opt.(rpc.Client); ok { |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 611 | client = clientOpt |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 612 | } |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 613 | } |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 614 | return implPlayerClientStub{name, client} |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 617 | type implPlayerClientStub struct { |
| 618 | name string |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 619 | client rpc.Client |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 622 | func (c implPlayerClientStub) c(ctx *context.T) rpc.Client { |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 623 | if c.client != nil { |
| 624 | return c.client |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 625 | } |
Jiri Simsa | 386ce3e | 2015-02-23 16:14:20 -0800 | [diff] [blame] | 626 | return v23.GetClient(ctx) |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 629 | func (c implPlayerClientStub) Challenge(ctx *context.T, i0 string, i1 GameId, i2 GameOptions, opts ...rpc.CallOpt) (err error) { |
| 630 | var call rpc.ClientCall |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 631 | if call, err = c.c(ctx).StartCall(ctx, c.name, "Challenge", []interface{}{i0, i1, i2}, opts...); err != nil { |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 632 | return |
| 633 | } |
Todd Wang | 8f9f7f1 | 2015-02-18 13:21:09 -0800 | [diff] [blame] | 634 | err = call.Finish() |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 635 | return |
| 636 | } |
| 637 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 638 | // PlayerServerMethods is the interface a server writer |
| 639 | // implements for Player. |
| 640 | // |
| 641 | // Player can receive challenges from other players. |
| 642 | type 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 Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 645 | Challenge(call rpc.ServerCall, Address string, Id GameId, Opts GameOptions) error |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 648 | // PlayerServerStubMethods is the server interface containing |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 649 | // Player methods, as expected by rpc.Server. |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 650 | // There is no difference between this interface and PlayerServerMethods |
| 651 | // since there are no streaming methods. |
| 652 | type PlayerServerStubMethods PlayerServerMethods |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 653 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 654 | // PlayerServerStub adds universal methods to PlayerServerStubMethods. |
| 655 | type PlayerServerStub interface { |
| 656 | PlayerServerStubMethods |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 657 | // Describe the Player interfaces. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 658 | Describe__() []rpc.InterfaceDesc |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | // PlayerServer returns a server stub for Player. |
| 662 | // It converts an implementation of PlayerServerMethods into |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 663 | // an object that may be used by rpc.Server. |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 664 | func 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 Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 670 | if gs := rpc.NewGlobState(stub); gs != nil { |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 671 | stub.gs = gs |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 672 | } else if gs := rpc.NewGlobState(impl); gs != nil { |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 673 | stub.gs = gs |
| 674 | } |
| 675 | return stub |
| 676 | } |
| 677 | |
| 678 | type implPlayerServerStub struct { |
| 679 | impl PlayerServerMethods |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 680 | gs *rpc.GlobState |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 681 | } |
| 682 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 683 | func (s implPlayerServerStub) Challenge(call rpc.ServerCall, i0 string, i1 GameId, i2 GameOptions) error { |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 684 | return s.impl.Challenge(call, i0, i1, i2) |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 685 | } |
| 686 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 687 | func (s implPlayerServerStub) Globber() *rpc.GlobState { |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 688 | return s.gs |
| 689 | } |
| 690 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 691 | func (s implPlayerServerStub) Describe__() []rpc.InterfaceDesc { |
| 692 | return []rpc.InterfaceDesc{PlayerDesc} |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | // PlayerDesc describes the Player interface. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 696 | var PlayerDesc rpc.InterfaceDesc = descPlayer |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 697 | |
| 698 | // descPlayer hides the desc to keep godoc clean. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 699 | var descPlayer = rpc.InterfaceDesc{ |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 700 | Name: "Player", |
Jiri Simsa | 3b91302 | 2015-02-27 15:19:25 -0800 | [diff] [blame] | 701 | PkgPath: "v.io/x/ref/examples/rps", |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 702 | Doc: "// Player can receive challenges from other players.", |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 703 | Methods: []rpc.MethodDesc{ |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 704 | { |
| 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 Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 707 | InArgs: []rpc.ArgDesc{ |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 708 | {"Address", ``}, // string |
Benjamin Prosnitz | 2be28dc | 2015-03-11 13:33:22 -0700 | [diff] [blame] | 709 | {"Id", ``}, // GameId |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 710 | {"Opts", ``}, // GameOptions |
| 711 | }, |
Todd Wang | cab990d | 2015-02-19 19:54:06 -0800 | [diff] [blame] | 712 | Tags: []*vdl.Value{vdl.ValueOf(access.Tag("Write"))}, |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 713 | }, |
| 714 | }, |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 717 | // ScoreKeeperClientMethods is the client interface |
| 718 | // containing ScoreKeeper methods. |
| 719 | // |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 720 | // ScoreKeeper receives the outcome of games from Judges. |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 721 | type ScoreKeeperClientMethods interface { |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 722 | Record(ctx *context.T, Score ScoreCard, opts ...rpc.CallOpt) error |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 725 | // ScoreKeeperClientStub adds universal methods to ScoreKeeperClientMethods. |
| 726 | type ScoreKeeperClientStub interface { |
| 727 | ScoreKeeperClientMethods |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 728 | rpc.UniversalServiceMethods |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 729 | } |
| 730 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 731 | // ScoreKeeperClient returns a client stub for ScoreKeeper. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 732 | func ScoreKeeperClient(name string, opts ...rpc.BindOpt) ScoreKeeperClientStub { |
| 733 | var client rpc.Client |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 734 | for _, opt := range opts { |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 735 | if clientOpt, ok := opt.(rpc.Client); ok { |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 736 | client = clientOpt |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 737 | } |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 738 | } |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 739 | return implScoreKeeperClientStub{name, client} |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 740 | } |
| 741 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 742 | type implScoreKeeperClientStub struct { |
| 743 | name string |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 744 | client rpc.Client |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 747 | func (c implScoreKeeperClientStub) c(ctx *context.T) rpc.Client { |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 748 | if c.client != nil { |
| 749 | return c.client |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 750 | } |
Jiri Simsa | 386ce3e | 2015-02-23 16:14:20 -0800 | [diff] [blame] | 751 | return v23.GetClient(ctx) |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 754 | func (c implScoreKeeperClientStub) Record(ctx *context.T, i0 ScoreCard, opts ...rpc.CallOpt) (err error) { |
| 755 | var call rpc.ClientCall |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 756 | if call, err = c.c(ctx).StartCall(ctx, c.name, "Record", []interface{}{i0}, opts...); err != nil { |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 757 | return |
| 758 | } |
Todd Wang | 8f9f7f1 | 2015-02-18 13:21:09 -0800 | [diff] [blame] | 759 | err = call.Finish() |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 760 | return |
| 761 | } |
| 762 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 763 | // ScoreKeeperServerMethods is the interface a server writer |
| 764 | // implements for ScoreKeeper. |
| 765 | // |
| 766 | // ScoreKeeper receives the outcome of games from Judges. |
| 767 | type ScoreKeeperServerMethods interface { |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 768 | Record(call rpc.ServerCall, Score ScoreCard) error |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 771 | // ScoreKeeperServerStubMethods is the server interface containing |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 772 | // ScoreKeeper methods, as expected by rpc.Server. |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 773 | // There is no difference between this interface and ScoreKeeperServerMethods |
| 774 | // since there are no streaming methods. |
| 775 | type ScoreKeeperServerStubMethods ScoreKeeperServerMethods |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 776 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 777 | // ScoreKeeperServerStub adds universal methods to ScoreKeeperServerStubMethods. |
| 778 | type ScoreKeeperServerStub interface { |
| 779 | ScoreKeeperServerStubMethods |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 780 | // Describe the ScoreKeeper interfaces. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 781 | Describe__() []rpc.InterfaceDesc |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | // ScoreKeeperServer returns a server stub for ScoreKeeper. |
| 785 | // It converts an implementation of ScoreKeeperServerMethods into |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 786 | // an object that may be used by rpc.Server. |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 787 | func 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 Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 793 | if gs := rpc.NewGlobState(stub); gs != nil { |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 794 | stub.gs = gs |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 795 | } else if gs := rpc.NewGlobState(impl); gs != nil { |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 796 | stub.gs = gs |
| 797 | } |
| 798 | return stub |
| 799 | } |
| 800 | |
| 801 | type implScoreKeeperServerStub struct { |
| 802 | impl ScoreKeeperServerMethods |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 803 | gs *rpc.GlobState |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 804 | } |
| 805 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 806 | func (s implScoreKeeperServerStub) Record(call rpc.ServerCall, i0 ScoreCard) error { |
Suharsh Sivakumar | 31f4985 | 2015-03-03 16:13:20 -0800 | [diff] [blame] | 807 | return s.impl.Record(call, i0) |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 808 | } |
| 809 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 810 | func (s implScoreKeeperServerStub) Globber() *rpc.GlobState { |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 811 | return s.gs |
| 812 | } |
| 813 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 814 | func (s implScoreKeeperServerStub) Describe__() []rpc.InterfaceDesc { |
| 815 | return []rpc.InterfaceDesc{ScoreKeeperDesc} |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | // ScoreKeeperDesc describes the ScoreKeeper interface. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 819 | var ScoreKeeperDesc rpc.InterfaceDesc = descScoreKeeper |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 820 | |
| 821 | // descScoreKeeper hides the desc to keep godoc clean. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 822 | var descScoreKeeper = rpc.InterfaceDesc{ |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 823 | Name: "ScoreKeeper", |
Jiri Simsa | 3b91302 | 2015-02-27 15:19:25 -0800 | [diff] [blame] | 824 | PkgPath: "v.io/x/ref/examples/rps", |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 825 | Doc: "// ScoreKeeper receives the outcome of games from Judges.", |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 826 | Methods: []rpc.MethodDesc{ |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 827 | { |
| 828 | Name: "Record", |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 829 | InArgs: []rpc.ArgDesc{ |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 830 | {"Score", ``}, // ScoreCard |
| 831 | }, |
Todd Wang | cab990d | 2015-02-19 19:54:06 -0800 | [diff] [blame] | 832 | Tags: []*vdl.Value{vdl.ValueOf(access.Tag("Write"))}, |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 833 | }, |
| 834 | }, |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 837 | // RockPaperScissorsClientMethods is the client interface |
| 838 | // containing RockPaperScissors methods. |
| 839 | type RockPaperScissorsClientMethods interface { |
| 840 | JudgeClientMethods |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 841 | // Player can receive challenges from other players. |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 842 | PlayerClientMethods |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 843 | // ScoreKeeper receives the outcome of games from Judges. |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 844 | ScoreKeeperClientMethods |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 845 | } |
| 846 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 847 | // RockPaperScissorsClientStub adds universal methods to RockPaperScissorsClientMethods. |
| 848 | type RockPaperScissorsClientStub interface { |
| 849 | RockPaperScissorsClientMethods |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 850 | rpc.UniversalServiceMethods |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 851 | } |
| 852 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 853 | // RockPaperScissorsClient returns a client stub for RockPaperScissors. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 854 | func RockPaperScissorsClient(name string, opts ...rpc.BindOpt) RockPaperScissorsClientStub { |
| 855 | var client rpc.Client |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 856 | for _, opt := range opts { |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 857 | if clientOpt, ok := opt.(rpc.Client); ok { |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 858 | client = clientOpt |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 859 | } |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 860 | } |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 861 | return implRockPaperScissorsClientStub{name, client, JudgeClient(name, client), PlayerClient(name, client), ScoreKeeperClient(name, client)} |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 862 | } |
| 863 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 864 | type implRockPaperScissorsClientStub struct { |
| 865 | name string |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 866 | client rpc.Client |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 867 | |
| 868 | JudgeClientStub |
| 869 | PlayerClientStub |
| 870 | ScoreKeeperClientStub |
| 871 | } |
| 872 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 873 | func (c implRockPaperScissorsClientStub) c(ctx *context.T) rpc.Client { |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 874 | if c.client != nil { |
| 875 | return c.client |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 876 | } |
Jiri Simsa | 386ce3e | 2015-02-23 16:14:20 -0800 | [diff] [blame] | 877 | return v23.GetClient(ctx) |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 878 | } |
| 879 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 880 | // RockPaperScissorsServerMethods is the interface a server writer |
| 881 | // implements for RockPaperScissors. |
| 882 | type 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 Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 891 | // RockPaperScissors methods, as expected by rpc.Server. |
Todd Wang | 5cd2735 | 2014-11-11 20:27:39 -0800 | [diff] [blame] | 892 | // The only difference between this interface and RockPaperScissorsServerMethods |
| 893 | // is the streaming methods. |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 894 | type 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. |
| 903 | type RockPaperScissorsServerStub interface { |
| 904 | RockPaperScissorsServerStubMethods |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 905 | // Describe the RockPaperScissors interfaces. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 906 | Describe__() []rpc.InterfaceDesc |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | // RockPaperScissorsServer returns a server stub for RockPaperScissors. |
| 910 | // It converts an implementation of RockPaperScissorsServerMethods into |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 911 | // an object that may be used by rpc.Server. |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 912 | func 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 Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 921 | if gs := rpc.NewGlobState(stub); gs != nil { |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 922 | stub.gs = gs |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 923 | } else if gs := rpc.NewGlobState(impl); gs != nil { |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 924 | stub.gs = gs |
| 925 | } |
Robin Thellend | a754845 | 2014-11-05 18:10:24 -0800 | [diff] [blame] | 926 | return stub |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 927 | } |
| 928 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 929 | type implRockPaperScissorsServerStub struct { |
| 930 | impl RockPaperScissorsServerMethods |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 931 | JudgeServerStub |
| 932 | PlayerServerStub |
| 933 | ScoreKeeperServerStub |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 934 | gs *rpc.GlobState |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 935 | } |
| 936 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 937 | func (s implRockPaperScissorsServerStub) Globber() *rpc.GlobState { |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 938 | return s.gs |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 941 | func (s implRockPaperScissorsServerStub) Describe__() []rpc.InterfaceDesc { |
| 942 | return []rpc.InterfaceDesc{RockPaperScissorsDesc, JudgeDesc, PlayerDesc, ScoreKeeperDesc} |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | // RockPaperScissorsDesc describes the RockPaperScissors interface. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 946 | var RockPaperScissorsDesc rpc.InterfaceDesc = descRockPaperScissors |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 947 | |
| 948 | // descRockPaperScissors hides the desc to keep godoc clean. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 949 | var descRockPaperScissors = rpc.InterfaceDesc{ |
Todd Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 950 | Name: "RockPaperScissors", |
Jiri Simsa | 3b91302 | 2015-02-27 15:19:25 -0800 | [diff] [blame] | 951 | PkgPath: "v.io/x/ref/examples/rps", |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame^] | 952 | Embeds: []rpc.EmbedDesc{ |
Jiri Simsa | 3b91302 | 2015-02-27 15:19:25 -0800 | [diff] [blame] | 953 | {"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 Wang | dddc1fb | 2014-11-16 21:39:02 -0800 | [diff] [blame] | 956 | }, |
Jiri Simsa | 37cdfc0 | 2014-10-09 18:00:43 -0700 | [diff] [blame] | 957 | } |