Skip to content

Commit

Permalink
Couple of fixes in PlayerWrapper and SessionWrapper + run closing async
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Jan 15, 2021
1 parent f831010 commit b288bd1
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 7 deletions.
8 changes: 6 additions & 2 deletions api/src/main/java/xyz/gianlu/librespot/api/ApiServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ public class ApiServer {
private static final Logger LOGGER = LogManager.getLogger(ApiServer.class);
protected final RoutingHandler handler;
protected final EventsHandler events = new EventsHandler();
private final SessionWrapper wrapper;
private final int port;
private final String host;
private Undertow undertow = null;

public ApiServer(int port, @NotNull String host, @NotNull SessionWrapper wrapper) {
this.port = port;
this.host = host;
this.wrapper = wrapper;
this.handler = new RoutingHandler()
.post("/metadata/{type}/{uri}", new MetadataHandler(wrapper, true))
.post("/metadata/{uri}", new MetadataHandler(wrapper, false))
Expand All @@ -44,11 +46,13 @@ public void start() {
}

public void stop() {
wrapper.clear();

if (undertow != null) {
undertow.stop();
undertow = null;
}

LOGGER.info("Server stopped!");
LOGGER.info("Server stopped!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
* @author devgianlu
*/
public class PlayerApiServer extends ApiServer {
private final PlayerWrapper wrapper;

public PlayerApiServer(int port, @NotNull String host, @NotNull PlayerWrapper wrapper) {
super(port, host, wrapper);
this.wrapper = wrapper;

handler.post("/player/{cmd}", new PlayerHandler(wrapper));
handler.post("/instance/{action}", InstanceHandler.forPlayer(this, wrapper)); // Overrides session only handler
Expand Down
4 changes: 2 additions & 2 deletions api/src/main/java/xyz/gianlu/librespot/api/PlayerWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ protected void set(@NotNull Session session) {

@Override
protected void clear() {
super.clear();

Player old = playerRef.get();
if (old != null) old.close();
playerRef.set(null);

if (listener != null && old != null) listener.onPlayerCleared(old);

super.clear();
}

@Nullable
Expand Down
10 changes: 9 additions & 1 deletion api/src/main/java/xyz/gianlu/librespot/api/SessionWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import xyz.gianlu.librespot.ZeroconfServer;
import xyz.gianlu.librespot.core.Session;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicReference;

/**
Expand Down Expand Up @@ -70,7 +71,14 @@ protected void set(@NotNull Session session) {
protected void clear() {
Session old = sessionRef.get();
sessionRef.set(null);
if (listener != null && old != null) listener.onSessionCleared(old);
if (old != null) {
try {
old.close();
} catch (IOException ignored) {
}

if (listener != null) listener.onSessionCleared(old);
}
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected void handleRequest(@NotNull HttpServerExchange exchange, @NotNull Sess

switch (action) {
case "terminate":
server.stop();
new Thread(server::stop).start();
break;
case "close":
session.close();
Expand Down Expand Up @@ -93,7 +93,7 @@ protected void handleRequest(@NotNull HttpServerExchange exchange, @NotNull Sess

switch (action) {
case "terminate":
server.stop();
new Thread(server::stop).start();
break;
case "close":
player.close();
Expand Down
2 changes: 2 additions & 0 deletions player/src/main/java/xyz/gianlu/librespot/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,8 @@ public void close() {

scheduler.shutdown();
events.close();

LOGGER.info("Closed player.");
}

public interface EventsListener {
Expand Down

0 comments on commit b288bd1

Please sign in to comment.