Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
code review & refactoring: take 2
Browse files Browse the repository at this point in the history
  • Loading branch information
algorov committed Aug 14, 2023
1 parent 03cb8b7 commit c9cb1b0
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 99 deletions.
130 changes: 51 additions & 79 deletions src/main/java/org/semul/budny/account/Account.java
Original file line number Diff line number Diff line change
@@ -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<Intention> 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
Expand All @@ -52,109 +44,92 @@ 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() {
logger.info("Get info");
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() {
Expand All @@ -166,8 +141,6 @@ public boolean getBlockPlanningStatus() {
* Если при чтении <b>completionStatus == true</b>, то происходит изменение состояния.
*/
public boolean isCompletion() {
logger.info("Completion status: " + this.completionStatus);

if (this.completionStatus) {
this.completionStatus = false;
return true;
Expand All @@ -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";
}
}
13 changes: 2 additions & 11 deletions src/main/java/org/semul/budny/connection/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
}

/**
* <h1>Intents</h1>
*/
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/semul/budny/event/Intention.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.semul.budny.event;

public enum Intention {
DISABLE, GET_INFO, EMPLOY,
}
9 changes: 5 additions & 4 deletions src/main/java/org/semul/budny/helper/Task.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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);
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/org/semul/budny/manager/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand All @@ -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()) {
Expand All @@ -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();
}
}

Expand All @@ -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();
Expand All @@ -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);
}

Expand Down

0 comments on commit c9cb1b0

Please sign in to comment.