2021-05-15 20:53:58 +00:00
/ *
* Copyright ( C ) 2021 eichehome
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
* /
package server ;
import java.io.EOFException ;
import java.io.IOException ;
import java.io.ObjectInputStream ;
import java.io.ObjectOutputStream ;
2021-05-15 22:43:36 +00:00
import java.util.logging.Level ;
import java.util.logging.Logger ;
2021-05-15 20:53:58 +00:00
import utils.Message ;
/ * *
2021-05-16 18:03:38 +00:00
* Diese Klasse stellt einen Thread dar , der auf Nachrichten eines Clients
* lauscht und diese dann weiter gibt .
*
* @version 1 . 0 . 0
2021-05-15 20:53:58 +00:00
* @author eichehome
* /
public class ClientReciveMessageThread extends Thread {
2021-05-16 18:03:38 +00:00
/ * *
* Attribut , welches einen Zeiger auf das zentrale Verzeichnis aller
* verbundenen Clients enthält .
* /
2021-05-15 20:53:58 +00:00
private ClientStore clientStore ;
2021-05-16 18:03:38 +00:00
/ * *
* Attribut , welches den von Client kommenden Stream repräsentiert .
* /
2021-05-15 20:53:58 +00:00
private final ObjectInputStream in ;
2021-05-16 18:03:38 +00:00
/ * *
* Konstruktor
*
* @param in Stream , in dem die Nachrichten eines Clients ankommen .
* @param clientStore Zeiger auf das zentrale Verzeichnis der Clients , um
* alle benachrichtigen zu können .
* /
public ClientReciveMessageThread ( ObjectInputStream in , ClientStore clientStore ) {
this . in = in ;
this . clientStore = clientStore ;
}
2021-05-15 22:43:36 +00:00
/ * *
* Hauptmethode des Threads , in der alles abläuft .
* /
2021-05-15 20:53:58 +00:00
public void run ( ) {
2021-05-15 22:43:36 +00:00
Logger . getLogger ( ClientReciveMessageThread . class . getName ( ) ) . log ( Level . INFO , String . format ( " Reciveing Thread %d: Gestartet " , this . getId ( ) ) ) ;
2021-05-15 20:53:58 +00:00
boolean continueLoop = true ;
while ( continueLoop ) {
try {
2021-05-16 18:03:38 +00:00
Message empfangen = null ;
while ( ( empfangen = ( Message ) this . in . readObject ( ) ) ! = null ) { //Der Thread pleibt hier stehen bis er eine Nachricht vom Client erhält.
Logger . getLogger ( ClientReciveMessageThread . class . getName ( ) ) . log ( Level . INFO , String . format ( " Reciveing Thread %d: Time: %s Client: %s Message: %s " , this . getId ( ) , empfangen . getTime ( ) , empfangen . getUsername ( ) , empfangen . getMessage ( ) ) ) ;
for ( ObjectOutputStream stream : clientStore . getAllOutputStreams ( ) ) {
stream . writeObject ( empfangen ) ;
stream . flush ( ) ;
Logger . getLogger ( ClientReciveMessageThread . class . getName ( ) ) . log ( Level . INFO , String . format ( " Reciveing Thread %d: Weitergeleitet " , this . getId ( ) ) ) ;
}
2021-05-15 20:53:58 +00:00
}
} catch ( ClassNotFoundException ex ) {
2021-05-15 22:43:36 +00:00
Logger . getLogger ( ClientReciveMessageThread . class . getName ( ) ) . log ( Level . SEVERE , String . format ( " Reciveing Thread %d: Fehler: %s " , this . getId ( ) , ex ) ) ;
2021-05-16 18:03:38 +00:00
} catch ( EOFException ex ) { //Diese Exception zeigt, dass sich die Verbundung zum Client geschlossen hat. Die Aufräumarbeiten übernimmt der Sending Thread diese Clients.
2021-05-15 22:43:36 +00:00
Logger . getLogger ( ClientReciveMessageThread . class . getName ( ) ) . log ( Level . INFO , String . format ( " Reciveing Thread %d: Socket wurde beendet " , this . getId ( ) ) ) ;
2021-05-15 20:53:58 +00:00
continueLoop = false ;
} catch ( IOException ex ) {
2021-05-15 22:43:36 +00:00
Logger . getLogger ( ClientReciveMessageThread . class . getName ( ) ) . log ( Level . SEVERE , String . format ( " Reciveing Thread %d: Fehler: %s " , this . getId ( ) , ex ) ) ;
2021-05-15 20:53:58 +00:00
continueLoop = false ;
}
}
}
}