Add Protocol Buffers support to IPCManager and Maven build:
- Created `messages.proto` to define `TextMessage` schema. - Integrated Protocol Buffers Java library into Maven dependencies. - Configured Maven plugins for Protocol Buffers compilation and generated source management. - Implemented message serialization and deserialization in `IPCManager`. - Enhanced client handling to process and respond with Protocol Buffers messages.
This commit is contained in:
parent
3854635185
commit
23b6d54674
55
pom.xml
55
pom.xml
|
|
@ -78,9 +78,22 @@
|
||||||
<version>${junit.version}</version>
|
<version>${junit.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.protobuf</groupId>
|
||||||
|
<artifactId>protobuf-java</artifactId>
|
||||||
|
<version>4.32.0</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
<extensions>
|
||||||
|
<extension>
|
||||||
|
<groupId>kr.motd.maven</groupId>
|
||||||
|
<artifactId>os-maven-plugin</artifactId>
|
||||||
|
<version>1.7.1</version>
|
||||||
|
</extension>
|
||||||
|
</extensions>
|
||||||
|
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
|
@ -91,12 +104,54 @@
|
||||||
<target>17</target>
|
<target>17</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
<version>3.2.5</version>
|
<version>3.2.5</version>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
|
<!-- Protocol Buffers Plugin -->
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.xolstice.maven.plugins</groupId>
|
||||||
|
<artifactId>protobuf-maven-plugin</artifactId>
|
||||||
|
<version>0.6.1</version>
|
||||||
|
<configuration>
|
||||||
|
<protocExecutable>/usr/bin/protoc</protocExecutable>
|
||||||
|
<protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot>
|
||||||
|
<outputDirectory>${project.build.directory}/generated-sources/protobuf/java</outputDirectory>
|
||||||
|
<clearOutputDirectory>false</clearOutputDirectory>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>compile</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<!-- Build Helper Plugin - Adiciona generated sources ao classpath -->
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>build-helper-maven-plugin</artifactId>
|
||||||
|
<version>3.5.0</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>add-source</id>
|
||||||
|
<phase>generate-sources</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>add-source</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<sources>
|
||||||
|
<source>${project.build.directory}/generated-sources/protobuf/java</source>
|
||||||
|
</sources>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
<!-- Maven Shade Plugin - Creates executable JAR with all dependencies -->
|
<!-- Maven Shade Plugin - Creates executable JAR with all dependencies -->
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
package com.mediamanager.service.ipc;
|
package com.mediamanager.service.ipc;
|
||||||
|
|
||||||
|
|
||||||
|
import com.mediamanager.protocol.SimpleProtocol;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.StandardProtocolFamily;
|
import java.net.StandardProtocolFamily;
|
||||||
|
|
@ -219,15 +221,20 @@ public class IPCManager {
|
||||||
logger.debug("Client {} handler thread started", clientId);
|
logger.debug("Client {} handler thread started", clientId);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// TODO: No próximo passo, vamos implementar:
|
SimpleProtocol.TextMessage request = readMessage(channel);
|
||||||
// 1. Ler mensagens JSON do SocketChannel
|
if (request != null) {
|
||||||
// 2. Parsear o JSON para objetos Java
|
logger.info("Client {} sent: '{}'", clientId, request.getContent());
|
||||||
// 3. Processar a requisição
|
String response = "Server Received: " + request.getContent();
|
||||||
// 4. Criar uma resposta JSON
|
|
||||||
// 5. Escrever a resposta de volta no SocketChannel
|
|
||||||
|
|
||||||
// Por enquanto, apenas mantém a conexão aberta brevemente
|
SimpleProtocol.TextMessage responseMsg = SimpleProtocol.TextMessage.newBuilder()
|
||||||
// para testar que o sistema de aceitação e threads está funcionando
|
.setContent(response).build();
|
||||||
|
|
||||||
|
writeMessage(channel,responseMsg);
|
||||||
|
logger.info("Client {} response sent: '{}'", clientId, response);
|
||||||
|
|
||||||
|
}else {
|
||||||
|
logger.warn("Client {} sent null message", clientId);
|
||||||
|
}
|
||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
|
|
||||||
logger.debug("Client {} processing complete", clientId);
|
logger.debug("Client {} processing complete", clientId);
|
||||||
|
|
@ -245,6 +252,71 @@ public class IPCManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private SimpleProtocol.TextMessage readMessage(SocketChannel channel) throws IOException {
|
||||||
|
// Primeiro, lê o tamanho da mensagem (4 bytes = int32)
|
||||||
|
java.nio.ByteBuffer sizeBuffer = java.nio.ByteBuffer.allocate(4);
|
||||||
|
int bytesRead = 0;
|
||||||
|
|
||||||
|
while (bytesRead < 4) {
|
||||||
|
int read = channel.read(sizeBuffer);
|
||||||
|
if (read == -1) {
|
||||||
|
logger.debug("Client disconnected before sending size");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
bytesRead += read;
|
||||||
|
}
|
||||||
|
|
||||||
|
sizeBuffer.flip();
|
||||||
|
int messageSize = sizeBuffer.getInt();
|
||||||
|
logger.debug("Expecting message of {} bytes", messageSize);
|
||||||
|
|
||||||
|
// Validação básica de segurança
|
||||||
|
if (messageSize <= 0 || messageSize > 1024 * 1024) { // Max 1MB
|
||||||
|
throw new IOException("Invalid message size: " + messageSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Agora lê a mensagem completa
|
||||||
|
java.nio.ByteBuffer messageBuffer = java.nio.ByteBuffer.allocate(messageSize);
|
||||||
|
bytesRead = 0;
|
||||||
|
|
||||||
|
while (bytesRead < messageSize) {
|
||||||
|
int read = channel.read(messageBuffer);
|
||||||
|
if (read == -1) {
|
||||||
|
throw new IOException("Client disconnected while reading message");
|
||||||
|
}
|
||||||
|
bytesRead += read;
|
||||||
|
}
|
||||||
|
|
||||||
|
messageBuffer.flip();
|
||||||
|
|
||||||
|
// Deserializa o Protocol Buffers
|
||||||
|
byte[] messageBytes = new byte[messageSize];
|
||||||
|
messageBuffer.get(messageBytes);
|
||||||
|
|
||||||
|
return SimpleProtocol.TextMessage.parseFrom(messageBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeMessage(SocketChannel channel, SimpleProtocol.TextMessage message) throws IOException {
|
||||||
|
// Serializa a mensagem
|
||||||
|
byte[] messageBytes = message.toByteArray();
|
||||||
|
int messageSize = messageBytes.length;
|
||||||
|
|
||||||
|
logger.debug("Writing message of {} bytes", messageSize);
|
||||||
|
|
||||||
|
// Cria buffer com tamanho + mensagem
|
||||||
|
java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocate(4 + messageSize);
|
||||||
|
buffer.putInt(messageSize); // 4 bytes de tamanho
|
||||||
|
buffer.put(messageBytes); // N bytes da mensagem
|
||||||
|
buffer.flip();
|
||||||
|
|
||||||
|
// Escreve tudo no canal
|
||||||
|
while (buffer.hasRemaining()) {
|
||||||
|
channel.write(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.debug("Message written successfully");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
option java_package = "com.mediamanager.protocol";
|
||||||
|
option java_outer_classname = "SimpleProtocol";
|
||||||
|
package mediamanager;
|
||||||
|
|
||||||
|
message TextMessage {
|
||||||
|
string content = 1; // O texto da mensagem
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue