Skip to content

Commit 8e5e1d1

Browse files
authored
Merge pull request #7 from LuminiaDev/docs
docs: written javadoc for all classes
2 parents 867b09d + d41141c commit 8e5e1d1

File tree

17 files changed

+265
-11
lines changed

17 files changed

+265
-11
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,24 @@ import com.luminiadev.bridge.network.codec.packet.BridgePacketDirection;
1616
BridgeRabbitMQNetwork network = new BridgeRabbitMQNetwork(BridgeRabbitMQConfig.builder()
1717
.host("localhost")
1818
.credentials(new BridgeRabbitMQCredentials("guest", "guest"))
19+
.serviceId("my-service-id") // You can remove this parameter and the id will be generated automatically
1920
.build());
2021

21-
// Creating a codec with an example packet
22-
BridgeCodec codec = BridgeCodec.builder()
23-
.registerPacket("example_packet", ExamplePacket::new, new ExamplePacketSerializer())
24-
.build();
22+
// Creating a codec with an example packet
23+
// If any packet is not found in the codec, it will be handled as BridgeUnknownPacket.
24+
BridgeCodec codec = BridgeCodec.builder()
25+
.registerPacket("example_packet", ExamplePacket::new, new ExamplePacketSerializer())
26+
.build();
2527

2628
network.setCodec(codec); // Set codec to network
2729
network.start(); // Start network
2830

2931
// Adding packet handler
3032
network.addPacketHandler((packet, direction, serviceId) -> {
31-
if (direction == BridgePacketDirection.TO_SERVICE) {
33+
if (direction == BridgePacketDirection.TO_SERVICE) {
3234
System.out.println("Received packet " + packet + " from service " + serviceId);
3335
} else {
34-
System.out.println("Sent packet: " + packet);
36+
System.out.println("Sent packet: " + packet);
3537
}
36-
});
38+
});
3739
```

bridge-common/src/main/java/com/luminiadev/bridge/exception/BridgeCodecException.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
11
package com.luminiadev.bridge.exception;
22

3+
/**
4+
* Custom exception for bridge codec errors.
5+
*/
36
public class BridgeCodecException extends BridgeException {
47

8+
/**
9+
* Creates a new BridgeCodecException with the specified message.
10+
*
11+
* @param message The detail message
12+
*/
513
public BridgeCodecException(String message) {
614
super(message);
715
}
816

17+
/**
18+
* Creates a new BridgeCodecException with the specified message and cause.
19+
*
20+
* @param message The detail message
21+
* @param cause The cause of the exception
22+
*/
923
public BridgeCodecException(String message, Throwable cause) {
1024
super(message, cause);
1125
}
1226

27+
/**
28+
* Creates a new BridgeCodecException with the specified cause.
29+
*
30+
* @param cause The cause of the exception
31+
*/
1332
public BridgeCodecException(Throwable cause) {
1433
super(cause);
1534
}

bridge-common/src/main/java/com/luminiadev/bridge/exception/BridgeException.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
11
package com.luminiadev.bridge.exception;
22

3+
/**
4+
* Custom exception for bridge errors.
5+
*/
36
public class BridgeException extends RuntimeException {
47

8+
/**
9+
* Creates a new BridgeException with the specified message.
10+
*
11+
* @param message The detail message
12+
*/
513
public BridgeException(String message) {
614
super(message);
715
}
816

17+
/**
18+
* Creates a new BridgeException with the specified message and cause.
19+
*
20+
* @param message The detail message
21+
* @param cause The cause of the exception
22+
*/
923
public BridgeException(String message, Throwable cause) {
1024
super(message, cause);
1125
}
1226

27+
/**
28+
* Creates a new BridgeException with the specified cause.
29+
*
30+
* @param cause The cause of the exception
31+
*/
1332
public BridgeException(Throwable cause) {
1433
super(cause);
1534
}

bridge-common/src/main/java/com/luminiadev/bridge/network/AbstractBridgeNetwork.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import java.util.HashSet;
1313
import java.util.Set;
1414

15+
/**
16+
* Bridge network interface abstract class with basic methods implementation.
17+
*/
1518
public abstract class AbstractBridgeNetwork implements BridgeNetwork {
1619

1720
private BridgeCodec codec;
Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,98 @@
11
package com.luminiadev.bridge.network;
22

3+
import com.luminiadev.bridge.exception.BridgeCodecException;
34
import com.luminiadev.bridge.network.codec.BridgeCodec;
45
import com.luminiadev.bridge.network.codec.packet.BridgePacket;
56
import com.luminiadev.bridge.network.codec.packet.handler.BridgePacketHandler;
67
import io.netty.buffer.ByteBuf;
8+
import org.jetbrains.annotations.NotNull;
79
import org.jetbrains.annotations.Nullable;
810
import org.jetbrains.annotations.UnmodifiableView;
911

1012
import java.util.Set;
1113

14+
/**
15+
* Bridge network interface.
16+
*/
1217
public interface BridgeNetwork {
1318

14-
String getServiceId();
19+
/**
20+
* Get service id.
21+
*
22+
* @return Service id
23+
*/
24+
@NotNull String getServiceId();
1525

26+
/**
27+
* Set up codec.
28+
*
29+
* @param codec The BridgeCodec
30+
*/
1631
void setCodec(BridgeCodec codec);
1732

33+
/**
34+
* Get set codec.
35+
*
36+
* @return Set codec or null, if not set
37+
*/
1838
@Nullable BridgeCodec getCodec();
1939

20-
@UnmodifiableView Set<BridgePacketHandler> getPacketHandlers();
40+
/**
41+
* Get an immutable set of packet handlers.
42+
*
43+
* @return Set of packet handlers
44+
*/
45+
@UnmodifiableView
46+
Set<BridgePacketHandler> getPacketHandlers();
2147

48+
/**
49+
* Add a packet handler.
50+
*
51+
* @param handler The BridgePacketHandler
52+
*/
2253
void addPacketHandler(BridgePacketHandler handler);
2354

55+
/**
56+
* Remove a packet handler.
57+
*
58+
* @param handler The BridgePacketHandler
59+
*/
2460
void removePacketHandler(BridgePacketHandler handler);
2561

62+
/**
63+
* Send a bridge packet.
64+
*
65+
* @param packet The BridgePacket
66+
* @param <T> The packet type
67+
*/
2668
<T extends BridgePacket> void sendPacket(T packet);
2769

70+
/**
71+
* Decodes the byte buffer in BridgePacket.
72+
*
73+
* @param buffer Netty ByteBuf
74+
* @param packetId Packet id
75+
* @return The decoded BridgePacket
76+
* @throws BridgeCodecException If the codec is not set
77+
*/
2878
BridgePacket tryDecode(ByteBuf buffer, String packetId);
2979

80+
/**
81+
* Encodes the BridgePacket to byte buffer.
82+
*
83+
* @param buffer Netty ByteBuf
84+
* @param packet Packet object
85+
* @throws BridgeCodecException If the codec is not set
86+
*/
3087
<T extends BridgePacket> void tryEncode(ByteBuf buffer, T packet);
3188

89+
/**
90+
* Starts the Bridge network interface.
91+
*/
3292
void start();
3393

94+
/**
95+
* Closes the Bridge network interface.
96+
*/
3497
void close();
3598
}

bridge-common/src/main/java/com/luminiadev/bridge/network/codec/BridgeCodec.java

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,66 @@
1313
import java.util.HashMap;
1414
import java.util.Map;
1515

16+
/**
17+
* Bridge packet codec for registering and encoding packets.
18+
*/
1619
public final class BridgeCodec {
1720

1821
private final Map<String, BridgePacketDefinition<? extends BridgePacket>> packetsById = new HashMap<>();
1922
private final Map<Class<?>, BridgePacketDefinition<? extends BridgePacket>> packetsByClass = new HashMap<>();
2023

24+
/**
25+
* Creates an empty codec builder.
26+
*
27+
* @return Builder
28+
*/
2129
public static Builder builder() {
2230
return new Builder();
2331
}
2432

33+
/**
34+
* Gets the bridge packet definition by the id.
35+
*
36+
* @param id Packet id
37+
* @return BridgePacketDefinition or null
38+
*/
2539
public @Nullable BridgePacketDefinition<? extends BridgePacket> getPacketDefinition(String id) {
2640
return packetsById.get(id);
2741
}
2842

43+
/**
44+
* Gets the bridge packet definition by the id.
45+
* @param classOf Packet class
46+
* @param <T> Packet type
47+
* @return BridgePacketDefinition or null
48+
*/
2949
@SuppressWarnings("unchecked")
3050
public @Nullable <T extends BridgePacket> BridgePacketDefinition<T> getPacketDefinition(Class<T> classOf) {
3151
return (BridgePacketDefinition<T>) packetsByClass.get(classOf);
3252
}
3353

54+
/**
55+
* Register the packet definition.
56+
*
57+
* @param definition BridgePacketDefinition
58+
* @param <T> Packet type
59+
* @throws BridgeCodecException If the packet is already registered
60+
*/
3461
public <T extends BridgePacket> void registerPacket(BridgePacketDefinition<T> definition) {
3562
if (!packetsById.containsKey(definition.getId())) {
3663
packetsById.put(definition.getId(), definition);
37-
packetsByClass.put(definition.getClass(), definition);
64+
packetsByClass.put(definition.getFactory().getClass(), definition);
3865
} else {
3966
throw new BridgeCodecException("Packet with id " + definition.getId() + " is already registered");
4067
}
4168
}
4269

70+
/**
71+
* Deregister packet definition by packet id.
72+
*
73+
* @param packetId Packet id
74+
* @throws BridgeCodecException If the packet is not registered
75+
*/
4376
public void unregisterPacket(String packetId) {
4477
BridgePacketDefinition<? extends BridgePacket> definition = packetsById.remove(packetId);
4578
if (definition != null) {
@@ -49,6 +82,14 @@ public void unregisterPacket(String packetId) {
4982
}
5083
}
5184

85+
/**
86+
* Decodes the byte buffer in BridgePacket.
87+
*
88+
* @param buffer Netty ByteBuf
89+
* @param packetId Packet id
90+
* @return The decoded BridgePacket
91+
* @throws BridgeCodecException If the codec is not set
92+
*/
5293
@SuppressWarnings({"unchecked", "rawtypes"})
5394
public BridgePacket tryDecode(ByteBuf buffer, String packetId) {
5495
BridgePacketDefinition<? extends BridgePacket> definition = this.getPacketDefinition(packetId);
@@ -71,6 +112,13 @@ public BridgePacket tryDecode(ByteBuf buffer, String packetId) {
71112
return packet;
72113
}
73114

115+
/**
116+
* Encodes the BridgePacket to byte buffer.
117+
*
118+
* @param buffer Netty ByteBuf
119+
* @param packet Packet object
120+
* @throws BridgeCodecException If the codec is not set
121+
*/
74122
@SuppressWarnings("unchecked")
75123
public <T extends BridgePacket> void tryEncode(ByteBuf buffer, T packet) {
76124
BridgePacketSerializerHelper helper = new BridgePacketSerializerHelper(buffer);
@@ -89,6 +137,11 @@ public <T extends BridgePacket> void tryEncode(ByteBuf buffer, T packet) {
89137
serializer.serialize(buffer, helper, packet);
90138
}
91139

140+
/**
141+
* Creates Builder from codec with all codec parameters.
142+
*
143+
* @return Builder
144+
*/
92145
public Builder toBuilder() {
93146
Builder builder = new Builder();
94147
builder.definitions.putAll(packetsById);

bridge-common/src/main/java/com/luminiadev/bridge/network/codec/packet/BridgePacket.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22

33
import org.jetbrains.annotations.NotNull;
44

5+
/**
6+
* A bridge packet.
7+
*/
58
public interface BridgePacket {
69

10+
/**
11+
* Gets packet id.
12+
*
13+
* @return Packet id
14+
*/
715
@NotNull String getId();
816
}

bridge-common/src/main/java/com/luminiadev/bridge/network/codec/packet/BridgePacketDefinition.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import com.luminiadev.bridge.network.codec.packet.serializer.BridgePacketSerializer;
44
import lombok.Value;
55

6+
/**
7+
* Bridge packet definition, contains id, factory and serializer of the packet.
8+
*
9+
* @param <T> Packet type
10+
*/
611
@Value
712
public class BridgePacketDefinition<T extends BridgePacket> {
813
String id;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
package com.luminiadev.bridge.network.codec.packet;
22

3+
/**
4+
* Bridge packet direction.
5+
*/
36
public enum BridgePacketDirection {
7+
/**
8+
* The packet sent by the current service to other services.
9+
*/
410
FROM_SERVICE,
11+
/**
12+
* The packet was sent from another service to the current service.
13+
*/
514
TO_SERVICE
615
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
package com.luminiadev.bridge.network.codec.packet;
22

3+
/**
4+
* Bridge packet factory.
5+
*
6+
* @param <T> Packet type
7+
*/
38
@FunctionalInterface
49
public interface BridgePacketFactory<T extends BridgePacket> {
10+
11+
/**
12+
* Creates a new bridge packet instance.
13+
*
14+
* @return A new bridge packet instance
15+
*/
516
T create();
617
}

0 commit comments

Comments
 (0)