Class ChannelMessage.Builder
java.lang.Object
eu.cloudnetservice.driver.channel.ChannelMessage.Builder
- Enclosing class:
ChannelMessage
A builder for a channel message. This class should be used over direct constructor access as allows better
customization and validation of a channel message which gets created. Required properties are:
- channel, can be empty
- message, can be empty
- at least one target for the message
If no sender of the message is given the current network component will be used as the sender of the message.
- Since:
- 4.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Stringprivate DataBufprivate Stringprivate booleanprivate ChannelMessageSenderprivate booleanprivate final Collection<ChannelMessageTarget> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionSets the content of this message.build()Builds a channel message from this builder within the contract given in the Builder class java docs.Sets the channel of this message.Sets the message key of this message.prioritized(boolean prioritized) Sets if the channel message should be prioritized over other channel messages.sender(@NonNull ChannelMessageSender sender) Sets the sender of this message.sendSync(boolean sync) Sets if the channel message should get send sync, blocking the sending thread until the message was written and flushed through the network layer.target(@NonNull DriverEnvironment environment, @Nullable String name) Adds a channel message target to this message.target(@NonNull ChannelMessageTarget target) Adds the given target as a target of this message.target(ChannelMessageTarget.Type type, @Nullable String name) Adds a channel message target to this message.Targets all components within the network.Targets all components with the given type.targetEnvironment(@NonNull ServiceEnvironmentType environment) Targets all services with the given environment within the network.targetNode(@Nullable String name) Targets a specific node within the network.Targets all nodes within the network.targetService(@Nullable String name) Targets a specific service in the network.Targets all services within the network.targetTask(@Nullable String name) Targets all services of the given task within the network.
-
Field Details
-
targets
-
channel
-
message
-
sendSync
private boolean sendSync -
prioritized
private boolean prioritized -
-
sender
-
-
Constructor Details
-
Builder
public Builder()
-
-
Method Details
-
sender
@NonNull public @NonNull ChannelMessage.Builder sender(@NonNull @NonNull ChannelMessageSender sender) Sets the sender of this message. If no sender is given the current component will be used as the sender. Note that if you change the sender and try to receive a query response it will send the response to the given sender, not this component.- Parameters:
sender- the sender of this message.- Returns:
- the same builder as used to call the method, for chaining.
- Throws:
NullPointerException- if the given sender is null.- See Also:
-
channel
Sets the channel of this message. Might be empty but should be unique to identify within the network.- Parameters:
channel- the channel.- Returns:
- the same builder as used to call the method, for chaining.
- Throws:
NullPointerException- if the given channel is null.
-
message
Sets the message key of this message. Might be empty but should be unique to identify within the network.- Parameters:
message- the message key.- Returns:
- the same builder as used to call the method, for chaining.
- Throws:
NullPointerException- if the given message is null.
-
sendSync
Sets if the channel message should get send sync, blocking the sending thread until the message was written and flushed through the network layer.- Parameters:
sync- if the message should get send sync.- Returns:
- the same builder as used to call the method, for chaining.
-
prioritized
Sets if the channel message should be prioritized over other channel messages.USE WITH CAUTION! This can cause other packet to get read and handled delayed. Use this option only if you know what you're doing and are absolutely sure that the packet is urgent for CloudNet to work for as expected. Otherwise, don't touch this method.
- Parameters:
prioritized- if the channel message is prioritized- Returns:
- the same builder as used to call the method, for chaining.
-
buffer
-
target
@NonNull public @NonNull ChannelMessage.Builder target(@NonNull @NonNull ChannelMessageTarget target) Adds the given target as a target of this message.- Parameters:
target- the target to add.- Returns:
- the same builder as used to call the method, for chaining.
- Throws:
NullPointerException- if the given target is null.
-
target
@NonNull public @NonNull ChannelMessage.Builder target(@NonNull ChannelMessageTarget.Type type, @Nullable @Nullable String name) Adds a channel message target to this message. You may not target an environment using this method.- Parameters:
type- the type of the receiver.name- the name of the receiver.- Returns:
- the same builder as used to call the method, for chaining.
- Throws:
IllegalArgumentException- if type isChannelMessageTarget.Type.ENVIRONMENTNullPointerException- if the given target type is null.- See Also:
-
target
@NonNull public @NonNull ChannelMessage.Builder target(@NonNull @NonNull DriverEnvironment environment, @Nullable @Nullable String name) Adds a channel message target to this message. You may not target an environment using this method.- Parameters:
environment- the driver environment to target.name- the name of the target, might be null to target all components with the given environment.- Returns:
- the same builder as used to call the method, for chaining.
- Throws:
IllegalArgumentException- if type isChannelMessageTarget.Type.ENVIRONMENTNullPointerException- if the given environment is null.- See Also:
-
targetAll
Targets all components with the given type. You may not target an environment using this method.- Parameters:
type- the type of the receivers to target.- Returns:
- the same builder as used to call the method, for chaining.
- Throws:
IllegalArgumentException- if type isChannelMessageTarget.Type.ENVIRONMENTNullPointerException- if the given target type is null.- See Also:
-
targetAll
Targets all components within the network.- Returns:
- the same builder as used to call the method, for chaining.
- Throws:
IllegalArgumentException- if type isChannelMessageTarget.Type.ENVIRONMENT- See Also:
-
targetServices
Targets all services within the network.- Returns:
- the same builder as used to call the method, for chaining.
- Throws:
IllegalArgumentException- if type isChannelMessageTarget.Type.ENVIRONMENT- See Also:
-
targetService
-
targetTask
-
targetNode
-
targetNodes
Targets all nodes within the network.- Returns:
- the same builder as used to call the method, for chaining.
-
targetEnvironment
@NonNull public @NonNull ChannelMessage.Builder targetEnvironment(@NonNull @NonNull ServiceEnvironmentType environment) Targets all services with the given environment within the network.- Parameters:
environment- the environment to target.- Returns:
- the same builder as used to call the method, for chaining.
- Throws:
NullPointerException- if the given environment is null.
-
build
Builds a channel message from this builder within the contract given in the Builder class java docs.- Returns:
- the created channel message from this builder.
- Throws:
NullPointerException- if no message or channel is provided.IllegalArgumentException- if no target was specified.
-