Skip to content

Commit 64af36b

Browse files
committed
update network config API and reflection utils
1 parent 04ed677 commit 64af36b

9 files changed

Lines changed: 134 additions & 55 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ buildscript {
88
}
99
}
1010

11-
rootProject.version = '9.8.0'
11+
rootProject.version = '9.9.0'
1212
group = 'com.spaceshift'
1313

1414
allprojects {

rlib-common/src/main/java/com/ss/rlib/common/util/ReflectionUtils.java

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.ss.rlib.common.util;
22

3+
import static com.ss.rlib.common.util.ArrayUtils.contains;
34
import com.ss.rlib.common.util.array.Array;
4-
import com.ss.rlib.common.util.array.ArrayFactory;
55
import org.jetbrains.annotations.NotNull;
66
import org.jetbrains.annotations.Nullable;
77

@@ -20,41 +20,53 @@ public final class ReflectionUtils {
2020
* Get all fields of the class.
2121
*
2222
* @param container the field container.
23-
* @param cs the class.
24-
* @param last the last class.
25-
* @param declared the flag of getting private fields.
23+
* @param startClass the class.
24+
* @param lastClass the last class.
25+
* @param declared the flag to get private fields as well.
2626
* @param exceptions exception fields.
2727
*/
2828
public static void addAllFields(
29-
@NotNull Array<Field> container,
30-
@NotNull Class<?> cs,
31-
@NotNull Class<?> last,
32-
boolean declared,
33-
@Nullable String... exceptions
29+
@NotNull Array<Field> container,
30+
@NotNull Class<?> startClass,
31+
@NotNull Class<?> lastClass,
32+
boolean declared,
33+
@NotNull String... exceptions
3434
) {
3535

36-
Class<?> next = cs;
36+
var next = startClass;
3737

38-
while (next != null && next != last) {
38+
while (next != null && next != lastClass) {
3939

40-
Field[] fields = declared ? next.getDeclaredFields() : next.getFields();
40+
var fields = declared ? next.getDeclaredFields() : next.getFields();
4141

4242
next = next.getSuperclass();
4343

4444
if (fields.length < 1) {
4545
continue;
4646
}
4747

48-
if (exceptions == null || exceptions.length < 1) {
48+
if (exceptions.length < 1) {
4949
container.addAll(fields);
5050
} else {
51-
ArrayUtils.forEach(fields, toCheck ->
52-
!ArrayUtils.contains(exceptions, toCheck.getName()),
53-
container::add);
51+
ArrayUtils.forEach(fields, toCheck -> !contains(exceptions, toCheck.getName()), container::add);
5452
}
5553
}
5654
}
5755

56+
/**
57+
* Get all fields of a class.
58+
*
59+
* @param cs the class.
60+
* @param exceptions exception fields.
61+
* @return the all declared fields.
62+
* @since 9.9.0
63+
*/
64+
public static @NotNull Array<Field> getAllDeclaredFields(@NotNull Class<?> cs, @NotNull String... exceptions) {
65+
var container = Array.ofType(Field.class);
66+
addAllFields(container, cs, Object.class, true, exceptions);
67+
return container;
68+
}
69+
5870
/**
5971
* Get all fields of the class.
6072
*
@@ -65,12 +77,12 @@ public static void addAllFields(
6577
* @return the all fields
6678
*/
6779
public static @NotNull Array<Field> getAllFields(
68-
@NotNull Class<?> cs,
69-
@NotNull Class<?> last,
70-
boolean declared,
71-
@Nullable String... exceptions
80+
@NotNull Class<?> cs,
81+
@NotNull Class<?> last,
82+
boolean declared,
83+
@NotNull String... exceptions
7284
) {
73-
Array<Field> container = ArrayFactory.newArray(Field.class);
85+
var container = Array.ofType(Field.class);
7486
addAllFields(container, cs, last, declared, exceptions);
7587
return container;
7688
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.ss.rlib.common.test.util;
2+
3+
import static org.junit.jupiter.api.Assertions.assertNotNull;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
import com.ss.rlib.common.util.ReflectionUtils;
6+
import org.junit.jupiter.api.Test;
7+
8+
public class ReflectionUtilsTest {
9+
10+
private static class Type1 {
11+
12+
private String field1;
13+
private int field2;
14+
15+
private static class Inner1 {
16+
private String field1;
17+
private int field2;
18+
}
19+
20+
private static class Inner2 extends Inner1 {
21+
private int field3;
22+
private Object field4;
23+
}
24+
}
25+
26+
private static class Type2 extends Type1 {
27+
private int field3;
28+
private Object field4;
29+
}
30+
31+
@Test
32+
void getAllDeclaredFieldsTest() {
33+
34+
var allFields = ReflectionUtils.getAllDeclaredFields(Type1.class);
35+
36+
assertNotNull(allFields.findAny(object -> object.getName().equals("field1")));
37+
assertNotNull(allFields.findAny(object -> object.getName().equals("field2")));
38+
assertNull(allFields.findAny(object -> object.getName().equals("field3")));
39+
40+
allFields = ReflectionUtils.getAllDeclaredFields(Type2.class);
41+
42+
assertNotNull(allFields.findAny(object -> object.getName().equals("field1")));
43+
assertNotNull(allFields.findAny(object -> object.getName().equals("field2")));
44+
assertNotNull(allFields.findAny(object -> object.getName().equals("field3")));
45+
assertNotNull(allFields.findAny(object -> object.getName().equals("field4")));
46+
assertNull(allFields.findAny(object -> object.getName().equals("field5")));
47+
48+
allFields = ReflectionUtils.getAllDeclaredFields(Type1.Inner1.class);
49+
50+
assertNotNull(allFields.findAny(object -> object.getName().equals("field1")));
51+
assertNotNull(allFields.findAny(object -> object.getName().equals("field2")));
52+
assertNull(allFields.findAny(object -> object.getName().equals("field3")));
53+
54+
allFields = ReflectionUtils.getAllDeclaredFields(Type1.Inner2.class);
55+
56+
assertNotNull(allFields.findAny(object -> object.getName().equals("field1")));
57+
assertNotNull(allFields.findAny(object -> object.getName().equals("field2")));
58+
assertNotNull(allFields.findAny(object -> object.getName().equals("field3")));
59+
assertNotNull(allFields.findAny(object -> object.getName().equals("field4")));
60+
assertNull(allFields.findAny(object -> object.getName().equals("field5")));
61+
}
62+
}

rlib-network/src/main/java/com/ss/rlib/network/NetworkConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class SimpleNetworkConfig implements NetworkConfig {
3333
@NotNull NetworkConfig DEFAULT_CLIENT = new NetworkConfig() {
3434

3535
@Override
36-
public @NotNull String getGroupName() {
36+
public @NotNull String getThreadGroupName() {
3737
return "ClientNetworkThread";
3838
}
3939
};
@@ -43,7 +43,7 @@ class SimpleNetworkConfig implements NetworkConfig {
4343
*
4444
* @return the group name.
4545
*/
46-
default @NotNull String getGroupName() {
46+
default @NotNull String getThreadGroupName() {
4747
return "NetworkThread";
4848
}
4949

rlib-network/src/main/java/com/ss/rlib/network/ServerNetworkConfig.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public interface ServerNetworkConfig extends NetworkConfig {
1919
class SimpleServerNetworkConfig implements ServerNetworkConfig {
2020

2121
@Builder.Default
22-
private String groupName = "ServerNetworkThread";
22+
private String threadGroupName = "ServerNetworkThread";
2323
@Builder.Default
2424
private ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
2525
@Builder.Default
@@ -32,20 +32,20 @@ class SimpleServerNetworkConfig implements ServerNetworkConfig {
3232
@Builder.Default
3333
private int writeBufferSize = 2048;
3434
@Builder.Default
35-
private int groupSize = 1;
35+
private int threadGroupSize = 1;
3636
@Builder.Default
3737
private int threadPriority = Thread.NORM_PRIORITY;
3838
}
3939

4040
@NotNull ServerNetworkConfig DEFAULT_SERVER = new ServerNetworkConfig() {
4141

4242
@Override
43-
public int getGroupSize() {
44-
return 2;
43+
public int getThreadGroupMinSize() {
44+
return 1;
4545
}
4646

4747
@Override
48-
public @NotNull String getGroupName() {
48+
public @NotNull String getThreadGroupName() {
4949
return "ServerNetworkThread";
5050
}
5151
};
@@ -55,7 +55,7 @@ public int getGroupSize() {
5555
*
5656
* @return the minimal executor size.
5757
*/
58-
default int getGroupSize() {
58+
default int getThreadGroupMinSize() {
5959
return 1;
6060
}
6161

@@ -64,8 +64,8 @@ default int getGroupSize() {
6464
*
6565
* @return the maximum executor size.
6666
*/
67-
default int getGroupMaxSize() {
68-
return getGroupSize();
67+
default int getThreadGroupMaxSize() {
68+
return getThreadGroupMinSize();
6969
}
7070

7171
/**

rlib-network/src/main/java/com/ss/rlib/network/client/impl/DefaultClientNetwork.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import static com.ss.rlib.common.util.Utils.uncheckedGet;
55
import com.ss.rlib.common.concurrent.util.ThreadUtils;
66
import com.ss.rlib.common.util.AsyncUtils;
7+
import com.ss.rlib.common.util.Utils;
78
import com.ss.rlib.network.Connection;
89
import com.ss.rlib.network.Network;
910
import com.ss.rlib.network.NetworkConfig;
1011
import com.ss.rlib.network.client.ClientNetwork;
1112
import com.ss.rlib.network.impl.AbstractNetwork;
13+
import com.ss.rlib.network.util.NetworkUtils;
1214
import lombok.Getter;
1315
import org.jetbrains.annotations.NotNull;
1416
import org.jetbrains.annotations.Nullable;
@@ -72,6 +74,7 @@ public DefaultClientNetwork(
7274

7375
@Override
7476
public void completed(@Nullable Void result, @Nullable Void attachment) {
77+
LOGGER.info(channel, ch -> "Connected to server: " + NetworkUtils.getSocketAddress(ch));
7578
asyncResult.complete(channelToConnection.apply(DefaultClientNetwork.this, channel));
7679
}
7780

rlib-network/src/main/java/com/ss/rlib/network/impl/AbstractNetwork.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
public abstract class AbstractNetwork<C extends Connection<?, ?>> implements Network<C> {
1919

20-
private static final Logger LOGGER = LoggerManager.getLogger(AbstractNetwork.class);
20+
protected static final Logger LOGGER = LoggerManager.getLogger(AbstractNetwork.class);
2121

2222
protected final NetworkConfig config;
2323
protected final BiFunction<Network<C>, AsynchronousSocketChannel, C> channelToConnection;

rlib-network/src/main/java/com/ss/rlib/network/server/impl/DefaultServerNetwork.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
public final class DefaultServerNetwork<C extends Connection<?, ?>> extends AbstractNetwork<C> implements
3737
ServerNetwork<C> {
3838

39-
private static final Logger LOGGER = LoggerManager.getLogger(DefaultServerNetwork.class);
40-
4139
private interface ServerCompletionHandler<C extends Connection<?, ?>> extends
4240
CompletionHandler<AsynchronousSocketChannel, DefaultServerNetwork<C>> {}
4341

@@ -54,7 +52,7 @@ public void completed(@NotNull AsynchronousSocketChannel channel, @NotNull Defau
5452
@Override
5553
public void failed(@NotNull Throwable exc, @NotNull DefaultServerNetwork<C> network) {
5654
if (exc instanceof AsynchronousCloseException) {
57-
LOGGER.warning("Server network was closed.");
55+
LOGGER.warning("Server network was closed");
5856
} else {
5957
LOGGER.error("Got exception during accepting new connection:");
6058
LOGGER.error(exc);
@@ -78,28 +76,34 @@ public DefaultServerNetwork(
7876
super(config, channelToConnection);
7977

8078
var threadFactory = new GroupThreadFactory(
81-
config.getGroupName(),
79+
config.getThreadGroupName(),
8280
config.getThreadConstructor(),
8381
config.getThreadPriority(),
84-
true
82+
false
8583
);
8684

87-
var executor = config.getGroupSize() < config.getGroupMaxSize() ? new ThreadPoolExecutor(
88-
config.getGroupSize(),
89-
config.getGroupMaxSize(),
85+
var executor = config.getThreadGroupMinSize() < config.getThreadGroupMaxSize() ? new ThreadPoolExecutor(
86+
config.getThreadGroupMinSize(),
87+
config.getThreadGroupMaxSize(),
9088
120,
9189
TimeUnit.SECONDS,
9290
new SynchronousQueue<>(),
9391
threadFactory,
9492
new ThreadPoolExecutor.CallerRunsPolicy()
95-
) : Executors.newFixedThreadPool(config.getGroupSize(), threadFactory);
93+
) : Executors.newFixedThreadPool(config.getThreadGroupMinSize(), threadFactory);
9694

9795
// activate the executor
9896
executor.submit(() -> {});
9997

100-
LOGGER.info("Executor configuration:");
101-
LOGGER.info(config, conf -> "Min threads: " + conf.getGroupSize());
102-
LOGGER.info(config, conf -> "Max threads: " + conf.getGroupMaxSize());
98+
LOGGER.info(config, conf -> "Server network configuration: {\n" +
99+
" minThreads: " + conf.getThreadGroupMinSize() + ",\n" +
100+
" maxThreads: " + conf.getThreadGroupMaxSize() + ",\n" +
101+
" priority: " + conf.getThreadPriority() + ",\n" +
102+
" groupName: \"" + conf.getThreadGroupName() + "\",\n" +
103+
" readBufferSize: " + conf.getReadBufferSize() + ",\n" +
104+
" pendingBufferSize: " + conf.getPendingBufferSize() + ",\n" +
105+
" writeBufferSize: " + conf.getWriteBufferSize() + "\n" +
106+
"}");
103107

104108
this.group = uncheckedGet(executor, AsynchronousChannelGroup::withThreadPool);
105109
this.channel = uncheckedGet(group, AsynchronousServerSocketChannel::open);
@@ -121,7 +125,7 @@ public DefaultServerNetwork(
121125
}
122126
}
123127

124-
LOGGER.info(address, adr -> "Started server on address: " + adr);
128+
LOGGER.info(address, adr -> "Started server socket on address: " + adr);
125129

126130
if (!subscribers.isEmpty()) {
127131
acceptNext();
@@ -134,23 +138,21 @@ public DefaultServerNetwork(
134138
public <S extends ServerNetwork<C>> @NotNull S start(@NotNull InetSocketAddress serverAddress) {
135139
Utils.unchecked(channel, serverAddress, AsynchronousServerSocketChannel::bind);
136140

137-
LOGGER.info(serverAddress, addr -> "Started server on address: " + addr);
141+
LOGGER.info(serverAddress, addr -> "Started server socket on address: " + addr);
138142

139143
if (!subscribers.isEmpty()) {
140144
acceptNext();
141145
}
142146

143-
return ClassUtils.unsafeCast(this);
147+
return ClassUtils.unsafeNNCast(this);
144148
}
145149

146150
protected void acceptNext() {
147151
if (channel.isOpen()) {
148-
try {
149-
channel.accept(this, acceptHandler);
150-
} catch (AcceptPendingException ignored) {
151-
}
152+
try { channel.accept(this, acceptHandler); }
153+
catch (AcceptPendingException ignored) {}
152154
} else {
153-
LOGGER.warning("Cannot accept a next connection because server channel is already closed.");
155+
LOGGER.warning("Cannot accept a next connection because server channel is already closed");
154156
}
155157
}
156158

rlib-network/src/test/java/com/ss/rlib/network/test/StringNetworkTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ void shouldSendBiggerPacketThanWriteBuffer() {
212212
void testServerWithMultiplyClients() {
213213

214214
var serverConfig = SimpleServerNetworkConfig.builder()
215-
.groupSize(10)
215+
.threadGroupSize(10)
216216
.build();
217217

218218
var serverAllocator = new ReuseBufferAllocator(serverConfig);
@@ -266,7 +266,7 @@ void testServerWithMultiplyClients() {
266266
void testServerWithMultiplyClientsUsingOldApi() {
267267

268268
var serverNetwork = newStringDataServerNetwork(SimpleServerNetworkConfig.builder()
269-
.groupSize(10)
269+
.threadGroupSize(10)
270270
.build());
271271

272272
var serverAddress = serverNetwork.start();

0 commit comments

Comments
 (0)