Skip to content

Commit 433f4e1

Browse files
committed
Fixed errors when closing the application.
1 parent 48c58d2 commit 433f4e1

File tree

2 files changed

+53
-19
lines changed

2 files changed

+53
-19
lines changed

Server/src/main/java/controllers/clients/ClientController.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
import org.slf4j.Logger;
99
import org.slf4j.LoggerFactory;
1010

11+
import java.io.Closeable;
12+
import java.io.IOException;
13+
1114

1215
/**
1316
* Created by Esteban Luchsinger on 26.03.2016.
1417
* This controller handles the clients connected to the server.
1518
*/
16-
public class ClientController {
19+
public class ClientController implements Closeable{
1720
private final Logger logger;
1821
private final ObservableList<NetworkClient> clients;
1922

@@ -60,4 +63,23 @@ public ObservableList<NetworkClient> getClients() {
6063
return this.clients;
6164
}
6265

66+
/**
67+
* Closes this stream and releases any system resources associated
68+
* with it. If the stream is already closed then invoking this
69+
* method has no effect.
70+
* <p>
71+
* <p> As noted in {@link AutoCloseable#close()}, cases where the
72+
* close may fail require careful attention. It is strongly advised
73+
* to relinquish the underlying resources and to internally
74+
* <em>mark</em> the {@code Closeable} as closed, prior to throwing
75+
* the {@code IOException}.
76+
*
77+
* @throws IOException if an I/O error occurs
78+
*/
79+
@Override
80+
public void close() throws IOException {
81+
for(NetworkClient client : this.getClients()) {
82+
client.close();
83+
}
84+
}
6385
}

Server/src/main/java/viewmodels/MainWindowViewModel.java

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,14 @@ public void setStage(Stage stage) {
161161
if(this.serverDiscoveryService != null) {
162162
this.serverDiscoveryService.stop();
163163
this.logger.info("Stopped Discovery Service!");
164-
if(this.musicStreamController != null && this.musicStreamController instanceof Closeable) {
165-
try {
166-
((Closeable)this.musicStreamController).close();
167-
} catch (IOException e) {
168-
this.logger.error("Could not close the StreamController", e);
169-
}
164+
165+
}
166+
167+
if(this.musicStreamController != null && this.musicStreamController instanceof Closeable) {
168+
try {
169+
((Closeable)this.musicStreamController).close();
170+
} catch (IOException e) {
171+
this.logger.error("Could not close the StreamController", e);
170172
}
171173
}
172174

@@ -177,6 +179,14 @@ public void setStage(Stage stage) {
177179
this.logger.error("Could not close TCP Server", e);
178180
}
179181
}
182+
183+
if(this.clientController != null) {
184+
try {
185+
this.clientController.close();
186+
} catch (IOException e) {
187+
this.logger.error("Could not close client controller", e);
188+
}
189+
}
180190
});
181191
}
182192
}
@@ -206,9 +216,22 @@ public void onButtonPlayPauseClicked() {
206216

207217
if (this.mediaPlayer.isPlaying()) {
208218
this.mediaPlayer.pause();
219+
this.musicStreamController.stop();
209220
} else if (this.getSelectedSong() != null) {
210221
this.logger.info("Trying to play: " + this.getSelectedSong().getTitle());
211-
this.mediaPlayer.play(this.getSelectedSong(), true);
222+
223+
if(this.getSelectedSong() != null) {
224+
// Start streaming...
225+
this.logger.info("Streaming the new song: " + this.getSelectedSong().getTitle());
226+
try {
227+
this.musicStreamController.play(this.getSelectedSong());
228+
this.mediaPlayer.play(this.getSelectedSong(), true);
229+
}
230+
catch(IOException ioException) {
231+
this.logger.error("Error trying to stream", ioException);
232+
}
233+
}
234+
212235
}
213236
}
214237

@@ -251,20 +274,9 @@ public void onIsPlayingChanged() {
251274
this.buttonPlayPause.setId("pause-button");
252275
this.buttonPlayPause.setSelected(true);
253276

254-
if(this.getSelectedSong() != null) {
255-
// Start streaming...
256-
this.logger.info("Streaming the new song: " + this.getSelectedSong().getTitle());
257-
try {
258-
this.musicStreamController.play(this.getSelectedSong());
259-
}
260-
catch(IOException ioException) {
261-
this.logger.error("Error trying to stream", ioException);
262-
}
263-
}
264277
} else {
265278
this.buttonPlayPause.setId("play-button");
266279
this.buttonPlayPause.setSelected(false);
267-
this.musicStreamController.stop();
268280
}
269281
}
270282
//endregion

0 commit comments

Comments
 (0)