Skip to content

Commit f6121dd

Browse files
committed
Several fixes.
Prepare for Socket-Revision.
1 parent 4231d17 commit f6121dd

File tree

8 files changed

+102
-26
lines changed

8 files changed

+102
-26
lines changed

Client/src/main/java/controllers/networking/streaming/music/TCPMusicStreamingService.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ public void start() {
9191
this.setCurrentServiceStatus(ServiceStatus.WAITING);
9292
} catch (IOException exception) {
9393
this.running = false;
94-
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Starting Streaming Service", exception);
94+
Logger.getLogger(this.getClass().getName())
95+
.log(Level.SEVERE, "Starting Streaming Service", exception);
9596
}
9697
}
9798

@@ -112,6 +113,18 @@ public void stop() {
112113
} finally {
113114
if (this.listeningThread == null || !this.listeningThread.isAlive())
114115
this.setCurrentServiceStatus(ServiceStatus.STOPPED);
116+
117+
// Close the socket.
118+
if(this.getSocket() != null) {
119+
try {
120+
if(!this.getSocket().isClosed())
121+
this.getSocket().close();
122+
} catch (IOException e) {
123+
Logger.getLogger(this.getClass().getName())
124+
.log(Level.SEVERE,
125+
"Error closing the open socket", e);
126+
}
127+
}
115128
}
116129
}
117130

@@ -192,25 +205,34 @@ private synchronized Socket getSocket() {
192205
* @throws IOException
193206
*/
194207
private synchronized void setSocket(Socket socket) throws IOException {
195-
this.socket = socket;
196208

197-
if(this.socket != null) {
198-
if (!this.socket.isInputShutdown()) {
199-
this.currentOIS =
200-
new ObjectInputStream(socket.getInputStream());
209+
// Change only if the socket really changed.
210+
// (By Reference)
211+
if(this.socket != socket) {
212+
// If the socket is not null, close it first!
213+
if(this.socket != null && !this.socket.isClosed()) {
214+
this.socket.close();
215+
}
216+
217+
this.socket = socket;
218+
219+
if (this.socket != null) {
220+
if (!this.socket.isInputShutdown()) {
221+
this.currentOIS =
222+
new ObjectInputStream(socket.getInputStream());
223+
} else {
224+
if (this.currentOIS != null) {
225+
this.currentOIS.close();
226+
this.currentOIS = null;
227+
}
228+
}
201229
} else {
202230
if (this.currentOIS != null) {
203231
this.currentOIS.close();
204232
this.currentOIS = null;
205233
}
206234
}
207235
}
208-
else {
209-
if(this.currentOIS != null) {
210-
this.currentOIS.close();
211-
this.currentOIS = null;
212-
}
213-
}
214236
}
215237

216238
/**

Client/src/main/java/viewmodels/ClientWindowViewModel.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public class ClientWindowViewModel {
3737

3838
private MediaPlayer mediaPlayer;
3939

40-
private ServiceStatus lastStatus = ServiceStatus.STOPPED;
4140

4241
/**
4342
* Constructor
@@ -67,10 +66,8 @@ public void setStage(Stage stage) {
6766

6867
if(this.stage != null){
6968
this.stage.setOnCloseRequest(event -> {
70-
7169
this.musicStreamingService.stop();
7270
this.discoveryService.stop();
73-
7471
});
7572
}
7673
}

Server/src/main/java/controllers/networking/discovery/DiscoveryService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public class DiscoveryService {
8686
/**
8787
* This boolean controls the finishing of the
8888
* responseThread. If the thread is started, it is set to true.
89-
* If the isListening is set to false, the thread will stop (probable delay: DiscoveryService.READING_TIMEOUT)
89+
* If the isListening is set to false, the thread will stopPlaying (probable delay: DiscoveryService.READING_TIMEOUT)
9090
*/
9191
private volatile boolean isListening = false;
9292

Server/src/main/java/controllers/networking/streaming/music/MusicStreamController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ public interface MusicStreamController {
1212
* @param song Song to stream.
1313
*/
1414
void play(Song song);
15-
void stop();
15+
void stopPlaying();
1616
}

Server/src/main/java/controllers/networking/streaming/music/TCPMusicStreamController.java

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
import models.networking.dtos.StopCommand;
77
import models.songs.Song;
88

9+
import java.io.Closeable;
910
import java.io.File;
1011
import java.io.IOException;
1112
import java.io.ObjectOutputStream;
1213
import java.net.ServerSocket;
1314
import java.net.Socket;
15+
import java.net.SocketException;
1416
import java.nio.file.Files;
1517
import java.util.ArrayList;
1618
import java.util.Collections;
@@ -22,7 +24,7 @@
2224
/**
2325
* Created by Esteban Luchsinger on 07.03.2016.
2426
*/
25-
public class TCPMusicStreamController implements MusicStreamController {
27+
public class TCPMusicStreamController implements MusicStreamController, Closeable {
2628

2729
/**
2830
* The destination port for datagrams sent to the clients.
@@ -35,6 +37,9 @@ public class TCPMusicStreamController implements MusicStreamController {
3537
*/
3638
private final static int TIMEOUT_FOR_RESPONSES = 1000;
3739

40+
/**
41+
* Server TCP Socket.
42+
*/
3843
private ServerSocket serverSocket;
3944

4045
/**
@@ -49,6 +54,8 @@ public class TCPMusicStreamController implements MusicStreamController {
4954
*/
5055
private List<NetworkClient> connections;
5156

57+
private boolean isStopped;
58+
5259
/**
5360
* Starts the service.
5461
* @throws IOException
@@ -59,6 +66,7 @@ public TCPMusicStreamController() throws IOException {
5966
this.connectionAcceptingThread = new Thread(this::acceptConnections);
6067
this.connectionAcceptingThread.setDaemon(true);
6168
this.connectionAcceptingThread.start();
69+
this.isStopped = false;
6270
}
6371

6472
/**
@@ -88,10 +96,10 @@ public void play(Song song) {
8896
}
8997

9098
/**
91-
* Sends a stop command to the connected clients.
99+
* Sends a Stop command to the connected clients.
92100
*/
93101
@Override
94-
public void stop() {
102+
public void stopPlaying() {
95103
try {
96104
synchronized (this.connections) {
97105
Iterator<NetworkClient> iterator = this.connections.iterator();
@@ -107,6 +115,13 @@ public void stop() {
107115
}
108116
}
109117

118+
/**
119+
* Stops the TCPStreaming Service and frees the Sockets and Data.
120+
* ALWAYS call this method, when the Service is not needed.
121+
*/
122+
public void stop() throws IOException {
123+
}
124+
110125
/**
111126
* Returns the data of the song.
112127
* @param song
@@ -129,7 +144,13 @@ private void acceptConnections() {
129144
socket.setKeepAlive(true);
130145
socket.setReuseAddress(true);
131146
this.connections.add(new NetworkClient(socket));
132-
} catch (IOException e) {
147+
} catch (SocketException socketException) {
148+
if(socketException.getMessage().equals("socket closed")) {
149+
Logger.getLogger(this.getClass().getName())
150+
.log(Level.INFO, "Socket closed", socketException);
151+
}
152+
}
153+
catch (IOException e) {
133154
Logger.getLogger(this.getClass().getName())
134155
.log(Level.WARNING, "Error accepting connection", e);
135156
}
@@ -160,4 +181,28 @@ private static Socket prepareSocket(Socket originalSocket) throws IOException {
160181
private synchronized ServerSocket getServerSocket(){
161182
return this.serverSocket;
162183
}
184+
185+
/**
186+
* Closes this stream and releases any system resources associated
187+
* with it. If the stream is already closed then invoking this
188+
* method has no effect.
189+
* <p>
190+
* <p> As noted in {@link AutoCloseable#close()}, cases where the
191+
* close may fail require careful attention. It is strongly advised
192+
* to relinquish the underlying resources and to internally
193+
* <em>mark</em> the {@code Closeable} as closed, prior to throwing
194+
* the {@code IOException}.
195+
*
196+
* @throws IOException if an I/O error occurs
197+
*/
198+
@Override
199+
public void close() throws IOException {
200+
if(this.getServerSocket() != null && !this.getServerSocket().isClosed()) {
201+
this.getServerSocket().close();
202+
}
203+
204+
for(NetworkClient networkClient : this.connections) {
205+
networkClient.close();
206+
}
207+
}
163208
}

Server/src/main/java/controllers/networking/streaming/music/UDPMusicStreamController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void play(Song song) {
6565
}
6666

6767
@Override
68-
public void stop() {
68+
public void stopPlaying() {
6969
throw new RuntimeException("Not Implemented method");
7070
}
7171

Server/src/main/java/models/NetworkClient.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public ObjectOutputStream getOutputStream() {
4848
* Closes the NetworkClient.
4949
*/
5050
public void close() throws IOException {
51-
this.outputStream.close();
51+
if(this.getSocket() != null && !this.getSocket().isClosed()) {
52+
this.socket.close();
53+
}
5254
}
5355
}

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
import controllers.networking.discovery.DiscoveryService;
77
import controllers.networking.streaming.music.MusicStreamController;
88
import controllers.networking.streaming.music.TCPMusicStreamController;
9-
import controllers.networking.streaming.music.UDPMusicStreamController;
109
import javafx.application.Platform;
1110
import javafx.stage.Stage;
1211
import models.clients.Client;
13-
import models.clients.Clients;
1412
import models.songs.Song;
1513
import utils.DurationStringConverter;
1614
import javafx.beans.binding.Bindings;
@@ -24,9 +22,12 @@
2422
import javafx.scene.control.cell.PropertyValueFactory;
2523
import javafx.stage.DirectoryChooser;
2624

25+
import java.io.Closeable;
2726
import java.io.File;
2827
import java.io.IOException;
2928
import java.util.List;
29+
import java.util.logging.Level;
30+
import java.util.logging.Logger;
3031

3132
/**
3233
* Created by Esteban Luchsinger on 30.11.2015.
@@ -143,6 +144,15 @@ public void setStage(Stage stage) {
143144
if(this.discoveryService != null) {
144145
this.discoveryService.stop();
145146
System.out.println("Stopped Discovery Service!");
147+
if(this.musicStreamController instanceof Closeable) {
148+
try {
149+
((Closeable)this.musicStreamController).close();
150+
} catch (IOException e) {
151+
Logger.getLogger(this.getClass().getName())
152+
.log(Level.SEVERE, "Could not close the StreamController!",
153+
e);
154+
}
155+
}
146156
}
147157
});
148158
}
@@ -218,7 +228,7 @@ public void onIsPlayingChanged() {
218228
} else {
219229
this.buttonPlayPause.setId("play-button");
220230
this.buttonPlayPause.setSelected(false);
221-
this.musicStreamController.stop();
231+
this.musicStreamController.stopPlaying();
222232
}
223233
}
224234
//endregion

0 commit comments

Comments
 (0)