Explorar o código

niet te veel connecties tegelijkertijd WIP

Harry de Boer %!s(int64=10) %!d(string=hai) anos
pai
achega
2194608cfb
Modificáronse 3 ficheiros con 83 adicións e 62 borrados
  1. 15 1
      battlecamp-go-boardgenerator/main.go
  2. 56 59
      board/board.go
  3. 12 2
      main.go

+ 15 - 1
battlecamp-go-boardgenerator/main.go

@@ -1,16 +1,30 @@
 package main
 
 import (
+	"net"
+	"golang.org/x/net/netutil"
 	"battlecamp-go-server/board"
 	"log"
 	"net/http"
+	"os"
 	"strconv"
 )
 
 func main() {
+	logFile, err := os.OpenFile("server.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
+
+	if err == nil {
+		log.SetOutput(logFile)
+	} else {
+		log.Println("ERROR: Cannot open log file, using console.")
+		log.Printf("%v=n", err)
+	}
+
 	http.HandleFunc("/", generateBoard)
 	http.HandleFunc("/status", status)
-	log.Fatal(http.ListenAndServe(":8081", nil))
+	l, _ := net.Listen("tcp", ":8081")
+	limitListerer := netutil.LimitListener(l, 100)
+	log.Fatal(http.Serve(limitListerer, nil))
 }
 
 func generateBoard(w http.ResponseWriter, r *http.Request) {

+ 56 - 59
board/board.go

@@ -21,16 +21,16 @@ type Board struct {
 	data   []byte     `json:"-"`
 }
 
-type req struct {
-	x, y, width, height int
-}
-
 type response struct {
 	boardTile           io.ReadCloser
 	x, y, width, height int
 }
 
-const maxIceWidth = 26
+const (
+	maxIceWidth  = 26
+	regionWidth  = 32
+	regionHeight = 32
+)
 
 // Create a new randomly generated board.
 func New(width, height int) *Board {
@@ -43,60 +43,30 @@ func NewPartial(startX, startY, width, height, totalWidth, totalHeight int) *Boa
 }
 
 func NewRemote(width, height int) *Board {
-	b := newBlank(width, height)
-
 	log.Printf("Start creating board %v x %v", width, height)
+	b := newBlank(width, height)
 
-	const regionWidth = 32
-	const regionHeight = 32
-
-	regionWidthNum := (width + (regionWidth - 1)) / regionWidth
-	regionHeightNum := (height + (regionHeight - 1)) / regionHeight
-
-	returnChan := make(chan response)
-
-	for i := 0; i < regionWidthNum; i++ {
-		for j := 0; j < regionHeightNum; j++ {
-			var partialWidth, partialHeight int
-			x := i * regionWidth
-			y := j * regionHeight
-
-			if x+regionWidth > b.Width {
-				partialWidth = b.Width - x
-			} else {
-				partialWidth = regionWidth
-			}
-
-			if y+regionHeight > b.Height {
-				partialHeight = b.Height - y
-			} else {
-				partialHeight = regionHeight
-			}
+	xRegions := (width + (regionWidth - 1)) / regionWidth
+	yRegions := (height + (regionHeight - 1)) / regionHeight
 
-			region := req{
-				x:      x,
-				y:      y,
-				width:  partialWidth,
-				height: partialHeight,
-			}
-			go b.genRegion(region, returnChan)
-		}
-	}
+	returnChan := make(chan response, 32)
+	go b.genRegions(xRegions, yRegions, returnChan)
 
-	answers := 0
-	for regionWidthNum*regionHeightNum > answers {
+	for answers := 0; xRegions*yRegions > answers; answers++ {
 		result := <-returnChan
 		log.Printf("Received partial: x=%v, y=%v, width=%v, height=%v\n", result.x, result.y, result.width, result.height)
 		for ry := 0; ry < result.height; ry++ {
 			i, _ := b.xyToIndex(result.x, result.y+ry)
-			result.boardTile.Read(b.data[i : i+byteIndex(result.width)])
+			n, err := result.boardTile.Read(b.data[i : i+byteIndex(result.width)])
+			if err != nil {
+				log.Printf("ERROR reading after %v bytes: %v", n, err)
+			}
 		}
-		answers++
+		result.boardTile.Close()
 	}
 
 	b.placeIglo()
 	log.Printf("Finished creating board %v x %v\n", width, height)
-
 	return b
 }
 
@@ -110,20 +80,47 @@ func (b *Board) placeIglo() {
 	b.Set(b.Finish.X+1, b.Finish.Y, Water)
 }
 
-func (b *Board) genRegion(region req, responseChan chan response) {
-	requestUrl := fmt.Sprintf("http://localhost:8081/?totalWidth=%v&totalHeight=%v&width=%v&height=%v&x=%v&y=%v", b.Width, b.Height, region.width, region.height, region.x, region.y)
-	log.Printf("Requested generation of tile with url: %v\n", requestUrl)
-	resp, _ := http.Get(requestUrl)
-
-	result := response{
-		x:         region.x,
-		y:         region.y,
-		width:     region.width,
-		height:    region.height,
-		boardTile: resp.Body,
-	}
+func (b *Board) genRegions(xRegions, yRegions int, responseChan chan response) {
+
+	for i := 0; i < xRegions; i++ {
+		for j := 0; j < yRegions; j++ {
+			var partialWidth, partialHeight int
+			x := i * regionWidth
+			y := j * regionHeight
 
-	responseChan <- result
+			if x+regionWidth > b.Width {
+				partialWidth = b.Width - x
+			} else {
+				partialWidth = regionWidth
+			}
+
+			if y+regionHeight > b.Height {
+				partialHeight = b.Height - y
+			} else {
+				partialHeight = regionHeight
+			}
+
+			requestUrl := fmt.Sprintf("http://localhost:8081/?totalWidth=%v&totalHeight=%v&width=%v&height=%v&x=%v&y=%v", b.Width, b.Height, partialWidth, partialHeight, x, y)
+			log.Printf("Requested generation of tile with url: %v\n", requestUrl)
+
+			resp, err := http.Get(requestUrl)
+
+			if err != nil {
+				log.Printf("ERROR requesting region: %v", err)
+				continue
+			}
+
+			result := response{
+				x:         x,
+				y:         y,
+				width:     partialHeight,
+				height:    partialHeight,
+				boardTile: resp.Body,
+			}
+
+			responseChan <- result
+		}
+	}
 }
 
 func (b *Board) Set(x, y int, t TileType) {

+ 12 - 2
main.go

@@ -2,10 +2,11 @@
 package main
 
 import (
-	"time"
-	"math/rand"
 	"log"
+	"math/rand"
 	"net/http"
+	"os"
+	"time"
 
 	extStomp "github.com/go-stomp/stomp"
 
@@ -17,6 +18,15 @@ var currentGames games.GameServer = games.New()
 var stompConnection *extStomp.Conn
 
 func main() {
+	logFile, err := os.OpenFile("server.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
+
+	if err == nil {
+		log.SetOutput(logFile)
+	} else {
+		log.Println("ERROR: Cannot open log file, using console.")
+		log.Printf("%v=n", err)
+	}
+
 	rand.Seed(time.Now().UnixNano())
 	stompConnection = stomp.DailStomp()
 	defer stompConnection.Disconnect()