2021-05-11 11:59:08 +00:00
|
|
|
/*
|
2021-05-13 19:09:10 +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/>.
|
2021-05-11 11:59:08 +00:00
|
|
|
*/
|
|
|
|
package server;
|
|
|
|
|
2021-05-15 20:53:58 +00:00
|
|
|
import java.io.ObjectInputStream;
|
|
|
|
import java.io.ObjectOutputStream;
|
|
|
|
import java.io.PipedInputStream;
|
|
|
|
import java.io.PipedOutputStream;
|
2021-05-11 11:59:08 +00:00
|
|
|
import java.net.*;
|
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.Client;
|
2021-05-11 11:59:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hauptklasse des Servers
|
|
|
|
*
|
|
|
|
* @version 0.0.1
|
|
|
|
* @author eichehome
|
|
|
|
*/
|
|
|
|
public class MainServer {
|
|
|
|
|
|
|
|
/**
|
2021-05-15 22:43:36 +00:00
|
|
|
* Einstiegsklasse des Servers. Diese wartet auf Anfragen der Clients und erstellt dann die nötigen Resourcen (Streams und Threads).
|
2021-05-11 11:59:08 +00:00
|
|
|
* @param args the command line arguments
|
|
|
|
*/
|
|
|
|
public static void main(String[] args) {
|
2021-05-13 22:25:42 +00:00
|
|
|
try (ServerSocket serverSocket = new ServerSocket(1236)) {
|
2021-05-15 20:53:58 +00:00
|
|
|
ClientStore clientStore = new ClientStore();
|
2021-05-15 22:43:36 +00:00
|
|
|
while (true) {
|
|
|
|
Logger.getLogger(MainServer.class.getName()).log(Level.INFO, String.format("Main: Warte auf Clients"));
|
2021-05-11 11:59:08 +00:00
|
|
|
Socket client = serverSocket.accept();
|
2021-05-15 22:43:36 +00:00
|
|
|
Logger.getLogger(MainServer.class.getName()).log(Level.INFO, String.format("Main: Client verbunden"));
|
|
|
|
|
2021-05-15 20:53:58 +00:00
|
|
|
PipedOutputStream pipedOutputStream = new PipedOutputStream();
|
|
|
|
PipedInputStream pipedInputStream = new PipedInputStream(pipedOutputStream);
|
2021-05-15 22:43:36 +00:00
|
|
|
|
2021-05-15 20:53:58 +00:00
|
|
|
ObjectOutputStream pipedObjectOutputStream = new ObjectOutputStream(pipedOutputStream);
|
|
|
|
ObjectInputStream pipedObjectInputStream = new ObjectInputStream(pipedInputStream);
|
2021-05-15 22:43:36 +00:00
|
|
|
|
2021-05-15 20:53:58 +00:00
|
|
|
ObjectInputStream in = new ObjectInputStream(client.getInputStream());
|
|
|
|
ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());
|
2021-05-15 22:43:36 +00:00
|
|
|
|
|
|
|
Logger.getLogger(MainServer.class.getName()).log(Level.INFO, String.format("Main: Streams erstellt"));
|
|
|
|
|
2021-05-15 20:53:58 +00:00
|
|
|
Thread threadRecive = new ClientReciveMessageThread(in, clientStore);
|
|
|
|
Thread threadPush = new ClientPushMessageThread(out, pipedObjectInputStream, clientStore);
|
2021-05-15 22:43:36 +00:00
|
|
|
|
|
|
|
Logger.getLogger(MainServer.class.getName()).log(Level.INFO, String.format("Main: Threads erstellt"));
|
|
|
|
|
2021-05-15 20:53:58 +00:00
|
|
|
threadPush.start();
|
|
|
|
threadRecive.start();
|
2021-05-15 22:43:36 +00:00
|
|
|
|
|
|
|
Logger.getLogger(MainServer.class.getName()).log(Level.INFO, String.format("Main: Threads gestartet"));
|
2021-05-15 20:53:58 +00:00
|
|
|
|
2021-05-15 22:43:36 +00:00
|
|
|
clientStore.addClient(new Client(pipedObjectOutputStream, threadPush, threadRecive));
|
|
|
|
|
2021-05-11 11:59:08 +00:00
|
|
|
}
|
2021-05-13 19:09:10 +00:00
|
|
|
} catch (Exception ex) {
|
2021-05-15 22:43:36 +00:00
|
|
|
Logger.getLogger(MainServer.class.getName()).log(Level.SEVERE, String.format("Main: Fehler: %s", ex));
|
2021-05-11 11:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|