Skip to content

Commit

Permalink
8297778: Modernize and improve module jdk.sctp
Browse files Browse the repository at this point in the history
Reviewed-by: dfuchs, stsypanov
  • Loading branch information
minborg authored and dfuch committed Dec 9, 2022
1 parent 11aece2 commit cb766c5
Show file tree
Hide file tree
Showing 19 changed files with 260 additions and 339 deletions.
Expand Up @@ -36,7 +36,7 @@
*/
public enum HandlerResult {
/**
* Try to receieve another message or notification.
* Try to receive another message or notification.
*/
CONTINUE,

Expand Down
Expand Up @@ -24,6 +24,8 @@
*/
package com.sun.nio.sctp;

import java.io.Serial;

/**
* Unchecked exception thrown when an attempt is made to invoke the
* {@code receive} method of {@link SctpChannel} or {@link SctpMultiChannel}
Expand All @@ -32,6 +34,7 @@
* @since 1.7
*/
public class IllegalReceiveException extends IllegalStateException {
@Serial
private static final long serialVersionUID = 2296619040988576224L;

/**
Expand Down
Expand Up @@ -24,6 +24,8 @@
*/
package com.sun.nio.sctp;

import java.io.Serial;

/**
* Unchecked exception thrown when an attempt is made to remove an
* address that is not bound to the channel, or remove an address from a
Expand All @@ -32,6 +34,7 @@
* @since 1.7
*/
public class IllegalUnbindException extends IllegalStateException {
@Serial
private static final long serialVersionUID = -310540883995532224L;

/**
Expand Down
Expand Up @@ -24,13 +24,16 @@
*/
package com.sun.nio.sctp;

import java.io.Serial;

/**
* Unchecked exception thrown when an attempt is made to send a
* message to an invalid stream.
*
* @since 1.7
*/
public class InvalidStreamException extends IllegalArgumentException {
@Serial
private static final long serialVersionUID = -9172703378046665558L;

/**
Expand Down
Expand Up @@ -34,23 +34,24 @@
*
* @since 1.7
*/
public class SctpStandardSocketOptions {
public final class SctpStandardSocketOptions {
private SctpStandardSocketOptions() {}
/**
* Enables or disables message fragmentation.
*
* <P> The value of this socket option is a {@code Boolean} that represents
* whether the option is enabled or disabled. If enabled no SCTP message
* fragmentation will be performed. Instead if a message being sent
* fragmentation will be performed. Instead, if a message being sent
* exceeds the current PMTU size, the message will NOT be sent and
* an error will be indicated to the user.
*
* <P> It is implementation specific whether or not this option is
* supported.
*/
public static final SctpSocketOption<Boolean> SCTP_DISABLE_FRAGMENTS = new
SctpStdSocketOption<Boolean>("SCTP_DISABLE_FRAGMENTS", Boolean.class,
sun.nio.ch.sctp.SctpStdSocketOption.SCTP_DISABLE_FRAGMENTS);
public static final SctpSocketOption<Boolean> SCTP_DISABLE_FRAGMENTS =
new SctpStdSocketOption<>("SCTP_DISABLE_FRAGMENTS",
Boolean.class,
SctpStdSocketOption.SCTP_DISABLE_FRAGMENTS);

/**
* Enables or disables explicit message completion.
Expand All @@ -67,9 +68,10 @@ private SctpStandardSocketOptions() {}
* option is disabled. It is implementation specific whether or not this
* option is supported.
*/
public static final SctpSocketOption<Boolean> SCTP_EXPLICIT_COMPLETE = new
SctpStdSocketOption<Boolean>("SCTP_EXPLICIT_COMPLETE", Boolean.class,
sun.nio.ch.sctp.SctpStdSocketOption.SCTP_EXPLICIT_COMPLETE);
public static final SctpSocketOption<Boolean> SCTP_EXPLICIT_COMPLETE =
new SctpStdSocketOption<>("SCTP_EXPLICIT_COMPLETE",
Boolean.class,
SctpStdSocketOption.SCTP_EXPLICIT_COMPLETE);

/**
* Fragmented interleave controls how the presentation of messages occur
Expand Down Expand Up @@ -118,9 +120,9 @@ private SctpStandardSocketOptions() {}
* supported.
*/
public static final SctpSocketOption<Integer> SCTP_FRAGMENT_INTERLEAVE =
new SctpStdSocketOption<Integer>("SCTP_FRAGMENT_INTERLEAVE",
new SctpStdSocketOption<>("SCTP_FRAGMENT_INTERLEAVE",
Integer.class,
sun.nio.ch.sctp.SctpStdSocketOption.SCTP_FRAGMENT_INTERLEAVE);
SctpStdSocketOption.SCTP_FRAGMENT_INTERLEAVE);

/**
* The maximum number of streams requested by the local endpoint during
Expand Down Expand Up @@ -158,8 +160,8 @@ private SctpStandardSocketOptions() {}
*/
public static final SctpSocketOption
<SctpStandardSocketOptions.InitMaxStreams> SCTP_INIT_MAXSTREAMS =
new SctpStdSocketOption<SctpStandardSocketOptions.InitMaxStreams>(
"SCTP_INIT_MAXSTREAMS", SctpStandardSocketOptions.InitMaxStreams.class);
new SctpStdSocketOption<>("SCTP_INIT_MAXSTREAMS",
SctpStandardSocketOptions.InitMaxStreams.class);

/**
* Enables or disables a Nagle-like algorithm.
Expand All @@ -170,8 +172,9 @@ private SctpStandardSocketOptions() {}
* improve network efficiency.
*/
public static final SctpSocketOption<Boolean> SCTP_NODELAY =
new SctpStdSocketOption<Boolean>("SCTP_NODELAY", Boolean.class,
sun.nio.ch.sctp.SctpStdSocketOption.SCTP_NODELAY);
new SctpStdSocketOption<>("SCTP_NODELAY",
Boolean.class,
SctpStdSocketOption.SCTP_NODELAY);

/**
* Requests that the local SCTP stack use the given peer address as
Expand All @@ -191,8 +194,8 @@ private SctpStandardSocketOptions() {}
* set or queried directly.
*/
public static final SctpSocketOption<SocketAddress> SCTP_PRIMARY_ADDR =
new SctpStdSocketOption<SocketAddress>
("SCTP_PRIMARY_ADDR", SocketAddress.class);
new SctpStdSocketOption<>("SCTP_PRIMARY_ADDR",
SocketAddress.class);

/**
* Requests that the peer mark the enclosed address as the association
Expand All @@ -216,8 +219,8 @@ private SctpStandardSocketOptions() {}
* option is supported.
*/
public static final SctpSocketOption<SocketAddress> SCTP_SET_PEER_PRIMARY_ADDR =
new SctpStdSocketOption<SocketAddress>
("SCTP_SET_PEER_PRIMARY_ADDR", SocketAddress.class);
new SctpStdSocketOption<>("SCTP_SET_PEER_PRIMARY_ADDR",
SocketAddress.class);

/**
* The size of the socket send buffer.
Expand Down Expand Up @@ -245,8 +248,9 @@ private SctpStandardSocketOptions() {}
* dependent.
*/
public static final SctpSocketOption<Integer> SO_SNDBUF =
new SctpStdSocketOption<Integer>("SO_SNDBUF", Integer.class,
sun.nio.ch.sctp.SctpStdSocketOption.SO_SNDBUF);
new SctpStdSocketOption<>("SO_SNDBUF",
Integer.class,
SctpStdSocketOption.SO_SNDBUF);

/**
* The size of the socket receive buffer.
Expand All @@ -272,8 +276,9 @@ private SctpStandardSocketOptions() {}
* dependent.
*/
public static final SctpSocketOption<Integer> SO_RCVBUF =
new SctpStdSocketOption<Integer>("SO_RCVBUF", Integer.class,
sun.nio.ch.sctp.SctpStdSocketOption.SO_RCVBUF);
new SctpStdSocketOption<>("SO_RCVBUF",
Integer.class,
SctpStdSocketOption.SO_RCVBUF);

/**
* Linger on close if data is present.
Expand Down Expand Up @@ -303,8 +308,9 @@ private SctpStandardSocketOptions() {}
* its maximum value.
*/
public static final SctpSocketOption<Integer> SO_LINGER =
new SctpStdSocketOption<Integer>("SO_LINGER", Integer.class,
sun.nio.ch.sctp.SctpStdSocketOption.SO_LINGER);
new SctpStdSocketOption<>("SO_LINGER",
Integer.class,
SctpStdSocketOption.SO_LINGER);

/**
* This class is used to set the maximum number of inbound/outbound streams
Expand All @@ -316,8 +322,8 @@ private SctpStandardSocketOptions() {}
* @since 1.7
*/
public static class InitMaxStreams {
private int maxInStreams;
private int maxOutStreams;
private final int maxInStreams;
private final int maxOutStreams;

private InitMaxStreams(int maxInStreams, int maxOutStreams) {
this.maxInStreams = maxInStreams;
Expand Down Expand Up @@ -378,11 +384,9 @@ public int maxOutStreams() {
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(super.toString()).append(" [");
sb.append("maxInStreams:").append(maxInStreams);
sb.append("maxOutStreams:").append(maxOutStreams).append("]");
return sb.toString();
return super.toString() + " [" +
"maxInStreams:" + maxInStreams +
"maxOutStreams:" + maxOutStreams + "]";
}

/**
Expand All @@ -398,11 +402,9 @@ public String toString() {
*/
@Override
public boolean equals(Object obj) {
if (obj != null && obj instanceof InitMaxStreams) {
InitMaxStreams that = (InitMaxStreams) obj;
if (this.maxInStreams == that.maxInStreams &&
this.maxOutStreams == that.maxOutStreams)
return true;
if (obj instanceof InitMaxStreams that) {
return this.maxInStreams == that.maxInStreams &&
this.maxOutStreams == that.maxOutStreams;
}
return false;
}
Expand Down
Expand Up @@ -31,7 +31,7 @@
* Notification emitted when a send failed notification has been received.
*
* <P> A send failed notification indicates that a message cannot be delivered.
* Typically this is because the association has been shutdown with unsent data
* Typically, this is because the association has been shutdown with unsent data
* in the socket output buffer, or in the case of a {@link SctpMultiChannel}
* the association failed to setup.
*
Expand Down Expand Up @@ -81,7 +81,7 @@ protected SendFailedNotification() {}
public abstract int errorCode();

/**
* Returns the stream number that the messge was to be sent on.
* Returns the stream number that the message was to be sent on.
*
* @return The stream number
*/
Expand Down
18 changes: 8 additions & 10 deletions src/jdk.sctp/share/classes/sun/nio/ch/sctp/MessageInfoImpl.java
Expand Up @@ -156,15 +156,13 @@ public MessageInfo timeToLive(long millis) {

@Override
public String toString() {
StringBuilder sb = new StringBuilder(super.toString());
sb.append( "[Address: ").append(address)
.append(", Association: ").append(association)
.append(", Assoc ID: ").append(assocId)
.append(", Bytes: ").append(bytes)
.append(", Stream Number: ").append(streamNumber)
.append(", Complete: ").append(complete)
.append(", isUnordered: ").append(unordered)
.append("]");
return sb.toString();
return super.toString() + "[Address: " + address +
", Association: " + association +
", Assoc ID: " + assocId +
", Bytes: " + bytes +
", Stream Number: " + streamNumber +
", Complete: " + complete +
", isUnordered: " + unordered +
"]";
}
}
Expand Up @@ -27,7 +27,7 @@
import com.sun.nio.sctp.SctpSocketOption;
import java.lang.annotation.Native;

public class SctpStdSocketOption<T>
public final class SctpStdSocketOption<T>
implements SctpSocketOption<T>
{
/* for native mapping of int options */
Expand All @@ -43,11 +43,10 @@ public class SctpStdSocketOption<T>
private final Class<T> type;

/* for native mapping of int options */
private int constValue;
private final int constValue;

public SctpStdSocketOption(String name, Class<T> type) {
this.name = name;
this.type = type;
this(name, type, 0);
}

public SctpStdSocketOption(String name, Class<T> type, int constValue) {
Expand Down
47 changes: 17 additions & 30 deletions src/jdk.sctp/unix/classes/sun/nio/ch/sctp/AssociationChange.java
Expand Up @@ -43,38 +43,27 @@ public class AssociationChange extends AssociationChangeNotification

private Association association;

/* assocId is used to lookup the association before the notification is
/* assocId is used to look up the association before the notification is
* returned to user code */
private int assocId;
private AssocChangeEvent event;
private int maxOutStreams;
private int maxInStreams;
private final int assocId;
private final AssocChangeEvent event;
private final int maxOutStreams;
private final int maxInStreams;

/* Invoked from native */
private AssociationChange(int assocId,
int intEvent,
int maxOutStreams,
int maxInStreams) {
switch (intEvent) {
case SCTP_COMM_UP :
this.event = AssocChangeEvent.COMM_UP;
break;
case SCTP_COMM_LOST :
this.event = AssocChangeEvent.COMM_LOST;
break;
case SCTP_RESTART :
this.event = AssocChangeEvent.RESTART;
break;
case SCTP_SHUTDOWN :
this.event = AssocChangeEvent.SHUTDOWN;
break;
case SCTP_CANT_START :
this.event = AssocChangeEvent.CANT_START;
break;
default :
throw new AssertionError(
"Unknown Association Change Event type: " + intEvent);
}
this.event = switch (intEvent) {
case SCTP_COMM_UP -> AssocChangeEvent.COMM_UP;
case SCTP_COMM_LOST -> AssocChangeEvent.COMM_LOST;
case SCTP_RESTART -> AssocChangeEvent.RESTART;
case SCTP_SHUTDOWN -> AssocChangeEvent.SHUTDOWN;
case SCTP_CANT_START -> AssocChangeEvent.CANT_START;
default -> throw new AssertionError(
"Unknown Association Change Event type: " + intEvent);
};

this.assocId = assocId;
this.maxOutStreams = maxOutStreams;
Expand Down Expand Up @@ -112,10 +101,8 @@ int maxInStreams() {

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(super.toString()).append(" [");
sb.append("Association:").append(association);
sb.append(", Event: ").append(event).append("]");
return sb.toString();
return super.toString() + " [" +
"Association:" + association +
", Event: " + event + "]";
}
}
16 changes: 7 additions & 9 deletions src/jdk.sctp/unix/classes/sun/nio/ch/sctp/AssociationImpl.java
Expand Up @@ -38,15 +38,13 @@ public AssociationImpl(int associationID,

@Override
public String toString() {
StringBuffer sb = new StringBuffer(super.toString());
return sb.append("[associationID:")
.append(associationID())
.append(", maxIn:")
.append(maxInboundStreams())
.append(", maxOut:")
.append(maxOutboundStreams())
.append("]")
.toString();
return super.toString() + "[associationID:" +
associationID() +
", maxIn:" +
maxInboundStreams() +
", maxOut:" +
maxOutboundStreams() +
"]";
}
}

1 comment on commit cb766c5

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.