Skip to content

Commit

Permalink
Merge branch 'release/v0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
Xharos committed Jun 15, 2024
2 parents dfe7966 + 27cb709 commit f209fcc
Show file tree
Hide file tree
Showing 21 changed files with 1,311 additions and 27 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "fr.islandswars"
version = "0.2.8"
version = "0.3"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ public ByteBuffer getByteBuffer() {

@Override
public byte[] getBuffer() {
return getByteBuffer().array();
byte[] usedBuffer = new byte[buffer.position()];
buffer.flip(); // Prepare the buffer for reading
buffer.get(usedBuffer);
return usedBuffer;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public interface ServiceConnection<T> {

void close() throws Exception;

void connect();
void connect() throws Exception;

T getConnection();

boolean isClosed();

void load() throws NullPointerException;
void load();
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,22 @@ public class MongoDBConnection implements ServiceConnection<MongoDatabase> {
private CodecRegistry codecRegistry;

public MongoDBConnection() {
this.status = new AtomicBoolean(false);
this.status = new AtomicBoolean(true);
}

@Override
public void close() {
public void close() throws Exception {
client.close();
status.set(false);
status.set(true);
}

@Override
public void connect() {
public void connect() throws Exception {
Preconditions.checkNotNull(settings);

this.client = MongoClients.create(settings);
this.base = client.getDatabase(DATABASE);
status.set(true);
status.set(false);
}

@Override
Expand All @@ -81,7 +81,7 @@ public boolean isClosed() {
}

@Override
public void load() throws NullPointerException {
public void load() {
var pass = DockerSecretsLoader.getValue(ServiceType.MONGO_PASSWORD);
var user = DockerSecretsLoader.getValue(ServiceType.MONGO_USERNAME);
var host = DockerSecretsLoader.getValue(ServiceType.MONGO_HOSTNAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import fr.islandswars.commons.utils.Preconditions;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
* File <b>RabbitMQConnection</b> located on fr.islandswars.commons.service.rabbitmq
Expand Down Expand Up @@ -47,14 +46,10 @@ public void close() throws Exception {
}

@Override
public void connect() {
public void connect() throws Exception {
Preconditions.checkNotNull(factory);

try {
this.connection = factory.newConnection();
} catch (IOException | TimeoutException e) {
LogUtils.error(e);
}
this.connection = factory.newConnection();
}

@Override
Expand All @@ -77,7 +72,7 @@ public boolean isClosed() {
}

@Override
public void load() throws NullPointerException {
public void load() {
var host = DockerSecretsLoader.getValue(ServiceType.RMQ_HOSTNAME);
var port = DockerSecretsLoader.getValue(ServiceType.RMQ_PORT);
var user = DockerSecretsLoader.getValue(ServiceType.RMQ_USERNAME);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package fr.islandswars.commons.service.rabbitmq.packet;

import com.rabbitmq.client.BuiltinExchangeType;
import fr.islandswars.commons.service.rabbitmq.RabbitMQConnection;
import fr.islandswars.commons.utils.LogUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

/**
* File <b>IslandsMQ</b> located on fr.islandswars.commons.service.rabbitmq.packet
* IslandsMQ is a part of commons.
* <p>
* Copyright (c) 2017 - 2024 Islands Wars.
* <p>
* commons is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <a href="http://www.gnu.org/licenses/">GNU license</a>.
* <p>
*
* @author Jangliu, {@literal <[email protected]>}
* Created the 02/06/2024 at 16:47
* @since 0.3
*/
public class IslandsMQ {

private final String EXCHANGE_NAME = "islands";
private final String EXCHANGE_FIRST = "server";
private final String EXCHANGE_ALL = "all";
private final BuiltinExchangeType EXCHANGE_TYPE = BuiltinExchangeType.TOPIC;
private final RabbitMQConnection connection;

public IslandsMQ(RabbitMQConnection connection) {
this.connection = connection;
}

public void gameServerListen(String serverType, UUID id) {
var channel = connection.getConnection();
try {
var queue = EXCHANGE_FIRST + "." + serverType + "." + id;
channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE);
channel.queueDeclare(queue, false, true, false, null);
channel.queueBind(queue, EXCHANGE_NAME, queue); //listen to specific message
channel.queueBind(queue, EXCHANGE_NAME, EXCHANGE_FIRST + "." + serverType + "." + EXCHANGE_ALL); //listen to all same server's type message
channel.queueBind(queue, EXCHANGE_NAME, EXCHANGE_FIRST + "." + EXCHANGE_ALL); //listen to all servers message

channel.basicConsume(queue, true, (tag, delivery) -> {
String receivedMessage = new String(delivery.getBody(), StandardCharsets.UTF_8);
}, consumerTag -> {
});
} catch (IOException e) {
LogUtils.error(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package fr.islandswars.commons.service.rabbitmq.packet;

import fr.islandswars.commons.network.NetInput;
import fr.islandswars.commons.network.NetOutput;

/**
* File <b>Packet</b> located on fr.islandswars.commons.service.rabbitmq.packet
* Packet is a part of commons.
* <p>
* Copyright (c) 2017 - 2024 Islands Wars.
* <p>
* commons is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <a href="http://www.gnu.org/licenses/">GNU license</a>.
* <p>
*
* @author Jangliu, {@literal <[email protected]>}
* Created the 02/06/2024 at 16:55
* @since 0.3
*/
public abstract class Packet {

private final int id;

public Packet(int id) {
this.id = id;
}

public int getId() {
return id;
}

public abstract void decode(NetInput input) throws Exception;

public abstract void encode(NetOutput output) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package fr.islandswars.commons.service.rabbitmq.packet;

/**
* File <b>PacketEvent</b> located on fr.islandswars.commons.service.rabbitmq.packet
* PacketEvent is a part of commons.
* <p>
* Copyright (c) 2017 - 2024 Islands Wars.
* <p>
* commons is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <a href="http://www.gnu.org/licenses/">GNU license</a>.
* <p>
*
* @author Jangliu, {@literal <[email protected]>}
* Created the 02/06/2024 at 20:31
* @since 0.3
*/
public interface PacketEvent<T extends Packet> {

void receivePacket(T packet);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package fr.islandswars.commons.service.rabbitmq.packet;

import fr.islandswars.commons.network.nio.ByteBufferPool;
import fr.islandswars.commons.network.nio.InputByteBuffer;
import fr.islandswars.commons.utils.LogUtils;
import fr.islandswars.commons.utils.ReflectionUtil;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
* File <b>PacketManager</b> located on fr.islandswars.commons.service.rabbitmq.packet
* PacketManager is a part of commons.
* <p>
* Copyright (c) 2017 - 2024 Islands Wars.
* <p>
* commons is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <a href="http://www.gnu.org/licenses/">GNU license</a>.
* <p>
*
* @author Jangliu, {@literal <[email protected]>}
* Created the 02/06/2024 at 17:01
* @since 0.3
*/
public class PacketManager {

private final Map<Integer, List<PacketEvent<? extends Packet>>> handlers;
private final ByteBufferPool pool;
private final PacketType.Bound bound;

public PacketManager(PacketType.Bound bound, int size, boolean direct) {
this.pool = new ByteBufferPool(size, direct);
this.handlers = new ConcurrentHashMap<>();
this.bound = bound;
}

public <T extends Packet> void addListener(PacketType<T> type, PacketEvent<T> event) {
var id = type.getId();
if (type.getBound().equals(bound)) {
LogUtils.error(new IllegalArgumentException("Cannot listen to packet with the same Bound " + bound));
return;
}
handlers.compute(id, (k, v) -> {
if (v == null)
v = new CopyOnWriteArrayList<>();
v.add(event);
return v;
});
}

public byte[] encode(Packet packet) throws Exception {
var output = pool.allocateNetOutput();
output.writeInt(packet.getId());
packet.encode(output);
var result = output.getBuffer();
pool.free(output);
return result;
}

public <T extends Packet> void decode(byte[] delivery) throws Exception {
var input = pool.allocateNetInput();
((InputByteBuffer) input).getByteBuffer().put(delivery);
((InputByteBuffer) input).getByteBuffer().flip();
var packetId = input.readInt();
PacketType<T> packetType = PacketType.getPacketType(packetId);
if (packetType != null) {
T packet = ReflectionUtil.getConstructorAccessor(packetType.getPacketClass()).newInstance();
packet.decode(input);
pool.free(input);
if (handlers.containsKey(packetId)) {
handlers.get(packetId).forEach(event -> {
@SuppressWarnings("unchecked")
PacketEvent<T> typedEvent = (PacketEvent<T>) event;
typedEvent.receivePacket(packet);
});
}
}
}
}
Loading

0 comments on commit f209fcc

Please sign in to comment.