Messages structure
A single message comprises of two parts: a header and a payload. The header is a form of message metadata that should contain at least information about a sender (its address) and about a receiver (or receivers) in the form of the address selector. The payload can be any serializable object. All agents, components and nodes in the platofrm use the same interfaces and implementations for messages. However, exact mechanisms of message sending and delivery may depend on the type of sender and receiver.
The platform provides default implementations for a message and a header. They are both read-only and are simple containers for aforementioned components. However, the code of the platform does not depend on these implementations and they can be easily replaced by more sophisticated, problem-dependent classes.
Message sending and delivery
This section briefly describes the API for message sending and delivery for all scenarios available in the platform.
Within agent environment
The most basic case of the send-delivery mechanism is communication between agents in a single aggregate. The following code shows how it works for an agent (red on the diagram) that wants to send a message to agents in its environment (green agents) or its aggregate (blue).
Subclasses of the AbstractAgent
class use a FIFO queue of delivered messages. The message is delivered (usually by the aggregate) using the method:
public void deliverMessage(IMessage<IAgentAddress, ?> message)
Then, it can be received by one of two methods:
protected final <T extends Serializable> IMessage<IAgentAddress, T> receiveMessage()
and
protected final <T extends Serializable> IMessage<IAgentAddress, T> receiveMessage(Class<T> klass)
The second form performs a runtime type checking of the payload.
For sending messages, simple agents (and its aggregates) use the actions mechanism (AkcjeImpl). A usual call looks like this:
doAction(new SingleAction(message.getHeader().getReceiverSelector(), new SendMessageActionContext(message)));
For convenience, there is a method defined for sending messages from a simple agent:
void sendMessage(Message<IAgentAddress, ?> message) throws AgentException