Skip to content

Commit

Permalink
Refactor EndpointInfo and ConnectionInfo in Java (#3217)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardnormier authored Dec 2, 2024
1 parent f2714f7 commit ea9a2b8
Show file tree
Hide file tree
Showing 34 changed files with 465 additions and 330 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2391,22 +2391,20 @@ private void dispatchException(LocalException ex, int requestCount) {
}

private ConnectionInfo initConnectionInfo() {
// Called in synchronization

if (_state > StateNotInitialized
&& _info != null) // Update the connection information until it's initialized
{
return _info;
}

try {
_info = _transceiver.getInfo();
} catch (LocalException ex) {
_info = new ConnectionInfo();
}
for (ConnectionInfo info = _info; info != null; info = info.underlying) {
info.connectionId = _endpoint.connectionId();
info.adapterName = _adapter != null ? _adapter.getName() : "";
info.incoming = _connector == null;
}
boolean incoming = _connector == null;
_info =
_transceiver.getInfo(
incoming,
_adapter != null ? _adapter.getName() : "",
_endpoint.connectionId());
return _info;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@
package com.zeroc.Ice;

/** Base class providing access to the connection details. */
public class ConnectionInfo implements Cloneable {
public class ConnectionInfo {
/** The information of the underyling transport or null if there's no underlying transport. */
public ConnectionInfo underlying;
public final ConnectionInfo underlying;

/** Whether or not the connection is an incoming or outgoing connection. */
public boolean incoming;
public final boolean incoming;

/** The name of the adapter associated with the connection. */
public String adapterName = "";
public final String adapterName;

/** The connection id. */
public String connectionId = "";
public final String connectionId;

@Override
public ConnectionInfo clone() {
try {
return (ConnectionInfo) super.clone();
} catch (CloneNotSupportedException ex) {
assert false; // impossible
return null;
}
protected ConnectionInfo(ConnectionInfo underlying) {
assert underlying != null;
this.underlying = underlying;
incoming = underlying.incoming;
adapterName = underlying.adapterName;
connectionId = underlying.connectionId;
}

protected ConnectionInfo(boolean incoming, String adapterName, String connectionId) {
underlying = null;
this.incoming = incoming;
this.adapterName = adapterName;
this.connectionId = connectionId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,53 @@
package com.zeroc.Ice;

/** Base class providing access to the endpoint details. */
public abstract class EndpointInfo implements Cloneable {
public class EndpointInfo {
/** The information of the underlying endpoint or null if there's no underlying endpoint. */
public EndpointInfo underlying;
public final EndpointInfo underlying;

/** The timeout for the endpoint in milliseconds. 0 means non-blocking, -1 means no timeout. */
public int timeout;
public final int timeout;

/**
* Specifies whether or not compression should be used if available when using this endpoint.
*/
public boolean compress;
public final boolean compress;

/**
* Returns the type of the endpoint.
*
* @return The endpoint type.
*/
public abstract short type();
public short type() {
return underlying != null ? underlying.type() : -1;
}

/**
* Returns true if this endpoint is a datagram endpoint.
*
* @return True for a datagram endpoint.
*/
public abstract boolean datagram();
public boolean datagram() {
return underlying != null ? underlying.datagram() : false;
}

/**
* @return True for a secure endpoint.
*/
public abstract boolean secure();

@Override
public EndpointInfo clone() {
try {
return (EndpointInfo) super.clone();
} catch (CloneNotSupportedException ex) {
assert false; // impossible
return null;
}
public boolean secure() {
return underlying != null ? underlying.secure() : false;
}

protected EndpointInfo(EndpointInfo underlying) {
assert underlying != null;
this.underlying = underlying;
this.timeout = underlying.timeout;
this.compress = underlying.compress;
}

protected EndpointInfo(int timeout, boolean compress) {
this.underlying = null;
this.timeout = timeout;
this.compress = compress;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,29 @@
/** Provides access to the connection details of an IP connection */
public class IPConnectionInfo extends ConnectionInfo {
/** The local address. */
public String localAddress = "";
public final String localAddress;

/** The local port. */
public int localPort = -1;
public final int localPort;

/** The remote address. */
public String remoteAddress = "";
public final String remoteAddress;

/** The remote port. */
public int remotePort = -1;
public final int remotePort;

protected IPConnectionInfo(
boolean incoming,
String adapterName,
String connectionId,
String localAddress,
int localPort,
String remoteAddress,
int remotePort) {
super(incoming, adapterName, connectionId);
this.localAddress = localAddress;
this.localPort = localPort;
this.remoteAddress = remoteAddress;
this.remotePort = remotePort;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,6 @@ protected IPEndpointI(ProtocolInstance instance, InputStream s) {
this(instance, s.readString(), s.readInt(), null, "");
}

@Override
public EndpointInfo getInfo() {
IPEndpointInfo info =
new IPEndpointInfo() {
@Override
public short type() {
return IPEndpointI.this.type();
}

@Override
public boolean datagram() {
return IPEndpointI.this.datagram();
}

@Override
public boolean secure() {
return IPEndpointI.this.secure();
}
};
fillEndpointInfo(info);
return info;
}

@Override
public short type() {
return _instance.type();
Expand Down Expand Up @@ -239,14 +216,6 @@ public void streamWriteImpl(OutputStream s) {
s.writeInt(_port);
}

public void fillEndpointInfo(IPEndpointInfo info) {
info.timeout = timeout();
info.compress = compress();
info.host = _host;
info.port = _port;
info.sourceAddress = _sourceAddr == null ? "" : _sourceAddr.getAddress().getHostAddress();
}

void initWithOptions(java.util.ArrayList<String> args, boolean oaEndpoint) {
super.initWithOptions(args);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@
*
* @see Endpoint
*/
public abstract class IPEndpointInfo extends EndpointInfo {
public class IPEndpointInfo extends EndpointInfo {
/** The host or address configured with the endpoint. */
public String host = "";
public final String host;

/** The port number. */
public int port;
public final int port;

/** The source IP address. */
public String sourceAddress = "";
public final String sourceAddress;

protected IPEndpointInfo(
int timeout, boolean compress, String host, int port, String sourceAddress) {
super(timeout, compress);
this.host = host;
this.port = port;
this.sourceAddress = sourceAddress;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public String toDetailedString() {
}

@Override
public ConnectionInfo getInfo() {
return _decoratee.getInfo();
public ConnectionInfo getInfo(boolean incoming, String adapterName, String connectionId) {
return _decoratee.getInfo(incoming, adapterName, connectionId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,7 @@ public void streamWriteImpl(OutputStream s) {
//
@Override
public EndpointInfo getInfo() {
var info =
new OpaqueEndpointInfo() {
@Override
public short type() {
return _type;
}

@Override
public boolean datagram() {
return false;
}

@Override
public boolean secure() {
return false;
}
};

info.rawEncoding = _rawEncoding;
info.rawBytes = _rawBytes;
return info;
return new OpaqueEndpointInfo(_type, _rawEncoding, _rawBytes);
}

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,25 @@
*
* @see Endpoint
*/
public abstract class OpaqueEndpointInfo extends EndpointInfo {
public final class OpaqueEndpointInfo extends EndpointInfo {
/** The encoding version of the opaque endpoint (to decode or encode the rawBytes). */
public EncodingVersion rawEncoding;
public final EncodingVersion rawEncoding;

/** The raw encoding of the opaque endpoint. */
public byte[] rawBytes;
public final byte[] rawBytes;

private final short _type;

@Override
public short type() {
return _type;
}

// internal constructor
OpaqueEndpointInfo(short type, EncodingVersion rawEncoding, byte[] rawBytes) {
super(-1, false);
this._type = type;
this.rawEncoding = rawEncoding;
this.rawBytes = rawBytes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@
/** Provides access to the connection details of an SSL connection */
public final class ConnectionInfo extends com.zeroc.Ice.ConnectionInfo {
/** The negotiated cipher suite. */
public String cipher = "";
public final String cipher;

/** The certificate chain. */
public java.security.cert.Certificate[] certs;
public final java.security.cert.Certificate[] certs;

/** The certificate chain verification status. */
public boolean verified;
public final boolean verified;

// internal constructor
ConnectionInfo(
com.zeroc.Ice.ConnectionInfo underlying,
String cipher,
java.security.cert.Certificate[] certs,
boolean verified) {
super(underlying);
this.cipher = cipher;
this.certs = certs;
this.verified = verified;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,7 @@ public void streamWriteImpl(com.zeroc.Ice.OutputStream s) {
//
@Override
public com.zeroc.Ice.EndpointInfo getInfo() {
var info =
new EndpointInfo() {
@Override
public short type() {
return EndpointI.this.type();
}

@Override
public boolean datagram() {
return EndpointI.this.datagram();
}

@Override
public boolean secure() {
return EndpointI.this.secure();
}
};
info.underlying = _delegate.getInfo();
info.timeout = info.underlying.timeout;
info.compress = info.underlying.compress;
return info;
return new EndpointInfo(_delegate.getInfo());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@
package com.zeroc.Ice.SSL;

/** Provides access to an SSL endpoint information. */
public abstract class EndpointInfo extends com.zeroc.Ice.EndpointInfo {}
public final class EndpointInfo extends com.zeroc.Ice.EndpointInfo {
// internal constructor
EndpointInfo(com.zeroc.Ice.EndpointInfo underlying) {
super(underlying);
}
}
Loading

0 comments on commit ea9a2b8

Please sign in to comment.