- Add `HeartbeatHandler` and enhance `EchoHandler` for Protocol Buffers support.
- Extend `test.proto` schema with heartbeat and echo message definitions. - Register `HeartbeatHandler` in `DelegateActionManager`. - Refactor Log4j2 configuration to standardize log levels. - Optimize client wait time in `IPCManager` for reduced latency.
This commit is contained in:
parent
a0ad10b1bc
commit
6576b54057
|
|
@ -3,6 +3,7 @@ package com.mediamanager.service.delegate;
|
|||
import com.google.protobuf.ByteString;
|
||||
import com.mediamanager.protocol.TransportProtocol;
|
||||
import com.mediamanager.service.delegate.handler.EchoHandler;
|
||||
import com.mediamanager.service.delegate.handler.HeartbeatHandler;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
|
|
@ -23,6 +24,7 @@ public class DelegateActionManager {
|
|||
|
||||
private void registerHandlers() {
|
||||
handlerRegistry.put("echo",new EchoHandler());
|
||||
handlerRegistry.put("heartbeat",new HeartbeatHandler());
|
||||
}
|
||||
|
||||
public void start(){
|
||||
|
|
|
|||
|
|
@ -1,17 +1,39 @@
|
|||
package com.mediamanager.service.delegate.handler;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import com.mediamanager.protocol.TestProtocol.EchoCommand; // ← Import
|
||||
import com.mediamanager.protocol.TestProtocol.EchoResponse; // ← Import
|
||||
import com.mediamanager.protocol.TransportProtocol;
|
||||
import com.mediamanager.service.delegate.ActionHandler;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class EchoHandler implements ActionHandler {
|
||||
@Override
|
||||
public TransportProtocol.Response.Builder handle(ByteString requestPayload) {
|
||||
String payloadText = requestPayload.toStringUtf8();
|
||||
String responseText = "Server received: " + payloadText;
|
||||
private static final Logger logger = LogManager.getLogger(EchoHandler.class);
|
||||
|
||||
@Override
|
||||
public TransportProtocol.Response.Builder handle(ByteString requestPayload)
|
||||
throws InvalidProtocolBufferException { // ← Pode lançar exceção
|
||||
|
||||
// 1. Parse Protobuf bytes → EchoCommand
|
||||
EchoCommand command = EchoCommand.parseFrom(requestPayload);
|
||||
|
||||
logger.debug("Echo received: {}", command.getMessage());
|
||||
|
||||
// 2. Cria EchoResponse (Protobuf)
|
||||
EchoResponse echoResponse = EchoResponse.newBuilder()
|
||||
.setMessage(command.getMessage())
|
||||
.setServerTimestamp(System.currentTimeMillis())
|
||||
.build();
|
||||
|
||||
// 3. Serializa EchoResponse → bytes
|
||||
ByteString responsePayload = ByteString.copyFrom(echoResponse.toByteArray());
|
||||
|
||||
// 4. Retorna Response
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setPayload(ByteString.copyFromUtf8(responseText))
|
||||
.setStatusCode(200);
|
||||
.setPayload(responsePayload)
|
||||
.setStatusCode(200)
|
||||
.putHeaders("Content-Type", "application/x-protobuf");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
package com.mediamanager.service.delegate.handler;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import com.mediamanager.protocol.TestProtocol.HeartbeatCommand;
|
||||
import com.mediamanager.protocol.TestProtocol.HeartbeatResponse;
|
||||
import com.mediamanager.protocol.TransportProtocol;
|
||||
import com.mediamanager.service.delegate.ActionHandler;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class HeartbeatHandler implements ActionHandler {
|
||||
private static final Logger logger = LogManager.getLogger(HeartbeatHandler.class);
|
||||
|
||||
@Override
|
||||
public TransportProtocol.Response.Builder handle(ByteString requestPayload)
|
||||
throws InvalidProtocolBufferException {
|
||||
|
||||
HeartbeatCommand command = HeartbeatCommand.parseFrom(requestPayload);
|
||||
|
||||
long serverTime = System.currentTimeMillis();
|
||||
|
||||
logger.debug("Heartbeat received. Client T1={}, Server T2={}",
|
||||
command.getClientTimestamp(), serverTime);
|
||||
|
||||
HeartbeatResponse response = HeartbeatResponse.newBuilder()
|
||||
.setClientTimestamp(command.getClientTimestamp()) // Echo T1
|
||||
.setServerTimestamp(serverTime) // T2
|
||||
.build();
|
||||
|
||||
return TransportProtocol.Response.newBuilder()
|
||||
.setPayload(ByteString.copyFrom(response.toByteArray()))
|
||||
.setStatusCode(200)
|
||||
.putHeaders("Content-Type", "application/x-protobuf");
|
||||
}
|
||||
}
|
||||
|
|
@ -185,7 +185,7 @@ public class IPCManager {
|
|||
// Nenhum cliente conectado no momento
|
||||
// Dorme por um curto período antes de verificar novamente
|
||||
// Isso evita consumir CPU desnecessariamente em um loop vazio
|
||||
Thread.sleep(100); // 100 milissegundos
|
||||
Thread.sleep(1); // 1 milissegundos
|
||||
}
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
syntax = "proto3";
|
||||
|
||||
option java_package = "com.mediamanager.protocol";
|
||||
option java_outer_classname = "TestProtocol";
|
||||
|
||||
package mediamanager.test;
|
||||
|
||||
message EchoCommand {
|
||||
string message = 1;
|
||||
}
|
||||
|
||||
message EchoResponse {
|
||||
string message = 1;
|
||||
int64 server_timestamp = 2;
|
||||
}
|
||||
|
||||
message HeartbeatCommand {
|
||||
int64 client_timestamp = 1;
|
||||
}
|
||||
|
||||
message HeartbeatResponse {
|
||||
int64 client_timestamp = 1;
|
||||
int64 server_timestamp = 2;
|
||||
}
|
||||
|
|
@ -18,17 +18,17 @@
|
|||
</Appenders>
|
||||
|
||||
<Loggers>
|
||||
<Logger name="com.mediamanager" level="debug" additivity="false">
|
||||
<Logger name="com.mediamanager" level="INFO" additivity="false">
|
||||
<AppenderRef ref="Console"/>
|
||||
<AppenderRef ref="FileAppender"/>
|
||||
</Logger>
|
||||
|
||||
<Logger name="org.hibernate" level="info" additivity="false">
|
||||
<Logger name="org.hibernate" level="INFO" additivity="false">
|
||||
<AppenderRef ref="Console"/>
|
||||
<AppenderRef ref="FileAppender"/>
|
||||
</Logger>
|
||||
|
||||
<Root level="info">
|
||||
<Root level="INFO">
|
||||
<AppenderRef ref="Console"/>
|
||||
<AppenderRef ref="FileAppender"/>
|
||||
</Root>
|
||||
|
|
|
|||
Loading…
Reference in New Issue