wanna kms, so dumb

This commit is contained in:
HorizonCode 2022-02-18 09:59:47 +01:00
parent 86fdcffec2
commit b067d432f6
19 changed files with 240 additions and 14 deletions

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

34
log/latest.log Normal file
View File

@ -0,0 +1,34 @@
[09:58:54 INFO] Got ticket with UUID: eb64d1ad-a701-4890-a629-7fbbbc551778
[09:58:54 INFO] Got ticket with UUID: f9a11e85-a7e7-422e-9533-6b7b23e4163b
[09:58:54 WARN] max amount of tickets reached, waiting...
[09:58:59 INFO] ticket with UUID f9a11e85-a7e7-422e-9533-6b7b23e4163b expired.
[09:58:59 INFO] ticket with UUID eb64d1ad-a701-4890-a629-7fbbbc551778 expired.
[09:58:59 INFO] i expired, oof :c
[09:58:59 INFO] i expired, oof :c
[09:58:59 INFO] Got ticket with UUID: 3df300a7-3e63-48be-9f96-85297927ad0f
[09:58:59 INFO] Got ticket with UUID: 9e166410-dcb5-46d5-b294-f3df7c1f93ca
[09:58:59 WARN] max amount of tickets reached, waiting...
[09:59:04 INFO] ticket with UUID 3df300a7-3e63-48be-9f96-85297927ad0f expired.
[09:59:04 INFO] ticket with UUID 9e166410-dcb5-46d5-b294-f3df7c1f93ca expired.
[09:59:04 INFO] i expired, oof :c
[09:59:04 INFO] i expired, oof :c
[09:59:04 INFO] Got ticket with UUID: 13ba05cd-c27f-4e68-b75d-7794aeeeb45a
[09:59:04 INFO] Got ticket with UUID: 58c4c964-8bb7-4b0f-897a-fa5025d31977
[09:59:04 WARN] max amount of tickets reached, waiting...
[09:59:09 INFO] ticket with UUID 58c4c964-8bb7-4b0f-897a-fa5025d31977 expired.
[09:59:09 INFO] ticket with UUID 13ba05cd-c27f-4e68-b75d-7794aeeeb45a expired.
[09:59:09 INFO] i expired, oof :c
[09:59:09 INFO] i expired, oof :c
[09:59:09 INFO] Got ticket with UUID: f01ad999-9819-4b0f-9f36-87f2bd337935
[09:59:09 INFO] Got ticket with UUID: 1db2ff99-9f5b-425e-9ee8-62e2072dd8ce
[09:59:09 WARN] max amount of tickets reached, waiting...
[09:59:14 INFO] ticket with UUID f01ad999-9819-4b0f-9f36-87f2bd337935 expired.
[09:59:14 INFO] ticket with UUID 1db2ff99-9f5b-425e-9ee8-62e2072dd8ce expired.
[09:59:14 INFO] i expired, oof :c
[09:59:14 INFO] Got ticket with UUID: e0f84463-9df9-4fc5-b9ff-5144207e0e30
[09:59:14 INFO] i expired, oof :c
[09:59:14 INFO] Got ticket with UUID: 3ba2bf87-0264-4799-a921-68c08383a245
[09:59:19 INFO] ticket with UUID e0f84463-9df9-4fc5-b9ff-5144207e0e30 expired.
[09:59:19 INFO] ticket with UUID 3ba2bf87-0264-4799-a921-68c08383a245 expired.
[09:59:19 INFO] i expired, oof :c
[09:59:19 INFO] i expired, oof :c

View File

@ -1,21 +1,25 @@
package net.horizoncode.tickettest;
import lombok.Getter;
import net.horizoncode.tickettest.manager.Ticket;
import net.horizoncode.tickettest.manager.TicketCallback;
import net.horizoncode.tickettest.logging.Logger;
import net.horizoncode.tickettest.manager.TicketManager;
import java.io.File;
import java.util.stream.IntStream;
public class TicketTest {
@Getter private static TicketTest instance;
@Getter private final TicketManager ticketManager;
@Getter private final Logger logger;
public static void main(String[] args) {
new TicketTest();
}
public TicketTest() {
instance = this;
this.logger = new Logger(new File("log"), true);
this.ticketManager = TicketManager.builder().ticketLifespan(5000).maxTicketsExisting(2).build();
IntStream.range(0, 10)
@ -24,7 +28,7 @@ public class TicketTest {
getTicketManager()
.addTicket(
() -> {
System.out.println("i expired oof");
getLogger().info("i expired, oof :c");
})
.use());

View File

@ -0,0 +1,68 @@
package net.horizoncode.tickettest.logging;
public class ConsoleColors
{
public static final String RESET = "\u001b[0m";
public static final String BLACK = "\u001b[0;30m";
public static final String RED = "\u001b[0;31m";
public static final String GREEN = "\u001b[0;32m";
public static final String YELLOW = "\u001b[0;33m";
public static final String BLUE = "\u001b[0;34m";
public static final String PURPLE = "\u001b[0;35m";
public static final String CYAN = "\u001b[0;36m";
public static final String WHITE = "\u001b[0;37m";
public static final String BLACK_BOLD = "\u001b[1;30m";
public static final String RED_BOLD = "\u001b[1;31m";
public static final String GREEN_BOLD = "\u001b[1;32m";
public static final String YELLOW_BOLD = "\u001b[1;33m";
public static final String BLUE_BOLD = "\u001b[1;34m";
public static final String PURPLE_BOLD = "\u001b[1;35m";
public static final String CYAN_BOLD = "\u001b[1;36m";
public static final String WHITE_BOLD = "\u001b[1;37m";
public static final String BLACK_UNDERLINED = "\u001b[4;30m";
public static final String RED_UNDERLINED = "\u001b[4;31m";
public static final String GREEN_UNDERLINED = "\u001b[4;32m";
public static final String YELLOW_UNDERLINED = "\u001b[4;33m";
public static final String BLUE_UNDERLINED = "\u001b[4;34m";
public static final String PURPLE_UNDERLINED = "\u001b[4;35m";
public static final String CYAN_UNDERLINED = "\u001b[4;36m";
public static final String WHITE_UNDERLINED = "\u001b[4;37m";
public static final String BLACK_BACKGROUND = "\u001b[40m";
public static final String RED_BACKGROUND = "\u001b[41m";
public static final String GREEN_BACKGROUND = "\u001b[42m";
public static final String YELLOW_BACKGROUND = "\u001b[43m";
public static final String BLUE_BACKGROUND = "\u001b[44m";
public static final String PURPLE_BACKGROUND = "\u001b[45m";
public static final String CYAN_BACKGROUND = "\u001b[46m";
public static final String WHITE_BACKGROUND = "\u001b[47m";
public static final String BLACK_BRIGHT = "\u001b[0;90m";
public static final String RED_BRIGHT = "\u001b[0;91m";
public static final String GREEN_BRIGHT = "\u001b[0;92m";
public static final String YELLOW_BRIGHT = "\u001b[0;93m";
public static final String BLUE_BRIGHT = "\u001b[0;94m";
public static final String PURPLE_BRIGHT = "\u001b[0;95m";
public static final String CYAN_BRIGHT = "\u001b[0;96m";
public static final String WHITE_BRIGHT = "\u001b[0;97m";
public static final String BLACK_BOLD_BRIGHT = "\u001b[1;90m";
public static final String RED_BOLD_BRIGHT = "\u001b[1;91m";
public static final String GREEN_BOLD_BRIGHT = "\u001b[1;92m";
public static final String YELLOW_BOLD_BRIGHT = "\u001b[1;93m";
public static final String BLUE_BOLD_BRIGHT = "\u001b[1;94m";
public static final String PURPLE_BOLD_BRIGHT = "\u001b[1;95m";
public static final String CYAN_BOLD_BRIGHT = "\u001b[1;96m";
public static final String WHITE_BOLD_BRIGHT = "\u001b[1;97m";
public static final String BLACK_BACKGROUND_BRIGHT = "\u001b[0;100m";
public static final String RED_BACKGROUND_BRIGHT = "\u001b[0;101m";
public static final String GREEN_BACKGROUND_BRIGHT = "\u001b[0;102m";
public static final String YELLOW_BACKGROUND_BRIGHT = "\u001b[0;103m";
public static final String BLUE_BACKGROUND_BRIGHT = "\u001b[0;104m";
public static final String PURPLE_BACKGROUND_BRIGHT = "\u001b[0;105m";
public static final String CYAN_BACKGROUND_BRIGHT = "\u001b[0;106m";
public static final String WHITE_BACKGROUND_BRIGHT = "\u001b[0;107m";
public static String stripColors(final String msg) {
String stripped = msg;
stripped = stripped.replace("\u001b[0m", "").replace("\u001b[0;30m", "").replace("\u001b[0;31m", "").replace("\u001b[0;32m", "").replace("\u001b[0;33m", "").replace("\u001b[0;34m", "").replace("\u001b[0;35m", "").replace("\u001b[0;36m", "").replace("\u001b[0;37m", "").replace("\u001b[40m", "").replace("\u001b[41m", "").replace("\u001b[42m", "").replace("\u001b[43m", "").replace("\u001b[44m", "").replace("\u001b[45m", "").replace("\u001b[46m", "").replace("\u001b[47m", "").replace("\u001b[0;100m", "").replace("\u001b[0;101m", "").replace("\u001b[0;102m", "").replace("\u001b[0;103m", "").replace("\u001b[0;104m", "").replace("\u001b[0;105m", "").replace("\u001b[0;106m", "").replace("\u001b[0;107m", "").replace("\u001b[1;30m", "").replace("\u001b[1;31m", "").replace("\u001b[1;32m", "").replace("\u001b[1;33m", "").replace("\u001b[1;34m", "").replace("\u001b[1;35m", "").replace("\u001b[1;36m", "").replace("\u001b[1;37m", "").replace("\u001b[1;90m", "").replace("\u001b[1;91m", "").replace("\u001b[1;92m", "").replace("\u001b[1;93m", "").replace("\u001b[1;94m", "").replace("\u001b[1;95m", "").replace("\u001b[1;96m", "").replace("\u001b[1;97m", "").replace("\u001b[0;90m", "").replace("\u001b[0;91m", "").replace("\u001b[0;92m", "").replace("\u001b[0;93m", "").replace("\u001b[0;94m", "").replace("\u001b[0;95m", "").replace("\u001b[0;96m", "").replace("\u001b[0;97m", "").replace("\u001b[4;30m", "").replace("\u001b[4;31m", "").replace("\u001b[4;32m", "").replace("\u001b[4;33m", "").replace("\u001b[4;34m", "").replace("\u001b[4;35m", "").replace("\u001b[4;36m", "").replace("\u001b[4;37m", "");
return stripped;
}
}

View File

@ -0,0 +1,120 @@
package net.horizoncode.tickettest.logging;
import org.apache.commons.io.FileUtils;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ConcurrentLinkedDeque;
public class Logger {
private final SimpleDateFormat dateFormat;
private final File logfile;
private final ConcurrentLinkedDeque<String> logList;
private final boolean debug;
public Logger(final File dir, boolean debug) {
this.debug = debug;
SimpleDateFormat dateFormat_logfile = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss");
this.dateFormat = new SimpleDateFormat("HH:mm:ss");
this.logList = new ConcurrentLinkedDeque<>();
if (!dir.exists()) {
dir.mkdir();
}
final File latestdel = new File(dir, "latest.log");
if (latestdel.exists() && !latestdel.isDirectory()) {
final String date = dateFormat_logfile.format(new Date(latestdel.lastModified()));
final File newdatefile = new File(dir, date + ".log");
try {
FileUtils.copyFile(latestdel, newdatefile);
} catch (IOException e) {
e.printStackTrace();
}
}
this.logfile = new File(dir, "latest.log");
if (!this.logfile.exists()) {
try {
this.logfile.createNewFile();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
public void emptyLine() {
System.out.println(" ");
this.logToFile(" ");
}
public void logCommand(final String msg) {
this.logToFile("> " + msg);
}
public void info(final String msg) {
final String date = this.dateFormat.format(new Date());
final String[] split = msg.split("\n");
for (final String msgeh : split) {
final String colout = "\u001b[0m[" + date + " " + "\u001b[0;34m" + "INFO" + "\u001b[0m" + "] " + msgeh;
final String out = "[" + date + " INFO] " + ConsoleColors.stripColors(msgeh);
System.out.println(colout);
this.logToFile(out);
}
}
public void debug(final String msg) {
final String date = this.dateFormat.format(new Date());
final String[] split = msg.split("\n");
for (final String msgeh : split) {
final String colout = "\u001b[0m[" + date + " " + "\u001b[4;33m" + "DEBUG" + "\u001b[0m" + "] " + msgeh;
final String out = "[" + date + " DEBUG] " + ConsoleColors.stripColors(msgeh);
if (debug)
System.out.println(colout);
this.logToFile(out);
}
}
public void warning(final String msg) {
final String date = this.dateFormat.format(new Date());
final String[] split = msg.split("\n");
for (final String msgeh : split) {
final String colout = "\u001b[0m[" + date + " " + "\u001b[4;33m" + "WARN" + "\u001b[0m" + "] " + msgeh;
final String out = "[" + date + " WARN] " + ConsoleColors.stripColors(msgeh);
System.out.println(colout);
this.logToFile(out);
}
}
public void error(final String msg) {
final String date = this.dateFormat.format(new Date());
final String[] split = msg.split("\n");
for (final String msgeh : split) {
final String colout = "\u001b[0m[" + date + " " + "\u001b[1;31m" + "ERROR" + "\u001b[0m" + "] " + msgeh;
final String out = "[" + date + " ERROR] " + ConsoleColors.stripColors(msgeh);
System.out.println(colout);
this.logToFile(out);
}
}
private void logToFile(final String s) {
this.logList.add(s);
this.saveFile();
}
private void saveFile() {
try {
synchronized (this.logfile) {
final BufferedWriter bw = new BufferedWriter(new FileWriter(this.logfile));
for (final String str : this.logList) {
bw.write(str);
bw.newLine();
}
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -3,6 +3,7 @@ package net.horizoncode.tickettest.manager;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import net.horizoncode.tickettest.TicketTest;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
@ -29,7 +30,7 @@ public class Ticket extends CompletableFuture<Boolean> {
.submit(
() -> {
TimeUnit.MILLISECONDS.sleep(ticketManager.getTicketLifespan());
System.out.println("ticket with UUID: " + getTicketID() + " expired.");
TicketTest.getInstance().getLogger().info("ticket with UUID " + getTicketID() + " expired.");
ticketManager.getTicketSet().remove(this);
getOnExpire().run();
return null;

View File

@ -1,8 +0,0 @@
package net.horizoncode.tickettest.manager;
public class TicketCallback{
public Ticket call(Ticket ticket) {
System.out.println("Ticket " + ticket.getTicketID() + " expired.");
return ticket;
}
}

View File

@ -5,6 +5,7 @@ import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import lombok.Builder;
import lombok.Getter;
import net.horizoncode.tickettest.TicketTest;
import java.util.HashSet;
import java.util.Set;
@ -29,14 +30,14 @@ public class TicketManager {
service.submit(
() -> {
if (waitForFreeTicket())
System.out.println("max amount of tickets reached, waiting...");
TicketTest.getInstance().getLogger().warning("max amount of tickets reached, waiting...");
while (waitForFreeTicket()) {
TimeUnit.MILLISECONDS.sleep(1);
}
Ticket t =
Ticket.builder().timeTicketCreated(ticketLifespan).onExpire(onExpire).ticketManager(this).build();
System.out.println("Got ticket with UUID: " + t.getTicketID());
TicketTest.getInstance().getLogger().info("Got ticket with UUID: " + t.getTicketID());
return t;
});
try {