Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import Client;
- public class CommandsMethodsClient {
- //The Codes in the comments are for the methods that should be executed when the given code has been received
- //0x00
- void placeholder() {}
- //0x01
- void commandNotRegistered() {}
- //0x02
- void commandNotInUse() {}
- //0x03
- void commandObsolete() {}
- //0x04
- void commandReminder() {
- System.out.println("Received Reminder"); //TODO
- }
- //0x05
- void taskSuccessful() {}
- //0x06
- void taskFailed() {}
- //0x07
- void forceDisconnect() {}
- //0x08
- void forceAuthData() {
- acceptAuthRequest();
- }
- //0x09
- void acceptAuthRequest() {
- try {
- OutputStream output = Client.socket.getOutputStream();
- ObjectOutputStream outputObject = new ObjectOutputStream(output);
- Code code = new Code(Commands.databaseClient[9][0], false, null);
- outputObject.writeObject(code);
- Client.setLastCommandSent(code);
- createAuthPackage();
- } catch (IOException | ClassNotFoundException e) {
- System.out.println("Error occurred while trying to authenticate: " + e + " in " + Thread.currentThread());
- }
- }
- private void createAuthPackage() throws ClassNotFoundException, IOException {
- //PacketAuth packetAuth = new PacketAuth("String", (byte) 0); TODO
- Code auth = new Code(Commands.databaseClient[14][0], true, "authcode");
- waitForServerAuthPackageReady(auth);
- }
- private void waitForServerAuthPackageReady(Code authPackage) throws IOException, ClassNotFoundException {
- InputStream input = Client.socket.getInputStream();
- ObjectInputStream inputObject = new ObjectInputStream(input);
- Code code;
- System.out.println("Waiting for Server response...");
- while ((code = (Code) inputObject.readObject()) != null) {
- System.out.println("Received Data...");
- System.out.println("Input received: " + code);
- System.out.println("Code received: " + code.getCode());
- if (code.getCode().contentEquals(Commands.databaseServer[14][0])) {
- sendAuthPackage(authPackage);
- return;
- } else {
- }
- }
- }
- private void sendAuthPackage(Code authPackage) throws ClassNotFoundException, IOException {
- OutputStream output = Client.socket.getOutputStream();
- ObjectOutputStream outputObject = new ObjectOutputStream(output);
- outputObject.writeObject(authPackage);
- Client.setLastCommandSent(authPackage);
- waitForServerAuthResponse();
- }
- private void waitForServerAuthResponse() throws IOException, ClassNotFoundException {
- InputStream input = Client.socket.getInputStream();
- ObjectInputStream inputObject = new ObjectInputStream(input);
- Code code;
- System.out.println("Waiting for Server response...");
- while ((code = (Code) inputObject.readObject()) != null) {
- System.out.println("Received Data...");
- System.out.println("Input received: " + code);
- System.out.println("Code received: " + code.getCode());
- if (code.getCode().contentEquals(Commands.databaseServer[10][0])) {
- sendAnswerAuthSuccessful();
- return;
- } else {
- }
- }
- }
- private void sendAnswerAuthSuccessful() throws IOException {
- try {
- Thread.sleep(5000);
- } catch (InterruptedException e) {
- return;
- }
- OutputStream output = Client.socket.getOutputStream();
- ObjectOutputStream outputObject = new ObjectOutputStream(output);
- Code code = new Code(Commands.databaseClient[10][0], false, null);
- outputObject.writeObject(code);
- Client.setLastCommandSent(code);
- }
- //0x10
- void authSuccessful() {
- System.out.println("Nope...");
- }
- //0x11
- void authFailedClient() {}
- //0x12
- void authFailedOutdatedServer() {}
- //0x13
- void authFailedOutdatedClient() {}
- //0x14
- void authPackageReady() {}
- //0x31
- void acceptDataPackageRequest() {
- try {
- OutputStream output = Client.socket.getOutputStream();
- ObjectOutputStream outputObject = new ObjectOutputStream(output);
- Code code = new Code(Commands.databaseClient[33][0], false, null);
- outputObject.writeObject(code);
- Client.setLastCommandSent(code);
- createDataPackage();
- } catch (IOException | ClassNotFoundException | NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
- System.out.println("Error occurred while trying to send Data Package: " + e + " in " + Thread.currentThread());
- }
- }
- private void createDataPackage() throws IOException, ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
- //PacketDataClient packetAuth = new PacketDataClient("String", (byte) 0); TODO
- Code dataPackage = new Code(Commands.databaseClient[32][0], true, Client.getPlayerData());
- sendDataPackage(dataPackage);
- }
- private void sendDataPackage(Code packageData) throws IOException, ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
- OutputStream output = Client.socket.getOutputStream();
- ObjectOutputStream outputObject = new ObjectOutputStream(output);
- outputObject.writeObject(packageData);
- Client.setLastCommandSent(packageData);
- waitForServerPacketDataResponse();
- }
- private void waitForServerPacketDataResponse() throws IOException, ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
- InputStream input = Client.socket.getInputStream(); //TODO BREAKPOINT
- ObjectInputStream inputObject = new ObjectInputStream(input);
- Code code;
- System.out.println("Waiting for Server response...");
- while ((code = (Code) inputObject.readObject()) != null) {
- System.out.println("Received Data...");
- System.out.println("Input received: " + code);
- System.out.println("Code received: " + code.getCode());
- if (code.getCode().contentEquals(Commands.databaseServer[32][0])) {
- getPackageData(code);
- return;
- } else {
- }
- return;
- }
- }
- private void getPackageData(Code packageData) throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
- Client.setGameData(packageData);
- sendPacketDataServerAnswer();
- }
- private void sendPacketDataServerAnswer() throws IOException {
- OutputStream output = Client.socket.getOutputStream();
- ObjectOutputStream outputObject = new ObjectOutputStream(output);
- Code code = new Code(Commands.databaseClient[5][0], false, null);
- outputObject.writeObject(code);
- Client.setLastCommandSent(code);
- }
- //0x32
- void sendingDataPackage() {}
- //0x33
- void sendDataPackageRequest() {
- }
- // Obsolete/ old methods
- public void clearLastCommand() {
- Client.setLastCommandSent(new Code(Commands.databaseClient[0][0], false, null));
- }
- public void disconnect() {
- try {
- OutputStream output = Client.socket.getOutputStream();
- ObjectOutputStream outputObject = new ObjectOutputStream(output);
- Code code = new Code(Commands.databaseClient[7][0], false, null);
- outputObject.writeObject(code);
- Client.setLastCommandSent(code);
- } catch (IOException e) {
- }
- Client.setKillThread(true);
- //connectTest.socket.close(); TODO
- }
- public void checkLastCommand() {
- System.out.println("Checking Last Command...");
- try {
- Thread.sleep(5000);
- PrintWriter output = new PrintWriter(Client.socket.getOutputStream(), true);
- output.println("1x14");
- //output.println(connectTest.lastCommandSent);
- } catch (IOException | InterruptedException e) {
- System.out.println("An Error occurred while trying to resend last command.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement