diff --git a/src/fr/univ/lyon1/client/Client.java b/src/fr/univ/lyon1/client/Client.java index 36b4b73..046be00 100644 --- a/src/fr/univ/lyon1/client/Client.java +++ b/src/fr/univ/lyon1/client/Client.java @@ -18,6 +18,7 @@ import java.io.ObjectOutputStream; import java.net.Socket; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; public class Client { private final int port; @@ -92,6 +93,7 @@ public class Client { case login -> commandLogin(); case message -> commandMessage(cmd); case list -> commandList(cmd); + case listChannels -> commandListChannels(cmd); case join -> commandJoin(cmd); } } @@ -99,6 +101,8 @@ public class Client { private void commandLogin() throws IOException { out.writeObject(new Command(CommandType.list, null)); out.flush(); + out.writeObject(new Command(CommandType.listChannels, null)); + out.flush(); out.writeObject(new Command(CommandType.join, List.of("general"))); out.flush(); } @@ -109,10 +113,11 @@ public class Client { } private void commandList(Command cmd) { - List users = cmd.getArgs(); - for (Object u : users) { - System.out.println(u); - } + System.out.println("Users: "+cmd.getArgs().stream().map(Object::toString).collect(Collectors.joining(", "))); + } + + private void commandListChannels(Command cmd) { + System.out.println("Channels: "+cmd.getArgs().stream().map(Object::toString).collect(Collectors.joining(", "))); } private void commandJoin(Command cmd) { diff --git a/src/fr/univ/lyon1/server/ConnectedClient.java b/src/fr/univ/lyon1/server/ConnectedClient.java index 09f98c6..cb3e226 100644 --- a/src/fr/univ/lyon1/server/ConnectedClient.java +++ b/src/fr/univ/lyon1/server/ConnectedClient.java @@ -48,6 +48,7 @@ public class ConnectedClient implements Runnable { case login -> commandLogin(command); case message -> commandMessage(command); case list -> commandList(); + case listChannels -> commandListChannels(); case join -> commandJoin(command); } } @@ -89,7 +90,12 @@ public class ConnectedClient implements Runnable { } private void commandList() throws IOException { - out.writeObject(new Command(CommandType.list, Collections.singletonList(server.getUsers()))); + out.writeObject(new Command(CommandType.list, Collections.singletonList(server.getUsers()))); + out.flush(); + } + + private void commandListChannels() throws IOException { + out.writeObject(new Command(CommandType.listChannels, Collections.singletonList((List)(List) ChannelModel.getAll()))); out.flush(); } diff --git a/src/fr/univ/lyon1/server/models/ChannelModel.java b/src/fr/univ/lyon1/server/models/ChannelModel.java index a5d52fa..111ee31 100644 --- a/src/fr/univ/lyon1/server/models/ChannelModel.java +++ b/src/fr/univ/lyon1/server/models/ChannelModel.java @@ -6,6 +6,8 @@ import fr.univ.lyon1.common.User; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; public class ChannelModel extends Channel implements Model { @@ -63,6 +65,25 @@ public class ChannelModel extends Channel implements Model { return null; } + public static List getAll() { + List channels = new ArrayList<>(); + try { + PreparedStatement ps = database.getConnection().prepareStatement("SELECT * FROM "+TABLE_NAME); + if (ps.execute()) { + ResultSet rs = ps.getResultSet(); + while (rs.next()) + channels.add(new ChannelModel( + UUID.fromString(rs.getString("UUID")), + rs.getString("NAME")) + ); + } + } catch (SQLException err) { + err.printStackTrace(); + return null; + } + return channels; + } + private boolean exist() { try { PreparedStatement ps = database.getConnection().prepareStatement("SELECT UUID FROM "+TABLE_NAME+" WHERE UUID = ?");