From c9cb1b04204060d970a27c07ecec728f9f36fb40 Mon Sep 17 00:00:00 2001 From: SEMUL Date: Tue, 15 Aug 2023 02:07:35 +0300 Subject: [PATCH] code review & refactoring: take 2 --- .../java/org/semul/budny/account/Account.java | 130 +++++++----------- .../org/semul/budny/connection/Session.java | 13 +- .../java/org/semul/budny/event/Intention.java | 5 + .../java/org/semul/budny/helper/Task.java | 9 +- .../java/org/semul/budny/manager/Manager.java | 11 +- 5 files changed, 69 insertions(+), 99 deletions(-) create mode 100644 src/main/java/org/semul/budny/event/Intention.java diff --git a/src/main/java/org/semul/budny/account/Account.java b/src/main/java/org/semul/budny/account/Account.java index ed0cd69..4b20635 100644 --- a/src/main/java/org/semul/budny/account/Account.java +++ b/src/main/java/org/semul/budny/account/Account.java @@ -1,48 +1,40 @@ package org.semul.budny.account; import org.semul.budny.connection.Session; +import org.semul.budny.event.EventDriver; +import org.semul.budny.event.Intention; import org.semul.budny.exception.FailEmployException; import org.semul.budny.exception.StartSessionException; import org.semul.budny.helper.ThreadsController; -import org.semul.budny.manager.Manager; import java.util.LinkedList; import java.util.Queue; public class Account extends Thread { public static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Account.class); - private Manager manager; private final String username; private final String password; private volatile AccountInfo info; - private volatile boolean blockPlanning; - private volatile boolean completionStatus; private Session session; private final Queue taskQueue; + private volatile boolean blockPlanning; + private volatile boolean completionStatus; - public enum Intention { - DISABLE, GET_INFO, EMPLOY - } - - public static synchronized Account getInstance(Manager manager, String username, String password) { - Account account = new Account(manager, username, password); + public static synchronized Account getInstance(String username, String password) { + Account account = new Account(username, password); ThreadsController.threads.add(account); - return account; } - private Account(Manager manager, String username, String password) { - logger.info("Initialization..."); + private Account(String username, String password) { + logger.info("Initialization."); - this.manager = manager; this.username = username; this.password = password; + this.session = Session.getInstance(this, new EventDriver(EventDriver.getDriver(), username, password)); this.taskQueue = new LinkedList<>(); - setBlockPlanningStatus(false); + this.blockPlanning = false; this.completionStatus = false; - this.session = null; - - logger.info("Done."); } @Override @@ -52,86 +44,83 @@ public void run() { while (!Thread.currentThread().isInterrupted()) { if (this.taskQueue.size() > 0) { - postRequest(this.taskQueue.poll()); + runIntent(this.taskQueue.poll()); } - Thread.sleep(5000); + Thread.sleep(2000); + } + } catch (InterruptedException ignoredOne) { + try { + quit(false); + } catch (InterruptedException ignoredTwo) { + Thread.currentThread().interrupt(); } - } catch (InterruptedException ignored) { - disable(); } - } private void launch() throws InterruptedException { - logger.info("Launch..."); - - this.session = Session.getInstance(this, this.username, this.password); + logger.info("Launch"); try { this.session.start(); this.completionStatus = true; - logger.info("Successfully"); } catch (StartSessionException e) { logger.error(e); - System.out.println("Чтоооооо"); throw new InterruptedException(); } } - private void disable() { - logger.info("Disable..."); - - quitSession(); + // Если это действие от намерения менеджера то true если же при неудачном входе в аккаунт false (для синхронизации) + private void quit(boolean flag) throws InterruptedException { + logger.info("Disable"); - this.manager = null; - this.completionStatus = true; + if (this.session != null) { + this.session.close(); + this.session = null; + } - logger.info("Successfully"); + if (flag) { + throw new InterruptedException(); + } + this.completionStatus = true; Thread.currentThread().interrupt(); } - private void quitSession() { - if (this.session != null) { - this.session.close(); - this.session = null; - } + public boolean isLive() { + boolean status = !this.isInterrupted(); + logger.info("Alive: " + status); + return status; } public void addTask(Intention intent) { - logger.info("Adding a task: " + intent); + logger.info("Add a task: " + intent); this.taskQueue.add(intent); - - if (intent == Intention.GET_INFO) { - setBlockPlanningStatus(true); - } - logger.info("Done"); + setBlockPlanningStatus(intent); } - private void postRequest(Intention intent) { + private void runIntent(Intention intent) throws InterruptedException { logger.info("POST: " + intent); try { - this.session.getRequest(intent); + switch (intent) { + case GET_INFO -> session.getAccountInfo(); + case EMPLOY -> session.getEmploy(); + case DISABLE -> quit(true); + } } catch (FailEmployException employEx) { logger.warn(employEx); } catch (StartSessionException sessionEx) { logger.error(sessionEx); - this.manager.delAccount(this); - } - - this.completionStatus = true; - - if (intent != Intention.GET_INFO) { - setBlockPlanningStatus(false); + } finally { + setBlockPlanningStatus(intent); + this.completionStatus = true; } } - public boolean isLive() { - boolean status = !this.isInterrupted(); - logger.info("Alive: " + status); - return status; + public void setInfo(AccountInfo info) { + logger.info("Set info"); + this.info = info; } public AccountInfo getInfo() { @@ -139,22 +128,8 @@ public AccountInfo getInfo() { return this.info; } - public void setInfo(AccountInfo info) { - this.info = info; - } - - public String getUsername() { - logger.info("Get username"); - return this.username; - } - - public String getPassword() { - logger.info("Get password"); - return this.password; - } - - public void setBlockPlanningStatus(boolean status) { - this.blockPlanning = status; + private void setBlockPlanningStatus(Intention intent) { + this.blockPlanning = Intention.GET_INFO == intent; } public boolean getBlockPlanningStatus() { @@ -166,8 +141,6 @@ public boolean getBlockPlanningStatus() { * Если при чтении completionStatus == true, то происходит изменение состояния. */ public boolean isCompletion() { - logger.info("Completion status: " + this.completionStatus); - if (this.completionStatus) { this.completionStatus = false; return true; @@ -176,10 +149,9 @@ public boolean isCompletion() { @Override public String toString() { - logger.info("toString."); return "\n● Account:\n" + "▬▬ username: " + this.username + ";\n" + "▬▬ password: " + this.password + ";\n" + - "▬▬ status: " + (!this.isInterrupted() ? "launched" : "stopped") + ";\n"; + "▬▬ status: " + (!this.isInterrupted() ? "started" : "stopped") + ";\n"; } } diff --git a/src/main/java/org/semul/budny/connection/Session.java b/src/main/java/org/semul/budny/connection/Session.java index e865c95..98ef2a1 100644 --- a/src/main/java/org/semul/budny/connection/Session.java +++ b/src/main/java/org/semul/budny/connection/Session.java @@ -12,9 +12,8 @@ public class Session { private Account account; private EventDriver exec; - public static Session getInstance(Account account, String username, String password) { - EventDriver eventDriver = new EventDriver(EventDriver.getDriver(), username, password); - return new Session(account, eventDriver); + public static synchronized Session getInstance(Account account, EventDriver driver) { + return new Session(account, driver); } private Session(Account account, EventDriver eventDriver) { @@ -58,14 +57,6 @@ private boolean isConnect() { return connect; } - public void getRequest(Account.Intention intent) throws StartSessionException, FailEmployException { - switch (intent) { - case GET_INFO -> getAccountInfo(); - case EMPLOY -> getEmploy(); - case DISABLE -> close(); - } - } - /** *

Intents

*/ diff --git a/src/main/java/org/semul/budny/event/Intention.java b/src/main/java/org/semul/budny/event/Intention.java new file mode 100644 index 0000000..17c9637 --- /dev/null +++ b/src/main/java/org/semul/budny/event/Intention.java @@ -0,0 +1,5 @@ +package org.semul.budny.event; + +public enum Intention { + DISABLE, GET_INFO, EMPLOY, +} diff --git a/src/main/java/org/semul/budny/helper/Task.java b/src/main/java/org/semul/budny/helper/Task.java index a1df801..290a70e 100644 --- a/src/main/java/org/semul/budny/helper/Task.java +++ b/src/main/java/org/semul/budny/helper/Task.java @@ -1,6 +1,7 @@ package org.semul.budny.helper; import org.semul.budny.account.Account; +import org.semul.budny.event.Intention; import org.semul.budny.manager.Manager; import java.util.Objects; @@ -10,17 +11,17 @@ public class Task extends Thread { public static volatile int taskCount = 0; private Manager manager; private Account account; - private Account.Intention intent; + private Intention intent; private int countdown; - public static Task getInstance(Manager manager, Account account, Account.Intention intent, int countdown) { + public static Task getInstance(Manager manager, Account account, Intention intent, int countdown) { Task task = new Task(manager, account, intent, countdown); TasksController.tasks.add(task); return task; } - private Task(Manager manager, Account account, Account.Intention intent, int countdown) { + private Task(Manager manager, Account account, Intention intent, int countdown) { logger.info("Initialization..."); this.manager = manager; @@ -43,7 +44,7 @@ public void run() { if (account.isLive()) { logger.info("Signal to the manager about '" + intent + '.'); - if (Objects.requireNonNull(intent) == Account.Intention.EMPLOY) { + if (Objects.requireNonNull(intent) == Intention.EMPLOY) { manager.getJob(account); } } diff --git a/src/main/java/org/semul/budny/manager/Manager.java b/src/main/java/org/semul/budny/manager/Manager.java index ddae9ba..aa1693b 100644 --- a/src/main/java/org/semul/budny/manager/Manager.java +++ b/src/main/java/org/semul/budny/manager/Manager.java @@ -2,6 +2,7 @@ import org.semul.budny.account.Account; import org.semul.budny.account.AccountInfo; +import org.semul.budny.event.Intention; import org.semul.budny.helper.Controller; import org.semul.budny.helper.Task; import org.semul.budny.helper.TasksController; @@ -46,7 +47,7 @@ public void run() { public synchronized void addAccount(String username, String password) { logger.info("Enable account..."); - Account account = Account.getInstance(this, username, password); + Account account = Account.getInstance(username, password); account.start(); synch(account); if (account.isLive()) { @@ -60,7 +61,7 @@ public synchronized void addAccount(String username, String password) { public synchronized void delAccount(Account account) { logger.info("Disable account..."); - account.addTask(Account.Intention.DISABLE); + account.addTask(Intention.DISABLE); synch(account); if (!account.isLive()) { @@ -77,7 +78,7 @@ private void planning() { for (Account account : this.accounts) { if (!account.getBlockPlanningStatus()) { AccountInfo accountInfo = getAccountInfo(account); - Task.getInstance(this, account, Account.Intention.EMPLOY, accountInfo.workEndCountdown()).start(); + Task.getInstance(this, account, Intention.EMPLOY, accountInfo.workEndCountdown()).start(); } } @@ -87,7 +88,7 @@ private void planning() { private AccountInfo getAccountInfo(Account account) { logger.info("Get account info..."); - account.addTask(Account.Intention.GET_INFO); + account.addTask(Intention.GET_INFO); synch(account); AccountInfo accountInfo = account.getInfo(); @@ -98,7 +99,7 @@ private AccountInfo getAccountInfo(Account account) { public synchronized void getJob(Account account) { logger.info("Signal to employ"); - account.addTask(Account.Intention.EMPLOY); + account.addTask(Intention.EMPLOY); synch(account); }