21 changed files with 700 additions and 157 deletions
@ -0,0 +1,190 @@ |
|||||||
|
/* |
||||||
|
* Spreed WebRTC. |
||||||
|
* Copyright (C) 2013-2015 struktur AG |
||||||
|
* |
||||||
|
* This file is part of Spreed WebRTC. |
||||||
|
* |
||||||
|
* This program is free software: you can redistribute it and/or modify |
||||||
|
* it under the terms of the GNU Affero General Public License as published by |
||||||
|
* the Free Software Foundation, either version 3 of the License, or |
||||||
|
* (at your option) any later version. |
||||||
|
* |
||||||
|
* This program is distributed in the hope that it will be useful, |
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
* GNU Affero General Public License for more details. |
||||||
|
* |
||||||
|
* You should have received a copy of the GNU Affero General Public License |
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
package channelling |
||||||
|
|
||||||
|
import ( |
||||||
|
"bytes" |
||||||
|
"encoding/json" |
||||||
|
"errors" |
||||||
|
"log" |
||||||
|
"sync" |
||||||
|
"time" |
||||||
|
|
||||||
|
"github.com/strukturag/spreed-webrtc/go/buffercache" |
||||||
|
) |
||||||
|
|
||||||
|
type PipelineFeedLine struct { |
||||||
|
Seq int |
||||||
|
Msg *DataOutgoing |
||||||
|
} |
||||||
|
|
||||||
|
type Pipeline struct { |
||||||
|
PipelineManager PipelineManager |
||||||
|
mutex sync.RWMutex |
||||||
|
namespace string |
||||||
|
id string |
||||||
|
from *Session |
||||||
|
expires *time.Time |
||||||
|
data []*DataOutgoing |
||||||
|
sink Sink |
||||||
|
} |
||||||
|
|
||||||
|
func NewPipeline(manager PipelineManager, |
||||||
|
namespace string, |
||||||
|
id string, |
||||||
|
from *Session, |
||||||
|
duration time.Duration) *Pipeline { |
||||||
|
pipeline := &Pipeline{ |
||||||
|
PipelineManager: manager, |
||||||
|
namespace: namespace, |
||||||
|
id: id, |
||||||
|
from: from, |
||||||
|
} |
||||||
|
pipeline.Refresh(duration) |
||||||
|
return pipeline |
||||||
|
} |
||||||
|
|
||||||
|
func (pipeline *Pipeline) GetID() string { |
||||||
|
return pipeline.id |
||||||
|
} |
||||||
|
|
||||||
|
func (pipeline *Pipeline) Refresh(duration time.Duration) { |
||||||
|
pipeline.mutex.Lock() |
||||||
|
expiration := time.Now().Add(duration) |
||||||
|
pipeline.expires = &expiration |
||||||
|
pipeline.mutex.Unlock() |
||||||
|
} |
||||||
|
|
||||||
|
func (pipeline *Pipeline) Add(msg *DataOutgoing) *Pipeline { |
||||||
|
pipeline.mutex.Lock() |
||||||
|
pipeline.data = append(pipeline.data, msg) |
||||||
|
pipeline.mutex.Unlock() |
||||||
|
|
||||||
|
return pipeline |
||||||
|
} |
||||||
|
|
||||||
|
func (pipeline *Pipeline) Send(b buffercache.Buffer) { |
||||||
|
// noop for now
|
||||||
|
} |
||||||
|
|
||||||
|
func (pipeline *Pipeline) Index() uint64 { |
||||||
|
return 0 |
||||||
|
} |
||||||
|
|
||||||
|
func (pipeline *Pipeline) Close() { |
||||||
|
pipeline.mutex.Lock() |
||||||
|
pipeline.expires = nil |
||||||
|
if pipeline.sink != nil { |
||||||
|
pipeline.sink.Close() |
||||||
|
pipeline.sink = nil |
||||||
|
} |
||||||
|
pipeline.mutex.Unlock() |
||||||
|
} |
||||||
|
|
||||||
|
func (pipeline *Pipeline) Expired() bool { |
||||||
|
var expired bool |
||||||
|
pipeline.mutex.RLock() |
||||||
|
if pipeline.expires == nil { |
||||||
|
expired = true |
||||||
|
} else { |
||||||
|
expired = pipeline.expires.Before(time.Now()) |
||||||
|
} |
||||||
|
pipeline.mutex.RUnlock() |
||||||
|
|
||||||
|
return expired |
||||||
|
} |
||||||
|
|
||||||
|
func (pipeline *Pipeline) Session() *Session { |
||||||
|
return pipeline.from |
||||||
|
} |
||||||
|
|
||||||
|
func (pipeline *Pipeline) JSONFeed(since, limit int) ([]byte, error) { |
||||||
|
pipeline.mutex.RLock() |
||||||
|
var lineRaw []byte |
||||||
|
var line *PipelineFeedLine |
||||||
|
var buffer bytes.Buffer |
||||||
|
var err error |
||||||
|
data := pipeline.data[since:] |
||||||
|
count := 0 |
||||||
|
for seq, msg := range data { |
||||||
|
line = &PipelineFeedLine{ |
||||||
|
Seq: seq + since, |
||||||
|
Msg: msg, |
||||||
|
} |
||||||
|
lineRaw, err = json.Marshal(line) |
||||||
|
if err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
buffer.Write(lineRaw) |
||||||
|
buffer.WriteString("\n") |
||||||
|
|
||||||
|
count++ |
||||||
|
if limit > 0 && count >= limit { |
||||||
|
break |
||||||
|
} |
||||||
|
} |
||||||
|
pipeline.mutex.RUnlock() |
||||||
|
|
||||||
|
return buffer.Bytes(), nil |
||||||
|
} |
||||||
|
|
||||||
|
func (pipeline *Pipeline) FlushOutgoing(hub Hub, client *Client, to string, outgoing *DataOutgoing) bool { |
||||||
|
pipeline.mutex.RLock() |
||||||
|
log.Println("Flush outgoing via pipeline", to, client == nil) |
||||||
|
if client == nil { |
||||||
|
sink := pipeline.sink |
||||||
|
pipeline.mutex.RUnlock() |
||||||
|
pipeline.Add(outgoing) |
||||||
|
|
||||||
|
// It is possible to retrieve the userid for fake sessions here.
|
||||||
|
if session, found := pipeline.PipelineManager.GetSession(to); found { |
||||||
|
log.Println("Pipeline found userid via manager", session.Userid()) |
||||||
|
} |
||||||
|
|
||||||
|
if sink == nil { |
||||||
|
return true |
||||||
|
} |
||||||
|
// Sink it.
|
||||||
|
pipeline.sink.Write(outgoing) |
||||||
|
return true |
||||||
|
} |
||||||
|
pipeline.mutex.RUnlock() |
||||||
|
|
||||||
|
return false |
||||||
|
} |
||||||
|
|
||||||
|
func (pipeline *Pipeline) Attach(sink Sink) error { |
||||||
|
pipeline.mutex.Lock() |
||||||
|
defer pipeline.mutex.Unlock() |
||||||
|
if pipeline.sink != nil { |
||||||
|
return errors.New("pipeline already attached to sink") |
||||||
|
} |
||||||
|
pipeline.sink = sink |
||||||
|
|
||||||
|
// Sink existing data first.
|
||||||
|
log.Println("Attach sink to pipeline", pipeline.id) |
||||||
|
for _, msg := range pipeline.data { |
||||||
|
sink.Write(msg) |
||||||
|
} |
||||||
|
|
||||||
|
return nil |
||||||
|
} |
@ -0,0 +1,120 @@ |
|||||||
|
/* |
||||||
|
* Spreed WebRTC. |
||||||
|
* Copyright (C) 2013-2015 struktur AG |
||||||
|
* |
||||||
|
* This file is part of Spreed WebRTC. |
||||||
|
* |
||||||
|
* This program is free software: you can redistribute it and/or modify |
||||||
|
* it under the terms of the GNU Affero General Public License as published by |
||||||
|
* the Free Software Foundation, either version 3 of the License, or |
||||||
|
* (at your option) any later version. |
||||||
|
* |
||||||
|
* This program is distributed in the hope that it will be useful, |
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
* GNU Affero General Public License for more details. |
||||||
|
* |
||||||
|
* You should have received a copy of the GNU Affero General Public License |
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
package channelling |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
"log" |
||||||
|
"sync" |
||||||
|
"time" |
||||||
|
) |
||||||
|
|
||||||
|
const ( |
||||||
|
PipelineNamespaceCall = "call" |
||||||
|
) |
||||||
|
|
||||||
|
type PipelineManager interface { |
||||||
|
BusManager |
||||||
|
SessionStore |
||||||
|
UserStore |
||||||
|
GetPipelineByID(id string) (pipeline *Pipeline, ok bool) |
||||||
|
GetPipeline(namespace string, sender Sender, session *Session, to string) *Pipeline |
||||||
|
} |
||||||
|
|
||||||
|
type pipelineManager struct { |
||||||
|
BusManager |
||||||
|
SessionStore |
||||||
|
UserStore |
||||||
|
mutex sync.RWMutex |
||||||
|
pipelines map[string]*Pipeline |
||||||
|
duration time.Duration |
||||||
|
} |
||||||
|
|
||||||
|
func NewPipelineManager(busManager BusManager, sessionStore SessionStore, userStore UserStore) PipelineManager { |
||||||
|
plm := &pipelineManager{ |
||||||
|
BusManager: busManager, |
||||||
|
SessionStore: sessionStore, |
||||||
|
UserStore: userStore, |
||||||
|
pipelines: make(map[string]*Pipeline), |
||||||
|
duration: 30 * time.Minute, |
||||||
|
} |
||||||
|
plm.start() |
||||||
|
return plm |
||||||
|
} |
||||||
|
|
||||||
|
func (plm *pipelineManager) cleanup() { |
||||||
|
plm.mutex.Lock() |
||||||
|
for id, pipeline := range plm.pipelines { |
||||||
|
if pipeline.Expired() { |
||||||
|
pipeline.Close() |
||||||
|
delete(plm.pipelines, id) |
||||||
|
} |
||||||
|
} |
||||||
|
plm.mutex.Unlock() |
||||||
|
} |
||||||
|
|
||||||
|
func (plm *pipelineManager) start() { |
||||||
|
c := time.Tick(30 * time.Second) |
||||||
|
go func() { |
||||||
|
for _ = range c { |
||||||
|
plm.cleanup() |
||||||
|
} |
||||||
|
}() |
||||||
|
} |
||||||
|
|
||||||
|
func (plm *pipelineManager) GetPipelineByID(id string) (*Pipeline, bool) { |
||||||
|
plm.mutex.RLock() |
||||||
|
pipeline, ok := plm.pipelines[id] |
||||||
|
if !ok { |
||||||
|
// XXX(longsleep): Hack for development
|
||||||
|
for _, pipeline = range plm.pipelines { |
||||||
|
ok = true |
||||||
|
break |
||||||
|
} |
||||||
|
} |
||||||
|
plm.mutex.RUnlock() |
||||||
|
return pipeline, ok |
||||||
|
} |
||||||
|
|
||||||
|
func (plm *pipelineManager) PipelineID(namespace string, sender Sender, session *Session, to string) string { |
||||||
|
return fmt.Sprintf("%s.%s.%s", namespace, session.Id, to) |
||||||
|
} |
||||||
|
|
||||||
|
func (plm *pipelineManager) GetPipeline(namespace string, sender Sender, session *Session, to string) *Pipeline { |
||||||
|
id := plm.PipelineID(namespace, sender, session, to) |
||||||
|
|
||||||
|
plm.mutex.Lock() |
||||||
|
pipeline, ok := plm.pipelines[id] |
||||||
|
if ok { |
||||||
|
// Refresh. We do not care if the pipeline is expired.
|
||||||
|
pipeline.Refresh(plm.duration) |
||||||
|
plm.mutex.Unlock() |
||||||
|
return pipeline |
||||||
|
} |
||||||
|
|
||||||
|
log.Println("Creating pipeline", namespace, id) |
||||||
|
pipeline = NewPipeline(plm, namespace, id, session, plm.duration) |
||||||
|
plm.pipelines[id] = pipeline |
||||||
|
plm.mutex.Unlock() |
||||||
|
|
||||||
|
return pipeline |
||||||
|
} |
@ -0,0 +1,98 @@ |
|||||||
|
/* |
||||||
|
* Spreed WebRTC. |
||||||
|
* Copyright (C) 2013-2015 struktur AG |
||||||
|
* |
||||||
|
* This file is part of Spreed WebRTC. |
||||||
|
* |
||||||
|
* This program is free software: you can redistribute it and/or modify |
||||||
|
* it under the terms of the GNU Affero General Public License as published by |
||||||
|
* the Free Software Foundation, either version 3 of the License, or |
||||||
|
* (at your option) any later version. |
||||||
|
* |
||||||
|
* This program is distributed in the hope that it will be useful, |
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
* GNU Affero General Public License for more details. |
||||||
|
* |
||||||
|
* You should have received a copy of the GNU Affero General Public License |
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
package server |
||||||
|
|
||||||
|
import ( |
||||||
|
"encoding/json" |
||||||
|
"net/http" |
||||||
|
"strconv" |
||||||
|
|
||||||
|
"github.com/strukturag/spreed-webrtc/go/channelling" |
||||||
|
|
||||||
|
"github.com/gorilla/mux" |
||||||
|
) |
||||||
|
|
||||||
|
type Pipelines struct { |
||||||
|
channelling.PipelineManager |
||||||
|
API channelling.ChannellingAPI |
||||||
|
} |
||||||
|
|
||||||
|
func (pipelines *Pipelines) Get(request *http.Request) (int, interface{}, http.Header) { |
||||||
|
vars := mux.Vars(request) |
||||||
|
id, ok := vars["id"] |
||||||
|
if !ok { |
||||||
|
return http.StatusNotFound, "", nil |
||||||
|
} |
||||||
|
|
||||||
|
pipeline, ok := pipelines.GetPipelineByID(id) |
||||||
|
if !ok { |
||||||
|
return http.StatusNotFound, "", nil |
||||||
|
} |
||||||
|
|
||||||
|
since := 0 |
||||||
|
limit := 0 |
||||||
|
if sinceParam := request.Form.Get("since"); sinceParam != "" { |
||||||
|
since, _ = strconv.Atoi(sinceParam) |
||||||
|
} |
||||||
|
if limitParam := request.Form.Get("limit"); limitParam != "" { |
||||||
|
limit, _ = strconv.Atoi(limitParam) |
||||||
|
} |
||||||
|
|
||||||
|
result, err := pipeline.JSONFeed(since, limit) |
||||||
|
if err != nil { |
||||||
|
return http.StatusInternalServerError, err.Error(), nil |
||||||
|
} |
||||||
|
|
||||||
|
return http.StatusOK, result, nil |
||||||
|
} |
||||||
|
|
||||||
|
func (pipelines *Pipelines) Post(request *http.Request) (int, interface{}, http.Header) { |
||||||
|
vars := mux.Vars(request) |
||||||
|
id, ok := vars["id"] |
||||||
|
if !ok { |
||||||
|
return http.StatusNotFound, "", nil |
||||||
|
} |
||||||
|
|
||||||
|
pipeline, ok := pipelines.GetPipelineByID(id) |
||||||
|
if !ok { |
||||||
|
return http.StatusNotFound, "", nil |
||||||
|
} |
||||||
|
|
||||||
|
var incoming channelling.DataIncoming |
||||||
|
dec := json.NewDecoder(request.Body) |
||||||
|
if err := dec.Decode(&incoming); err != nil { |
||||||
|
return http.StatusBadRequest, err.Error(), nil |
||||||
|
} |
||||||
|
|
||||||
|
result := &channelling.DataOutgoing{ |
||||||
|
From: pipeline.Session().Id, |
||||||
|
Iid: incoming.Iid, |
||||||
|
} |
||||||
|
reply, err := pipelines.API.OnIncoming(pipeline, pipeline.Session(), &incoming) |
||||||
|
if err == nil { |
||||||
|
result.Data = reply |
||||||
|
} else { |
||||||
|
result.Data = err |
||||||
|
} |
||||||
|
|
||||||
|
return http.StatusOK, result, nil |
||||||
|
} |
@ -0,0 +1,32 @@ |
|||||||
|
/* |
||||||
|
* Spreed WebRTC. |
||||||
|
* Copyright (C) 2013-2015 struktur AG |
||||||
|
* |
||||||
|
* This file is part of Spreed WebRTC. |
||||||
|
* |
||||||
|
* This program is free software: you can redistribute it and/or modify |
||||||
|
* it under the terms of the GNU Affero General Public License as published by |
||||||
|
* the Free Software Foundation, either version 3 of the License, or |
||||||
|
* (at your option) any later version. |
||||||
|
* |
||||||
|
* This program is distributed in the hope that it will be useful, |
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
* GNU Affero General Public License for more details. |
||||||
|
* |
||||||
|
* You should have received a copy of the GNU Affero General Public License |
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
package channelling |
||||||
|
|
||||||
|
import () |
||||||
|
|
||||||
|
// Sink connects a Pipeline with end points in both directions by
|
||||||
|
// getting attached to a Pipeline.
|
||||||
|
type Sink interface { |
||||||
|
// Write sends outgoing data on the sink from the
|
||||||
|
Write(interface{}) |
||||||
|
Close() |
||||||
|
} |
@ -0,0 +1,26 @@ |
|||||||
|
/* |
||||||
|
* Spreed WebRTC. |
||||||
|
* Copyright (C) 2013-2015 struktur AG |
||||||
|
* |
||||||
|
* This file is part of Spreed WebRTC. |
||||||
|
* |
||||||
|
* This program is free software: you can redistribute it and/or modify |
||||||
|
* it under the terms of the GNU Affero General Public License as published by |
||||||
|
* the Free Software Foundation, either version 3 of the License, or |
||||||
|
* (at your option) any later version. |
||||||
|
* |
||||||
|
* This program is distributed in the hope that it will be useful, |
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
* GNU Affero General Public License for more details. |
||||||
|
* |
||||||
|
* You should have received a copy of the GNU Affero General Public License |
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
package channelling |
||||||
|
|
||||||
|
type UserStore interface { |
||||||
|
GetUser(id string) (user *User, ok bool) |
||||||
|
} |
Loading…
Reference in new issue