basic client/server structure
This commit is contained in:
35
ChatApp/src/client/Client.java
Normal file
35
ChatApp/src/client/Client.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketException;
|
||||
|
||||
import network.protocol.packets.LoginPacket;
|
||||
import network.protocol.packets.SendChatMessagePacket;
|
||||
|
||||
public class Client {
|
||||
|
||||
private final ClientConnexion connexion;
|
||||
|
||||
public Client(InetSocketAddress serverAddress) throws SocketException {
|
||||
this.connexion = new ClientConnexion(new DatagramSocket(), serverAddress);
|
||||
login("Moi");
|
||||
}
|
||||
|
||||
private void login(String pseudo) {
|
||||
try {
|
||||
this.connexion.sendPacket(new LoginPacket(pseudo));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void SendChatMessage(String message) {
|
||||
try {
|
||||
this.connexion.sendPacket(new SendChatMessagePacket(message));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
94
ChatApp/src/client/ClientConnexion.java
Normal file
94
ChatApp/src/client/ClientConnexion.java
Normal file
@@ -0,0 +1,94 @@
|
||||
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.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;
|
||||
|
||||
public ClientConnexion(DatagramSocket socket, InetSocketAddress serverAddress) {
|
||||
this.serverAddress = serverAddress;
|
||||
this.writer = new SocketWriter(socket);
|
||||
this.reader = new SocketReader(socket, this);
|
||||
}
|
||||
|
||||
public void close() {
|
||||
this.reader.stop();
|
||||
}
|
||||
|
||||
public void sendPacket(Packet packet) throws IOException {
|
||||
this.writer.sendPacket(packet, serverAddress);
|
||||
}
|
||||
|
||||
@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) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
}
|
||||
|
||||
@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(RoomListPacket packet) {
|
||||
System.out.println("Handled room list !");
|
||||
// throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(SendChatMessagePacket packet) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(ServerResponsePacket packet) {
|
||||
System.out.println(packet.getResponse());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user