Skip to content

Commit 5b53af7

Browse files
committed
Dispatch session closing when using Zeroconf
1 parent 39a6950 commit 5b53af7

4 files changed

Lines changed: 43 additions & 3 deletions

File tree

api/src/main/java/xyz/gianlu/librespot/api/PlayerWrapper.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,18 @@ private PlayerWrapper(@NotNull PlayerConfiguration conf) {
3131
@NotNull
3232
public static PlayerWrapper fromZeroconf(@NotNull ZeroconfServer server, @NotNull PlayerConfiguration conf) {
3333
PlayerWrapper wrapper = new PlayerWrapper(conf);
34-
server.addSessionListener(wrapper::set);
34+
server.addSessionListener(new ZeroconfServer.SessionListener() {
35+
@Override
36+
public void sessionClosing(@NotNull Session session) {
37+
if (wrapper.getSession() == session)
38+
wrapper.clear();
39+
}
40+
41+
@Override
42+
public void sessionChanged(@NotNull Session session) {
43+
wrapper.set(session);
44+
}
45+
});
3546
return wrapper;
3647
}
3748

api/src/main/java/xyz/gianlu/librespot/api/SessionWrapper.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,18 @@ protected SessionWrapper() {
2626
@NotNull
2727
public static SessionWrapper fromZeroconf(@NotNull ZeroconfServer server) {
2828
SessionWrapper wrapper = new SessionWrapper();
29-
server.addSessionListener(wrapper::set);
29+
server.addSessionListener(new ZeroconfServer.SessionListener() {
30+
@Override
31+
public void sessionClosing(@NotNull Session session) {
32+
if (wrapper.getSession() == session)
33+
wrapper.clear();
34+
}
35+
36+
@Override
37+
public void sessionChanged(@NotNull Session session) {
38+
wrapper.set(session);
39+
}
40+
});
3041
return wrapper;
3142
}
3243

lib/src/main/java/xyz/gianlu/librespot/ZeroconfServer.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ public void close() throws IOException {
201201
}
202202

203203
public void closeSession() throws IOException {
204+
sessionListeners.forEach(l -> l.sessionClosing(session));
205+
204206
if (session != null) session.close();
205207
session = null;
206208
}
@@ -377,6 +379,18 @@ public void removeSessionListener(@NotNull SessionListener listener) {
377379
}
378380

379381
public interface SessionListener {
382+
/**
383+
* The session instance is going to be closed after this call.
384+
*
385+
* @param session The old {@link Session}
386+
*/
387+
void sessionClosing(@NotNull Session session);
388+
389+
/**
390+
* The session instance changed. {@link #sessionClosing(Session)} has been already called.
391+
*
392+
* @param session The new {@link Session}
393+
*/
380394
void sessionChanged(@NotNull Session session);
381395
}
382396

player/src/main/java/xyz/gianlu/librespot/player/Main.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
3333
}
3434

3535
@Override
36-
public void sessionChanged(@NotNull Session session) {
36+
public void sessionClosing(@NotNull Session session) {
3737
if (lastPlayer != null) lastPlayer.close();
38+
}
39+
40+
@Override
41+
public void sessionChanged(@NotNull Session session) {
3842
lastPlayer = new Player(conf.toPlayer(), session);
3943
}
4044
});

0 commit comments

Comments
 (0)