
net.sf.eBus.client.sysmessages.UdpConnectReply Maven / Gradle / Ivy
//
// Copyright 2020 Charles W. Rapp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package net.sf.eBus.client.sysmessages;
import java.io.Serializable;
import java.util.Objects;
import net.sf.eBus.messages.EReplyMessage.ReplyStatus;
import net.sf.eBus.messages.ESystemMessage;
import net.sf.eBus.util.Validator;
/**
* Response to a {@link UdpConnectRequest} containing the UDP
* port to which the initiator should send packets. The idea is
* that the {@code UdpConnectRequest} is sent to the UDP "server"
* port which creates a new UDP channel bound to an ephemeral
* port. It is this ephemeral port that is contained in this
* message.
*
* @author Charles W. Rapp
*/
public final class UdpConnectReply
extends ESystemMessage
implements Serializable
{
//---------------------------------------------------------------
// Member data.
//
//-----------------------------------------------------------
// Constants.
//
/**
* Serialization version identifier.
*/
private static final long serialVersionUID = 0x050300L;
//-----------------------------------------------------------
// Locals.
//
/**
* Set to {@code OK_FINAL} if the connection process
* succeeded and {@code ERROR} if process failed.
*/
public final ReplyStatus status;
/**
* Text explaining why a connect attempt failed. May be
* {@code null} or empty if {@link #status} is
* {@link ReplyStatus#OK_FINAL}.
*/
public final String text;
/**
* UDP port to which the requester should communicate.
*/
public final int port;
//---------------------------------------------------------------
// Member methods.
//
//-----------------------------------------------------------
// Constructors.
//
/**
* Creates a new instance of UdpConnectReply.
*/
private UdpConnectReply(final Builder builder)
{
super (builder);
this.status = builder.mStatus;
this.text = builder.mText;
this.port = builder.mPort;
} // end of UdpConnectReply(Builder)
//
// end of Constructors.
//-----------------------------------------------------------
//-----------------------------------------------------------
// Object Method Overrides.
//
/**
* Returns a human-readable text version of this message.
* @return text version of this message.
*/
@Override
public String toString()
{
return (
String.format(
"%s%n status: %s%n text: %s%n port: %d",
super.toString(),
status,
text,
port));
} // end of toString()
//
// end of Object Method Overrides.
//-----------------------------------------------------------
/**
* Returns a new instance of the {@code ResumeReply} message
* builder.
* @return message builder instance.
*/
public static Builder builder()
{
return (new Builder());
} // end of builder()
//---------------------------------------------------------------
// Inner classes.
//
/**
* Collects a {@code UdpConnectReply} field settings and
* provides validation.
*/
public static final class Builder
extends ESystemMessage.Builder
{
//-----------------------------------------------------------
// Member data.
//
//-------------------------------------------------------
// Locals.
//
private ReplyStatus mStatus;
private String mText;
private int mPort;
//-----------------------------------------------------------
// Member methods.
//
//-------------------------------------------------------
// Constructors.
//
private Builder()
{
super (UdpConnectReply.class);
mPort = -1;
} // end of Builder()
//
// end of Constructors.
//-------------------------------------------------------
//-------------------------------------------------------
// Builder Method Overrides.
//
/**
* Returns a UDP connect reply based on {@code this}
* builder's configuration.
* @return new UDP connect reply instance.
*/
@Override
protected UdpConnectReply buildImpl()
{
return (new UdpConnectReply(this));
} // end of buildImpl()
/**
* Checks if reply status and port are set.
* @param problems used to check field validity and
* collect discovered invalid fields.
* @return {@code problems} to allow for method chaining.
*/
@Override
protected Validator validate(final Validator problems)
{
return (super.validate(problems)
.requireNotNull(mStatus, "status")
.requireTrue(v -> (mPort >= 0),
mStatus,
"port",
Validator.NOT_SET));
} // end of validate(Validator)
//
// end of Builder Method Overrides.
//-------------------------------------------------------
//-------------------------------------------------------
// Set Methods.
//
public Builder status(final ReplyStatus status)
{
mStatus =
Objects.requireNonNull(status, "status is null");
return (this);
} // end of status(ReplyStatus)
public Builder text(final String text)
{
mText = text;
return (this);
} // end of text(String)
public Builder port(final int port)
{
if (port <= 0)
{
throw (
new IllegalArgumentException(
"port " + port + " invalid"));
}
mPort = port;
return (this);
} // end of port(int)
//
// end of Set Methods.
//-------------------------------------------------------
} // end of class Builder
} // end of class UdpConnectReply
© 2015 - 2025 Weber Informatics LLC | Privacy Policy