Advertisement
fjboc

CommandsMethodsClient.java

Apr 21st, 2019
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.60 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.ObjectInputStream;
  4. import java.io.ObjectOutputStream;
  5. import java.io.OutputStream;
  6. import java.io.PrintWriter;
  7. import Client;
  8.  
  9. public class CommandsMethodsClient {
  10.    
  11.     //The Codes in the comments are for the methods that should be executed when the given code has been received
  12.     //0x00
  13.     void placeholder() {}
  14.     //0x01
  15.     void commandNotRegistered() {}
  16.     //0x02
  17.     void commandNotInUse() {}
  18.     //0x03
  19.     void commandObsolete() {}
  20.     //0x04
  21.     void commandReminder() {
  22.         System.out.println("Received Reminder"); //TODO
  23.     }
  24.     //0x05
  25.     void taskSuccessful() {}
  26.     //0x06
  27.     void taskFailed() {}
  28.     //0x07
  29.     void forceDisconnect() {}
  30.     //0x08
  31.     void forceAuthData() {
  32.         acceptAuthRequest();
  33.     }
  34.     //0x09
  35.     void acceptAuthRequest() {
  36.         try {
  37.             OutputStream output = Client.socket.getOutputStream();
  38.             ObjectOutputStream outputObject = new ObjectOutputStream(output);
  39.             Code code = new Code(Commands.databaseClient[9][0], false, null);
  40.             outputObject.writeObject(code);
  41.             Client.setLastCommandSent(code);
  42.             createAuthPackage();
  43.         } catch (IOException | ClassNotFoundException e) {
  44.             System.out.println("Error occurred while trying to authenticate: " + e + " in " + Thread.currentThread());
  45.         }
  46.     }
  47.     private void createAuthPackage() throws ClassNotFoundException, IOException {
  48.         //PacketAuth packetAuth = new PacketAuth("String", (byte) 0); TODO
  49.         Code auth = new Code(Commands.databaseClient[14][0], true, "authcode");
  50.         waitForServerAuthPackageReady(auth);
  51.     }
  52.     private void waitForServerAuthPackageReady(Code authPackage) throws IOException, ClassNotFoundException {
  53.         InputStream input = Client.socket.getInputStream();
  54.         ObjectInputStream inputObject = new ObjectInputStream(input);
  55.         Code code;
  56.         System.out.println("Waiting for Server response...");
  57.         while ((code = (Code) inputObject.readObject()) != null) {
  58.             System.out.println("Received Data...");
  59.             System.out.println("Input received: " + code);
  60.             System.out.println("Code received: " + code.getCode());
  61.            
  62.             if (code.getCode().contentEquals(Commands.databaseServer[14][0])) {
  63.                 sendAuthPackage(authPackage);
  64.                 return;
  65.             } else {
  66.                
  67.             }
  68.         }
  69.     }
  70.     private void sendAuthPackage(Code authPackage) throws ClassNotFoundException, IOException {
  71.         OutputStream output = Client.socket.getOutputStream();
  72.         ObjectOutputStream outputObject = new ObjectOutputStream(output);
  73.         outputObject.writeObject(authPackage);
  74.         Client.setLastCommandSent(authPackage);
  75.         waitForServerAuthResponse();
  76.     }
  77.     private void waitForServerAuthResponse() throws IOException, ClassNotFoundException {
  78.         InputStream input = Client.socket.getInputStream();
  79.         ObjectInputStream inputObject = new ObjectInputStream(input);
  80.         Code code;
  81.         System.out.println("Waiting for Server response...");
  82.         while ((code = (Code) inputObject.readObject()) != null) {
  83.             System.out.println("Received Data...");
  84.             System.out.println("Input received: " + code);
  85.             System.out.println("Code received: " + code.getCode());
  86.            
  87.             if (code.getCode().contentEquals(Commands.databaseServer[10][0])) {
  88.                 sendAnswerAuthSuccessful();
  89.                 return;
  90.             } else {
  91.                
  92.             }
  93.         }
  94.     }
  95.     private void sendAnswerAuthSuccessful() throws IOException {
  96.         try {
  97.             Thread.sleep(5000);
  98.         } catch (InterruptedException e) {
  99.             return;
  100.         }
  101.         OutputStream output = Client.socket.getOutputStream();
  102.         ObjectOutputStream outputObject = new ObjectOutputStream(output);
  103.         Code code = new Code(Commands.databaseClient[10][0], false, null);
  104.         outputObject.writeObject(code);
  105.         Client.setLastCommandSent(code);
  106.     }
  107.     //0x10
  108.     void authSuccessful() {
  109.         System.out.println("Nope...");
  110.     }
  111.     //0x11
  112.     void authFailedClient() {}
  113.     //0x12
  114.     void authFailedOutdatedServer() {}
  115.     //0x13
  116.     void authFailedOutdatedClient() {}
  117.     //0x14
  118.     void authPackageReady() {}
  119.     //0x31
  120.     void acceptDataPackageRequest() {
  121.         try {
  122.             OutputStream output = Client.socket.getOutputStream();
  123.             ObjectOutputStream outputObject = new ObjectOutputStream(output);
  124.             Code code = new Code(Commands.databaseClient[33][0], false, null);
  125.             outputObject.writeObject(code);
  126.             Client.setLastCommandSent(code);
  127.             createDataPackage();
  128.         } catch (IOException | ClassNotFoundException | NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
  129.             System.out.println("Error occurred while trying to send Data Package: " + e + " in " + Thread.currentThread());
  130.         }
  131.     }
  132.     private void createDataPackage() throws IOException, ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
  133.         //PacketDataClient packetAuth = new PacketDataClient("String", (byte) 0); TODO
  134.         Code dataPackage = new Code(Commands.databaseClient[32][0], true, Client.getPlayerData());
  135.         sendDataPackage(dataPackage);
  136.     }
  137.     private void sendDataPackage(Code packageData) throws IOException, ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
  138.         OutputStream output = Client.socket.getOutputStream();
  139.         ObjectOutputStream outputObject = new ObjectOutputStream(output);
  140.         outputObject.writeObject(packageData);
  141.         Client.setLastCommandSent(packageData);
  142.         waitForServerPacketDataResponse();
  143.     }
  144.     private void waitForServerPacketDataResponse() throws IOException, ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
  145.         InputStream input = Client.socket.getInputStream(); //TODO BREAKPOINT
  146.         ObjectInputStream inputObject = new ObjectInputStream(input);
  147.         Code code;
  148.         System.out.println("Waiting for Server response...");
  149.         while ((code = (Code) inputObject.readObject()) != null) {
  150.             System.out.println("Received Data...");
  151.             System.out.println("Input received: " + code);
  152.             System.out.println("Code received: " + code.getCode());
  153.            
  154.             if (code.getCode().contentEquals(Commands.databaseServer[32][0])) {
  155.                 getPackageData(code);
  156.                 return;
  157.             } else {
  158.                
  159.             }
  160.             return;
  161.         }
  162.     }
  163.     private void getPackageData(Code packageData) throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
  164.         Client.setGameData(packageData);
  165.         sendPacketDataServerAnswer();
  166.     }
  167.     private void sendPacketDataServerAnswer() throws IOException {
  168.         OutputStream output = Client.socket.getOutputStream();
  169.         ObjectOutputStream outputObject = new ObjectOutputStream(output);
  170.         Code code = new Code(Commands.databaseClient[5][0], false, null);
  171.         outputObject.writeObject(code);
  172.         Client.setLastCommandSent(code);
  173.     }
  174.     //0x32
  175.     void sendingDataPackage() {}
  176.     //0x33
  177.     void sendDataPackageRequest() {
  178.        
  179.     }
  180.    
  181.  
  182.  
  183.  
  184.  
  185.     //  Obsolete/ old methods
  186.    
  187.     public void clearLastCommand() {
  188.         Client.setLastCommandSent(new Code(Commands.databaseClient[0][0], false, null));
  189.     }
  190.     public void disconnect() {
  191.         try {
  192.             OutputStream output = Client.socket.getOutputStream();
  193.             ObjectOutputStream outputObject = new ObjectOutputStream(output);
  194.             Code code = new Code(Commands.databaseClient[7][0], false, null);
  195.             outputObject.writeObject(code);
  196.             Client.setLastCommandSent(code);
  197.         } catch (IOException e) {
  198.            
  199.         }
  200.         Client.setKillThread(true);
  201.         //connectTest.socket.close(); TODO
  202.     }
  203.     public void checkLastCommand() {
  204.         System.out.println("Checking Last Command...");
  205.         try {
  206.             Thread.sleep(5000);
  207.             PrintWriter output = new PrintWriter(Client.socket.getOutputStream(), true);
  208.             output.println("1x14");
  209.             //output.println(connectTest.lastCommandSent);
  210.         } catch (IOException | InterruptedException e) {
  211.             System.out.println("An Error occurred while trying to resend last command.");
  212.         }
  213.     }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement