Guest User

Untitled

a guest
Feb 11th, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.19 KB | None | 0 0
  1.  
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.util.logging.Level;
  6. import java.util.logging.Logger;
  7.  
  8. import org.json.JSONObject;
  9.  
  10. import jsc.kennel.ChatClient;
  11. import jsc.kennel.ChatResponseHandler;
  12. import jsc.kennel.ChatSetting;
  13. import jsc.kennel.util.FileUtils;
  14. import jsc.kennel.util.ServerConstants;
  15.  
  16. public class ClientStart implements ChatSetting {
  17.  
  18.     ChatClient client = null;
  19.     SendContentThread st = null;
  20.     String address = "127.0.0.1";
  21.     int port = 13456;
  22.     boolean bLogin = false;
  23.     String username = "";
  24.     String password = "";
  25.     String id = "";
  26.  
  27.     public ClientStart() {
  28.  
  29.         File userClient = new File("userclient.txt");
  30.         if (!userClient.exists()) {
  31.             try {
  32.                 userClient.createNewFile();
  33.             } catch (IOException e) {
  34.                 // TODO Auto-generated catch block
  35.                 e.printStackTrace();
  36.             }
  37.         }
  38.  
  39.     }
  40.     public void initUser() {
  41.         try {
  42.             String jsonvalue = FileUtils
  43.                     .readFileAsString(ServerConstants.USERS_CLIENT_FILE);
  44.             JSONObject obj = new JSONObject(jsonvalue);
  45.             // id = obj.get("key").toString();
  46.         } catch (Exception ex) {
  47.             System.out.println(ex);
  48.         }
  49.     }
  50.     public void SaveData() {
  51.         try {
  52.             JSONObject obj = new JSONObject();
  53.             obj.put("key", id);
  54.             PrintWriter writer = new PrintWriter(
  55.                     ServerConstants.USERS_CLIENT_FILE, "UTF-8");
  56.  
  57.             writer.println(obj.toString());
  58.             writer.close();
  59.         } catch (Exception ex) {
  60.             Logger.getLogger(ClientStart.class.getName()).log(Level.SEVERE,
  61.                     null, ex);
  62.         }
  63.     }
  64.  
  65.     public void AnalysisCommand(String msg) {
  66.         String[] tmp = msg.split("\\s");
  67.         /*
  68.          * if (tmp.length < 1) { Desc(); return; }
  69.          */
  70.         if (tmp[0].equals(CMD_LOGIN)) {
  71.             if (tmp.length < 3) {
  72.                 Desc();
  73.                 return;
  74.             }
  75.             username = tmp[1];
  76.             password = tmp[2];
  77.             String sms = MESSAGE_LOGIN + MESSAGE_UNIT_SEP + username
  78.                     + MESSAGE_UNIT_SEP + password;
  79.             sendMessage(sms);
  80.  
  81.         } else if (tmp[0].equals(CMD_MENU)) {
  82.             Desc();
  83.         } else if (tmp[0].equals(CMD_LOGOUT)) {
  84.             String sms = MESSAGE_LOGOUT;
  85.             sendMessage(sms);
  86.  
  87.         }
  88.  
  89.         else if (tmp[0].equals(CMD_SETUSRPASS)) {
  90.             username = tmp[1];
  91.             password = tmp[2];
  92.             String sms = MESSAGE_SETNICKPASS + MESSAGE_UNIT_SEP + username
  93.                     + MESSAGE_UNIT_SEP + password;
  94.             sendMessage(sms);
  95.  
  96.         } else if (tmp[0].equals(CMD_GETNICK)) {
  97.  
  98.             String sms = MESSAGE_GETNICK;
  99.             sendMessage(sms);
  100.  
  101.         }
  102.  
  103.         else if (tmp[0].equals((CMD_ADD_FRIEND))) {
  104.             String sms = MESSAGE_ADD_FRIEND + msg;
  105.             sendMessage(sms);
  106.  
  107.         } else if (tmp[0].equals(CMD_GET_FRIENDS_LIST)) {
  108.  
  109.             String sms = GET_FRIENDS_LIST;
  110.             System.out.println("client send");
  111.             sendMessage(sms);
  112.  
  113.         }
  114.  
  115.         else if (tmp[0].equals(CMD_SPLIT)) {
  116.             String sms = MESSAGE_SPLIT + msg;
  117.             System.out.println("client will split");
  118.             sendMessage(sms);
  119.  
  120.         } else if (tmp[0].equals(CMD_STATUS)) {
  121.             String sms = MESSAGE_STATUS + msg;
  122.             System.out.println("client will status");
  123.             sendMessage(sms);
  124.  
  125.         } else if (tmp[0].equals(CMD_PAY)) {
  126.             String sms = MESSAGE_PAYED + msg;
  127.             System.out.println("client will payed");
  128.             sendMessage(sms);
  129.  
  130.         } else if (tmp[0].equals(CMD_CREATE_GROUP)) {
  131.             String sms = MESSAGE_CREATE_GROUP + msg;
  132.             System.out.println("client will make group");
  133.             sendMessage(sms);
  134.  
  135.         } else if (tmp[0].equals(CMD_SPLIT_GROUP)) {
  136.             String sms = MESSAGE_SPLIT_GROUP + msg;
  137.             System.out.println("client will split in group");
  138.             sendMessage(sms);
  139.  
  140.         } else {
  141.             System.out.println("Illegal command, try one from the menu");
  142.  
  143.         }
  144.         return;
  145.  
  146.     }
  147.     public void sendMessage(String sms) {
  148.         sms = id + MESSAGE_UNIT_SEP + sms;
  149.         client.sendMessage(sms);
  150.     }
  151.  
  152.     public void AnalysisContent(String msg) {
  153.  
  154.         if (msg.contains(MESSAGE_NO_LOGIN_CREDENTIALS)) {
  155.             String tmp[] = msg.split(MESSAGE_UNIT_SEP);
  156.             System.out.println(tmp[2]);
  157.         }
  158.  
  159.         if (msg.contains(MESSAGE_REGISTER_FAIL)) {
  160.             String tmp[] = msg.split(MESSAGE_UNIT_SEP);
  161.             System.out.println(tmp[2]);
  162.             return;
  163.         }
  164.         if (msg.contains(MESSAGE_LOGIN_STATUS_FAIL)) {
  165.             String tmp[] = msg.split(MESSAGE_UNIT_SEP);
  166.             System.out.println(tmp[2]);
  167.         }
  168.  
  169.         if (msg.contains(MESSAGE_PAYED_STATUS_OK)
  170.                 || msg.contains(MESSAGE_PAYED_STATUS_FAIL)) {
  171.             String tmp[] = msg.split(MESSAGE_UNIT_SEP);
  172.             System.out.println(tmp[2]);
  173.         }
  174.  
  175.         if (msg.contains(MESSAGE_LOGIN_OK)) {
  176.             // if (!bLogin) {
  177.             String tmp[] = msg.split(MESSAGE_UNIT_SEP);
  178.  
  179.             this.id = tmp[2];
  180.  
  181.             System.out.println("You logged in successfully");
  182.  
  183.             return;
  184.         }
  185.         if (msg.contains(MESSAGE_LOGIN_FAIL)) {
  186.  
  187.             String tmp[] = msg.split(MESSAGE_UNIT_SEP);
  188.             System.out.println(tmp[2]);
  189.             return;
  190.         }
  191.  
  192.         if (msg.contains(MESSAGE_SETNICKPASS_OK)) {
  193.             String tmp[] = msg.split(MESSAGE_UNIT_SEP);
  194.  
  195.             String username = tmp[2];
  196.             System.out.println("Nickname changed to " + username);
  197.             return;
  198.         }
  199.         if (msg.contains(MESSAGE_SETNICKPASS_FAIL)) {
  200.             String tmp[] = msg.split(MESSAGE_UNIT_SEP);
  201.             String reason = tmp[2];
  202.             System.out.println("Failed because : " + reason);
  203.             return;
  204.         }
  205.  
  206.         if (msg.contains(MESSAGE_GETNICKOK)
  207.                 || msg.contains(MESSAGE_GETNICKFAIL)) {
  208.             String tmp[] = msg.split(MESSAGE_UNIT_SEP);
  209.  
  210.             String result = tmp[2];
  211.             System.out.println(result);
  212.             return;
  213.         }
  214.  
  215.         if (msg.contains(MESSAGE_SPLIT_FAIL)
  216.                 || msg.contains(MESSAGE_SPLIT_OK)) {
  217.             String tmp[] = msg.split(MESSAGE_UNIT_SEP);
  218.  
  219.             String result = tmp[2];
  220.             System.out.println(result);
  221.             return;
  222.  
  223.         }
  224.         if (msg.contains(MESSAGE_LOGOUT_OK)) {
  225.             System.out.println("Successfull logout.");
  226.             bLogin = false;
  227.             return;
  228.         }
  229.  
  230.         if (msg.contains(MESSAGE_GETFRIENDSOK)) {
  231.             String tmp[] = msg.split(MESSAGE_UNIT_SEP);
  232.             System.out.println(tmp[1]);
  233.             return;
  234.         }
  235.  
  236.         if (msg.contains(MESSAGE_GETFRIENDSFAIL)) {
  237.  
  238.             String tmp[] = msg.split(MESSAGE_UNIT_SEP);
  239.             System.out.println(tmp[1]);
  240.             return;
  241.         }
  242.  
  243.         if (msg.contains(MESSAGE_ADD_FRIEND_OK)
  244.                 || msg.contains(MESSAGE_ADD_FRIEND_FAIL)) {
  245.             String tmp[] = msg.split(MESSAGE_UNIT_SEP);
  246.             System.out.println(tmp[2]);
  247.             return;
  248.         }
  249.  
  250.         if (msg.contains(MESSAGE_PAYMENT_STATUS)) {
  251.             String tmp[] = msg.split(MESSAGE_UNIT_SEP);
  252.             System.out.println(tmp[2]);
  253.  
  254.             return;
  255.         }
  256.         if (msg.contains(MESSAGE_CREATE_GROUP_OK)) {
  257.             String tmp[] = msg.split(MESSAGE_UNIT_SEP);
  258.             System.out.println(tmp[2]);
  259.  
  260.             return;
  261.         }
  262.         if (msg.contains(MESSAGE_CREATE_GROUP_FAIL)) {
  263.             String tmp[] = msg.split(MESSAGE_UNIT_SEP);
  264.             System.out.println(tmp[2]);
  265.  
  266.             return;
  267.         }
  268.         if (msg.contains(MESSAGE_SPLIT_GROUP_FAIL)
  269.                 || msg.contains(MESSAGE_SPLIT_GROUP_OK)) {
  270.             String tmp[] = msg.split(MESSAGE_UNIT_SEP);
  271.             System.out.println(tmp[2]);
  272.  
  273.             return;
  274.         }
  275.  
  276.     }
  277.  
  278.     class SendContentThread extends Thread {
  279.  
  280.         @Override
  281.         public void run() {
  282.  
  283.             while (client.isOpen()) {
  284.                 try {
  285.                     String msg = readInput();
  286.                     if (msg.length() > 0) {
  287.                         AnalysisCommand(msg);
  288.                     }
  289.                     sleep(100);
  290.                 } catch (InterruptedException ex) {
  291.                     Logger.getLogger(ClientStart.class.getName())
  292.                             .log(Level.SEVERE, null, ex);
  293.                 }
  294.             }
  295.         }
  296.  
  297.     }
  298.  
  299.     public String readInput() {
  300.         String str = "";
  301.         try {
  302.             byte b[] = new byte[1024];
  303.             int readedByte = 0;
  304.  
  305.             // while((readedByte=System.in.read(b))!=-1)
  306.             // {
  307.             readedByte = System.in.read(b);
  308.             str += new String(b, 0, readedByte);
  309.             // }
  310.             return str;
  311.         } catch (IOException ex) {
  312.             Logger.getLogger(ClientStart.class.getName()).log(Level.SEVERE,
  313.                     null, ex);
  314.         }
  315.         return "";
  316.     }
  317.     public void Desc() {
  318.         System.out.println("Commands are as follows");
  319.         System.out.println("    1) " + CMD_LOGIN + " <username> <password>");
  320.         System.out.println("    2) " + CMD_LOGOUT + "");
  321.         System.out.println("    3) " + CMD_MENU + "");
  322.         System.out.println("    4) ");
  323.         System.out
  324.                 .println("    5) " + CMD_SETUSRPASS + " <username> <password>");
  325.         System.out.println("    6) " + CMD_SHOWALLUSER);
  326.         System.out.println("    7) " + CMD_ADD_FRIEND + " <friend-name>");
  327.         System.out.println("    8) " + CMD_SPLIT_GROUP
  328.                 + " <amount> <group-name> <reason>");
  329.         System.out.println("    9) " + CMD_STATUS + " <friend-name>");
  330.         System.out.println("    10) " + CMD_PAY + " <amount> <friend-name> ");
  331.         System.out.println("    11) " + CMD_CREATE_GROUP
  332.                 + "  <group-name> <user1> ... <userN>");
  333.     }
  334.     public ClientStart(String add, int prt) {
  335.         address = add;
  336.         port = prt;
  337.         try {
  338.  
  339.             initUser();
  340.             if (id.isEmpty()) {
  341.                 id = System.currentTimeMillis() + "";
  342.                 SaveData();
  343.             }
  344.  
  345.             client = new ChatClient(id, address, port,
  346.                     new ChatResponseHandler() {
  347.                         @Override
  348.                         public void onMessage(String message) {
  349.                             AnalysisContent(message);
  350.                         }
  351.                     });
  352.             client.start();
  353.  
  354.             // SecurityThread secTh = new SecurityThread();
  355.             // secTh.start();
  356.             st = new SendContentThread();
  357.             st.start();
  358.  
  359.             client.sendMessage(MESSAGE_REG + MESSAGE_UNIT_SEP + id);
  360.  
  361.             Desc();
  362.         } catch (IOException ex) {
  363.             Logger.getLogger(ClientStart.class.getName()).log(Level.SEVERE,
  364.                     null, ex);
  365.         }
  366.     }
  367.  
  368.     public static void main(String... args) {
  369.         if (args.length < 2) {
  370.             System.out.println(
  371.                     "please input correct parameter . ex: java -jar client.jar 127.0.0.1 13456");
  372.             // System.exit(0);
  373.         }
  374.         new ClientStart("127.0.0.1", 13456);
  375.     }
  376. }
Add Comment
Please, Sign In to add comment