event reordering

This commit is contained in:
NatureFreshMilk 2019-01-28 13:31:48 +01:00
parent 83b02384cd
commit 66bd39a943
9 changed files with 38 additions and 60 deletions

View File

@ -3,7 +3,6 @@ package app
import (
"mapserver/colormapping"
"mapserver/db"
"mapserver/event"
"mapserver/mapblockaccessor"
"mapserver/mapblockrenderer"
"mapserver/mapobjectdb"
@ -28,5 +27,4 @@ type App struct {
Colormapping *colormapping.ColorMapping
Mapblockrenderer *mapblockrenderer.MapBlockRenderer
Tilerenderer *tilerenderer.TileRenderer
Events event.EventConsumer
}

View File

@ -1,5 +0,0 @@
package event
type EventConsumer interface {
SendJSON(eventtype string, o interface{})
}

View File

@ -4,6 +4,11 @@ import (
"sync"
)
const (
MAPBLOCK_RENDERED = "mapblock-rendered"
TILE_RENDERED = "rendered-tile"
)
type Listener interface {
OnEvent(eventtype string, o interface{})
}

View File

@ -4,8 +4,10 @@ import (
"fmt"
"mapserver/coords"
"mapserver/db"
"mapserver/eventbus"
"mapserver/layer"
"mapserver/mapblockparser"
"time"
cache "github.com/patrickmn/go-cache"
@ -13,13 +15,9 @@ import (
)
type MapBlockAccessor struct {
accessor db.DBAccessor
c *cache.Cache
listeners []MapBlockListener
}
type MapBlockListener interface {
OnParsedMapBlock(block *mapblockparser.MapBlock)
accessor db.DBAccessor
c *cache.Cache
Eventbus *eventbus.Eventbus
}
func getKey(pos coords.MapBlockCoords) string {
@ -29,13 +27,12 @@ func getKey(pos coords.MapBlockCoords) string {
func NewMapBlockAccessor(accessor db.DBAccessor) *MapBlockAccessor {
c := cache.New(500*time.Millisecond, 1000*time.Millisecond)
return &MapBlockAccessor{accessor: accessor, c: c}
return &MapBlockAccessor{
accessor: accessor,
c: c,
Eventbus: eventbus.New(),
}
}
func (a *MapBlockAccessor) AddListener(l MapBlockListener) {
a.listeners = append(a.listeners, l)
}
func (a *MapBlockAccessor) Update(pos coords.MapBlockCoords, mb *mapblockparser.MapBlock) {
key := getKey(pos)
a.c.Set(key, mb, cache.DefaultExpiration)
@ -102,9 +99,7 @@ func (a *MapBlockAccessor) FindMapBlocksByMtime(lastmtime int64, limit int, laye
return nil, err
}
for _, listener := range a.listeners {
listener.OnParsedMapBlock(mapblock)
}
a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, mapblock)
a.c.Set(key, mapblock, cache.DefaultExpiration)
mblist = append(mblist, mapblock)
@ -172,9 +167,7 @@ func (a *MapBlockAccessor) FindMapBlocksByPos(lastpos coords.MapBlockCoords, lim
return nil, err
}
for _, listener := range a.listeners {
listener.OnParsedMapBlock(mapblock)
}
a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, mapblock)
a.c.Set(key, mapblock, cache.DefaultExpiration)
mblist = append(mblist, mapblock)
@ -209,9 +202,7 @@ func (a *MapBlockAccessor) GetMapBlock(pos coords.MapBlockCoords) (*mapblockpars
return nil, err
}
for _, listener := range a.listeners {
listener.OnParsedMapBlock(mapblock)
}
a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, mapblock)
a.c.Set(key, mapblock, cache.DefaultExpiration)

View File

@ -20,7 +20,11 @@ type MapBlockRenderer struct {
}
func NewMapBlockRenderer(accessor *mapblockaccessor.MapBlockAccessor, colors *colormapping.ColorMapping) *MapBlockRenderer {
return &MapBlockRenderer{accessor: accessor, colors: colors, enableShadow: true}
return &MapBlockRenderer{
accessor: accessor,
colors: colors,
enableShadow: true,
}
}
const (

View File

@ -2,6 +2,7 @@ package mapobject
import (
"mapserver/app"
"mapserver/eventbus"
"mapserver/mapblockparser"
)
@ -9,7 +10,13 @@ type Listener struct {
ctx *app.App
}
func (this *Listener) OnParsedMapBlock(block *mapblockparser.MapBlock) {
func (this *Listener) OnEvent(eventtype string, o interface{}) {
if eventtype != eventbus.MAPBLOCK_RENDERED {
return
}
block := o.(*mapblockparser.MapBlock)
err := this.ctx.Objectdb.RemoveMapData(&block.Pos)
if err != nil {
panic(err)
@ -23,5 +30,5 @@ func (this *Listener) OnParsedMapBlock(block *mapblockparser.MapBlock) {
}
func Setup(ctx *app.App) {
ctx.BlockAccessor.AddListener(&Listener{ctx: ctx})
ctx.BlockAccessor.Eventbus.AddListener(&Listener{ctx: ctx})
}

View File

@ -8,6 +8,7 @@ import (
"image/png"
"mapserver/coords"
"mapserver/db"
"mapserver/eventbus"
"mapserver/layer"
"mapserver/mapblockrenderer"
"mapserver/mapobjectdb"
@ -22,11 +23,7 @@ type TileRenderer struct {
layers []layer.Layer
tdb mapobjectdb.DBAccessor
dba db.DBAccessor
listeners []TileListener
}
type TileListener interface {
OnRenderedTile(tc *coords.TileCoords)
Eventbus *eventbus.Eventbus
}
func NewTileRenderer(mapblockrenderer *mapblockrenderer.MapBlockRenderer,
@ -39,6 +36,7 @@ func NewTileRenderer(mapblockrenderer *mapblockrenderer.MapBlockRenderer,
layers: layers,
tdb: tdb,
dba: dba,
Eventbus: eventbus.New(),
}
}
@ -46,10 +44,6 @@ const (
IMG_SIZE = 256
)
func (tr *TileRenderer) AddListener(l TileListener) {
tr.listeners = append(tr.listeners, l)
}
func (tr *TileRenderer) Render(tc *coords.TileCoords, recursionDepth int) ([]byte, error) {
//Check cache
@ -252,9 +246,7 @@ func (tr *TileRenderer) RenderImage(tc *coords.TileCoords, recursionDepth int) (
}
log.WithFields(fields).Debug("Cross stitch")
for _, listener := range tr.listeners {
listener.OnRenderedTile(tc)
}
tr.Eventbus.Emit(eventbus.TILE_RENDERED, tc)
return img, buf.Bytes(), nil
}

View File

@ -25,13 +25,9 @@ func Serve(ctx *app.App) {
ws := NewWS(ctx)
mux.Handle("/api/ws", ws)
ctx.Events = ws
ctx.Tilerenderer.AddListener(ws)
ctx.Tilerenderer.Eventbus.AddListener(ws)
if ctx.Config.WebApi.EnableMapblock {
//websocket listener
ctx.BlockAccessor.AddListener(ws)
//mapblock endpoint
mux.Handle("/api/mapblock/", &MapblockHandler{ctx: ctx})
}

View File

@ -4,8 +4,6 @@ import (
"bytes"
"encoding/json"
"mapserver/app"
"mapserver/coords"
"mapserver/mapblockparser"
"math/rand"
"net/http"
"sync"
@ -32,15 +30,7 @@ var upgrader = websocket.Upgrader{
WriteBufferSize: 1024,
}
func (t *WS) OnParsedMapBlock(block *mapblockparser.MapBlock) {
t.SendJSON("parsed-mapblock", block)
}
func (t *WS) OnRenderedTile(tc *coords.TileCoords) {
t.SendJSON("rendered-tile", tc)
}
func (t *WS) SendJSON(eventtype string, o interface{}) {
func (t *WS) OnEvent(eventtype string, o interface{}) {
data, err := json.Marshal(o)
if err != nil {
panic(err)