Advertisement
Mohammad_Daoud

UDP Server and Student Class

Aug 23rd, 2021
1,357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.24 KB | None | 0 0
  1. package homeworks.requestUDP;
  2.  
  3. /**
  4.  * @TEAM_MEMBERS: MOHAMMAD ABDALLAH RAJA DAOUD - 0173632
  5.  * HAMZA MONTHER MOSTAFA AMIRAH - 0189136
  6.  * @instructor: Dr. Wesam Al Mobaideen
  7.  */
  8.  
  9. import java.net.DatagramPacket;
  10. import java.net.DatagramSocket;
  11. import java.util.HashMap;
  12. import java.util.NoSuchElementException;
  13. import java.util.StringTokenizer;
  14.  
  15. class Student {
  16.  
  17.  
  18.     private String studentName;
  19.     private double GPA;
  20.     private int age;
  21.  
  22.  
  23.     public String getStudentName() {
  24.         return studentName;
  25.     }
  26.  
  27.     public void setStudentName(String studentName) {
  28.         this.studentName = studentName;
  29.     }
  30.  
  31.     public double getGPA() {
  32.         return GPA;
  33.     }
  34.  
  35.     public void setGPA(double GPA) {
  36.         this.GPA = GPA;
  37.     }
  38.  
  39.     public int getAge() {
  40.         return age;
  41.     }
  42.  
  43.     public void setAge(int age) {
  44.         this.age = age;
  45.     }
  46.  
  47.     /*
  48.        TODO: function to return requested data from server
  49.     */
  50.     public String getRequest(String value) {
  51.         if (value.equalsIgnoreCase("gpa"))
  52.             return String.valueOf(getGPA());
  53.         else if (value.equalsIgnoreCase("age"))
  54.             return String.valueOf(getAge());
  55.         return String.valueOf(getStudentName());
  56.     }
  57. }
  58.  
  59. public class Server {
  60.     //TODO: function to check if requested information available
  61.  
  62.     static boolean checkInfo(String value) {
  63.         return value.equalsIgnoreCase("age") ||
  64.                 value.equalsIgnoreCase("gpa") ||
  65.                 value.equalsIgnoreCase("name");
  66.     }
  67.  
  68.     // main method
  69.     public static void main(String[] args) throws Exception {
  70.         // student samples
  71.         // student 1
  72.         Student mohammadDaoud = new Student();
  73.         mohammadDaoud.setAge(23);
  74.         mohammadDaoud.setGPA(2.5);
  75.         mohammadDaoud.setStudentName("Mohammad Abdallah Raja Daoud");
  76.  
  77.         // student 2
  78.         Student hamzaMonther = new Student();
  79.         hamzaMonther.setAge(23);
  80.         hamzaMonther.setGPA(3.9);
  81.         hamzaMonther.setStudentName("Hamza Monther Mostafa Amirah");
  82.  
  83.         // student 3
  84.         Student SaifSaboba = new Student();
  85.         SaifSaboba.setAge(23);
  86.         SaifSaboba.setGPA(1.5);
  87.         SaifSaboba.setStudentName("Saif Raed Mostafa Saboba");
  88.         /**
  89.          We use HashMap to identify each student by its ID (student number)
  90.          -because its unique- as a key, and the value of this key is object of type student
  91.          which contain student information.
  92.          */
  93.         HashMap<String, Student> dataPojo = new HashMap<String, Student>();
  94.         dataPojo.put("0173632", mohammadDaoud);
  95.         dataPojo.put("0156969", SaifSaboba);
  96.         dataPojo.put("0189136", hamzaMonther);
  97.  
  98.         DatagramSocket server = new DatagramSocket(5555);
  99.         byte data[] = new byte[4564];
  100.         DatagramPacket Packet_received = new DatagramPacket(data, data.length), // for receiving packets
  101.                 Packet_send = new DatagramPacket(data, data.length);//for sending packets
  102.         server.receive(Packet_received);
  103.         Packet_send.setAddress(Packet_received.getAddress());
  104.         Packet_send.setPort(Packet_received.getPort());
  105.  
  106.         String message;
  107.         String requestInfo;
  108.         String id;
  109.         StringTokenizer value;
  110.         String sendQuery;
  111.         while (true) {
  112.             data = new byte[4564];
  113.             Packet_received = new DatagramPacket(data, data.length);
  114.             server.receive(Packet_received);
  115.             message = new String(Packet_received.getData()).trim();
  116.             System.out.println(Packet_received.getAddress() + "//" + Packet_received.getPort() + " --> " + message);
  117.             value = new StringTokenizer(message);
  118.             if (message.equalsIgnoreCase("end")) break;
  119.             // TODO: we need to check if input contain two parts, info and id
  120.             try {
  121.                 requestInfo = value.nextToken();
  122.                 id = value.nextToken();
  123.             } catch (NoSuchElementException e) {
  124.                 Packet_send.setData(("\n****************\n" +
  125.                         "* WRONG INPUT !*\n" +
  126.                         "****************").getBytes());
  127.                 server.send(Packet_send);
  128.                 continue;
  129.             }
  130.             //TODO: then check if id exist
  131.             if (!dataPojo.containsKey(id)) {
  132.                 Packet_send.setData("There no such id in this server!, re-enter".getBytes());
  133.                 server.send(Packet_send);
  134.                 continue;
  135.             }
  136.             //TODO:then check information if it's available
  137.             if (!checkInfo(requestInfo)) {
  138.                 Packet_send.setData("Wrong request, no such information!, re-enter".getBytes());
  139.                 server.send(Packet_send);
  140.                 continue;
  141.             }
  142.             // TODO: otherwise return requestedInformation:RequestedInfo, StudentNumber:id to client
  143.             requestInfo = "requestedInformation:" + dataPojo.get(id).getRequest(requestInfo);
  144.             id = "studentNumber:" + id;
  145.             sendQuery = id + ", " + requestInfo;
  146.             Packet_send.setData(sendQuery.getBytes());
  147.             server.send(Packet_send);
  148.         }
  149.         System.out.println("Client has end this conversation !");
  150.         server.close();
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement