Skip to content

Commit

Permalink
I hate all this code now, but plowing forward nontheless, port to Loge
Browse files Browse the repository at this point in the history
  • Loading branch information
brendonh committed Feb 14, 2013
1 parent 3679da2 commit c46347a
Show file tree
Hide file tree
Showing 23 changed files with 521 additions and 657 deletions.
27 changes: 1 addition & 26 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -1,26 +1 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects) data
*.o
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe

pkg
binpkg
log
10 changes: 10 additions & 0 deletions client/js/dev/models/Server.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ define([
} else { } else {
console.log("Reply to unknown API call:", response); console.log("Reply to unknown API call:", response);
} }
} else {
console.log("Unknown packet:", dataViewToString(view));
} }
}, },


Expand Down Expand Up @@ -119,5 +121,13 @@ define([


}); });


function dataViewToString(view) {
var out = "";
for (var i = 0; i < view.byteLength; i++) {
out += String.fromCharCode(view.getUint8(i));
}
return out;
}

return Server; return Server;
}); });
3 changes: 2 additions & 1 deletion scripts/riak/find_ships.sh
Original file line number Original file line Diff line number Diff line change
@@ -1,10 +1,11 @@
# 998: gqFY0QPmoVnRA+c= # 998: gqFY0QPmoVnRA+c=
# 999: gqFY0QPnoVnRA+c= # 999: gqFY0QPnoVnRA+c=
# 0 0:


DATA=$(cat <<EOF DATA=$(cat <<EOF
{ {
"inputs": { "inputs": {
"bucket": "ShipLocation", "bucket": "PoweredBody",
"index": "Coords_bin", "index": "Coords_bin",
"key": "gqFY0QPmoVnRA+c=", "key": "gqFY0QPmoVnRA+c=",
"keep": "true" "keep": "true"
Expand Down
30 changes: 17 additions & 13 deletions src/drift/accounts/account.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package accounts
import ( import (
. "drift/common" . "drift/common"
"code.google.com/p/go.crypto/bcrypt" "code.google.com/p/go.crypto/bcrypt"
"github.com/brendonh/loge/src/loge"
) )


type Account struct { type Account struct {
Expand Down Expand Up @@ -41,20 +42,23 @@ func NewAccount(name string, password string) *Account {
return &Account{name, hash, false} return &Account{name, hash, false}
} }


// XXX BGH TODO: Serialize to avoid Riak races
func CreateAccount(name string, password string, context DriftServerContext) (*Account, bool) { func CreateAccount(name string, password string, context DriftServerContext) (*Account, bool) {
var client = context.Storage() var db = context.DB()

var account *Account = NewAccount(name, password)
existing := &Account{Name: name} var success bool
if client.Get(existing) {
return nil, false db.Transact(func (t *loge.Transaction) {
} if !t.Exists("account", loge.LogeKey(name)) {

t.Set("account", loge.LogeKey(name), account)
account := NewAccount(name, password) success = true
if !client.Put(account) { }
return nil, false }, 0)
}
return account, true if success {
return account, true
}

return nil, false
} }




Expand Down
20 changes: 2 additions & 18 deletions src/drift/common/interfaces.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common


import ( import (
"github.com/brendonh/go-service" "github.com/brendonh/go-service"
"github.com/brendonh/loge/src/loge"
) )


// ------------------------------------------ // ------------------------------------------
Expand All @@ -10,7 +11,7 @@ import (


type DriftServerContext interface { type DriftServerContext interface {
goservice.ServerContext goservice.ServerContext
Storage() StorageClient DB() *loge.LogeDB
} }


// ------------------------------------------ // ------------------------------------------
Expand Down Expand Up @@ -38,23 +39,6 @@ type Entity interface {
// Storage // Storage
// ------------------------------------------ // ------------------------------------------


type StorageClient interface {
GenerateID() string

Get(Storable) bool
Put(Storable) bool
IndexLookup(obj Storable, results interface{}, index string) bool

GetKey(bucket string, key string, target interface{}) bool
PutNew(bucket string, val interface{}) (string, bool)
PutKey(bucket string, key string, val interface{}) bool

Delete(bucket string, key string) bool

Keys(bucket string) ([]string, bool)
}


type Storable interface { type Storable interface {
StorageKey() string StorageKey() string
} }
Expand Down
4 changes: 2 additions & 2 deletions src/drift/common/types.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
) )


type SectorCoords struct { type SectorCoords struct {
X int X int64
Y int Y int64
} }


func (coords *SectorCoords) String() string { func (coords *SectorCoords) String() string {
Expand Down
167 changes: 167 additions & 0 deletions src/drift/control/controlmap.go
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,167 @@
package control

import (
"drift/ships"
"drift/endpoints"

"container/list"
)

type LMShipMap map[*ships.Ship]*list.List
type LMSessionMap map[*endpoints.ServerSession]*list.List

type ControlSpec bool

type ListenerMap struct {
byShip LMShipMap
bySession LMSessionMap
}

func NewListenerMap() *ListenerMap {
return &ListenerMap {
make(LMShipMap, 64),
make(LMSessionMap, 64),
}
}

type Control struct {
Ship *ships.Ship
Session *endpoints.ServerSession
Spec ControlSpec
}

func NewControl(ship *ships.Ship, session *endpoints.ServerSession, spec ControlSpec) *Control {
return &Control{
Ship: ship,
Session: session,
Spec: spec,
}
}


func (lm *ListenerMap) Set(ship *ships.Ship, session *endpoints.ServerSession, spec ControlSpec) {
lm.Remove(ship, session)

var control = NewControl(ship, session, spec)

shipMap, ok := lm.byShip[ship]
if !ok {
shipMap = list.New()
lm.byShip[ship] = shipMap
}
shipMap.PushBack(control)

sessionMap, ok := lm.bySession[session]
if !ok {
sessionMap = list.New()
lm.bySession[session] = sessionMap
}
sessionMap.PushBack(control)
}

func (lm *ListenerMap) Remove(ship *ships.Ship, session *endpoints.ServerSession) {
lm.byShip.RemoveSession(ship, session)
lm.bySession.RemoveShip(ship, session)
}

func (lm *ListenerMap) ClearShip(ship *ships.Ship) {
var shipMap, ok = lm.byShip[ship]
if !ok { return }
for e := shipMap.Front(); e != nil; e = e.Next() {
var session = e.Value.(*Control).Session
lm.bySession.RemoveShip(ship, session)
}
delete(lm.byShip, ship)
}

func (lm *ListenerMap) ClearSession(session *endpoints.ServerSession) {
var sessionMap, ok = lm.bySession[session]
if !ok { return }
for e := sessionMap.Front(); e != nil; e = e.Next() {
var ship = e.Value.(*Control).Ship
lm.byShip.RemoveSession(ship, session)
}
delete(lm.bySession, session)
}


// -------------------------------
// Queries
// -------------------------------

type SessionCallback func(*endpoints.ServerSession, ControlSpec)
type ShipCallback func(*ships.Ship, ControlSpec)

func (lm *ListenerMap) SessionApply(ship *ships.Ship, callback SessionCallback) {
var shipMap, ok = lm.byShip[ship]
if !ok { return }

for e := shipMap.Front(); e != nil; e = e.Next() {
var control = e.Value.(*Control)
callback(control.Session, control.Spec)
}
}


func (lm *ListenerMap) ShipApply(session *endpoints.ServerSession, callback ShipCallback) {
var sessionMap, ok = lm.bySession[session]
if !ok { return }

for e := sessionMap.Front(); e != nil; e = e.Next() {
var control = e.Value.(*Control)
callback(control.Ship, control.Spec)
}
}

func (lm *ListenerMap) HasControl(ship *ships.Ship, session *endpoints.ServerSession) bool {
var sessionMap, ok = lm.bySession[session]
if !ok { return false }

for e := sessionMap.Front(); e != nil; e = e.Next() {
var control = e.Value.(*Control)
if (control.Ship == ship) {
return bool(control.Spec)
}
}
return false
}


// -------------------------------
// Internal
// -------------------------------

func (shipMap LMShipMap) RemoveSession(ship *ships.Ship, session *endpoints.ServerSession) {
var shipList, ok = shipMap[ship]
if !ok {
return;
}

for e := shipList.Front(); e != nil; e = e.Next() {
if e.Value.(*Control).Session == session {
shipList.Remove(e)
}
}

if shipList.Front() == nil {
delete(shipMap, ship)
}
}


func (sessionMap LMSessionMap) RemoveShip(ship *ships.Ship, session *endpoints.ServerSession) {
var sessionList, ok = sessionMap[session]
if !ok {
return;
}

for e := sessionList.Front(); e != nil; e = e.Next() {
if e.Value.(*Control).Ship == ship {
sessionList.Remove(e)
}
}

if sessionList.Front() == nil {
delete(sessionMap, session)
}
}
15 changes: 10 additions & 5 deletions src/drift/server/session.go → src/drift/endpoints/session.go
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
package server package endpoints


import ( import (
. "drift/common" . "drift/common"
Expand All @@ -13,9 +13,9 @@ import (
type ServerSession struct { type ServerSession struct {
id string id string
user User user User
endpoint Endpoint
*sync.Mutex *sync.Mutex
avatar Entity avatar Entity
connection SessionConnection
} }


// ------------------------------------------ // ------------------------------------------
Expand All @@ -36,7 +36,12 @@ func (session *ServerSession) SetUser(user User) {
} }


func (session *ServerSession) Send(msg []byte) { func (session *ServerSession) Send(msg []byte) {
fmt.Printf("Session send: %s: %v\n", session.id, msg) fmt.Printf("Drift session send: %s: %v\n", session.id, msg)
session.connection.Send(msg)
}

func (session *ServerSession) String() string {
return fmt.Sprintf("%s<%s>", session.user.ID(), session.id)
} }


// ------------------------------------------ // ------------------------------------------
Expand All @@ -54,12 +59,12 @@ func (session *ServerSession) Avatar() Entity {
return session.avatar return session.avatar
} }


func ServerSessionCreator(endpoint Endpoint) Session { func ServerSessionCreator(conn SessionConnection) Session {
return &ServerSession{ return &ServerSession{
id: uuid.New(), id: uuid.New(),
user: nil, user: nil,
avatar: nil, avatar: nil,
Mutex: new(sync.Mutex), Mutex: new(sync.Mutex),
endpoint: endpoint, connection: conn,
} }
} }
Loading

0 comments on commit c46347a

Please sign in to comment.