Skip to content

Commit 8f57a64

Browse files
committed
add new factory method for long dictionary
1 parent e993c63 commit 8f57a64

3 files changed

Lines changed: 45 additions & 92 deletions

File tree

README.md

Lines changed: 28 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ repositories {
1616
}
1717
1818
dependencies {
19-
compile 'com.spaceshift:rlib.common:9.2.0'
20-
compile 'com.spaceshift:rlib.fx:9.2.0'
21-
compile 'com.spaceshift:rlib.network:9.2.0'
22-
compile 'com.spaceshift:rlib.mail:9.2.0'
23-
compile 'com.spaceshift:rlib.testcontainers:9.2.0'
19+
compile 'com.spaceshift:rlib.common:9.2.1'
20+
compile 'com.spaceshift:rlib.fx:9.2.1'
21+
compile 'com.spaceshift:rlib.network:9.2.1'
22+
compile 'com.spaceshift:rlib.mail:9.2.1'
23+
compile 'com.spaceshift:rlib.testcontainers:9.2.1'
2424
}
2525
```
2626

@@ -41,27 +41,27 @@ dependencies {
4141
<dependency>
4242
<groupId>com.spaceshift</groupId>
4343
<artifactId>rlib.common</artifactId>
44-
<version>9.2.0</version>
44+
<version>9.2.1</version>
4545
</dependency>
4646
<dependency>
4747
<groupId>com.spaceshift</groupId>
4848
<artifactId>rlib.fx</artifactId>
49-
<version>9.2.0</version>
49+
<version>9.2.1</version>
5050
</dependency>
5151
<dependency>
5252
<groupId>com.spaceshift</groupId>
5353
<artifactId>rlib.network</artifactId>
54-
<version>9.2.0</version>
54+
<version>9.2.1</version>
5555
</dependency>
5656
<dependency>
5757
<groupId>com.spaceshift</groupId>
5858
<artifactId>rlib.mail</artifactId>
59-
<version>9.2.0</version>
59+
<version>9.2.1</version>
6060
</dependency>
6161
<dependency>
6262
<groupId>com.spaceshift</groupId>
6363
<artifactId>rlib.testcontainers</artifactId>
64-
<version>9.2.0</version>
64+
<version>9.2.1</version>
6565
</dependency>
6666

6767
```
@@ -290,82 +290,24 @@ dependencies {
290290
.thenAccept(aVoid -> System.out.println("done!"));
291291
```
292292
### Network API
293-
293+
#### Simple String Echo Server/Client
294294
```java
295295

296-
public static class ServerPackets {
297-
298-
@PacketDescription(id = 1)
299-
public static class MessageRequest extends AbstractReadablePacket {
300-
301-
@Override
302-
protected void readImpl(@NotNull ConnectionOwner owner, @NotNull ByteBuffer buffer) {
303-
var message = readString(buffer);
304-
System.out.println("Server: received \"" + message + "\"");
305-
owner.sendPacket(new MessageResponse("Response of " + message));
306-
}
307-
}
308-
309-
@PacketDescription(id = 2)
310-
public static class MessageResponse extends AbstractWritablePacket {
311-
312-
@NotNull
313-
private final String message;
314-
315-
public MessageResponse(@NotNull String message) {
316-
this.message = message;
317-
}
318-
319-
@Override
320-
protected void writeImpl(@NotNull ByteBuffer buffer) {
321-
super.writeImpl(buffer);
322-
writeString(buffer, message);
323-
}
324-
}
325-
}
326-
327-
public static class ClientPackets {
328-
329-
@PacketDescription(id = 1)
330-
public static class MessageRequest extends AbstractWritablePacket {
331-
332-
@NotNull
333-
private final String message;
334-
335-
public MessageRequest(@NotNull String message) {
336-
this.message = message;
337-
}
338-
339-
@Override
340-
protected void writeImpl(@NotNull ByteBuffer buffer) {
341-
super.writeImpl(buffer);
342-
writeString(buffer, message);
343-
}
344-
}
345-
346-
@PacketDescription(id = 2)
347-
public static class MessageResponse extends AbstractReadablePacket {
348-
349-
@Override
350-
protected void readImpl(@NotNull ConnectionOwner owner, @NotNull ByteBuffer buffer) {
351-
var message = readString(buffer);
352-
System.out.println("client: received \"" + message + "\"");
353-
}
354-
}
355-
}
356-
357-
var address = new InetSocketAddress(2222);
358-
359-
serverNetwork = NetworkFactory.newDefaultAsyncServerNetwork(
360-
ReadablePacketRegistry.of(ServerPackets.MessageRequest.class));
361-
362-
serverNetwork.bind(address);
363-
364-
clientNetwork = NetworkFactory.newDefaultAsyncClientNetwork(
365-
ReadablePacketRegistry.of(ClientPackets.MessageResponse.class));
366-
367-
clientNetwork.connect(address);
368-
369-
var server = clientNetwork.getCurrentServer();
370-
server.sendPacket(new ClientPackets.MessageRequest("Test client message"));
296+
var serverNetwork = NetworkFactory.newStringDataServerNetwork();
297+
var serverAddress = serverNetwork.start();
298+
299+
serverNetwork.accepted()
300+
.flatMap(Connection::receivedEvents)
301+
.subscribe(event -> {
302+
var message = event.packet.getData();
303+
System.out.println("Received from client: " + message);
304+
event.connection.send(new StringWritablePacket("Echo: " + message));
305+
});
306+
307+
var clientNetwork = newStringDataClientNetwork();
308+
clientNetwork.connected(serverAddress)
309+
.doOnNext(connection -> IntStream.range(10, 100)
310+
.forEach(length -> connection.send(new StringWritablePacket(StringUtils.generate(length)))))
311+
.flatMapMany(Connection::receivedEvents)
312+
.subscribe(event -> System.out.println("Received from server: " + event.packet.getData()));
371313
```

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.2.0'
11+
rootProject.version = '9.2.1'
1212
group = 'com.spaceshift'
1313

1414
allprojects {

rlib-common/src/main/java/com/ss/rlib/common/util/dictionary/DictionaryFactory.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,31 @@ public final class DictionaryFactory {
112112
}
113113

114114
/**
115-
* New long dictionary long dictionary.
115+
* Create a new long dictionary.
116116
*
117-
* @param <V> the type parameter
117+
* @param <V> the value's type.
118118
* @return the new {@link FastLongDictionary}.
119119
*/
120120
public static <V> @NotNull LongDictionary<V> newLongDictionary() {
121121
return new FastLongDictionary<>();
122122
}
123123

124124
/**
125-
* New long dictionary long dictionary.
125+
* Create a new long dictionary.
126126
*
127-
* @param loadFactor the load factor
128-
* @param initCapacity the init capacity
127+
* @param initCapacity the init capacity.
128+
* @param <V> the value's type.
129+
* @return the new {@link FastLongDictionary}.
130+
*/
131+
public static <V> @NotNull LongDictionary<V> newLongDictionary(int initCapacity) {
132+
return new FastLongDictionary<>(initCapacity);
133+
}
134+
135+
/**
136+
* Create a new long dictionary.
137+
*
138+
* @param loadFactor the load factor.
139+
* @param initCapacity the init capacity.
129140
* @param <V> the value's type.
130141
* @return the new {@link FastLongDictionary}.
131142
*/

0 commit comments

Comments
 (0)