mirror of
https://github.com/davidalves04/Trabalho-Pratico-SD.git
synced 2025-12-08 12:33:31 +00:00
Define message types
This commit is contained in:
142
main/src/main/java/sd/model/Message.java
Normal file
142
main/src/main/java/sd/model/Message.java
Normal file
@@ -0,0 +1,142 @@
|
||||
package sd.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Represents a message exchanged between processes in the distributed simulation.
|
||||
* Each message has a unique ID, a type, a sender, a destination, and a payload.
|
||||
* This class implements {@link Serializable} to allow transmission over the network.
|
||||
*/
|
||||
public class Message implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Unique identifier for this message.
|
||||
*/
|
||||
private final String messageId;
|
||||
|
||||
/**
|
||||
* The type of this message (e.g., VEHICLE_TRANSFER, STATS_UPDATE).
|
||||
*/
|
||||
private final MessageType type;
|
||||
|
||||
/**
|
||||
* Identifier of the process that sent this message.
|
||||
*/
|
||||
private final String senderId;
|
||||
|
||||
/**
|
||||
* Identifier of the destination process. Can be null for broadcast messages.
|
||||
*/
|
||||
private final String destinationId;
|
||||
|
||||
/**
|
||||
* The actual data being transmitted. Type depends on the message type.
|
||||
*/
|
||||
private final Object payload;
|
||||
|
||||
/**
|
||||
* Timestamp when this message was created (simulation time or real time).
|
||||
*/
|
||||
private final long timestamp;
|
||||
|
||||
/**
|
||||
* Creates a new message with all parameters.
|
||||
*
|
||||
* @param type The message type
|
||||
* @param senderId The ID of the sending process
|
||||
* @param destinationId The ID of the destination process (null for broadcast)
|
||||
* @param payload The message payload
|
||||
* @param timestamp The timestamp of message creation
|
||||
*/
|
||||
public Message(MessageType type, String senderId, String destinationId,
|
||||
Object payload, long timestamp) {
|
||||
this.messageId = UUID.randomUUID().toString();
|
||||
this.type = type;
|
||||
this.senderId = senderId;
|
||||
this.destinationId = destinationId;
|
||||
this.payload = payload;
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new message with current system time as timestamp.
|
||||
*
|
||||
* @param type The message type
|
||||
* @param senderId The ID of the sending process
|
||||
* @param destinationId The ID of the destination process
|
||||
* @param payload The message payload
|
||||
*/
|
||||
public Message(MessageType type, String senderId, String destinationId, Object payload) {
|
||||
this(type, senderId, destinationId, payload, System.currentTimeMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a broadcast message (no specific destination).
|
||||
*
|
||||
* @param type The message type
|
||||
* @param senderId The ID of the sending process
|
||||
* @param payload The message payload
|
||||
*/
|
||||
public Message(MessageType type, String senderId, Object payload) {
|
||||
this(type, senderId, null, payload, System.currentTimeMillis());
|
||||
}
|
||||
|
||||
//Getters
|
||||
|
||||
public String getMessageId() {
|
||||
return messageId;
|
||||
}
|
||||
|
||||
public MessageType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getSenderId() {
|
||||
return senderId;
|
||||
}
|
||||
|
||||
public String getDestinationId() {
|
||||
return destinationId;
|
||||
}
|
||||
|
||||
public Object getPayload() {
|
||||
return payload;
|
||||
}
|
||||
|
||||
public long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this is a broadcast message (no specific destination).
|
||||
*
|
||||
* @return true if destinationId is null, false otherwise
|
||||
*/
|
||||
public boolean isBroadcast() {
|
||||
return destinationId == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the payload cast to a specific type.
|
||||
* Use with caution and ensure type safety.
|
||||
*
|
||||
* @param <T> The expected payload type
|
||||
* @return The payload cast to type T
|
||||
* @throws ClassCastException if the payload is not of type T
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getPayloadAs(Class<T> clazz) {
|
||||
return (T) payload;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Message[id=%s, type=%s, from=%s, to=%s, timestamp=%d]",
|
||||
messageId, type, senderId,
|
||||
destinationId != null ? destinationId : "BROADCAST",
|
||||
timestamp);
|
||||
}
|
||||
}
|
||||
81
main/src/main/java/sd/model/MessageType.java
Normal file
81
main/src/main/java/sd/model/MessageType.java
Normal file
@@ -0,0 +1,81 @@
|
||||
package sd.model;
|
||||
|
||||
/**
|
||||
* Enumeration representing all possible message types for distributed communication.
|
||||
* These types are used for inter-process communication between different components
|
||||
* of the distributed traffic simulation system.
|
||||
*/
|
||||
public enum MessageType {
|
||||
|
||||
/**
|
||||
* Message to transfer a vehicle between intersections or processes.
|
||||
* Payload: Vehicle object with current state
|
||||
*/
|
||||
VEHICLE_TRANSFER,
|
||||
|
||||
/**
|
||||
* Message to update statistics across the distributed system.
|
||||
* Payload: Statistics data (waiting times, queue sizes, etc.)
|
||||
*/
|
||||
STATS_UPDATE,
|
||||
|
||||
/**
|
||||
* Message to synchronize traffic light states between processes.
|
||||
* Payload: TrafficLight state and timing information
|
||||
*/
|
||||
TRAFFIC_LIGHT_SYNC,
|
||||
|
||||
/**
|
||||
* Heartbeat message to check if a process is alive.
|
||||
* Payload: Process ID and timestamp
|
||||
*/
|
||||
HEARTBEAT,
|
||||
|
||||
/**
|
||||
* Request to join the distributed simulation.
|
||||
* Payload: Process information and capabilities
|
||||
*/
|
||||
JOIN_REQUEST,
|
||||
|
||||
/**
|
||||
* Response to a join request.
|
||||
* Payload: Acceptance status and configuration
|
||||
*/
|
||||
JOIN_RESPONSE,
|
||||
|
||||
/**
|
||||
* Message to notify about a new vehicle generation.
|
||||
* Payload: Vehicle generation parameters
|
||||
*/
|
||||
VEHICLE_SPAWN,
|
||||
|
||||
/**
|
||||
* Message to request the current state of an intersection.
|
||||
* Payload: Intersection ID
|
||||
*/
|
||||
STATE_REQUEST,
|
||||
|
||||
/**
|
||||
* Response containing the current state of an intersection.
|
||||
* Payload: Complete intersection state
|
||||
*/
|
||||
STATE_RESPONSE,
|
||||
|
||||
/**
|
||||
* Message to signal shutdown of a process.
|
||||
* Payload: Process ID and reason
|
||||
*/
|
||||
SHUTDOWN,
|
||||
|
||||
/**
|
||||
* Acknowledgment message for reliable communication.
|
||||
* Payload: Message ID being acknowledged
|
||||
*/
|
||||
ACK,
|
||||
|
||||
/**
|
||||
* Error message to report problems in the distributed system.
|
||||
* Payload: Error description and context
|
||||
*/
|
||||
ERROR
|
||||
}
|
||||
Reference in New Issue
Block a user