From f86bacbd69d1461f113eee2855d916c7fea0c131 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Mon, 24 Nov 2014 14:42:11 -0800 Subject: [PATCH] Used the idg.Generator from github --- control.go | 5 +++-- id.go | 43 ------------------------------------------- id_test.go | 19 ------------------- robot.go | 4 +++- 4 files changed, 6 insertions(+), 65 deletions(-) delete mode 100644 id.go delete mode 100644 id_test.go diff --git a/control.go b/control.go index a1b8985..286e3c9 100644 --- a/control.go +++ b/control.go @@ -12,6 +12,7 @@ import ( "strings" "sync" + "github.com/smcquay/idg" "golang.org/x/net/websocket" ) @@ -28,7 +29,7 @@ func (h JsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { // Controller is the shepherd of a collection of games. The main package in // server simply populates one of these and starts an http server. type Controller struct { - Idg *IdGenerator + Idg *idg.Generator Conf Config Games MapLock Memprofile string @@ -40,7 +41,7 @@ type Controller struct { // Controller. func NewController(conf Config, mprof, pprof string) *http.ServeMux { c := &Controller{ - Idg: NewIdGenerator(), + Idg: idg.NewGenerator(), Conf: conf, Games: MapLock{ M: make(map[string]*Game), diff --git a/id.go b/id.go deleted file mode 100644 index f6bf294..0000000 --- a/id.go +++ /dev/null @@ -1,43 +0,0 @@ -package server - -import ( - "crypto/md5" - "fmt" - "io" - "time" -) - -// This thing contains a channel that when initialized (see NewIdGenerator) -// will return a bunch of (as best as I can tell) unique md5 hashes. -// -// we use this for naming players, games, etc. -// -// It will consume a single goroutine -type IdGenerator struct { - id chan int64 -} - -func NewIdGenerator() *IdGenerator { - return &IdGenerator{ - id: make(chan int64), - } -} - -// Run is called (typically in a gorotine) to allow for queries to be made -// against IdGenerator.id throgh the Hash method. -func (idg *IdGenerator) Run() { - var i int64 - for i = 0; ; i++ { - idg.id <- i - } -} - -// Hash is the method used by a properly instantiated IdGenerator that gives -// fairly unique strings. They are currently truncated md5 hashes of the time -// plus a unique counter -func (id *IdGenerator) Hash() string { - h := md5.New() - ns := time.Now().UnixNano() + <-id.id - io.WriteString(h, fmt.Sprintf("%d", ns)) - return fmt.Sprintf("%x", h.Sum(nil))[:8] -} diff --git a/id_test.go b/id_test.go deleted file mode 100644 index 24b4bf2..0000000 --- a/id_test.go +++ /dev/null @@ -1,19 +0,0 @@ -package server - -import ( - "testing" -) - -func TestIDGenerator(t *testing.T) { - cache := make(map[string]bool) - var cur string - gg := NewIdGenerator() - go gg.Run() - for i := 0; i < 10000; i++ { - cur = gg.Hash() - if _, ok := cache[cur]; ok { - t.Errorf("unexpected duplicate key") - } - cache[cur] = true - } -} diff --git a/robot.go b/robot.go index 5a25d7f..f3a2484 100644 --- a/robot.go +++ b/robot.go @@ -5,6 +5,8 @@ import ( "math" "math/rand" + "github.com/smcquay/idg" + v "bitbucket.org/hackerbots/vector" ) @@ -34,7 +36,7 @@ type Robot struct { ProbeResult *Collision `json:"probe_result"` gameStats *BotStats `json:"-"` Delta float64 `json:"-"` - idg *IdGenerator + idg *idg.Generator } // Collision is basically a Point2d.