Skip to content

Commit 48c58d2

Browse files
committed
Implemented first trivial synchronization mechanisms.
1 parent f09a1ad commit 48c58d2

File tree

5 files changed

+43
-20
lines changed

5 files changed

+43
-20
lines changed

Client/src/main/java/controllers/io/cache/file/StaticFileCacheService.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public class StaticFileCacheService implements FileCacheService {
2121

2222
private File rootFile = null;
2323

24-
2524
/**
2625
* Default constructor
2726
* @throws IOException

Client/src/main/java/controllers/networking/streaming/music/tcp/TCPMusicStreamingController.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import controllers.networking.streaming.music.callback.OnRename;
1010
import controllers.networking.streaming.music.callback.OnStop;
1111
import models.clients.Server;
12+
import models.networking.dtos.CacheSongCommand;
1213
import models.networking.dtos.PlayCommand;
1314
import models.networking.dtos.RenameCommand;
1415
import models.networking.dtos.StopCommand;
@@ -187,18 +188,25 @@ private void listen() {
187188
Object receivedObject;
188189
receivedObject = this.objectInputStream.readObject();
189190

190-
// If it's a play command.
191-
if(receivedObject instanceof PlayCommand) {
192-
this.logger.info("Received PlayCommand");
193-
PlayCommand command = (PlayCommand) receivedObject;
191+
// If it's a cache song command.
192+
if(receivedObject instanceof CacheSongCommand) {
193+
this.logger.info("Received CacheSongCommand");
194+
CacheSongCommand command = (CacheSongCommand) receivedObject;
194195
this.cache.writeData(command.data);
195196
this.setCurrentServiceStatus(ServiceStatus.READY);
196-
this.onPlayCommandReceived(command.songTitle, command.artist);
197197
}
198+
// A play command
199+
else if(receivedObject instanceof PlayCommand) {
200+
this.logger.info("Received PlayCommand");
201+
PlayCommand command = (PlayCommand) receivedObject;
202+
this.onPlayCommandReceived(command.title, command.artist);
203+
}
204+
// A stop command
198205
else if(receivedObject instanceof StopCommand) {
199206
this.logger.info("Received StopCommand");
200207
this.onStopCommandReceived();
201208
}
209+
// A rename command
202210
else if(receivedObject instanceof RenameCommand) {
203211
this.logger.info("Received RenameCommand");
204212

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import controllers.clients.ClientController;
44
import controllers.networking.streaming.music.MusicStreamController;
55
import models.networking.clients.NetworkClient;
6+
import models.networking.dtos.CacheSongCommand;
67
import models.networking.dtos.PlayCommand;
78
import models.networking.dtos.StopCommand;
89
import models.songs.Song;
@@ -52,13 +53,21 @@ public TCPMusicStreamController(ClientController clientController) {
5253
public void play(Song song) throws IOException {
5354
byte[] songData = SongUtils.getSongData(song);
5455

55-
PlayCommand playCommand = new PlayCommand(song.getTitle(), song.getArtist(), songData);
56+
CacheSongCommand cacheSongCommand = new CacheSongCommand(songData);
57+
PlayCommand playCommand = new PlayCommand(song.getTitle(), song.getArtist());
5658

59+
// First send the cache song command to make the clients ready.
5760
for(NetworkClient client : this.clientController.getClients()) {
58-
client.send(playCommand);
61+
client.send(cacheSongCommand);
5962
}
6063

64+
// Then wait for the clients until they all received the song completely.
6165
this.waitForClientsReceived();
66+
67+
// Then send all clients the play command.
68+
for(NetworkClient client : this.clientController.getClients()) {
69+
client.send(playCommand);
70+
}
6271
}
6372

6473
/**
@@ -70,7 +79,6 @@ public void stop() {
7079
for(NetworkClient client : this.clientController.getClients()) {
7180
client.send(stopCommand);
7281
}
73-
this.waitForClientsReceived();
7482
}
7583

7684
/**
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package models.networking.dtos;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* Created by Esteban Luchsinger on 08.04.2016.
7+
* The cache command tells the client to cache the song. (Not start playing!)
8+
*/
9+
public class CacheSongCommand implements Serializable {
10+
private static final long serialVersionUID = 2322268523175827924L;
11+
12+
public byte[] data;
13+
14+
public CacheSongCommand(byte[] data) { this.data = data; }
15+
}

Shared/src/main/java/models/networking/dtos/PlayCommand.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,11 @@
99
public class PlayCommand implements Serializable {
1010
private static final long serialVersionUID = -3809733967947659045L;
1111

12-
public String songTitle;
13-
public String artist;
14-
public byte[] data;
12+
public final String title;
13+
public final String artist;
1514

16-
public PlayCommand(byte[] data) { this("", "", data); }
17-
18-
public PlayCommand(String songTitle, byte[] data) { this(songTitle, "", data); }
19-
20-
public PlayCommand(String songTitle, String artist, byte[] data) {
21-
22-
this.songTitle = songTitle;
15+
public PlayCommand(String title, String artist) {
16+
this.title = title;
2317
this.artist = artist;
24-
this.data = data;
2518
}
2619
}

0 commit comments

Comments
 (0)