Chat System - Admin Command Kick - Server Side

Introduction: This tutorial is the nineteenth in my Java Network Programming using KryoNet series, or fourteenth in creating a chat client and server system, in which we are going to be continuing the previous tutorial. Previous: In the previous tutorial we added private message features to the client. The System: We are going to give the user a GUI to interact with the system - to send messages and see the currently connected members of the chat. When a client connects, add them to a list. Send incoming messages to everyone within the client list except the sender - or send them one back saying that it is received, as confirmation. This Tutorial: This tutorial is going to be making the admin requests from the previous tutorial handle on the server. Listener: First we need to handle the packet within the server listener. We have two options here, either we can send the request to all admins, or one randomly. I have made both for this tutorial, you may choose one or use both...
  1. else if(object instanceof Packet7AdminKick) {
  2.         Packet7AdminKick packet = (Packet7AdminKick) object;
  3.         clientHandler.sendToAdmin(packet);
  4.         clientHandler.sendToAllAdmins(packet);
  5.         if (packet.broadcast){
  6.                 Packet2Message p2 = new Packet2Message();
  7.                 p2.message = (packet.userFrom + " has requested to kick " + packet.userKick + " for " + clientHandler.convertReason(packet.reason));
  8.                 clientHandler.circulateMessage(connection, p2);
  9.         }
  10. }
sendToAdmin Method: So first lets make the sendToAdmin method in our ClientHandler class. This method will simply randomly select an admin, compile a Packet2Message packet from the information given in the parsed Packet7AdminLKick packet, and send it to the randomly selected admin.
  1. public void sendToAdmin(Packet7AdminKick packet) {
  2.         CustomClient admin;
  3.         ArrayList<CustomClient> admins = new ArrayList<CustomClient>();
  4.         for (CustomClient c : this.clients) {
  5.                 if (c.getLevel() == 2) //isAdmin
  6.                         admins.add(c);
  7.         }
  8.         if (admins.size() > 0) {
  9.                 Random random = new Random();
  10.                 admin = admins.get(random.nextInt(admins.size()));
  11.                 Packet2Message p2 = new Packet2Message();
  12.                 String globality = "This message is private to you and " + packet.userFrom;
  13.                 if (packet.broadcast)
  14.                         globality = "This message has been publicly broadcast within the chat.";
  15.                 p2.message = "You've received a kick request from " + packet.userFrom + " to kick " + packet.userKick + ". Reason: " + convertReason(packet.reason) + ". " + globality;
  16.                 admin.getConnection().sendTCP(p2);
  17.         }else
  18.                 System.out.println("No admins found.");
  19. }
So as you can see, the method iterates through all the clients adding the admins to a new ArrayList, then it randomly selects one of them and sets them as the value of the CustomClient variable 'admin'. Then it creates the Packet2Message and sends it to the admin. sendToAllAdmins Method: This method is very similar to the sendToAdmin method except it doesn't randomly select just one single admin, it simply iterates through the currently connected clients list, and sends a new Packet2Message poacket to each of the clients who have the level of 2 (admin).
  1. public void sendToAllAdmins(Packet7AdminKick packet) {
  2.         for (CustomClient c : this.clients) {
  3.                 if (c.getLevel() == 2) { //isAdmin
  4.                         Packet2Message p2 = new Packet2Message();
  5.                         String globality = "This message is private to you and " + packet.userFrom;
  6.                         if (packet.broadcast)
  7.                                 globality = "This message has been publicly broadcast within the chat.";
  8.                         p2.message = "You've received a kick request from " + packet.userFrom + " to kick " + packet.userKick + ". Reason: " + convertReason(packet.reason) + ". " + globality;
  9.                         c.getConnection().sendTCP(p2);
  10.                         System.out.println("Sent request to " + c.getUsername());
  11.                 }
  12.         }
  13. }
convertReason Method: Finally we need to write the convertReason method. This method has the same reasons String array as the one in the client side application. It simply returns the option indexed at the parsed reason. If the reason does not exist (there are not enough elements in the array, we return 'Unfound.')...
  1. public String convertReason(String reason) {
  2.         String reasons[] = {"Spamming Chat", "Abusing Other Members", "Abusing Me", "Sending False Information", "Making Threats", "Other"};
  3.         if (reasons.length >= Integer.parseInt(reason)) {
  4.                 return reasons[Integer.parseInt(reason)];
  5.         }else
  6.                 return "{Unfound}";
  7.         }
Test: You can test this by opening one server, four clients, make two clients admins by typing:
  1. /admin {username}
Then within the two other clients, type a few requests, change the global variable from false to true and vice versa and see if the right people receive the requests and global messages. Example...
  1. /admin kick admin2 4 true

Add new comment