2015-07-26 16:33:29 +02:00
|
|
|
// Copyright 2014, 2015 by Sascha L. Teichmann
|
|
|
|
// Use of this source code is governed by the MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2014-09-17 09:51:34 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-09-17 10:16:51 +02:00
|
|
|
"encoding/json"
|
2014-09-17 09:51:34 +02:00
|
|
|
"log"
|
2014-09-17 10:45:44 +02:00
|
|
|
"net/http"
|
2014-09-17 09:51:34 +02:00
|
|
|
"sync"
|
|
|
|
|
2014-09-17 10:45:44 +02:00
|
|
|
"bytes"
|
|
|
|
|
2014-10-03 19:10:40 +02:00
|
|
|
"bitbucket.org/s_l_teichmann/mtsatellite/common"
|
2014-09-17 09:51:34 +02:00
|
|
|
)
|
|
|
|
|
2014-09-18 11:43:12 +02:00
|
|
|
// Pull up if it _really_ produces too much data.
|
|
|
|
const quantizationFactor = 1
|
|
|
|
|
2014-09-17 10:16:51 +02:00
|
|
|
type quantizedXZ struct {
|
|
|
|
X, Z int16
|
|
|
|
}
|
|
|
|
|
2014-09-17 09:51:34 +02:00
|
|
|
type ChangeTracker struct {
|
2014-09-17 10:16:51 +02:00
|
|
|
changes map[quantizedXZ]bool
|
|
|
|
mutex sync.Mutex
|
2014-09-17 09:51:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewChangeTracker() *ChangeTracker {
|
2014-09-17 10:16:51 +02:00
|
|
|
return &ChangeTracker{changes: make(map[quantizedXZ]bool)}
|
2014-09-17 09:51:34 +02:00
|
|
|
}
|
|
|
|
|
2015-07-25 22:18:16 +02:00
|
|
|
func (ct *ChangeTracker) BlockChanged(coord common.Coord) {
|
2014-09-17 10:16:51 +02:00
|
|
|
ct.mutex.Lock()
|
2014-09-18 11:43:12 +02:00
|
|
|
ct.changes[quantizedXZ{
|
|
|
|
X: coord.X / quantizationFactor,
|
|
|
|
Z: coord.Z / quantizationFactor}] = true
|
2014-09-17 10:16:51 +02:00
|
|
|
ct.mutex.Unlock()
|
2014-09-17 09:51:34 +02:00
|
|
|
}
|
|
|
|
|
2014-11-03 18:57:10 +01:00
|
|
|
func (ct *ChangeTracker) FlushChanges(url string) {
|
2014-09-17 10:16:51 +02:00
|
|
|
var oldChanges map[quantizedXZ]bool
|
|
|
|
ct.mutex.Lock()
|
2014-09-18 06:49:50 +02:00
|
|
|
if len(ct.changes) > 0 {
|
|
|
|
oldChanges = ct.changes
|
2014-09-17 17:34:03 +02:00
|
|
|
ct.changes = make(map[quantizedXZ]bool)
|
|
|
|
}
|
2014-09-17 10:16:51 +02:00
|
|
|
ct.mutex.Unlock()
|
2014-09-18 06:49:50 +02:00
|
|
|
if oldChanges == nil {
|
2014-09-17 17:34:03 +02:00
|
|
|
return
|
|
|
|
}
|
2014-09-17 10:16:51 +02:00
|
|
|
go func() {
|
|
|
|
changes := make([]quantizedXZ, len(oldChanges))
|
|
|
|
i := 0
|
2015-03-14 19:03:25 +01:00
|
|
|
for change := range oldChanges {
|
2014-09-17 10:16:51 +02:00
|
|
|
changes[i] = change
|
|
|
|
i++
|
|
|
|
}
|
2014-11-03 18:57:10 +01:00
|
|
|
var err error
|
2014-09-18 06:49:50 +02:00
|
|
|
var buf bytes.Buffer
|
|
|
|
encoder := json.NewEncoder(&buf)
|
|
|
|
if err = encoder.Encode(changes); err != nil {
|
2015-07-20 14:19:41 +02:00
|
|
|
log.Printf("WARN: encode changes to JSON failed: %s\n", err)
|
2014-09-17 10:16:51 +02:00
|
|
|
return
|
|
|
|
}
|
2016-01-24 14:31:15 +01:00
|
|
|
var resp *http.Response
|
|
|
|
resp, err = http.Post(
|
|
|
|
url, "application/json", bytes.NewBuffer(buf.Bytes()))
|
|
|
|
if resp != nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2015-07-20 14:19:41 +02:00
|
|
|
log.Printf("WARN: posting changes to %s failed: %s\n", url, err)
|
2014-09-17 10:45:44 +02:00
|
|
|
}
|
2014-09-17 10:16:51 +02:00
|
|
|
}()
|
2014-09-17 09:51:34 +02:00
|
|
|
return
|
|
|
|
}
|