-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
166 lines (129 loc) · 4.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"encoding/json"
"fmt"
"os"
"os/signal"
"runtime"
"sync/atomic"
"syscall"
"time"
"unsafe"
"golang.org/x/sys/unix"
"github.com/dwood15/mediaplayer/sockets"
"github.com/dwood15/mediaplayer/songplayer"
)
func init() {
//This section is my (pitiful) attempt at keeping clean-boot performance reasonable
runtime.GOMAXPROCS(3)
if err := syscall.Setpriority(syscall.PRIO_PROCESS, 0x0, 19); err != nil {
panic("failed setting process priority")
}
cfg := loadConfig()
songplayer.SetLibraryDir(cfg.MusicDir)
songplayer.SetPlaylistMaxSize(cfg.MaxPlaylistSize)
go handleShutdown()
}
const sockName = "/tmp/mediaplayer.sock"
const stIdx = int(unsafe.Offsetof(songplayer.PlayingSong{}.SongTime))
const ssIdx = int(unsafe.Offsetof(songplayer.PlayingSong{}.SongScore))
const slIdx = int(unsafe.Offsetof(songplayer.PlayingSong{}.SongLength))
const sNIdx = int(unsafe.Offsetof(songplayer.PlayingSong{}.CurrentSong))
const szOf = int(unsafe.Sizeof(songplayer.PlayingSong{}))
func isClosed(fd int) bool {
b := make([]byte, 1)
n, _, err := unix.Recvfrom(fd, b, unix.MSG_DONTWAIT|unix.MSG_PEEK)
if err != nil && !(err.(unix.Errno)).Temporary() {
fmt.Printf("connection closed potentially detected! err:\n\t[%v]\n", err)
return true
}
return n == 0
}
func amServ() {
fmt.Println("Client not found, assuming we're the server.")
addr := &unix.SockaddrUnix{Name: sockName}
srv := sockets.Server{
SockAddr: addr,
OnConnect: func(cFD int, done chan bool) {
fmt.Println("client connection made!")
for {
select {
case ss := <-songplayer.SongState:
//binary.PutVarint(bytesToSend[stIdx:stIdx+10:szOf], int64(ss.SongTime))
//binary.PutUvarint(bytesToSend[ssIdx:ssIdx+10:szOf], ss.SongScore)
//binary.PutVarint(bytesToSend[slIdx:slIdx+10:szOf], int64(ss.SongLength))
//
//copy(bytesToSend[sNIdx:], ss.CurrentSong)
//fmt.Printf("num bytes Copied to toSend slice: [%d]\n", n)
/* QUARANTINE SECTION */
//THIS SECTION OF CODE IS INCREDIBLY FINNICKY, AND I HAVEN'T
// SPENT TIME TO FIGURE OUT HOW TO MAKE IT ROBUST...
// CONSIDER WRITING SOME UNIT TESTS OR MAKING INCREDIBLY MINOR
// CHANGES BETWEEN TESTS
bytesToSend, _ := json.Marshal(ss)
if len(bytesToSend) > szOf + 128 {
fmt.Println("WARNING: NUMBER OF BYTES LARGER THAN CLIENT EXPECTS: ", len(bytesToSend))
}
if _, err := unix.Write(cFD, bytesToSend); err != nil {
fmt.Printf("non-nil err when attempting sendTo: %v\n", err)
}
//SEE ALSO: sockets/client.go
/* QUARANTINE SECTION */
case <-done:
fmt.Println("close signal detected, closing connection")
return
default:
if isClosed(cFD) {
fmt.Println("client connection closed. returning from OnConnect")
return
}
}
}
},
}
go func() {
if err := srv.LaunchServer(); err != nil {
panic("launchsrvr: " + err.Error())
}
}()
time.Sleep(1 * time.Second)
fmt.Println("loading library and preparing to play")
fmt.Println("server will wait for incoming connection before playing")
//BeginPlaying enters into an infinite loop
songplayer.GetLibrary().BeginPlaying()
}
var uiInput = make(chan int64)
var state = new(atomic.Value)
func main() {
state.Store(songplayer.PlayingSong{})
c := sockets.Client{
Addr: &unix.SockaddrUnix{Name: sockName},
ServerSongState: state,
}
fmt.Println("attempting to launch ui client")
if err := c.LaunchClient(uiInput); err != nil {
amServ()
os.Exit(0)
}
time.Sleep(7 * time.Second)
f, err := os.OpenFile("stderr.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
//Attempt to redirect panics and regular stderr messages to stderr.log
_ = syscall.Dup2(int(f.Fd()), 2)
uiC := UIController{
SongState: state,
InputChan: uiInput,
}
uiC.launchUI()
}
func handleShutdown() {
// Handle graceful shutdown
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
<-quit
//Indicate to the player that we're about to GO DOWN
uiInput <- songplayer.SignalExit
os.Exit(0)
}