Refactor main

This commit is contained in:
Kristóf Tóth 2020-05-06 15:41:14 +02:00
parent aeb974a820
commit e234be7f0a
3 changed files with 114 additions and 74 deletions

44
listen.go Normal file
View File

@ -0,0 +1,44 @@
package main
import (
"fmt"
"time"
"remote-mic/networking"
"remote-mic/audio"
)
func listen() error {
fmt.Print("Listening for connection on port 8080... ")
conn, err := networking.ListenAndAcceptTCP(":8080")
if err != nil {
return err
}
reader := networking.NewAsyncReader(conn)
defer reader.Close()
fmt.Println("Connection accepted!")
recvChan := reader.RecvChan()
for {
buf, open := <-recvChan
if !open {
break
}
fmt.Printf("Bytes read: %d\n", len(buf))
fmt.Printf("Buf: %v\n", buf)
fmt.Printf("%v\n", string(buf))
}
return nil
}
func pulsectlExample() {
pulsectl := audio.NewPulsectl()
err := pulsectl.LoadPipeSourceModule()
if err != nil {
panic(err)
}
time.Sleep(10 * time.Second)
pulsectl.UnloadPipeSourceModule()
}

101
main.go
View File

@ -2,93 +2,46 @@ package main
import ( import (
"fmt" "fmt"
"io" "log"
"os" "os"
"remote-mic/audio"
"remote-mic/networking"
"time"
) )
func pulsectlExample() {
pulsectl := audio.NewPulsectl()
err := pulsectl.LoadPipeSourceModule()
if err != nil {
panic(err)
}
time.Sleep(10 * time.Second)
pulsectl.UnloadPipeSourceModule()
}
func micStreamExample() {
mic, err := audio.NewMicrophone()
if err != nil {
panic(err)
}
err = mic.Start()
if err != nil {
panic(err)
}
defer mic.Stop()
for {
io.Copy(os.Stdout, mic.AudioStream())
}
}
func usage() {
fmt.Println("Usage: remote-mic <command>")
os.Exit(1)
}
func listen() {
fmt.Print("Listening for connection on port 8080... ")
conn, err := networking.ListenAndAcceptTCP(":8080")
if err != nil {
panic(err)
}
reader := networking.NewAsyncReader(conn)
defer reader.Close()
fmt.Println("Connection accepted!")
recvChan := reader.RecvChan()
for {
buf, open := <-recvChan
if !open {
break
}
fmt.Printf("Bytes read: %d\n", len(buf))
fmt.Printf("Buf: %v\n", buf)
fmt.Printf("%v\n", string(buf))
}
}
func stream() {
conn, err := networking.ConnectTCP("localhost:8080")
if err != nil {
panic(err)
}
writer := networking.NewAsyncWriter(conn)
defer writer.Close()
c := writer.SendChan()
c <- []byte("cicasajtok")
fmt.Println("Exiting.")
}
func main() { func main() {
cmd := tryParseCmd()
err := dispatchCmd(cmd)
if err != nil {
log.Fatalln(err)
}
}
func tryParseCmd() string {
if len(os.Args) < 2 { if len(os.Args) < 2 {
usage() usage()
} }
cmd := os.Args[1] return os.Args[1]
}
func usage() {
fmt.Println("Usage: remote-mic [listen|stream]")
os.Exit(1)
}
func dispatchCmd(cmd string) error {
switch cmd { switch cmd {
case "listen": case "listen":
listen() err := listen()
if err != nil {
return err
}
case "stream": case "stream":
stream() err := stream()
if err != nil {
return err
}
default: default:
usage() usage()
} }
return nil
} }

43
stream.go Normal file
View File

@ -0,0 +1,43 @@
package main
import (
"fmt"
"io"
"os"
"remote-mic/networking"
"remote-mic/audio"
)
func stream() error {
conn, err := networking.ConnectTCP("localhost:8080")
if err != nil {
return err
}
writer := networking.NewAsyncWriter(conn)
defer writer.Close()
c := writer.SendChan()
c <- []byte("cicasajtok")
fmt.Println("Exiting.")
return nil
}
func micStreamExample() {
mic, err := audio.NewMicrophone()
if err != nil {
panic(err)
}
err = mic.Start()
if err != nil {
panic(err)
}
defer mic.Stop()
for {
io.Copy(os.Stdout, mic.AudioStream())
}
}