Skip to content

Commit 0656a95

Browse files
committed
Merge pull request #3 from eluchsinger/client-renaming
Client renaming
2 parents c1a15a1 + a757522 commit 0656a95

File tree

24 files changed

+501
-862
lines changed

24 files changed

+501
-862
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import controllers.io.cache.file.FileCacheService;
44
import controllers.networking.streaming.music.callback.OnMusicStreamingStatusChanged;
55
import controllers.networking.streaming.music.callback.OnPlay;
6+
import controllers.networking.streaming.music.callback.OnRename;
67
import controllers.networking.streaming.music.callback.OnStop;
78
import models.clients.Server;
89

@@ -12,9 +13,20 @@
1213
*/
1314
public interface MusicStreamingService {
1415

16+
/**
17+
* Starts the streaming service.
18+
*/
1519
void start();
20+
21+
/**
22+
* Stops the streaming service.
23+
*/
1624
void stop();
1725

26+
/**
27+
* Sets the current server.
28+
* @param server The new server object.
29+
*/
1830
void setServer(Server server);
1931

2032
void addServiceStatusChangedListener(OnMusicStreamingStatusChanged listener);
@@ -26,9 +38,14 @@ public interface MusicStreamingService {
2638
void addOnStopListener(OnStop listener);
2739
void removeOnStopListener(OnStop listener);
2840

41+
void addOnRenameListener(OnRename listener);
42+
void removeOnRenameListener(OnRename listener);
43+
2944
/**
3045
* Returns the cache (FileCache) of the MusicStreamingService.
3146
* @return FileCacheService.
3247
*/
3348
FileCacheService getCache();
49+
50+
void sendName(String name);
3451
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package controllers.networking.streaming.music.callback;
2+
3+
/**
4+
* Created by Esteban on 01.04.2016.
5+
*
6+
*/
7+
@FunctionalInterface
8+
public interface OnRename {
9+
/**
10+
* Renames the client.
11+
* @param name The new name of the client.
12+
*/
13+
void rename(String name);
14+
}

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

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import controllers.networking.streaming.music.ServiceStatus;
77
import controllers.networking.streaming.music.callback.OnMusicStreamingStatusChanged;
88
import controllers.networking.streaming.music.callback.OnPlay;
9+
import controllers.networking.streaming.music.callback.OnRename;
910
import controllers.networking.streaming.music.callback.OnStop;
1011
import models.clients.Server;
1112
import models.networking.dtos.PlayCommand;
13+
import models.networking.dtos.RenameCommand;
1214
import models.networking.dtos.StopCommand;
1315

1416
import java.io.IOException;
@@ -56,6 +58,8 @@ public class TCPMusicStreamingController implements MusicStreamingService {
5658

5759
//endregion
5860

61+
private final Logger logger;
62+
5963
/**
6064
* Current status of the service.
6165
*/
@@ -92,33 +96,36 @@ public class TCPMusicStreamingController implements MusicStreamingService {
9296
/**
9397
* List of listeners listening for currentServiceStatus changes.
9498
*/
95-
private List<OnMusicStreamingStatusChanged> statusChangedListeners;
99+
private final List<OnMusicStreamingStatusChanged> statusChangedListeners;
96100

97101
/**
98102
* List of listeners listening for playCommands received.
99103
*/
100-
private List<OnPlay> playCommandListeners;
104+
private final List<OnPlay> playCommandListeners;
101105

102106
/**
103107
* List of listeners listening for stopCommands received.
104108
*/
105-
private List<OnStop> stopCommandListeners;
109+
private final List<OnStop> stopCommandListeners;
110+
111+
private final ArrayList<OnRename> renameCommandListeners;
106112

107113
//endregion listeners
108114

109115
/**
110116
* Default constructor
111117
*/
112118
public TCPMusicStreamingController() throws IOException {
113-
this.initializeListeners();
114-
this.cache = new StaticFileCacheService();
115-
this.setCurrentServiceStatus(ServiceStatus.STOPPED);
116-
}
119+
this.logger = Logger.getLogger(this.getClass().getName());
117120

118-
private void initializeListeners() {
121+
// Initialize Listeners
119122
this.statusChangedListeners = new ArrayList<>();
120123
this.playCommandListeners = new ArrayList<>();
121124
this.stopCommandListeners = new ArrayList<>();
125+
this.renameCommandListeners = new ArrayList<>();
126+
127+
this.cache = new StaticFileCacheService();
128+
this.setCurrentServiceStatus(ServiceStatus.STOPPED);
122129
}
123130

124131
/**
@@ -134,8 +141,8 @@ public void start() {
134141
this.setCurrentServiceStatus(ServiceStatus.WAITING);
135142
} catch (IOException exception) {
136143
this.isRunning = false;
137-
Logger.getLogger(this.getClass().getName())
138-
.log(Level.SEVERE, "Failed starting Music Streaming Service.", exception);
144+
this.logger.log(Level.SEVERE, "Failed starting Music Streaming Service.", exception);
145+
139146
}
140147
}
141148

@@ -151,7 +158,7 @@ public void stop() {
151158
if (this.listeningThread != null && !this.listeningThread.isAlive())
152159
this.listeningThread.join(SOCKET_TIMEOUT + 1000);
153160
} catch (InterruptedException e) {
154-
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE,
161+
this.logger.log(Level.SEVERE,
155162
"Error joining TCP Listening Thread", e);
156163
} finally {
157164
if (this.listeningThread == null || !this.listeningThread.isAlive())
@@ -163,8 +170,7 @@ public void stop() {
163170
if(!this.getSocket().isClosed())
164171
this.getSocket().close();
165172
} catch (IOException e) {
166-
Logger.getLogger(this.getClass().getName())
167-
.log(Level.SEVERE,
173+
this.logger.log(Level.SEVERE,
168174
"Error closing the open socket", e);
169175
}
170176
}
@@ -185,16 +191,22 @@ private void listen() {
185191

186192
// If it's a play command.
187193
if(receivedObject instanceof PlayCommand) {
188-
System.out.println("Received PlayCommand");
194+
this.logger.log(Level.INFO, "Received PlayCommand");
189195
PlayCommand command = (PlayCommand) receivedObject;
190196
this.cache.writeData(command.data);
191197
this.setCurrentServiceStatus(ServiceStatus.READY);
192198
this.onPlayCommandReceived(command.songTitle, command.artist);
193199
}
194200
else if(receivedObject instanceof StopCommand) {
195-
System.out.println("Received StopCommand");
201+
this.logger.log(Level.INFO, "Received StopCommand");
196202
this.onStopCommandReceived();
197203
}
204+
else if(receivedObject instanceof RenameCommand) {
205+
this.logger.log(Level.INFO, "Received RenameCommand");
206+
207+
RenameCommand command = (RenameCommand) receivedObject;
208+
this.onRenameCommandReceived(command.getName());
209+
}
198210

199211
} catch (SocketTimeoutException ignore) {
200212
} catch(SocketException socketException) {
@@ -207,17 +219,16 @@ else if(receivedObject instanceof StopCommand) {
207219
}
208220
}
209221
catch(Exception e) {
210-
Logger.getLogger(this.getClass().getName())
211-
.log(Level.SEVERE, "Error in the TCPStreaming listener!", e);
222+
this.logger.log(Level.SEVERE, "Error in the TCPStreaming listener!", e);
212223
}
213224
}
214225
catch (IOException | ClassNotFoundException e) {
215-
Logger.getLogger(this.getClass().getName())
216-
.log(Level.SEVERE, "Error in the TCPStreaming listener!", e);
226+
this.logger.log(Level.SEVERE, "Error in the TCPStreaming listener!", e);
217227
}
218228
}
219229
}
220230

231+
221232
@Override
222233
public void setServer(Server server) {
223234
if (this.currentServer != server) {
@@ -335,34 +346,28 @@ private synchronized void setCurrentServiceStatus(ServiceStatus currentServiceSt
335346
}
336347

337348
@Override
338-
public void addServiceStatusChangedListener(OnMusicStreamingStatusChanged listener) {
339-
this.statusChangedListeners.add(listener);
340-
}
349+
public void addServiceStatusChangedListener(OnMusicStreamingStatusChanged listener) { this.statusChangedListeners.add(listener); }
341350

342351
@Override
343-
public void removeServiceStatusChangedListener(OnMusicStreamingStatusChanged listener) {
344-
this.statusChangedListeners.remove(listener);
345-
}
352+
public void removeServiceStatusChangedListener(OnMusicStreamingStatusChanged listener) { this.statusChangedListeners.remove(listener); }
346353

347354
@Override
348-
public void addOnPlayListener(OnPlay listener) {
349-
this.playCommandListeners.add(listener);
350-
}
355+
public void addOnPlayListener(OnPlay listener) { this.playCommandListeners.add(listener); }
351356

352357
@Override
353-
public void removeOnPlayListener(OnPlay listener) {
354-
this.playCommandListeners.remove(listener);
355-
}
358+
public void removeOnPlayListener(OnPlay listener) { this.playCommandListeners.remove(listener); }
356359

357360
@Override
358-
public void addOnStopListener(OnStop listener) {
359-
this.stopCommandListeners.add(listener);
360-
}
361+
public void addOnStopListener(OnStop listener) { this.stopCommandListeners.add(listener); }
361362

362363
@Override
363-
public void removeOnStopListener(OnStop listener) {
364-
this.stopCommandListeners.remove(listener);
365-
}
364+
public void removeOnStopListener(OnStop listener) { this.stopCommandListeners.remove(listener); }
365+
366+
@Override
367+
public void addOnRenameListener(OnRename listener) { this.renameCommandListeners.add(listener);}
368+
369+
@Override
370+
public void removeOnRenameListener(OnRename listener) { this.renameCommandListeners.remove(listener); }
366371

367372
/**
368373
* Returns the cache (FileCache) of the MusicStreamingService.
@@ -374,6 +379,18 @@ public FileCacheService getCache() {
374379
return this.cache;
375380
}
376381

382+
/**
383+
* Tells the server the name of this client.
384+
* @param name
385+
*/
386+
public void sendName(String name) {
387+
try {
388+
this.objectOutputStream.writeObject(new RenameCommand(name));
389+
} catch(IOException ioException) {
390+
this.logger.log(Level.WARNING, "Failed sending the current name to the server", ioException);
391+
}
392+
}
393+
377394
//region Event Launchers
378395

379396
/**
@@ -389,8 +406,8 @@ private void onServiceStatusChanged() {
389406
/**
390407
*
391408
* Fires the corresponding event to all the listeners.
392-
* @param songTitle
393-
* @param artist
409+
* @param songTitle The title of the received song. (May be null)
410+
* @param artist The artist of the received song. (May be null)
394411
*/
395412
private void onPlayCommandReceived(String songTitle, String artist) {
396413
this.playCommandListeners.forEach(onPlay -> onPlay.play(songTitle, artist));
@@ -404,5 +421,10 @@ private void onStopCommandReceived() {
404421
this.stopCommandListeners.forEach(OnStop::stop);
405422
}
406423

424+
/**
425+
* Fires the corresponding event to all the listeners.
426+
*/
427+
private void onRenameCommandReceived(String name) { this.renameCommandListeners.forEach(l -> l.rename(name));}
428+
407429
//endregion Event Launchers
408430
}

0 commit comments

Comments
 (0)