-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig.go
147 lines (139 loc) · 6.32 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) 2016 IBM Corp. All rights reserved.
// Use of this source code is governed by the Apache License,
// Version 2.0, a copy of which can be found in the LICENSE file.
// Configuration
package main
import (
"flag"
"fmt"
log "github.com/sirupsen/logrus"
)
const localSecret = "<LocalSecret>"
// A RoomConfig struct contains important, frequently-needed, information
// about our current room registration as well as our interaction with
// our Game On! server. Typically these values are collected from commandline
// arguments.
type RoomConfig struct {
// The basic address of the GameOn! server.
gameonAddr string
callbackAddr string
// The callback port is the externally-visible published port that
// that will receive websocket traffic from the GameOn! server.
callbackPort int
// The listening port is the port that our websocket server listens
// to internally. For example, if this code is placed into a
// container we could choose to always listen to one port, say 3000,
// and map that to a different host port.
listeningPort int
// This is the name of our room; use this name when connecting
// to our room from another using north, south, east and west.
roomName string
// Text descriptions of doors that connect us to other rooms.
// Although up and down are provided, the GameOn! server typically
// ignores any direction other than n,w,e or w.
north, south, east, west, up, down string
// If true, print a bunch of debugging information useful mostly
// to programmers.
debug bool
// This is the authorization id that was obtained during your
// GameOn! browser login. It might look something like this
// if you logged in using your Google ID:
// 'google:132043241444397884152'
id string
// This is the shared secret that was obtained during your GameOn!
// browser login. If you logged in using your Google ID it might
// look like this: 'LNIkaoiu62addlGp/rCZc7g,n3s9jUtOpXErr062kos='
secret string
localServer bool
timeShift int
retries int
secondsBetween int
// This is a room id and it is only used in the context of a
// delete request.
roomToDelete string
// The protocol to be used when talking to the game server.
protocol string
maxSecondsBetweenConversations int
}
// config is our single, package-wide, source of configuration data.
// (cmdling processing) ==> config ==> (used by code in this package)
var config RoomConfig
// Processes our commandline and establishes the contents of
// the package-wide config struct.
//
// Returns nil if successful or an error otherwise
func processCommandline() (err error) {
flag.StringVar(&config.gameonAddr, "g", "", "GameOn! server address")
flag.StringVar(&config.callbackAddr, "c", "", "Our published callback address")
flag.IntVar(&config.callbackPort, "cp", -1, "Our published callback port")
flag.IntVar(&config.listeningPort, "lp", -1, "Our listening port")
flag.StringVar(&config.roomName, "r", "", "Our room name.")
flag.BoolVar(&config.debug, "d", false, "Enables debug mode")
flag.StringVar(&config.north, "north", "A frost-covered door leads to the south.", "Describes the outside of our northern door")
flag.StringVar(&config.south, "south", "A moss-covered door leads to the north", "Describes the outside of our southern door")
flag.StringVar(&config.east, "east", "A badly-painted door opens to the west.", "Describes the outside of our eastern door")
flag.StringVar(&config.west, "west", "An old swinging door leads east.", "Describes the outside of our western door")
flag.StringVar(&config.up, "down", "There is a rickety set of steps leading up.", "Describes the outside/bottom of door in the floor. GameOn! often ignores this door")
flag.StringVar(&config.down, "up", "Heat eminates from an opening in the floor.", "Describes the outside/top of the hatch in the ceiling. GameOn! often ignores this door")
flag.StringVar(&config.id, "id", "", "The id associated with our shared secret.")
flag.StringVar(&config.secret, "secret", localSecret, "Our shared secret.")
flag.BoolVar(&config.localServer, "local", false, "We are using a local server. Local servers expect http://; remote servers expect https://")
flag.IntVar(&config.timeShift, "ts", 0, "The number of milleseconds to add or subtract from our timestamp so that we can better match the server clock")
flag.IntVar(&config.retries, "retries", 5, "The number of initial registration attempts.")
flag.IntVar(&config.secondsBetween, "between", 5, "The number of seconds between registration attempts.")
flag.StringVar(&config.roomToDelete, "delete", "", "Delete the room with this id and exit.")
flag.IntVar(&config.maxSecondsBetweenConversations, "quietTime", 60,
"The maimum number of seconds between randomly injected conversations.")
flag.Parse()
if config.gameonAddr == "" {
err = ArgError{"Missing Game-on server address."}
return
}
if len(config.roomToDelete) == 0 {
// This is not a deletion request so make sure the information
// we need to register a room and run the websocket server is valid.
if config.callbackAddr == "" {
err = ArgError{"Missing callback address."}
return
}
if config.callbackPort < 0 {
err = ArgError{"Missing or invalid callback port."}
return
}
if config.listeningPort < 0 {
// listening port defaults to callback port
config.listeningPort = config.callbackPort
}
if config.roomName == "" {
config.roomName = fmt.Sprintf("ROOM.%05d", config.callbackPort)
}
}
if config.localServer {
config.protocol = "http"
} else {
config.protocol = "https"
}
return
}
func printConfig(c *RoomConfig) {
log.Printf("gameonAddr=%s\n", config.gameonAddr)
// Many things are useless when we are just doing a delete.
if config.roomToDelete == "" {
log.Printf("callbackAddr=%s\n", config.callbackAddr)
log.Printf("callbackPort=%d\n", config.callbackPort)
log.Printf("listeningPort=%d\n", config.listeningPort)
log.Printf("roomName=%s\n", config.roomName)
log.Printf("north=%s\n", config.north)
log.Printf("south=%s\n", config.south)
log.Printf("east=%s\n", config.east)
log.Printf("west=%s\n", config.west)
}
log.Printf("debug=%v\n", config.debug)
log.Printf("roomToDelete=%v\n", config.roomToDelete)
log.Printf("localServer=%v\n", config.localServer)
log.Printf("timeShift=%d\n", config.timeShift)
if config.debug {
log.Printf("id=%s\n", config.id)
log.Printf("secret=%s\n", config.secret)
}
}