Skip to content

Commit

Permalink
REDDIS DU HUND AAAAAAAAAAHHHHHHHHHHH
Browse files Browse the repository at this point in the history
  • Loading branch information
DaNussi committed Oct 28, 2023
1 parent 0f5e9b1 commit 5d5d93d
Show file tree
Hide file tree
Showing 4 changed files with 430 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ public static String Reset() {

public static JedisPool jedisPool;
public static Jedis getJedis() {
// if(jedisPool == null) {
// jedisPool = new JedisPool(new JedisPoolConfig(), CONFIG_VALUE_REDIS_URI.get());
// }
if(jedisPool == null) {
jedisPool = new JedisPool(new JedisPoolConfig(), CONFIG_VALUE_REDIS_URI.get());
}
return jedisPool.getResource();

return new Jedis(CONFIG_VALUE_REDIS_URI.get());
// Jedis jedis = new Jedis(CONFIG_VALUE_REDIS_URI.get());
// return jedis;
}


Expand Down Expand Up @@ -132,7 +134,7 @@ public Controllable() {
protected abstract void onStart();
protected abstract void onStop();

protected Jedis getJedis() {
public Jedis getJedis() {
Jedis jedis = DedicatedAppliedEnergisticsController.getJedis();
jedisInstances.add(jedis);
return jedis;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
import appeng.api.stacks.AEKey;
import appeng.api.stacks.KeyCounter;
import com.mojang.logging.LogUtils;
import net.minecraft.world.level.Level;
import net.nussi.dedicated_applied_energistics.DedicatedAppliedEnergisticsController;
import net.nussi.dedicated_applied_energistics.blockentities.InterDimensionalInterfaceBlockEntity;
import org.slf4j.Logger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;

import java.io.*;
import java.util.*;

public class InterDimensionalInterfaceCrafting extends DedicatedAppliedEnergisticsController.Controllable implements ICraftingProvider {
Expand All @@ -26,10 +24,11 @@ public class InterDimensionalInterfaceCrafting extends DedicatedAppliedEnergisti
private NetworkPatternList networkPatterns;
private Thread thread;
private boolean runningThread;
private Level level;

public InterDimensionalInterfaceCrafting(InterDimensionalInterfaceBlockEntity instance) {
this.instance = instance;

this.level = instance.getLevel();

DedicatedAppliedEnergisticsController.addControllable(this);
if(DedicatedAppliedEnergisticsController.IsRunning && !this.isRunning()) this.externalStart();
Expand All @@ -39,12 +38,7 @@ public InterDimensionalInterfaceCrafting(InterDimensionalInterfaceBlockEntity in
@Override
protected void onStart() {
LOGGER.info(uuid + " | Starting crafting provider!");
this.networkPatterns = new NetworkPatternList(
getJedis(),
getJedis(),
getJedis(),
uuid
);
this.networkPatterns = new NetworkPatternList(this, uuid, level);
this.networkPatterns.redisFetch();
this.networkPatterns.redisSubscriber();

Expand Down Expand Up @@ -87,7 +81,9 @@ public void onStartPatternScanner() {
LOGGER.debug(uuid + " | Checking for unregister patterns");
this.allPatterns.forEach(pattern -> {
if(!networkPatterns.isRegistered(pattern)) {
networkPatterns.add(new NetworkPattern(pattern, this.uuid));
NetworkPattern networkPattern = new NetworkPattern(pattern, this.uuid, true, this.level);
networkPattern.enableResponse = true;
networkPatterns.add(networkPattern);
LOGGER.info(uuid + " | Added new pattern to network!");
}
});
Expand Down Expand Up @@ -127,9 +123,7 @@ public void onStartPatternScanner() {
LOGGER.debug(uuid + " | NetworkPatterns[" + networkPatterns.size() + "]: " + Arrays.toString(networkPatterns.toArray()));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
} catch (InterruptedException e) {}
}
});

Expand Down Expand Up @@ -163,169 +157,6 @@ public Set<AEKey> getEmitableItems() {
return ICraftingProvider.super.getEmitableItems();
}

public static class NetworkPatternList extends ArrayList<NetworkPattern> {
private Jedis fetchJedis;
private Jedis subJedis;
private Jedis writeJedis;
private String uuid;
private Thread thread;
private JedisPubSub pubSub;
private NetworkPatternList instance;

public NetworkPatternList(Jedis fetchJedis, Jedis subJedis, Jedis writeJedis, String uuid) {
super();
this.instance = this;
this.fetchJedis = fetchJedis;
this.subJedis = subJedis;
this.writeJedis = writeJedis;
this.uuid = uuid;
}

public void close() {
if(!this.isEmpty()) {
this.clear();
}

if(pubSub != null) {
pubSub.unsubscribe(redisChannel());
pubSub.unsubscribe();
pubSub = null;
}

if(thread != null) {
try {
thread.interrupt();
thread.join();
thread = null;
} catch (Exception e) {
LOGGER.error(e.getMessage());
e.printStackTrace();
}
}
}

private void redisFetch() {
Set<String> keys = fetchJedis.keys(redisChannel()+"/*.pattern");
long maxCount = keys.size();
int counter = 1;

LOGGER.info("Fetching Patterns ...");
for(String key : keys) {
LOGGER.debug("Fetching Patterns [" + counter++ + "/" + maxCount + "]");

try {
String data = fetchJedis.get(key);
NetworkPattern networkPattern = NetworkPattern.fromByteArray(data.getBytes());
if(networkPattern.origin.equals(uuid)) continue;
this.add(networkPattern);
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
}
LOGGER.debug("Fetching Patterns finished");
}

private void redisSubscriber() {
thread = new Thread(() -> {
try {
pubSub = new JedisPubSub() {
@Override
public void onMessage(String channel, String message) {
try {
NetworkPattern networkPattern = NetworkPattern.fromByteArray(message.getBytes());
if(networkPattern.equals(uuid)) return;

if(networkPattern.exists) {
instance.add(networkPattern);
} else {
instance.remove(networkPattern);
}
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
}
};

subJedis.subscribe(pubSub, redisChannel());
} catch (Exception e) {}
});
thread.start();
}

public String redisChannel() {
return 0 + ".pattern";
}

@Override
public boolean add(NetworkPattern networkPattern) {
if(networkPattern.origin.equals(uuid)) {
networkPattern.exists = true;
writeJedis.publish(redisChannel(), new String(NetworkPattern.toByteArray(networkPattern)));
}

return super.add(networkPattern);
}

@Override
public boolean remove(Object o) {
if(o instanceof NetworkPattern networkPattern) {
if(networkPattern.origin.equals(uuid)) {
networkPattern.exists = false;
writeJedis.publish(redisChannel(), new String(NetworkPattern.toByteArray(networkPattern)));
}
}

return super.remove(o);
}

public boolean isRegistered(IPatternDetails pattern) {
for(NetworkPattern networkPattern : this) {
if(networkPattern.pattern == pattern) return true;
}
return false;
}
}

public static class NetworkPattern implements Serializable {
public IPatternDetails pattern;
public String origin;
public boolean exists;

public NetworkPattern(IPatternDetails pattern, String origin) {
this.pattern = pattern;
this.origin = origin;
this.exists = true;
}

/** Read the object from Base64 string. */
public static NetworkPattern fromByteArray(byte [] data ) {
try {
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream( data ) );
NetworkPattern o = (NetworkPattern) ois.readObject();
ois.close();
return o;
} catch (Exception e) {
LOGGER.error(e.getMessage());
e.printStackTrace();
return null;
}
}

/** Write the object to a Base64 string. */
public static byte[] toByteArray( NetworkPattern o ) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( baos );
oos.writeObject( o );
oos.close();
return baos.toByteArray();
} catch (Exception e) {
LOGGER.error(e.getMessage());
e.printStackTrace();
return new byte[0];
}
}
}

public static List<IPatternDetails> getPatterns(IGrid grid) {
List<IPatternDetails> currentPatterns = new ArrayList<>();
Expand Down
Loading

0 comments on commit 5d5d93d

Please sign in to comment.