This commit is contained in:
28
app/src/main/java/network/server/Server.java
Normal file
28
app/src/main/java/network/server/Server.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package network.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Server {
|
||||
|
||||
final ServerSocket serverSocket;
|
||||
final List<ServerConnexion> connexions;
|
||||
private final ServerThread thread;
|
||||
|
||||
public Server(short port) throws IOException {
|
||||
this.serverSocket = new ServerSocket(port);
|
||||
this.connexions = new ArrayList<>();
|
||||
this.thread = new ServerThread(this);
|
||||
this.thread.start();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
this.thread.cancel();
|
||||
for (ServerConnexion connexion : this.connexions) {
|
||||
connexion.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
35
app/src/main/java/network/server/ServerConnexion.java
Normal file
35
app/src/main/java/network/server/ServerConnexion.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package network.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.util.Random;
|
||||
|
||||
import network.Connexion;
|
||||
import network.packets.ConnexionInfoPacket;
|
||||
import network.packets.KeepAlivePacket;
|
||||
|
||||
public class ServerConnexion extends Connexion{
|
||||
|
||||
public ServerConnexion(Socket socket) throws IOException {
|
||||
super(socket);
|
||||
System.out.println("Bonjour le client !");
|
||||
sendKeepAlive();
|
||||
}
|
||||
|
||||
public void sendKeepAlive() throws IOException {
|
||||
Random r = new Random();
|
||||
sendPacket(new KeepAlivePacket(r.nextLong()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ConnexionInfoPacket packet) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visit'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(KeepAlivePacket packet) {
|
||||
System.out.println("Je l'ai reçu !");
|
||||
}
|
||||
|
||||
}
|
||||
36
app/src/main/java/network/server/ServerThread.java
Normal file
36
app/src/main/java/network/server/ServerThread.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package network.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
|
||||
public class ServerThread extends Thread {
|
||||
|
||||
private final Server server;
|
||||
|
||||
public ServerThread(Server server) {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
try {
|
||||
this.server.serverSocket.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
interrupt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while(!interrupted()) {
|
||||
Socket newConnection = this.server.serverSocket.accept();
|
||||
ServerConnexion serverConnection = new ServerConnexion(newConnection);
|
||||
this.server.connexions.add(serverConnection);
|
||||
}
|
||||
} catch(IOException e) {
|
||||
// e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user