Skip to content

Commit

Permalink
more WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Jun 10, 2024
1 parent 969c30f commit e0974fd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 24 deletions.
59 changes: 37 additions & 22 deletions backend/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"slices"
"time"

"github.com/dweymouth/supersonic/backend/ipc"
"github.com/dweymouth/supersonic/backend/mediaprovider"
"github.com/dweymouth/supersonic/backend/player"
"github.com/dweymouth/supersonic/backend/player/mpv"
Expand Down Expand Up @@ -83,22 +84,31 @@ func StartupApp(appName, displayAppName, appVersionTag, latestReleaseURL string)
configdir.MakePath(confDir)
configdir.MakePath(cacheDir)

sessionPath := path.Join(confDir, sessionDir)
if _, err := os.Stat(path.Join(sessionPath, sessionLockFile)); err == nil {
cli, err := ipc.Connect()
if 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
}
cli.Show()
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("Using config dir: %s", confDir)
log.Printf("Using cache dir: %s", cacheDir)
Expand All @@ -114,16 +124,18 @@ func StartupApp(appName, displayAppName, appVersionTag, latestReleaseURL string)
a.readConfig()
a.startConfigWriter(a.bgrndCtx)

if !a.Config.Application.AllowMultiInstance {
log.Println("Creating session lock file")
os.MkdirAll(sessionPath, 0770)
if f, err := os.Create(path.Join(sessionPath, sessionLockFile)); err == nil {
f.Close()
} else {
log.Printf("error creating session file: %s", err.Error())
/*
if !a.Config.Application.AllowMultiInstance {
log.Println("Creating session lock file")
os.MkdirAll(sessionPath, 0770)
if f, err := os.Create(path.Join(sessionPath, sessionLockFile)); err == nil {
f.Close()
} 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.Start(a.bgrndCtx, 24*time.Hour)
Expand All @@ -143,6 +155,9 @@ func StartupApp(appName, displayAppName, appVersionTag, latestReleaseURL string)
a.ServerManager.SetPrefetchAlbumCoverCallback(func(coverID string) {
_, _ = a.ImageManager.GetCoverThumbnail(coverID)
})
listener, _ := ipc.Listen()
server := ipc.NewServer(a.PlaybackManager, nil)
go server.Serve(listener)

// OS media center integrations
a.setupMPRIS(displayAppName)
Expand Down
16 changes: 14 additions & 2 deletions backend/ipc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"log"
"net"
"net/http"
)
Expand All @@ -18,6 +19,7 @@ type Client struct {
func Connect() (*Client, error) {
conn, err := Dial()
if err != nil {
log.Println("dial error")
return nil, err
}
client := &Client{httpC: http.Client{
Expand All @@ -28,6 +30,7 @@ func Connect() (*Client, error) {
},
}}
if err := client.Ping(); err != nil {
log.Println("ping error")
return nil, err
}
return client, nil
Expand Down Expand Up @@ -60,17 +63,26 @@ func (c *Client) SeekBackOrPrevious() error {
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 {
var resp *http.Response
var err error
switch method {
case http.MethodGet:
resp, err = c.httpC.Get(path)
resp, err = c.httpC.Get("http://supersonic/" + path)
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 {
log.Printf("http err: %v\n", err)
return err
}
defer resp.Body.Close()
Expand Down

0 comments on commit e0974fd

Please sign in to comment.