-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start streams tcp implementation, extract tcp connection handler
- Loading branch information
1 parent
9ae782b
commit bbec651
Showing
10 changed files
with
174 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/java/rs/iggy/clients/blocking/tcp/StreamsTcpClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package rs.iggy.clients.blocking.tcp; | ||
|
||
import io.netty.buffer.Unpooled; | ||
import rs.iggy.clients.blocking.StreamsClient; | ||
import rs.iggy.identifier.StreamId; | ||
import rs.iggy.stream.StreamBase; | ||
import rs.iggy.stream.StreamDetails; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
class StreamsTcpClient implements StreamsClient { | ||
|
||
private static final int CREATE_STREAM_CODE = 202; | ||
private final TcpConnectionHandler connection; | ||
|
||
StreamsTcpClient(TcpConnectionHandler connection) { | ||
this.connection = connection; | ||
} | ||
|
||
@Override | ||
public void createStream(Optional<Long> streamId, String name) { | ||
var payloadSize = 4 + 1 + name.length(); | ||
var payload = Unpooled.buffer(payloadSize); | ||
|
||
payload.writeIntLE(streamId.orElse(0L).intValue()); | ||
payload.writeByte(name.length()); | ||
payload.writeBytes(name.getBytes()); | ||
connection.send(CREATE_STREAM_CODE, payload); | ||
} | ||
|
||
@Override | ||
public StreamDetails getStream(Long streamId) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public StreamDetails getStream(StreamId streamId) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public List<StreamBase> getStreams() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void updateStream(Long streamId, String name) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void updateStream(StreamId streamId, String name) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void deleteStream(Long streamId) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void deleteStream(StreamId streamId) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/rs/iggy/clients/blocking/tcp/TcpConnectionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package rs.iggy.clients.blocking.tcp; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import reactor.core.publisher.Mono; | ||
import reactor.netty.Connection; | ||
import reactor.netty.tcp.TcpClient; | ||
|
||
final class TcpConnectionHandler { | ||
|
||
private static final int REQUEST_INITIAL_BYTES_LENGTH = 4; | ||
private static final int COMMAND_LENGTH = 4; | ||
private static final int RESPONSE_INITIAL_BYTES_LENGTH = 8; | ||
|
||
private final Connection connection; | ||
|
||
TcpConnectionHandler(String host, Integer port) { | ||
this.connection = TcpClient.create().host(host).port(port).connectNow(); | ||
} | ||
|
||
void send(int command, ByteBuf payload) { | ||
var payloadSize = payload.readableBytes() + COMMAND_LENGTH; | ||
var buffer = Unpooled.buffer(REQUEST_INITIAL_BYTES_LENGTH + payloadSize); | ||
buffer.writeIntLE(payloadSize); | ||
buffer.writeIntLE(command); | ||
buffer.writeBytes(payload); | ||
|
||
connection.outbound().send(Mono.just(buffer)).then().block(); | ||
} | ||
|
||
ByteBuf sendWithResponse(int command, ByteBuf payload) { | ||
send(command, payload); | ||
var response = connection.inbound().receive().asByteArray().blockFirst(); | ||
if (response == null) { | ||
throw new RuntimeException("No response"); | ||
} | ||
|
||
var responseBuffer = Unpooled.wrappedBuffer(response); | ||
if (!responseBuffer.isReadable(RESPONSE_INITIAL_BYTES_LENGTH)) { | ||
throw new RuntimeException("Received an invalid or empty response"); | ||
} | ||
|
||
var status = responseBuffer.readUnsignedIntLE(); | ||
var responseLengthL = responseBuffer.readUnsignedIntLE(); | ||
// unsafe cast | ||
var responseLength = (int) responseLengthL; | ||
|
||
return handleResponse(status, responseLength, responseBuffer); | ||
} | ||
|
||
ByteBuf handleResponse(long status, int responseLength, ByteBuf responseBuffer) { | ||
if (status != 0) { | ||
throw new RuntimeException("Received an invalid response with status " + status); | ||
} | ||
if (responseLength == 0) { | ||
return Unpooled.EMPTY_BUFFER; | ||
} | ||
return responseBuffer.readBytes(responseLength); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 0 additions & 34 deletions
34
src/test/java/rs/iggy/clients/blocking/tcp/IggyTcpClientTest.java
This file was deleted.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
src/test/java/rs/iggy/clients/blocking/tcp/StreamTcpClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package rs.iggy.clients.blocking.tcp; | ||
|
||
import org.junit.jupiter.api.Disabled; | ||
import rs.iggy.clients.blocking.IggyClient; | ||
import rs.iggy.clients.blocking.StreamClientBaseTest; | ||
|
||
@Disabled | ||
class StreamTcpClientTest extends StreamClientBaseTest { | ||
|
||
@Override | ||
protected IggyClient getClient() { | ||
return TcpClientFactory.create(iggyServer); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/test/java/rs/iggy/clients/blocking/tcp/UsersTcpClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package rs.iggy.clients.blocking.tcp; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import rs.iggy.clients.blocking.IggyClient; | ||
import rs.iggy.clients.blocking.UsersClient; | ||
import rs.iggy.clients.blocking.UsersClientBaseTest; | ||
|
||
class UsersTcpClientTest extends UsersClientBaseTest { | ||
|
||
protected UsersClient usersClient; | ||
|
||
@Override | ||
protected IggyClient getClient() { | ||
return TcpClientFactory.create(iggyServer); | ||
} | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
usersClient = client.users(); | ||
} | ||
|
||
} |