Skip to content
Browse files

uses a file to manage both global data and data to initialize

  • Loading branch information...
1 parent d864c74 commit fdd2f0d4b86483c614787f7f52cc1cbe22c3707e John Mac committed with Jun 20, 2010
Showing with 50 additions and 27 deletions.
  1. +2 −1 mongo/Makefile
  2. +48 −0 mongo/main.go
  3. +0 −26 mongo/tool.go
View
3 mongo/Makefile
@@ -2,14 +2,15 @@ include $(GOROOT)/src/Make.$(GOARCH)
TARG=mongo
GOFILES=\
+ main.go\
+\
connection.go\
database.go\
collection.go\
cursor.go\
message.go\
bson.go\
bson-struct.go\
- tool.go\
include $(GOROOT)/src/Make.pkg
View
48 mongo/main.go
@@ -0,0 +1,48 @@
+// Copyright 2009,2010 The 'gomongo' Authors. All rights reserved.
+// Use of this source code is governed by the New BSD License
+// that can be found in the LICENSE file.
+
+package mongo
+
+import (
+ "encoding/binary"
+ "rand"
+ crand "crypto/rand"
+)
+
+
+// Like BSON documents, all data in the mongo wire protocol is little-endian.
+var pack = binary.LittleEndian
+
+
+func init() {
+ // Uses the 'urandom' device to get a seed which will be used by 'rand'.
+ randombytes := make([]byte, 8)
+ if _, err := crand.Read(randombytes); err != nil {
+ panic("Pseudo-random source malfunction!")
+ }
+
+ random := binary.LittleEndian.Uint64(randombytes)
+ // If you seed it with something predictable like the time, the risk is obvious.
+ // rand.Seed(time.Nanoseconds())
+ rand.Seed(int64(random))
+}
+
+
+// *** Utility functions
+// ***
+
+func randToi32() int32 {
+ return rand.Int31()
+}
+
+// *** Bits data
+
+func setBit32(num *int32, position ...byte) {
+ const MASK = 1
+
+ for _, pos := range position {
+ *num |= MASK << pos
+ }
+}
+
View
26 mongo/tool.go
@@ -1,26 +0,0 @@
-// Copyright 2009,2010 The 'gomongo' Authors. All rights reserved.
-// Use of this source code is governed by the New BSD License
-// that can be found in the LICENSE file.
-
-package mongo
-
-import (
- "encoding/binary"
-)
-
-
-// Like BSON documents, all data in the mongo wire protocol is little-endian.
-var pack = binary.LittleEndian
-
-
-// *** Bits data
-// ***
-
-func setBit32(num *int32, position ...byte) {
- const MASK = 1
-
- for _, pos := range position {
- *num |= MASK << pos
- }
-}
-

0 comments on commit fdd2f0d

Please sign in to comment.
Something went wrong with that request. Please try again.