socket client
This commit is contained in:
@@ -5,7 +5,7 @@ import com.eactive.testmaster.client.dto.ApiRequestKeyValueDTO;
|
||||
import com.eactive.testmaster.client.dto.ApiResponse;
|
||||
import com.eactive.testmaster.common.exception.ServerNotFoundException;
|
||||
import com.eactive.testmaster.server.entity.Server;
|
||||
import com.eactive.testmaster.server.entity.ServerRepository;
|
||||
import com.eactive.testmaster.server.repository.ServerRepository;
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package com.eactive.testmaster.client.service;
|
||||
|
||||
import com.eactive.testmaster.client.dto.ApiRequestDTO;
|
||||
import com.eactive.testmaster.client.dto.ApiRequestKeyValueDTO;
|
||||
import com.eactive.testmaster.client.dto.ApiResponse;
|
||||
import com.eactive.testmaster.common.exception.ServerNotFoundException;
|
||||
import com.eactive.testmaster.server.entity.Server;
|
||||
import com.eactive.testmaster.server.entity.ServerRepository;
|
||||
import com.eactive.testmaster.server.entity.ServerOption;
|
||||
import com.eactive.testmaster.server.repository.ServerRepository;
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
@@ -34,6 +36,14 @@ public class NettyTcpApiClient implements ApiClient {
|
||||
this.serverRepository = serverRepository;
|
||||
}
|
||||
|
||||
public String fillPadding(int dataLength, String value) {
|
||||
StringBuilder sb = new StringBuilder(value);
|
||||
while (sb.length() < dataLength) {
|
||||
sb.insert(0, ' '); // Adds space at the beginning of the string
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public CompletableFuture<ApiResponse> handleRequest(ApiRequestDTO apiRequest) {
|
||||
Server server = serverRepository.findById(apiRequest.getServer()).orElseThrow(() -> new ServerNotFoundException("Server not found"));
|
||||
String host = server.getHostname();
|
||||
@@ -42,11 +52,25 @@ public class NettyTcpApiClient implements ApiClient {
|
||||
CompletableFuture<ApiResponse> responseFuture = new CompletableFuture<>();
|
||||
EventLoopGroup group = new NioEventLoopGroup();
|
||||
try {
|
||||
String response = sendMessage(host, port, group, apiRequest.getRequestBody());
|
||||
StringBuilder message = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < server.getServerOptions().size(); i++) {
|
||||
ServerOption serverOption = server.getServerOptions().get(i);
|
||||
if (serverOption.isEnabled()) {
|
||||
ApiRequestKeyValueDTO header = apiRequest.getHeaders().stream().filter(h -> h.getKey().equals(serverOption.getOptionKey())).findFirst().orElse(null);
|
||||
if (header != null) {
|
||||
String value = header.getValue();
|
||||
message.append(fillPadding(serverOption.getLength(), value));
|
||||
}
|
||||
}
|
||||
}
|
||||
message.append(apiRequest.getLayout().buildMessage());
|
||||
String response = sendMessage(host, port, group, message.toString());
|
||||
|
||||
ApiResponse apiResponse = new ApiResponse();
|
||||
apiResponse.setBody(response);
|
||||
responseFuture.complete(apiResponse);
|
||||
} catch (InterruptedException e) {
|
||||
} catch (Exception e) {
|
||||
Thread.currentThread().interrupt();
|
||||
ApiResponse response = new ApiResponse();
|
||||
response.setBody("An error occurred: " + e.getMessage());
|
||||
@@ -58,6 +82,7 @@ public class NettyTcpApiClient implements ApiClient {
|
||||
}
|
||||
|
||||
public String sendMessage(String host, int port, EventLoopGroup group, final String message) throws InterruptedException {
|
||||
logger.debug(message);
|
||||
final BlockingQueue<String> answer = new ArrayBlockingQueue<>(1);
|
||||
Bootstrap b = new Bootstrap();
|
||||
b.group(group)
|
||||
|
||||
Reference in New Issue
Block a user