2015-07-26 16:33:29 +02:00
|
|
|
// Copyright 2014, 2015 by Sascha L. Teichmann
|
2014-09-18 07:52:37 +02:00
|
|
|
// Use of this source code is governed by the MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
2014-09-18 15:21:40 +02:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-09-21 17:30:19 +02:00
|
|
|
"image"
|
|
|
|
"image/draw"
|
2014-09-18 15:21:40 +02:00
|
|
|
"log"
|
2014-11-15 13:40:39 +01:00
|
|
|
"net"
|
2014-09-18 15:21:40 +02:00
|
|
|
"net/http"
|
2014-09-19 13:06:04 +02:00
|
|
|
"path/filepath"
|
2014-09-21 17:30:19 +02:00
|
|
|
"strconv"
|
2014-11-15 13:40:39 +01:00
|
|
|
"strings"
|
2014-09-19 13:06:04 +02:00
|
|
|
"sync"
|
|
|
|
|
2015-12-25 22:07:54 +01:00
|
|
|
"github.com/bamiaux/rez"
|
2014-09-21 17:30:19 +02:00
|
|
|
|
2014-11-15 13:40:39 +01:00
|
|
|
"bytes"
|
|
|
|
|
2014-10-03 12:07:53 +02:00
|
|
|
"bitbucket.org/s_l_teichmann/mtsatellite/common"
|
2014-09-19 13:06:04 +02:00
|
|
|
)
|
|
|
|
|
2015-03-02 13:10:30 +01:00
|
|
|
type baseTilesUpdates interface {
|
|
|
|
BaseTilesUpdated(map[xz]bool)
|
|
|
|
}
|
|
|
|
|
2014-09-18 07:52:37 +02:00
|
|
|
type tileUpdater struct {
|
2014-09-19 13:06:04 +02:00
|
|
|
changes map[xz]bool
|
2015-03-02 13:10:30 +01:00
|
|
|
btu baseTilesUpdates
|
2014-09-19 13:06:04 +02:00
|
|
|
mapDir string
|
|
|
|
redisAddress string
|
2014-11-15 13:40:39 +01:00
|
|
|
ips []net.IP
|
2014-09-19 13:06:04 +02:00
|
|
|
colors *common.Colors
|
2015-07-27 19:03:47 +02:00
|
|
|
yMin, yMax int16
|
2014-09-19 13:06:04 +02:00
|
|
|
workers int
|
2014-10-26 18:36:47 +01:00
|
|
|
transparent bool
|
2014-09-19 13:06:04 +02:00
|
|
|
cond *sync.Cond
|
|
|
|
mu sync.Mutex
|
2014-09-18 07:52:37 +02:00
|
|
|
}
|
|
|
|
|
2014-09-18 15:21:40 +02:00
|
|
|
type xz struct {
|
|
|
|
X int16
|
|
|
|
Z int16
|
|
|
|
}
|
|
|
|
|
2014-09-21 12:57:21 +02:00
|
|
|
type xzm struct {
|
|
|
|
P xz
|
|
|
|
Mask uint16
|
|
|
|
}
|
|
|
|
|
2014-09-19 13:06:04 +02:00
|
|
|
func (c xz) quantize() xz {
|
2014-09-20 21:57:01 +02:00
|
|
|
return xz{X: (c.X - -1933) / 16, Z: (c.Z - -1933) / 16}
|
2014-09-19 13:06:04 +02:00
|
|
|
}
|
2014-09-18 07:52:37 +02:00
|
|
|
|
2014-09-21 12:57:21 +02:00
|
|
|
func (c xz) dequantize() xz {
|
|
|
|
return xz{X: c.X*16 + -1933, Z: c.Z*16 + -1933}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c xz) parent() xzm {
|
|
|
|
xp, xr := c.X>>1, uint16(c.X&1)
|
|
|
|
zp, zr := c.Z>>1, uint16(c.Z&1)
|
|
|
|
return xzm{
|
|
|
|
P: xz{X: xp, Z: zp},
|
2014-09-22 00:25:24 +02:00
|
|
|
Mask: 1 << (zr<<1 | xr)}
|
2014-09-21 17:30:19 +02:00
|
|
|
}
|
|
|
|
|
2014-10-26 18:36:47 +01:00
|
|
|
func newTileUpdater(
|
|
|
|
mapDir, redisAddress string,
|
2014-11-15 13:40:39 +01:00
|
|
|
ips []net.IP,
|
2014-10-26 18:36:47 +01:00
|
|
|
colors *common.Colors,
|
2015-07-27 19:03:47 +02:00
|
|
|
yMin, yMax int,
|
2014-10-26 18:36:47 +01:00
|
|
|
transparent bool,
|
2015-03-02 13:10:30 +01:00
|
|
|
workers int,
|
|
|
|
btu baseTilesUpdates) *tileUpdater {
|
2014-10-26 18:36:47 +01:00
|
|
|
|
2014-09-19 13:06:04 +02:00
|
|
|
tu := tileUpdater{
|
2015-03-02 13:10:30 +01:00
|
|
|
btu: btu,
|
2014-09-19 13:06:04 +02:00
|
|
|
mapDir: mapDir,
|
|
|
|
redisAddress: redisAddress,
|
2014-11-15 13:40:39 +01:00
|
|
|
ips: ips,
|
2014-09-19 13:06:04 +02:00
|
|
|
changes: map[xz]bool{},
|
|
|
|
colors: colors,
|
2015-07-27 19:03:47 +02:00
|
|
|
yMin: int16(yMin),
|
|
|
|
yMax: int16(yMax),
|
2014-10-26 18:36:47 +01:00
|
|
|
transparent: transparent,
|
2014-09-19 13:06:04 +02:00
|
|
|
workers: workers}
|
|
|
|
tu.cond = sync.NewCond(&tu.mu)
|
|
|
|
return &tu
|
2014-09-18 07:52:37 +02:00
|
|
|
}
|
|
|
|
|
2014-11-15 13:40:39 +01:00
|
|
|
func (tu *tileUpdater) checkIP(r *http.Request) bool {
|
|
|
|
if len(tu.ips) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
var host []string
|
|
|
|
if host = strings.SplitN(r.RemoteAddr, ":", 2); len(host) < 1 {
|
2015-07-20 14:19:41 +02:00
|
|
|
log.Printf("WARN: cannot extract host from '%s'.\n", r.RemoteAddr)
|
2014-11-15 13:40:39 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var ip net.IP
|
|
|
|
if ip = net.ParseIP(host[0]); ip == nil {
|
|
|
|
log.Printf("WARN: cannot get IP for host '%s'.\n", host[0])
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range tu.ips {
|
|
|
|
if bytes.Compare(tu.ips[i], ip) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-09-18 07:52:37 +02:00
|
|
|
func (tu *tileUpdater) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
2014-11-15 13:40:39 +01:00
|
|
|
if !tu.checkIP(r) {
|
2015-07-20 14:19:41 +02:00
|
|
|
log.Printf("WARN: Unauthorized update request from '%s'\n", r.RemoteAddr)
|
2014-11-15 13:40:39 +01:00
|
|
|
http.Error(rw, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-18 15:21:40 +02:00
|
|
|
var err error
|
2014-09-19 13:06:04 +02:00
|
|
|
var newChanges []xz
|
2014-09-18 15:21:40 +02:00
|
|
|
decoder := json.NewDecoder(r.Body)
|
2014-09-19 13:06:04 +02:00
|
|
|
if err = decoder.Decode(&newChanges); err != nil {
|
2015-07-20 14:19:41 +02:00
|
|
|
log.Printf("WARN: JSON document broken: %s\n", err)
|
2014-09-18 15:21:40 +02:00
|
|
|
http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2014-09-19 13:06:04 +02:00
|
|
|
|
2014-09-20 14:39:51 +02:00
|
|
|
if len(newChanges) > 0 {
|
|
|
|
tu.cond.L.Lock()
|
|
|
|
for _, c := range newChanges {
|
|
|
|
tu.changes[c.quantize()] = true
|
|
|
|
}
|
|
|
|
tu.cond.L.Unlock()
|
|
|
|
tu.cond.Signal()
|
2014-09-19 13:06:04 +02:00
|
|
|
}
|
|
|
|
|
2014-09-18 15:21:40 +02:00
|
|
|
rw.WriteHeader(http.StatusOK)
|
2014-09-18 07:52:37 +02:00
|
|
|
}
|
2014-09-19 13:06:04 +02:00
|
|
|
|
2014-09-20 15:21:01 +02:00
|
|
|
func (tu *tileUpdater) doUpdates() {
|
2014-09-19 13:06:04 +02:00
|
|
|
|
2014-09-20 15:21:01 +02:00
|
|
|
for {
|
|
|
|
var changes map[xz]bool
|
|
|
|
tu.cond.L.Lock()
|
|
|
|
for len(tu.changes) == 0 {
|
|
|
|
tu.cond.Wait()
|
|
|
|
}
|
|
|
|
changes = tu.changes
|
|
|
|
tu.changes = map[xz]bool{}
|
|
|
|
tu.cond.L.Unlock()
|
2014-09-19 13:06:04 +02:00
|
|
|
|
2014-09-20 15:21:01 +02:00
|
|
|
baseDir := filepath.Join(tu.mapDir, "8")
|
2014-09-19 13:06:04 +02:00
|
|
|
|
2014-09-20 15:21:01 +02:00
|
|
|
jobs := make(chan xz)
|
|
|
|
var done sync.WaitGroup
|
2014-09-19 13:06:04 +02:00
|
|
|
|
2015-07-26 12:32:59 +02:00
|
|
|
for i, n := 0, common.Min(tu.workers, len(changes)); i < n; i++ {
|
2014-09-20 15:21:01 +02:00
|
|
|
var client *common.RedisClient
|
|
|
|
var err error
|
|
|
|
if client, err = common.NewRedisClient("tcp", tu.redisAddress); err != nil {
|
2015-07-20 14:19:41 +02:00
|
|
|
log.Printf("WARN: Cannot connect to redis server: %s\n", err)
|
2014-09-20 15:21:01 +02:00
|
|
|
continue
|
2014-09-19 13:06:04 +02:00
|
|
|
}
|
2015-07-27 19:03:47 +02:00
|
|
|
btc := common.NewBaseTileCreator(
|
|
|
|
client, tu.colors,
|
|
|
|
tu.yMin, tu.yMax,
|
|
|
|
tu.transparent, baseDir, true)
|
2014-09-20 15:21:01 +02:00
|
|
|
done.Add(1)
|
|
|
|
go updateBaseTiles(jobs, btc, &done)
|
|
|
|
}
|
2014-09-19 13:06:04 +02:00
|
|
|
|
2014-09-21 12:57:21 +02:00
|
|
|
parentJobs := make(map[xz]uint16)
|
|
|
|
|
2015-03-02 13:10:30 +01:00
|
|
|
for c := range changes {
|
2015-07-20 14:19:41 +02:00
|
|
|
//log.Printf("job: %+v\n", c)
|
2014-09-20 15:21:01 +02:00
|
|
|
jobs <- c
|
2014-09-21 12:57:21 +02:00
|
|
|
pxz := c.parent()
|
|
|
|
parentJobs[pxz.P] |= pxz.Mask
|
2014-09-19 13:06:04 +02:00
|
|
|
}
|
2014-09-21 12:57:21 +02:00
|
|
|
close(jobs)
|
2014-09-20 15:21:01 +02:00
|
|
|
done.Wait()
|
2014-09-21 12:57:21 +02:00
|
|
|
|
|
|
|
for level := 7; level >= 0; level-- {
|
|
|
|
pJobs := make(chan xzm)
|
2015-07-26 12:32:59 +02:00
|
|
|
for i, n := 0, common.Min(len(parentJobs), tu.workers); i < n; i++ {
|
2014-09-21 12:57:21 +02:00
|
|
|
done.Add(1)
|
2014-09-21 13:29:03 +02:00
|
|
|
go updatePyramidTiles(level, tu.mapDir, pJobs, &done)
|
2014-09-21 12:57:21 +02:00
|
|
|
}
|
|
|
|
ppJobs := make(map[xz]uint16)
|
|
|
|
for c, mask := range parentJobs {
|
|
|
|
pJobs <- xzm{P: c, Mask: mask}
|
|
|
|
pxz := c.parent()
|
|
|
|
ppJobs[pxz.P] |= pxz.Mask
|
|
|
|
}
|
|
|
|
close(pJobs)
|
|
|
|
done.Wait()
|
|
|
|
parentJobs = ppJobs
|
|
|
|
}
|
2015-03-02 13:10:30 +01:00
|
|
|
|
|
|
|
if tu.btu != nil {
|
|
|
|
tu.btu.BaseTilesUpdated(changes)
|
|
|
|
}
|
2014-09-21 12:57:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updatePyramidTiles(level int, baseDir string, jobs chan xzm, done *sync.WaitGroup) {
|
|
|
|
defer done.Done()
|
2014-09-21 17:30:19 +02:00
|
|
|
scratch := image.NewRGBA(image.Rect(0, 0, 256, 256))
|
2015-12-25 22:07:54 +01:00
|
|
|
resized := image.NewRGBA(image.Rect(0, 0, 128, 128))
|
2014-09-21 17:30:19 +02:00
|
|
|
|
2014-09-21 12:57:21 +02:00
|
|
|
for job := range jobs {
|
2015-12-25 22:07:54 +01:00
|
|
|
if err := updatePyramidTile(scratch, resized, level, baseDir, job); err != nil {
|
2015-07-20 14:19:41 +02:00
|
|
|
log.Printf("Updating pyramid tile failed: %s\n", err)
|
2014-09-21 17:30:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-22 00:25:24 +02:00
|
|
|
/*
|
|
|
|
(0,0) (128, 0)
|
|
|
|
(0, 128) (128, 128)
|
|
|
|
*/
|
|
|
|
|
2014-09-21 17:30:19 +02:00
|
|
|
var dps = [4]image.Point{
|
|
|
|
image.Pt(0, 128),
|
|
|
|
image.Pt(128, 128),
|
2014-09-22 00:25:24 +02:00
|
|
|
image.Pt(0, 0),
|
|
|
|
image.Pt(128, 0),
|
|
|
|
}
|
2014-09-21 17:30:19 +02:00
|
|
|
|
|
|
|
var ofs = [4][2]int{
|
|
|
|
{0, 0},
|
|
|
|
{1, 0},
|
|
|
|
{0, 1},
|
|
|
|
{1, 1}}
|
|
|
|
|
2014-09-22 00:56:29 +02:00
|
|
|
var windowSize = image.Pt(128, 128)
|
2014-09-21 17:30:19 +02:00
|
|
|
|
2015-12-25 22:07:54 +01:00
|
|
|
func updatePyramidTile(scratch, resized *image.RGBA, level int, baseDir string, j xzm) error {
|
2014-09-21 17:30:19 +02:00
|
|
|
|
|
|
|
var orig image.Image
|
|
|
|
|
|
|
|
origPath := filepath.Join(
|
2014-09-22 00:56:29 +02:00
|
|
|
baseDir,
|
|
|
|
strconv.Itoa(level),
|
|
|
|
strconv.Itoa(int(j.P.X)),
|
2015-12-25 22:07:54 +01:00
|
|
|
strconv.Itoa(int(j.P.Z))+".png")
|
2014-09-21 17:30:19 +02:00
|
|
|
|
2015-12-25 22:07:54 +01:00
|
|
|
sr := resized.Bounds()
|
2014-09-21 17:30:19 +02:00
|
|
|
for i := uint16(0); i < 4; i++ {
|
|
|
|
if j.Mask&(1<<i) != 0 {
|
2015-07-20 14:19:41 +02:00
|
|
|
//log.Printf("level %d: modified %d\n", level, i)
|
2014-09-21 17:30:19 +02:00
|
|
|
o := ofs[i]
|
|
|
|
bx, bz := int(2*j.P.X), int(2*j.P.Z)
|
|
|
|
path := filepath.Join(
|
2014-09-22 00:56:29 +02:00
|
|
|
baseDir,
|
|
|
|
strconv.Itoa(level+1),
|
|
|
|
strconv.Itoa(bx+o[0]),
|
2015-05-27 18:36:03 +02:00
|
|
|
strconv.Itoa(bz+o[1])+".png")
|
2014-09-21 17:30:19 +02:00
|
|
|
img := common.LoadPNG(path)
|
2015-12-25 22:07:54 +01:00
|
|
|
if err := rez.Convert(resized, img, common.ResizeFilter); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-21 17:30:19 +02:00
|
|
|
r := sr.Sub(sr.Min).Add(dps[i])
|
2015-12-25 22:07:54 +01:00
|
|
|
draw.Draw(scratch, r, resized, sr.Min, draw.Src)
|
2014-09-21 17:30:19 +02:00
|
|
|
} else {
|
2014-09-22 02:37:44 +02:00
|
|
|
// Load lazy
|
|
|
|
if orig == nil {
|
|
|
|
orig = common.LoadPNG(origPath)
|
|
|
|
}
|
2015-07-20 14:19:41 +02:00
|
|
|
//log.Printf("level %d: copied %d\n", level, i)
|
2014-09-22 00:56:29 +02:00
|
|
|
min := orig.Bounds().Min.Add(dps[i])
|
|
|
|
r := image.Rectangle{min, min.Add(windowSize)}
|
2014-09-22 02:37:44 +02:00
|
|
|
draw.Draw(scratch, r, orig, min, draw.Src)
|
2014-09-21 17:30:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return common.SaveAsPNGAtomic(origPath, scratch)
|
2014-09-19 13:06:04 +02:00
|
|
|
}
|
|
|
|
|
2014-09-20 15:21:01 +02:00
|
|
|
func updateBaseTiles(jobs chan xz, btc *common.BaseTileCreator, done *sync.WaitGroup) {
|
2014-09-21 12:57:21 +02:00
|
|
|
defer btc.Close()
|
|
|
|
defer done.Done()
|
2014-09-19 13:06:04 +02:00
|
|
|
for job := range jobs {
|
2014-09-21 12:57:21 +02:00
|
|
|
xz := job.dequantize()
|
|
|
|
//log.Printf("%d/%d %d/%d", x, z, job.X, job.Z)
|
|
|
|
if err := btc.CreateTile(xz.X-1, xz.Z-1, int(job.X), int(job.Z)); err != nil {
|
2015-07-20 14:19:41 +02:00
|
|
|
log.Printf("WARN: create tile failed: %s\n", err)
|
2014-09-19 13:06:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|