Permalink
Browse files

I hate all this code now, but plowing forward nontheless, port to Loge

  • Loading branch information...
1 parent 3679da2 commit c46347a1933c61b53d51c8545324c0dabf127977 @brendonh committed Feb 14, 2013
View
@@ -1,26 +1 @@
-# Compiled Object files, Static and Dynamic libs (Shared Objects)
-*.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
+data
@@ -72,6 +72,8 @@ define([
} else {
console.log("Reply to unknown API call:", response);
}
+ } else {
+ console.log("Unknown packet:", dataViewToString(view));
}
},
@@ -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;
});
@@ -1,10 +1,11 @@
# 998: gqFY0QPmoVnRA+c=
# 999: gqFY0QPnoVnRA+c=
+# 0 0:
DATA=$(cat <<EOF
{
"inputs": {
- "bucket": "ShipLocation",
+ "bucket": "PoweredBody",
"index": "Coords_bin",
"key": "gqFY0QPmoVnRA+c=",
"keep": "true"
@@ -3,6 +3,7 @@ package accounts
import (
. "drift/common"
"code.google.com/p/go.crypto/bcrypt"
+ "github.com/brendonh/loge/src/loge"
)
type Account struct {
@@ -41,20 +42,23 @@ func NewAccount(name string, password string) *Account {
return &Account{name, hash, false}
}
-// XXX BGH TODO: Serialize to avoid Riak races
func CreateAccount(name string, password string, context DriftServerContext) (*Account, bool) {
- var client = context.Storage()
-
- existing := &Account{Name: name}
- if client.Get(existing) {
- return nil, false
- }
-
- account := NewAccount(name, password)
- if !client.Put(account) {
- return nil, false
- }
- return account, true
+ var db = context.DB()
+ var account *Account = NewAccount(name, password)
+ var success bool
+
+ db.Transact(func (t *loge.Transaction) {
+ if !t.Exists("account", loge.LogeKey(name)) {
+ t.Set("account", loge.LogeKey(name), account)
+ success = true
+ }
+ }, 0)
+
+ if success {
+ return account, true
+ }
+
+ return nil, false
}
@@ -2,6 +2,7 @@ package common
import (
"github.com/brendonh/go-service"
+ "github.com/brendonh/loge/src/loge"
)
// ------------------------------------------
@@ -10,7 +11,7 @@ import (
type DriftServerContext interface {
goservice.ServerContext
- Storage() StorageClient
+ DB() *loge.LogeDB
}
// ------------------------------------------
@@ -38,23 +39,6 @@ type Entity interface {
// 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 {
StorageKey() string
}
@@ -5,8 +5,8 @@ import (
)
type SectorCoords struct {
- X int
- Y int
+ X int64
+ Y int64
}
func (coords *SectorCoords) String() string {
@@ -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)
+ }
+}
@@ -1,4 +1,4 @@
-package server
+package endpoints
import (
. "drift/common"
@@ -13,9 +13,9 @@ import (
type ServerSession struct {
id string
user User
- endpoint Endpoint
*sync.Mutex
avatar Entity
+ connection SessionConnection
}
// ------------------------------------------
@@ -36,7 +36,12 @@ func (session *ServerSession) SetUser(user User) {
}
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)
}
// ------------------------------------------
@@ -54,12 +59,12 @@ func (session *ServerSession) Avatar() Entity {
return session.avatar
}
-func ServerSessionCreator(endpoint Endpoint) Session {
+func ServerSessionCreator(conn SessionConnection) Session {
return &ServerSession{
id: uuid.New(),
user: nil,
avatar: nil,
Mutex: new(sync.Mutex),
- endpoint: endpoint,
+ connection: conn,
}
}
Oops, something went wrong.

0 comments on commit c46347a

Please sign in to comment.