Commit c6087233 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Move group and client to their own package.

parent d9cf32ed
...@@ -16,10 +16,11 @@ import ( ...@@ -16,10 +16,11 @@ import (
"github.com/pion/webrtc/v3/pkg/media/samplebuilder" "github.com/pion/webrtc/v3/pkg/media/samplebuilder"
"sfu/conn" "sfu/conn"
"sfu/group"
) )
type diskClient struct { type diskClient struct {
group *group group *group.Group
id string id string
mu sync.Mutex mu sync.Mutex
...@@ -41,11 +42,11 @@ func newId() string { ...@@ -41,11 +42,11 @@ func newId() string {
return s return s
} }
func NewDiskClient(g *group) *diskClient { func NewDiskClient(g *group.Group) *diskClient {
return &diskClient{group: g, id: newId()} return &diskClient{group: g, id: newId()}
} }
func (client *diskClient) Group() *group { func (client *diskClient) Group() *group.Group {
return client.group return client.group
} }
...@@ -53,15 +54,15 @@ func (client *diskClient) Id() string { ...@@ -53,15 +54,15 @@ func (client *diskClient) Id() string {
return client.id return client.id
} }
func (client *diskClient) Credentials() clientCredentials { func (client *diskClient) Credentials() group.ClientCredentials {
return clientCredentials{"RECORDING", ""} return group.ClientCredentials{"RECORDING", ""}
} }
func (client *diskClient) SetPermissions(perms clientPermissions) { func (client *diskClient) SetPermissions(perms group.ClientPermissions) {
return return
} }
func (client *diskClient) pushClient(id, username string, add bool) error { func (client *diskClient) PushClient(id, username string, add bool) error {
return nil return nil
} }
...@@ -79,11 +80,11 @@ func (client *diskClient) Close() error { ...@@ -79,11 +80,11 @@ func (client *diskClient) Close() error {
func (client *diskClient) kick(message string) error { func (client *diskClient) kick(message string) error {
err := client.Close() err := client.Close()
delClient(client) group.DelClient(client)
return err return err
} }
func (client *diskClient) pushConn(id string, up conn.Up, tracks []conn.UpTrack, label string) error { func (client *diskClient) PushConn(id string, up conn.Up, tracks []conn.UpTrack, label string) error {
client.mu.Lock() client.mu.Lock()
defer client.mu.Unlock() defer client.mu.Unlock()
...@@ -101,7 +102,7 @@ func (client *diskClient) pushConn(id string, up conn.Up, tracks []conn.UpTrack, ...@@ -101,7 +102,7 @@ func (client *diskClient) pushConn(id string, up conn.Up, tracks []conn.UpTrack,
return nil return nil
} }
directory := filepath.Join(recordingsDir, client.group.name) directory := filepath.Join(recordingsDir, client.group.Name())
err := os.MkdirAll(directory, 0700) err := os.MkdirAll(directory, 0700)
if err != nil { if err != nil {
return err return err
......
package main package group
import ( import (
"sfu/conn" "sfu/conn"
) )
type clientCredentials struct { type ClientCredentials struct {
Username string `json:"username,omitempty"` Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"` Password string `json:"password,omitempty"`
} }
type clientPermissions struct { type ClientPermissions struct {
Op bool `json:"op,omitempty"` Op bool `json:"op,omitempty"`
Present bool `json:"present,omitempty"` Present bool `json:"present,omitempty"`
Record bool `json:"record,omitempty"` Record bool `json:"record,omitempty"`
} }
type client interface { type Client interface {
Group() *group Group() *Group
Id() string Id() string
Credentials() clientCredentials Credentials() ClientCredentials
SetPermissions(clientPermissions) SetPermissions(ClientPermissions)
pushConn(id string, conn conn.Up, tracks []conn.UpTrack, label string) error PushConn(id string, conn conn.Up, tracks []conn.UpTrack, label string) error
pushClient(id, username string, add bool) error PushClient(id, username string, add bool) error
} }
type kickable interface { type Kickable interface {
kick(message string) error Kick(message string) error
} }
This diff is collapsed.
...@@ -20,6 +20,7 @@ import ( ...@@ -20,6 +20,7 @@ import (
"sfu/conn" "sfu/conn"
"sfu/estimator" "sfu/estimator"
"sfu/group"
"sfu/jitter" "sfu/jitter"
"sfu/packetcache" "sfu/packetcache"
"sfu/rtptime" "sfu/rtptime"
...@@ -110,8 +111,8 @@ type rtpDownConnection struct { ...@@ -110,8 +111,8 @@ type rtpDownConnection struct {
iceCandidates []*webrtc.ICECandidateInit iceCandidates []*webrtc.ICECandidateInit
} }
func newDownConn(c client, id string, remote conn.Up) (*rtpDownConnection, error) { func newDownConn(c group.Client, id string, remote conn.Up) (*rtpDownConnection, error) {
pc, err := c.Group().API().NewPeerConnection(iceConfiguration()) pc, err := c.Group().API().NewPeerConnection(group.IceConfiguration())
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -371,8 +372,8 @@ func (up *rtpUpConnection) complete() bool { ...@@ -371,8 +372,8 @@ func (up *rtpUpConnection) complete() bool {
return true return true
} }
func newUpConn(c client, id string) (*rtpUpConnection, error) { func newUpConn(c group.Client, id string) (*rtpUpConnection, error) {
pc, err := c.Group().API().NewPeerConnection(iceConfiguration()) pc, err := c.Group().API().NewPeerConnection(group.IceConfiguration())
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -448,9 +449,9 @@ func newUpConn(c client, id string) (*rtpUpConnection, error) { ...@@ -448,9 +449,9 @@ func newUpConn(c client, id string) (*rtpUpConnection, error) {
up.mu.Unlock() up.mu.Unlock()
if complete { if complete {
clients := c.Group().getClients(c) clients := c.Group().GetClients(c)
for _, cc := range clients { for _, cc := range clients {
cc.pushConn(up.id, up, tracks, up.label) cc.PushConn(up.id, up, tracks, up.label)
} }
go rtcpUpSender(up) go rtcpUpSender(up)
} }
...@@ -750,8 +751,8 @@ func sendUpRTCP(conn *rtpUpConnection) error { ...@@ -750,8 +751,8 @@ func sendUpRTCP(conn *rtpUpConnection) error {
rate = r rate = r
} }
} }
if rate < minBitrate { if rate < group.MinBitrate {
rate = minBitrate rate = group.MinBitrate
} }
var ssrcs []uint32 var ssrcs []uint32
......
...@@ -14,14 +14,14 @@ import ( ...@@ -14,14 +14,14 @@ import (
"runtime" "runtime"
"runtime/pprof" "runtime/pprof"
"syscall" "syscall"
"sfu/group"
) )
var httpAddr string var httpAddr string
var staticRoot string var staticRoot string
var dataDir string var dataDir string
var groupsDir string
var recordingsDir string var recordingsDir string
var iceFilename string
func main() { func main() {
var cpuprofile, memprofile, mutexprofile string var cpuprofile, memprofile, mutexprofile string
...@@ -31,7 +31,7 @@ func main() { ...@@ -31,7 +31,7 @@ func main() {
"web server root `directory`") "web server root `directory`")
flag.StringVar(&dataDir, "data", "./data/", flag.StringVar(&dataDir, "data", "./data/",
"data `directory`") "data `directory`")
flag.StringVar(&groupsDir, "groups", "./groups/", flag.StringVar(&group.Directory, "groups", "./groups/",
"group description `directory`") "group description `directory`")
flag.StringVar(&recordingsDir, "recordings", "./recordings/", flag.StringVar(&recordingsDir, "recordings", "./recordings/",
"recordings `directory`") "recordings `directory`")
...@@ -81,9 +81,9 @@ func main() { ...@@ -81,9 +81,9 @@ func main() {
}() }()
} }
iceFilename = filepath.Join(dataDir, "ice-servers.json") group.IceFilename = filepath.Join(dataDir, "ice-servers.json")
go readPublicGroups() go group.ReadPublicGroups()
webserver() webserver()
terminate := make(chan os.Signal, 1) terminate := make(chan os.Signal, 1)
......
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"sfu/group"
"sfu/rtptime" "sfu/rtptime"
) )
...@@ -33,15 +34,15 @@ type trackStats struct { ...@@ -33,15 +34,15 @@ type trackStats struct {
} }
func getGroupStats() []groupStats { func getGroupStats() []groupStats {
names := getGroupNames() names := group.GetNames()
gs := make([]groupStats, 0, len(names)) gs := make([]groupStats, 0, len(names))
for _, name := range names { for _, name := range names {
g := getGroup(name) g := group.Get(name)
if g == nil { if g == nil {
continue continue
} }
clients := g.getClients(nil) clients := g.GetClients(nil)
stats := groupStats{ stats := groupStats{
name: name, name: name,
clients: make([]clientStats, 0, len(clients)), clients: make([]clientStats, 0, len(clients)),
......
This diff is collapsed.
...@@ -18,6 +18,8 @@ import ( ...@@ -18,6 +18,8 @@ import (
"time" "time"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"sfu/group"
) )
var server *http.Server var server *http.Server
...@@ -47,8 +49,8 @@ func webserver() { ...@@ -47,8 +49,8 @@ func webserver() {
IdleTimeout: 120 * time.Second, IdleTimeout: 120 * time.Second,
} }
server.RegisterOnShutdown(func() { server.RegisterOnShutdown(func() {
rangeGroups(func (g *group) bool { group.Range(func (g *group.Group) bool {
go g.shutdown("server is shutting down") go g.Shutdown("server is shutting down")
return true return true
}) })
}) })
...@@ -139,7 +141,7 @@ func groupHandler(w http.ResponseWriter, r *http.Request) { ...@@ -139,7 +141,7 @@ func groupHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
g, err := addGroup(name, nil) g, err := group.Add(name, nil)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
notFound(w) notFound(w)
...@@ -168,7 +170,7 @@ func publicHandler(w http.ResponseWriter, r *http.Request) { ...@@ -168,7 +170,7 @@ func publicHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
g := getPublicGroups() g := group.GetPublic()
e := json.NewEncoder(w) e := json.NewEncoder(w)
e.Encode(g) e.Encode(g)
return return
...@@ -409,8 +411,8 @@ func handleGroupAction(w http.ResponseWriter, r *http.Request, group string) { ...@@ -409,8 +411,8 @@ func handleGroupAction(w http.ResponseWriter, r *http.Request, group string) {
} }
} }
func checkGroupPermissions(w http.ResponseWriter, r *http.Request, group string) bool { func checkGroupPermissions(w http.ResponseWriter, r *http.Request, groupname string) bool {
desc, err := getDescription(group) desc, err := group.GetDescription(groupname)
if err != nil { if err != nil {
return false return false
} }
...@@ -420,7 +422,7 @@ func checkGroupPermissions(w http.ResponseWriter, r *http.Request, group string) ...@@ -420,7 +422,7 @@ func checkGroupPermissions(w http.ResponseWriter, r *http.Request, group string)
return false return false
} }
p, err := getPermission(desc, clientCredentials{user, pass}) p, err := desc.GetPermission(group.ClientCredentials{user, pass})
if err != nil || !p.Record { if err != nil || !p.Record {
return false return false
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment