Update package telnet with Quality structure

This commit is contained in:
Alexandre Iooss 2020-10-19 19:44:30 +02:00
parent 069b2155be
commit 340d0447a8
No known key found for this signature in database
GPG Key ID: 6C79278F3FCDCC02
2 changed files with 26 additions and 16 deletions

View File

@ -7,7 +7,7 @@ import (
"strings" "strings"
"time" "time"
"gitlab.crans.org/nounous/ghostream/stream" "gitlab.crans.org/nounous/ghostream/messaging"
) )
// Options holds telnet package configuration // Options holds telnet package configuration
@ -17,7 +17,7 @@ type Options struct {
} }
// Serve Telnet server // Serve Telnet server
func Serve(streams map[string]*stream.Stream, cfg *Options) { func Serve(streams *messaging.Streams, cfg *Options) {
if !cfg.Enabled { if !cfg.Enabled {
// Telnet is not enabled, ignore // Telnet is not enabled, ignore
return return
@ -32,17 +32,17 @@ func Serve(streams map[string]*stream.Stream, cfg *Options) {
// Handle each new client // Handle each new client
for { for {
s, err := listener.Accept() socket, err := listener.Accept()
if err != nil { if err != nil {
log.Printf("Error while accepting TCP socket: %s", s) log.Printf("Error while accepting TCP socket: %s", err)
continue continue
} }
go handleViewer(s, streams, cfg) go handleViewer(socket, streams, cfg)
} }
} }
func handleViewer(s net.Conn, streams map[string]*stream.Stream, cfg *Options) { func handleViewer(s net.Conn, streams *messaging.Streams, cfg *Options) {
// Prompt user about stream name // Prompt user about stream name
if _, err := s.Write([]byte("[GHOSTREAM]\nEnter stream name: ")); err != nil { if _, err := s.Write([]byte("[GHOSTREAM]\nEnter stream name: ")); err != nil {
log.Printf("Error while writing to TCP socket: %s", err) log.Printf("Error while writing to TCP socket: %s", err)
@ -67,9 +67,9 @@ func handleViewer(s net.Conn, streams map[string]*stream.Stream, cfg *Options) {
time.Sleep(time.Second) time.Sleep(time.Second)
// Get requested stream // Get requested stream
st, ok := streams[name] stream, err := streams.Get(name)
if !ok { if err != nil {
log.Println("Stream does not exist, kicking new Telnet viewer") log.Printf("Stream does not exist, kicking new Telnet viewer: %s", err)
if _, err := s.Write([]byte("This stream is inactive.\n")); err != nil { if _, err := s.Write([]byte("This stream is inactive.\n")); err != nil {
log.Printf("Error while writing to TCP socket: %s", err) log.Printf("Error while writing to TCP socket: %s", err)
} }
@ -77,11 +77,21 @@ func handleViewer(s net.Conn, streams map[string]*stream.Stream, cfg *Options) {
return return
} }
// Get requested quality
// FIXME: make qualities available
qualityName := "source"
q, err := stream.GetQuality(qualityName)
if err != nil {
log.Printf("Failed to get quality: %s", err)
s.Close()
return
}
log.Printf("New Telnet viewer for stream %s quality %s", name, qualityName)
// Register new client // Register new client
log.Printf("New Telnet viewer for stream '%s'", name)
c := make(chan []byte, 128) c := make(chan []byte, 128)
st.Register(c) q.Register(c)
st.IncrementClientCount() stream.IncrementClientCount()
// Hide terminal cursor // Hide terminal cursor
if _, err = s.Write([]byte("\033[?25l")); err != nil { if _, err = s.Write([]byte("\033[?25l")); err != nil {
@ -106,7 +116,7 @@ func handleViewer(s net.Conn, streams map[string]*stream.Stream, cfg *Options) {
} }
// Close output // Close output
st.Unregister(c) q.Unregister(c)
st.DecrementClientCount() stream.DecrementClientCount()
s.Close() s.Close()
} }

View File

@ -3,13 +3,13 @@ package telnet
import ( import (
"testing" "testing"
"gitlab.crans.org/nounous/ghostream/stream" "gitlab.crans.org/nounous/ghostream/messaging"
) )
// TestTelnetOutput creates a TCP client that connects to the server and get one image. // TestTelnetOutput creates a TCP client that connects to the server and get one image.
func TestTelnetOutput(t *testing.T) { func TestTelnetOutput(t *testing.T) {
// Try to start Telnet server while it is disabled // Try to start Telnet server while it is disabled
streams := make(map[string]*stream.Stream) streams := messaging.New()
go Serve(streams, &Options{Enabled: false}) go Serve(streams, &Options{Enabled: false})
// FIXME test connect // FIXME test connect