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