Migrate everything on Gradle
Also added tasks to run the server & client separately
This commit is contained in:
139
ChatApp/app/src/main/java/client/ClientConnexion.java
Normal file
139
ChatApp/app/src/main/java/client/ClientConnexion.java
Normal file
@@ -0,0 +1,139 @@
|
||||
package client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
import network.PacketHandler;
|
||||
import network.SocketReader;
|
||||
import network.SocketWriter;
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
import network.protocol.packets.*;
|
||||
|
||||
public class ClientConnexion implements PacketVisitor, PacketHandler {
|
||||
|
||||
private final InetSocketAddress serverAddress;
|
||||
private final SocketWriter writer;
|
||||
private final SocketReader reader;
|
||||
private final ClientListener callback;
|
||||
private volatile boolean connected = false;
|
||||
|
||||
public ClientConnexion(DatagramSocket socket, InetSocketAddress serverAddress, ClientListener callback) {
|
||||
this.serverAddress = serverAddress;
|
||||
this.writer = new SocketWriter(socket);
|
||||
this.reader = new SocketReader(socket, this);
|
||||
this.callback = callback;
|
||||
spamHandshake();
|
||||
}
|
||||
|
||||
private void spamHandshake() {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
sendPacket(new HandshakePacket());
|
||||
}
|
||||
new Thread(this::waitForHandshake).start();
|
||||
}
|
||||
|
||||
private void waitForHandshake() {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(!connected) {
|
||||
System.out.println("The server did not respond !");
|
||||
this.close();
|
||||
this.callback.handleConnexionError();
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
this.reader.stop();
|
||||
}
|
||||
|
||||
public void sendPacket(Packet packet) {
|
||||
try {
|
||||
this.writer.sendPacket(packet, serverAddress);
|
||||
} catch (IOException e) {
|
||||
this.close();
|
||||
this.callback.handleConnexionError();
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlePacket(Packet packet, InetSocketAddress address) {
|
||||
// we assume that the packet comes from the server
|
||||
visit(packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(ChatMessagePacket packet) {
|
||||
this.callback.handleChatMessage(packet.getTime(), packet.getChatter(), packet.getContent());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(RoomListPacket packet) {
|
||||
this.callback.handleRoomList(packet.getRoomNames());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(ServerResponsePacket packet) {
|
||||
this.callback.handleServerResponse(packet.getResponse());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(RequestActualRoomPacket packet) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(ActualRoomPacket packet) {
|
||||
this.callback.handleActualRoom(packet.getRoomName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(DisconnectPacket packet) {
|
||||
this.close();
|
||||
this.connected = false;
|
||||
this.callback.handleDisconnect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(HandshakePacket packet) {
|
||||
if (!connected)
|
||||
this.callback.handleConnect();
|
||||
this.connected = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(CreateRoomPacket packet) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(JoinRoomPacket packet) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(LeaveRoomPacket packet) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(LoginPacket packet) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(RequestRoomListPacket packet) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(SendChatMessagePacket packet) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user