1
0
Fork 0
mirror of https://github.com/FrankerFaceZ/FrankerFaceZ.git synced 2025-07-29 05:58:30 +00:00

Reformat and add global message subscriptions

This commit is contained in:
Kane York 2015-10-26 10:06:45 -07:00
parent df7d607556
commit 8918b9ac3a
11 changed files with 182 additions and 158 deletions

View file

@ -1,10 +1,10 @@
package main // import "bitbucket.org/stendec/frankerfacez/socketserver/cmd/socketserver"
import (
"../../internal/server"
"flag"
"log"
"net/http"
"../../internal/server"
)
var origin *string = flag.String("origin", "localhost:8001", "Client-visible origin of the socket server")
@ -61,7 +61,6 @@ func main() {
}
}
func GenerateKeys(outputFile string) {
if flag.NArg() < 1 {
log.Fatal("The server ID must be specified")

View file

@ -1,21 +1,21 @@
package server
import (
"golang.org/x/crypto/nacl/box"
"net/http"
"time"
"fmt"
"net/url"
"github.com/pmylund/go-cache"
"strconv"
"io/ioutil"
"encoding/json"
"sync"
"log"
"os"
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/pmylund/go-cache"
"golang.org/x/crypto/nacl/box"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync"
"time"
)
var backendHttpClient http.Client
@ -64,7 +64,7 @@ func getCacheKey(remoteCommand, data string) string {
return fmt.Sprintf("%s/%s", remoteCommand, data)
}
func HandlePublishRequest(w http.ResponseWriter, r *http.Request) {
func HBackendPublishRequest(w http.ResponseWriter, r *http.Request) {
formData, err := UnsealRequest(r.Form)
if err != nil {
w.WriteHeader(403)

View file

@ -1,9 +1,10 @@
package server
import (
"testing"
"net/url"
"golang.org/x/crypto/nacl/box"
"crypto/rand"
"golang.org/x/crypto/nacl/box"
"net/url"
"testing"
)
func SetupRandomKeys(t testing.TB) {

View file

@ -0,0 +1 @@
package server

View file

@ -1,11 +1,11 @@
package server
import (
"golang.org/x/net/websocket"
"github.com/satori/go.uuid"
"golang.org/x/net/websocket"
"log"
"sync"
"strconv"
"sync"
"time"
)
@ -203,6 +203,7 @@ type SurveySubmission struct {
User string
Json string
}
var SurveySubmissions []SurveySubmission
var SurveySubmissionLock sync.Mutex
@ -220,6 +221,7 @@ type FollowEvent struct {
NowFollowing bool
Timestamp time.Time
}
var FollowEvents []FollowEvent
var FollowEventsLock sync.Mutex
@ -268,7 +270,6 @@ func HandleEmoticonUses(conn *websocket.Conn, client *ClientInfo, msg ClientMess
}
}
return ResponseSuccess, nil
}

View file

@ -1,16 +1,16 @@
package server // import "bitbucket.org/stendec/frankerfacez/socketserver/server"
import (
"net/http"
"golang.org/x/net/websocket"
"crypto/tls"
"strings"
"strconv"
"errors"
"encoding/json"
"errors"
"fmt"
"sync"
"golang.org/x/net/websocket"
"log"
"net/http"
"strconv"
"strings"
"sync"
)
const MAX_PACKET_SIZE = 1024
@ -58,10 +58,13 @@ var CommandHandlers = map[Command]CommandHandler{
// Sent by the server in ClientMessage.Command to indicate success.
const SuccessCommand Command = "True"
// Sent by the server in ClientMessage.Command to indicate failure.
const ErrorCommand Command = "error"
// This must be the first command sent by the client once the connection is established.
const HelloCommand Command = "hello"
// A handler returning a ClientMessage with this Command will prevent replying to the client.
// It signals that the work has been handed off to a background goroutine.
const AsyncResponseCommand Command = "_async"
@ -124,7 +127,7 @@ func SetupServerAndHandle(config *Config, tlsConfig *tls.Config, serveMux *http.
serveMux = http.DefaultServeMux
}
serveMux.HandleFunc("/", sockServer.ServeHTTP)
serveMux.HandleFunc("/pub", HandlePublishRequest)
serveMux.HandleFunc("/pub", HBackendPublishRequest)
}
// Handle a new websocket connection from a FFZ client.
@ -201,7 +204,8 @@ func HandleSocketConnection(conn *websocket.Conn) {
// Launch message draining goroutine - we aren't out of the pub/sub records
go func() {
for _ = range _serverMessageChan {}
for _ = range _serverMessageChan {
}
}()
// Stop getting messages...
@ -316,7 +320,8 @@ func (cm *ClientMessage) ArgumentsAsString() (string1 string, err error) {
var ok bool
string1, ok = cm.Arguments.(string)
if !ok {
err = ExpectedSingleString; return
err = ExpectedSingleString
return
} else {
return string1, nil
}
@ -328,7 +333,8 @@ func (cm *ClientMessage) ArgumentsAsInt() (int1 int, err error) {
var num float64
num, ok = cm.Arguments.(float64)
if !ok {
err = ExpectedSingleInt; return
err = ExpectedSingleInt
return
} else {
int1 = int(num)
return int1, nil
@ -341,18 +347,22 @@ func (cm *ClientMessage) ArgumentsAsTwoStrings() (string1, string2 string, err e
var ary []interface{}
ary, ok = cm.Arguments.([]interface{})
if !ok {
err = ExpectedTwoStrings; return
err = ExpectedTwoStrings
return
} else {
if len(ary) != 2 {
err = ExpectedTwoStrings; return
err = ExpectedTwoStrings
return
}
string1, ok = ary[0].(string)
if !ok {
err = ExpectedTwoStrings; return
err = ExpectedTwoStrings
return
}
string2, ok = ary[1].(string)
if !ok {
err = ExpectedTwoStrings; return
err = ExpectedTwoStrings
return
}
return string1, string2, nil
}
@ -364,23 +374,28 @@ func (cm *ClientMessage) ArgumentsAsStringAndInt() (string1 string, int int64, e
var ary []interface{}
ary, ok = cm.Arguments.([]interface{})
if !ok {
err = ExpectedStringAndInt; return
err = ExpectedStringAndInt
return
} else {
if len(ary) != 2 {
err = ExpectedStringAndInt; return
err = ExpectedStringAndInt
return
}
string1, ok = ary[0].(string)
if !ok {
err = ExpectedStringAndInt; return
err = ExpectedStringAndInt
return
}
var num float64
num, ok = ary[1].(float64)
if !ok {
err = ExpectedStringAndInt; return
err = ExpectedStringAndInt
return
}
int = int64(num)
if float64(int) != num {
err = ExpectedStringAndIntGotFloat; return
err = ExpectedStringAndIntGotFloat
return
}
return string1, int, nil
}
@ -392,18 +407,22 @@ func (cm *ClientMessage) ArgumentsAsStringAndBool() (str string, flag bool, err
var ary []interface{}
ary, ok = cm.Arguments.([]interface{})
if !ok {
err = ExpectedStringAndBool; return
err = ExpectedStringAndBool
return
} else {
if len(ary) != 2 {
err = ExpectedStringAndBool; return
err = ExpectedStringAndBool
return
}
str, ok = ary[0].(string)
if !ok {
err = ExpectedStringAndBool; return
err = ExpectedStringAndBool
return
}
flag, ok = ary[1].(bool)
if !ok {
err = ExpectedStringAndBool; return
err = ExpectedStringAndBool
return
}
return str, flag, nil
}

View file

@ -1,8 +1,8 @@
package server
import (
"golang.org/x/net/websocket"
"fmt"
"golang.org/x/net/websocket"
"testing"
)

View file

@ -4,10 +4,10 @@ package server
// If I screwed up the locking, I won't know until it's too late.
import (
"fmt"
"net/http"
"sync"
"time"
"net/http"
"fmt"
)
type SubscriberList struct {
@ -19,14 +19,15 @@ var ChatSubscriptionInfo map[string]*SubscriberList = make(map[string]*Subscribe
var ChatSubscriptionLock sync.RWMutex
var WatchingSubscriptionInfo map[string]*SubscriberList = make(map[string]*SubscriberList)
var WatchingSubscriptionLock sync.RWMutex
var GlobalSubscriptionInfo SubscriberList
func PublishToChat(channel string, msg ClientMessage) (count int) {
ChatSubscriptionLock.RLock()
list := ChatSubscriptionInfo[channel]
if list != nil {
list.RLock()
for _, ch := range list.Members {
ch <- msg
for _, msgChan := range list.Members {
msgChan <- msg
count++
}
list.RUnlock()
@ -40,8 +41,8 @@ func PublishToWatchers(channel string, msg ClientMessage) (count int) {
list := WatchingSubscriptionInfo[channel]
if list != nil {
list.RLock()
for _, ch := range list.Members {
ch <- msg
for _, msgChan := range list.Members {
msgChan <- msg
count++
}
list.RUnlock()
@ -50,6 +51,15 @@ func PublishToWatchers(channel string, msg ClientMessage) (count int) {
return
}
func PublishToAll(msg ClientMessage) (count int) {
GlobalSubscriptionInfo.RLock()
for _, msgChan := range GlobalSubscriptionInfo.Members {
msgChan <- msg
count++
}
GlobalSubscriptionInfo.RUnlock()
}
// Add a channel to the subscriptions while holding a read-lock to the map.
// Locks:
// - ALREADY HOLDING a read-lock to the 'which' top-level map via the rlocker object
@ -73,6 +83,12 @@ func _subscribeWhileRlocked(which map[string]*SubscriberList, channelName string
}
}
func SubscribeGlobal(client *ClientInfo) {
GlobalSubscriptionInfo.Lock()
AddToSliceC(&GlobalSubscriptionInfo.Members, client.MessageChannel)
GlobalSubscriptionInfo.Unlock()
}
func SubscribeChat(client *ClientInfo, channelName string) {
ChatSubscriptionLock.RLock()
_subscribeWhileRlocked(ChatSubscriptionInfo, channelName, client.MessageChannel, ChatSubscriptionLock.RLocker(), &ChatSubscriptionLock)
@ -85,28 +101,16 @@ func SubscribeWatching(client *ClientInfo, channelName string) {
WatchingSubscriptionLock.RUnlock()
}
// Locks:
// - read lock to top-level maps
// - possible write lock to top-level maps
// - write lock to SubscriptionInfos
func SubscribeBatch(client *ClientInfo, chatSubs, channelSubs []string) {
mchan := client.MessageChannel
if len(chatSubs) > 0 {
rlocker := ChatSubscriptionLock.RLocker()
rlocker.Lock()
for _, v := range chatSubs {
_subscribeWhileRlocked(ChatSubscriptionInfo, v, mchan, rlocker, &ChatSubscriptionLock)
}
rlocker.Unlock()
}
if len(channelSubs) > 0 {
rlocker := WatchingSubscriptionLock.RLocker()
rlocker.Lock()
for _, v := range channelSubs {
_subscribeWhileRlocked(WatchingSubscriptionInfo, v, mchan, rlocker, &WatchingSubscriptionLock)
}
rlocker.Unlock()
}
func unsubscribeAllClients() {
GlobalSubscriptionInfo.Lock()
GlobalSubscriptionInfo.Members = nil
GlobalSubscriptionInfo.Unlock()
ChatSubscriptionLock.Lock()
ChatSubscriptionInfo = make(map[string]*SubscriberList)
ChatSubscriptionLock.Unlock()
WatchingSubscriptionLock.Lock()
WatchingSubscriptionInfo = make(map[string]*SubscriberList)
WatchingSubscriptionLock.Unlock()
}
// Unsubscribe the client from all channels, AND clear the CurrentChannels / WatchingChannels fields.
@ -120,6 +124,10 @@ func UnsubscribeAll(client *ClientInfo) {
client.PendingStreamBacklogs = nil
client.Mutex.Unlock()
GlobalSubscriptionInfo.Lock()
RemoveFromSliceC(&GlobalSubscriptionInfo.Members, client.MessageChannel)
GlobalSubscriptionInfo.Unlock()
ChatSubscriptionLock.RLock()
client.Mutex.Lock()
for _, v := range client.CurrentChannels {
@ -149,15 +157,6 @@ func UnsubscribeAll(client *ClientInfo) {
WatchingSubscriptionLock.RUnlock()
}
func unsubscribeAllClients() {
ChatSubscriptionLock.Lock()
ChatSubscriptionInfo = make(map[string]*SubscriberList)
ChatSubscriptionLock.Unlock()
WatchingSubscriptionLock.Lock()
WatchingSubscriptionInfo = make(map[string]*SubscriberList)
WatchingSubscriptionLock.Unlock()
}
func UnsubscribeSingleChat(client *ClientInfo, channelName string) {
ChatSubscriptionLock.RLock()
list := ChatSubscriptionInfo[channelName]

View file

@ -1,15 +1,16 @@
package server
import (
"testing"
"net/http/httptest"
"net/http"
"sync"
"golang.org/x/net/websocket"
"github.com/satori/go.uuid"
"fmt"
"syscall"
"os"
"github.com/satori/go.uuid"
"golang.org/x/net/websocket"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"sync"
"syscall"
"testing"
)
func CountOpenFDs() uint64 {

View file

@ -1,6 +1,9 @@
package server
import (
"encoding/json"
"errors"
"fmt"
"github.com/satori/go.uuid"
"sync"
"time"

View file

@ -1,15 +1,15 @@
package server
import (
"crypto/rand"
"net/url"
"golang.org/x/crypto/nacl/box"
"bytes"
"crypto/rand"
"encoding/base64"
"errors"
"golang.org/x/crypto/nacl/box"
"log"
"net/url"
"strconv"
"strings"
"errors"
"log"
)
func FillCryptoRandom(buf []byte) error {