This commit is contained in:
Drew Weymouth
2024-06-10 08:28:41 -07:00
parent 969c30f35a
commit e0974fdb03
2 changed files with 51 additions and 24 deletions
+37 -22
View File
@@ -12,6 +12,7 @@ import (
"slices" "slices"
"time" "time"
"github.com/dweymouth/supersonic/backend/ipc"
"github.com/dweymouth/supersonic/backend/mediaprovider" "github.com/dweymouth/supersonic/backend/mediaprovider"
"github.com/dweymouth/supersonic/backend/player" "github.com/dweymouth/supersonic/backend/player"
"github.com/dweymouth/supersonic/backend/player/mpv" "github.com/dweymouth/supersonic/backend/player/mpv"
@@ -83,22 +84,31 @@ func StartupApp(appName, displayAppName, appVersionTag, latestReleaseURL string)
configdir.MakePath(confDir) configdir.MakePath(confDir)
configdir.MakePath(cacheDir) configdir.MakePath(cacheDir)
sessionPath := path.Join(confDir, sessionDir) cli, err := ipc.Connect()
if _, err := os.Stat(path.Join(sessionPath, sessionLockFile)); err == nil { if err == nil {
log.Println("Another instance is running. Reactivating it...") log.Println("Another instance is running. Reactivating it...")
reactivateFile := path.Join(sessionPath, sessionActivateFile) cli.Show()
if f, err := os.Create(reactivateFile); err == nil { return nil, ErrAnotherInstance
f.Close()
}
time.Sleep(750 * time.Millisecond)
if _, err := os.Stat(reactivateFile); err == nil {
log.Println("No other instance responded. Starting as normal...")
os.RemoveAll(sessionPath)
} else {
return nil, ErrAnotherInstance
}
} }
/*
sessionPath := path.Join(confDir, sessionDir)
if _, err := os.Stat(path.Join(sessionPath, sessionLockFile)); err == nil {
log.Println("Another instance is running. Reactivating it...")
reactivateFile := path.Join(sessionPath, sessionActivateFile)
if f, err := os.Create(reactivateFile); err == nil {
f.Close()
}
time.Sleep(750 * time.Millisecond)
if _, err := os.Stat(reactivateFile); err == nil {
log.Println("No other instance responded. Starting as normal...")
os.RemoveAll(sessionPath)
} else {
return nil, ErrAnotherInstance
}
}
*/
log.Printf("Starting %s...", appName) log.Printf("Starting %s...", appName)
log.Printf("Using config dir: %s", confDir) log.Printf("Using config dir: %s", confDir)
log.Printf("Using cache dir: %s", cacheDir) log.Printf("Using cache dir: %s", cacheDir)
@@ -114,16 +124,18 @@ func StartupApp(appName, displayAppName, appVersionTag, latestReleaseURL string)
a.readConfig() a.readConfig()
a.startConfigWriter(a.bgrndCtx) a.startConfigWriter(a.bgrndCtx)
if !a.Config.Application.AllowMultiInstance { /*
log.Println("Creating session lock file") if !a.Config.Application.AllowMultiInstance {
os.MkdirAll(sessionPath, 0770) log.Println("Creating session lock file")
if f, err := os.Create(path.Join(sessionPath, sessionLockFile)); err == nil { os.MkdirAll(sessionPath, 0770)
f.Close() if f, err := os.Create(path.Join(sessionPath, sessionLockFile)); err == nil {
} else { f.Close()
log.Printf("error creating session file: %s", err.Error()) } else {
log.Printf("error creating session file: %s", err.Error())
}
a.startSessionWatcher(sessionPath)
} }
a.startSessionWatcher(sessionPath) */
}
a.UpdateChecker = NewUpdateChecker(appVersionTag, latestReleaseURL, &a.Config.Application.LastCheckedVersion) a.UpdateChecker = NewUpdateChecker(appVersionTag, latestReleaseURL, &a.Config.Application.LastCheckedVersion)
a.UpdateChecker.Start(a.bgrndCtx, 24*time.Hour) a.UpdateChecker.Start(a.bgrndCtx, 24*time.Hour)
@@ -143,6 +155,9 @@ func StartupApp(appName, displayAppName, appVersionTag, latestReleaseURL string)
a.ServerManager.SetPrefetchAlbumCoverCallback(func(coverID string) { a.ServerManager.SetPrefetchAlbumCoverCallback(func(coverID string) {
_, _ = a.ImageManager.GetCoverThumbnail(coverID) _, _ = a.ImageManager.GetCoverThumbnail(coverID)
}) })
listener, _ := ipc.Listen()
server := ipc.NewServer(a.PlaybackManager, nil)
go server.Serve(listener)
// OS media center integrations // OS media center integrations
a.setupMPRIS(displayAppName) a.setupMPRIS(displayAppName)
+14 -2
View File
@@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"log"
"net" "net"
"net/http" "net/http"
) )
@@ -18,6 +19,7 @@ type Client struct {
func Connect() (*Client, error) { func Connect() (*Client, error) {
conn, err := Dial() conn, err := Dial()
if err != nil { if err != nil {
log.Println("dial error")
return nil, err return nil, err
} }
client := &Client{httpC: http.Client{ client := &Client{httpC: http.Client{
@@ -28,6 +30,7 @@ func Connect() (*Client, error) {
}, },
}} }}
if err := client.Ping(); err != nil { if err := client.Ping(); err != nil {
log.Println("ping error")
return nil, err return nil, err
} }
return client, nil return client, nil
@@ -60,17 +63,26 @@ func (c *Client) SeekBackOrPrevious() error {
return c.makeSimpleRequest(http.MethodPost, NextPath) return c.makeSimpleRequest(http.MethodPost, NextPath)
} }
func (c *Client) Show() error {
return c.makeSimpleRequest(http.MethodPost, ShowPath)
}
func (c *Client) Quit() error {
return c.makeSimpleRequest(http.MethodPost, QuitPath)
}
func (c *Client) makeSimpleRequest(method string, path string) error { func (c *Client) makeSimpleRequest(method string, path string) error {
var resp *http.Response var resp *http.Response
var err error var err error
switch method { switch method {
case http.MethodGet: case http.MethodGet:
resp, err = c.httpC.Get(path) resp, err = c.httpC.Get("http://supersonic/" + path)
case http.MethodPost: case http.MethodPost:
resp, err = c.httpC.Post(path, "application/json", nil) resp, err = c.httpC.Post("http://supersonic/"+path, "application/json", nil)
} }
if err != nil { if err != nil {
log.Printf("http err: %v\n", err)
return err return err
} }
defer resp.Body.Close() defer resp.Body.Close()