Mysticforce

ceng320assignment4

Dec 2nd, 2022
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | Source Code | 0 0
  1.  
  2. import java.io.*;
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.net.*;
  6. // Essential Libraries
  7. /**
  8. *
  9. * @author Deo Anthony Madrid
  10. */
  11. public class main{
  12.  
  13. String helpMenu(String [] tokens)
  14. {
  15. int tokenSize = tokens.length;
  16. // Help menu for specified token
  17. String [] cmd = new String[tokenSize];
  18. String [][] helpArray = new String[][] {
  19.  
  20. {"help", "\r\n help gives you a description of the specified command.\n"},
  21. {"time", "\r\n time gives you the current UNIX time from a UDP based server.\n"},
  22. {"rand", "\r\n rand gives you a randum number generated from a TCP based server.\n"},
  23. {"dict", "\r\n dict gives you a detailed description of a phrase/word.\n"},
  24. {"exit", "\r\n exit - ends the client session\n"}
  25. };
  26.  
  27. for(int t = 1 ;t < tokenSize; t++){
  28. if(tokens[t]!=null){
  29. for(int i=0;i<5;i++){
  30. if(tokens[t].equals(helpArray[i][0])){
  31. cmd[t]=helpArray[i][1];
  32. }
  33. }
  34. }
  35. else{
  36. break;
  37. }
  38. t++;
  39. }
  40. if(tokenSize == 1) return "Available commands are: \r\n help \r\n time \r\n rand \r\n dict \r\n exit";
  41. else
  42. {
  43. StringBuffer sb = new StringBuffer();
  44. for(int s = 1; s < cmd.length; s++) {
  45. sb.append(cmd[s]);
  46. }
  47. String str = sb.toString();
  48. if(str.equals("null")){
  49. return("No Help for that Command!");
  50. }
  51. else{
  52. return str;
  53. }
  54. // return "This is the help command method";
  55. }
  56. }
  57.  
  58. String dayTime(String [] args) throws IOException
  59. {
  60. // Start a daytime client to connect to server
  61. System.out.println("UDP DaytimeClient Running");
  62.  
  63. String hostname = "localhost";
  64. int port = 40156;
  65.  
  66. InetAddress address = InetAddress.getByName(hostname);
  67. DatagramSocket socket = new DatagramSocket();
  68.  
  69. byte[] buffer = new byte[512];
  70.  
  71. DatagramPacket request = new DatagramPacket(buffer, buffer.length, address, port);
  72. socket.send(request);
  73.  
  74.  
  75. byte[] receive = new byte[66666];
  76. Date date = new Date();
  77. String strDate = (date.toString());
  78. byte[] dateMsg = strDate.getBytes();
  79.  
  80. DatagramPacket DpReceive = null;
  81. DpReceive = new DatagramPacket(receive, receive.length);
  82.  
  83. // Step 3 : recieve the data in byte buffer.
  84. socket.receive(DpReceive);
  85. System.out.println("Client: " + DpReceive);
  86. // display time and date using toString()
  87. receive = new byte[66666];
  88. return (date.toString());
  89. // return "This is the time command method";
  90. }
  91. String randNum(String [] tokens)
  92. {
  93. int min = 1;
  94. int max = Integer.parseInt(tokens[1]);
  95.  
  96. //Generate random int value from 1 to user specified max
  97. System.out.println("Random value in int from "+min+" to "+max+ ":");
  98. int randNum = (int)Math.floor(Math.random()*(max-min+1)+min);
  99. String output = Integer.toString(randNum);
  100.  
  101. return output;
  102. }
  103. String dicT(String [] tokens)
  104. {
  105. // To be run on VM with working dict function installed
  106. ProcessBuilder processBuilder = new ProcessBuilder();
  107.  
  108. processBuilder.command("dict",tokens[1]);
  109.  
  110. try {
  111.  
  112. Process process = processBuilder.start();
  113.  
  114. BufferedReader reader =
  115. new BufferedReader(new InputStreamReader(process.getInputStream()));
  116.  
  117. String line;
  118. while ((line = reader.readLine()) != null) {
  119. System.out.println(line);
  120. }
  121.  
  122. int exitCode = process.waitFor();
  123. System.out.println("\nExited with error code : " + exitCode);
  124.  
  125. } catch (IOException e) {
  126. e.printStackTrace();
  127. } catch (InterruptedException e) {
  128. e.printStackTrace();
  129. }
  130. return "This is the dict command method";
  131. }
  132.  
  133.  
  134. main(InputStream in, PrintStream out) throws IOException {
  135. while(true)
  136. {
  137. Scanner scanner=new Scanner(in);
  138. String line;
  139. String[] tokens;
  140. out.print("Enter a cmd: help, time, rand, dict, or exit: ");
  141. out.flush();
  142. while(scanner.hasNextLine()) {
  143. //Input stream for user input commands
  144. line=scanner.nextLine();
  145. tokens=line.split(" ");
  146. switch(tokens[0].toLowerCase())
  147. {
  148. case "help": out.println(helpMenu(tokens));
  149. break;
  150. case "time": out.println(dayTime(tokens));
  151. break;
  152. case "rand": out.println(randNum(tokens));
  153. break;
  154. case "dict": out.println(dicT(tokens));
  155. break;
  156. case "exit": out.println("Ending Client Session");
  157. System.exit(0);
  158.  
  159. default: out.println("Bad command: " + tokens[0]);
  160. }
  161. out.print("Next command: ");
  162. out.flush();
  163. }
  164.  
  165. }
  166. }
  167.  
  168.  
  169. public static void main(String[] args) throws IOException {
  170. // Main Method
  171. new main(System.in,System.out);
  172. }
  173.  
  174. }
  175.  
Advertisement
Add Comment
Please, Sign In to add comment