140 lines
3.7 KiB
Java
140 lines
3.7 KiB
Java
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.ChatMessagePacket;
|
|
import network.protocol.packets.CreateRoomPacket;
|
|
import network.protocol.packets.DisconnectPacket;
|
|
import network.protocol.packets.HandshakePacket;
|
|
import network.protocol.packets.JoinRoomPacket;
|
|
import network.protocol.packets.LeaveRoomPacket;
|
|
import network.protocol.packets.LoginPacket;
|
|
import network.protocol.packets.RequestRoomListPacket;
|
|
import network.protocol.packets.RoomListPacket;
|
|
import network.protocol.packets.SendChatMessagePacket;
|
|
import network.protocol.packets.ServerResponsePacket;
|
|
|
|
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(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'");
|
|
}
|
|
|
|
}
|