Skip to content

Commit

Permalink
Finish implementing Redis Support.
Browse files Browse the repository at this point in the history
Signed-off-by: creatorfromhell <[email protected]>
  • Loading branch information
creatorfromhell committed Sep 2, 2023
1 parent 6a14566 commit cd5943c
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 5 deletions.
9 changes: 9 additions & 0 deletions Core/resources/data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ Data:
#The redis DB index
Index: 1

#The amount of time before timeout.
Timeout: 2000

#Whether to use SSL.
SSL: false

#Settings related to the redis connection pool
Pool:

Expand All @@ -39,6 +45,9 @@ Data:
#The max idle amount.
MaxIdle: 10

#The min idle amount
MinIdle: 1


#Configurations relating to purging old data.
Purge:
Expand Down
4 changes: 2 additions & 2 deletions Core/src/net/tnemc/core/channel/handlers/BalanceHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public BalanceHandler() {
}

public static void send(final String account, String region, UUID currency, Identifier handler, BigDecimal amount) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
final ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(TNECore.instance().getServerID().toString());
out.writeUTF(account);
out.writeUTF(region);
out.writeUTF(currency.toString());
out.writeUTF(handler.asID());
out.writeUTF(amount.toPlainString());

TNECore.server().proxy().send("tne:balance", out.toByteArray());
TNECore.storage().sendMessage("tne:balance", out.toByteArray());
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion Core/src/net/tnemc/core/channel/handlers/SyncHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static void send(String account) {
out.writeUTF(TNECore.instance().getServerID().toString());
out.writeUTF(account);

TNECore.server().proxy().send("tne:sync", out.toByteArray());
TNECore.storage().sendMessage("tne:sync", out.toByteArray());
}

@Override
Expand Down
83 changes: 81 additions & 2 deletions Core/src/net/tnemc/core/io/redis/TNEJedisManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,20 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import net.tnemc.core.TNECore;
import net.tnemc.core.compatibility.log.DebugLevel;
import net.tnemc.core.config.DataConfig;
import net.tnemc.core.utils.Identifier;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

/**
* TNEJedisManager
*
Expand All @@ -29,8 +40,76 @@
public class TNEJedisManager {

protected final JedisPool pool;
protected final TNESubscriber subscriber = new TNESubscriber();
protected final byte[] channel = "tne:balance".getBytes(StandardCharsets.UTF_8);
final Thread redisThread;

private static TNEJedisManager instance;

public TNEJedisManager() {
final JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(DataConfig.yaml().getInt("Data.Sync.Redis.Pool.MaxSize", 10));
config.setMaxIdle(DataConfig.yaml().getInt("Data.Sync.Redis.Pool.MaxIdle", 10));
config.setMinIdle(DataConfig.yaml().getInt("Data.Sync.Redis.Pool.MinIdle", 1));
this.pool = new JedisPool(config, DataConfig.yaml().getString("Data.Sync.Redis.Host"), DataConfig.yaml().getInt("Data.Sync.Redis.Port"),
DataConfig.yaml().getInt("Data.Sync.Redis.Timeout"), DataConfig.yaml().getString("Data.Sync.Redis.User"),
DataConfig.yaml().getString("Data.Sync.Redis.Password"), DataConfig.yaml().getInt("Data.Sync.Redis.Index"),
DataConfig.yaml().getBoolean("Data.Sync.Redis.SSL"));

if(connectionTest()) {

redisThread = new Thread(() -> {
try(Jedis jedis = pool.getResource()) {
jedis.subscribe(subscriber, channel);
}
}, "TNE Redis Thread");

redisThread.start();

TNECore.log().error("Redis Subscriber Thread Started", DebugLevel.OFF);
} else {
TNECore.log().error("Redis Connection Test Failed!", DebugLevel.OFF);
redisThread = null;
}

instance = this;
}

public boolean connectionTest() {
try(Jedis jedis = pool.getResource()) {
jedis.ping();
return true;
} catch(Exception ignore) {
return false;
}
}

public void publish(final byte[] data) {
try(Jedis jedis = pool.getResource()) {
jedis.publish(channel, data);
}
}

public void publish(final String channel, final byte[] data) {
try(Jedis jedis = pool.getResource()) {
jedis.publish(channel.getBytes(StandardCharsets.UTF_8), data);
}
}

public static TNEJedisManager instance() {
return instance;
}

public static void send(final String account, final String region, final UUID currency, final Identifier handler, final BigDecimal amount) {

final ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(TNECore.instance().getServerID().toString());
out.writeUTF(account);
out.writeUTF(region);
out.writeUTF(currency.toString());
out.writeUTF(handler.asID());
out.writeUTF(amount.toPlainString());

public TNEJedisManager(JedisPool pool) {
this.pool = pool;
instance.publish(out.toByteArray());
}
}
7 changes: 7 additions & 0 deletions Core/src/net/tnemc/core/io/redis/TNESubscriber.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import net.tnemc.core.TNECore;
import redis.clients.jedis.BinaryJedisPubSub;

/**
Expand All @@ -26,4 +27,10 @@
* @since 0.1.2.0
*/
public class TNESubscriber extends BinaryJedisPubSub {

@Override
public void onMessage(byte[] channel, byte[] message) {
super.onMessage(channel, message);
TNECore.instance().getChannelMessageManager().handle("tne:balance", message);
}
}
27 changes: 27 additions & 0 deletions Core/src/net/tnemc/core/io/storage/StorageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.tnemc.core.compatibility.scheduler.ChoreExecution;
import net.tnemc.core.compatibility.scheduler.ChoreTime;
import net.tnemc.core.config.DataConfig;
import net.tnemc.core.io.redis.TNEJedisManager;
import net.tnemc.core.io.storage.connect.SQLConnector;
import net.tnemc.core.io.storage.connect.YAMLConnector;
import net.tnemc.core.io.storage.dialect.MariaDialect;
Expand Down Expand Up @@ -54,11 +55,25 @@ public class StorageManager {
private static StorageManager instance;
private StorageEngine engine;
private final StorageConnector<?> connector;
private final TNEJedisManager jedisManager;

final String sync;

public StorageManager() {
instance = this;

if(DataConfig.yaml().contains("Data.Sync")) {

sync = DataConfig.yaml().getString("Data.Sync.Type", "Bungee");
switch(sync.toLowerCase()) {
case "jedis" -> this.jedisManager = new TNEJedisManager();
default -> this.jedisManager = null;
}
} else {
sync = "Bungee";
this.jedisManager = null;
}

final String engine = DataConfig.yaml().getString("Data.Database.Type");

switch(engine.toLowerCase()) {
Expand Down Expand Up @@ -105,6 +120,18 @@ public StorageManager() {
initialize();
}

public void sendMessage(final String channel, final byte[] data) {
switch(sync.toLowerCase()) {
case "redis":
if(jedisManager != null) {
jedisManager.publish(channel, data);
}
break;
default:
TNECore.server().proxy().send(channel, data);
}
}

public boolean meetsRequirement() {
if(this.connector instanceof SQLConnector) {
return ((SQLConnector)this.connector).checkVersion();
Expand Down

0 comments on commit cd5943c

Please sign in to comment.