66import controllers .networking .streaming .music .ServiceStatus ;
77import controllers .networking .streaming .music .callback .OnMusicStreamingStatusChanged ;
88import controllers .networking .streaming .music .callback .OnPlay ;
9+ import controllers .networking .streaming .music .callback .OnRename ;
910import controllers .networking .streaming .music .callback .OnStop ;
1011import models .clients .Server ;
1112import models .networking .dtos .PlayCommand ;
13+ import models .networking .dtos .RenameCommand ;
1214import models .networking .dtos .StopCommand ;
1315
1416import 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