Skip to content

Commit

Permalink
Remove clunky initialize method from Client interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mbax committed Dec 23, 2024
1 parent 9ca2942 commit b15f931
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 182 deletions.
54 changes: 0 additions & 54 deletions src/main/java/org/kitteh/irc/client/library/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -888,60 +888,6 @@ interface WithManagement extends Client {
*/
void setServerAddress(@NonNull HostWithPort address);

/**
* Initialize with pre-connection information.
*
* @param name name
* @param networkHandler networkHandler
* @param serverHostWithPort serverHostWithPort
* @param serverPassword serverPassword
* @param bindAddress bindAddress
* @param proxyAddress proxyAddress
* @param proxyType proxyType
* @param nick nick
* @param userString userString
* @param realName realName
* @param actorTracker actorTracker
* @param authManager authManager
* @param capabilityManager capabilityManager
* @param eventManager eventManager
* @param listenerSuppliers listenerSuppliers
* @param messageTagManager messageTagManager
* @param iSupportManager iSupportManager
* @param defaultMessageMap defaultMessageMap
* @param messageSendingQueue messageSendingQueue
* @param serverInfo serverInfo
* @param exceptionListener exceptionListener
* @param inputListener inputListener
* @param outputListener outputListener
* @param secure secure
* @param secureKeyCertChain secureKeyCertChain
* @param secureKey secureKey
* @param secureKeyPassword secureKeyPassword
* @param trustManagerFactory trustManagerFactory
* @param stsStorageManager stsStorageManager
* @param webircHost webircHost
* @param webircIP webircIP
* @param webircPassword webircPassword
* @param webircUser webircUser
*/
void initialize(@NonNull String name, @NonNull NetworkHandler networkHandler,
@NonNull HostWithPort serverHostWithPort, @Nullable String serverPassword,
@Nullable InetSocketAddress bindAddress,
@Nullable HostWithPort proxyAddress, @Nullable ProxyType proxyType,
@NonNull String nick, @NonNull String userString, @NonNull String realName, @NonNull ActorTracker actorTracker,
@NonNull AuthManager authManager, CapabilityManager.@NonNull WithManagement capabilityManager,
@NonNull EventManager eventManager, @NonNull List<EventListenerSupplier> listenerSuppliers,
@NonNull MessageTagManager messageTagManager,
@NonNull ISupportManager iSupportManager, @Nullable DefaultMessageMap defaultMessageMap,
@NonNull Function<Client.WithManagement, ? extends MessageSendingQueue> messageSendingQueue,
@NonNull Function<Client.WithManagement, ? extends ServerInfo.WithManagement> serverInfo,
@Nullable Consumer<Exception> exceptionListener, @Nullable Consumer<String> inputListener,
@Nullable Consumer<String> outputListener, boolean secure, @Nullable Path secureKeyCertChain,
@Nullable Path secureKey, @Nullable String secureKeyPassword, @Nullable TrustManagerFactory trustManagerFactory,
@Nullable StsStorageManager stsStorageManager, @Nullable String webircHost,
@Nullable InetAddress webircIP, @Nullable String webircPassword, @Nullable String webircUser);

/**
* Sets the client's user modes.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,52 +340,52 @@ private class ManagementImpl implements Management {
private static final Function<Client.WithManagement, ? extends MessageTagManager> DEFAULT_MESSAGE_TAG_MANAGER = DefaultMessageTagManager::new;
private static final Function<Client.WithManagement, ? extends ServerInfo.WithManagement> DEFAULT_SERVER_INFO = DefaultServerInfo::new;

private String name = "Unnamed";
String name = "Unnamed";

private @Nullable String bindHost;
private int bindPort;
@Nullable String bindHost;
int bindPort;

private HostWithPort serverHostWithPort = HostWithPort.of(DefaultBuilder.DEFAULT_SERVER_HOST, DefaultBuilder.DEFAULT_SERVER_PORT);
private @Nullable String serverPassword = null;
private boolean secure = true;
private @Nullable Path secureKeyCertChain = null;
private @Nullable Path secureKey = null;
private @Nullable String secureKeyPassword = null;
private @Nullable TrustManagerFactory secureTrustManagerFactory = null;
HostWithPort serverHostWithPort = HostWithPort.of(DefaultBuilder.DEFAULT_SERVER_HOST, DefaultBuilder.DEFAULT_SERVER_PORT);
@Nullable String serverPassword = null;
boolean secure = true;
@Nullable Path secureKeyCertChain = null;
@Nullable Path secureKey = null;
@Nullable String secureKeyPassword = null;
@Nullable TrustManagerFactory secureTrustManagerFactory = null;

private String nick = "Kitteh";
private String userString = "Kitteh";
private String realName = "KICL " + Version.getVersion() + " - kitteh.org";
String nick = "Kitteh";
String userString = "Kitteh";
String realName = "KICL " + Version.getVersion() + " - kitteh.org";

// Listeners
private @Nullable Consumer<Exception> exceptionListener = Throwable::printStackTrace;
private @Nullable Consumer<String> inputListener = null;
private @Nullable Consumer<String> outputListener = null;
@Nullable Consumer<Exception> exceptionListener = Throwable::printStackTrace;
@Nullable Consumer<String> inputListener = null;
@Nullable Consumer<String> outputListener = null;

// Proxy
private @Nullable String proxyHost;
private int proxyPort;
private @Nullable ProxyType proxyType;
@Nullable String proxyHost;
int proxyPort;
@Nullable ProxyType proxyType;

// WebIRC
private @Nullable String webircHost = null;
private @Nullable InetAddress webircIP = null;
private @Nullable String webircPassword = null;
private @Nullable String webircGateway = null;
@Nullable String webircHost = null;
@Nullable InetAddress webircIP = null;
@Nullable String webircPassword = null;
@Nullable String webircGateway = null;

// Management
private Function<Client.WithManagement, ? extends ActorTracker> actorTracker = DefaultBuilder.DEFAULT_ACTOR_TRACKER;
private Function<Client.WithManagement, ? extends AuthManager> authManager = DefaultBuilder.DEFAULT_AUTH_MANAGER;
private Function<Client.WithManagement, ? extends CapabilityManager.WithManagement> capabilityManager = DefaultBuilder.DEFAULT_CAPABILITY_MANAGER;
private @Nullable DefaultMessageMap defaultMessageMap = null;
private Function<Client.WithManagement, ? extends EventManager> eventManager = DefaultBuilder.DEFAULT_EVENT_MANAGER;
private List<EventListenerSupplier> eventListeners = DefaultBuilder.DEFAULT_EVENT_LISTENERS;
private Function<Client.WithManagement, ? extends ISupportManager> iSupportManager = DefaultBuilder.DEFAULT_ISUPPORT_MANAGER;
private Function<Client.WithManagement, ? extends MessageSendingQueue> messageSendingQueue = DefaultBuilder.DEFAULT_MESSAGE_SENDING_QUEUE;
private Function<Client.WithManagement, ? extends MessageTagManager> messageTagManager = DefaultBuilder.DEFAULT_MESSAGE_TAG_MANAGER;
private NetworkHandler networkHandler = NettyNetworkHandler.getInstance();
private Function<Client.WithManagement, ? extends ServerInfo.WithManagement> serverInfo = DefaultBuilder.DEFAULT_SERVER_INFO;
private @Nullable StsStorageManager stsStorageManager = null;
Function<Client.WithManagement, ? extends ActorTracker> actorTracker = DefaultBuilder.DEFAULT_ACTOR_TRACKER;
Function<Client.WithManagement, ? extends AuthManager> authManager = DefaultBuilder.DEFAULT_AUTH_MANAGER;
Function<Client.WithManagement, ? extends CapabilityManager.WithManagement> capabilityManager = DefaultBuilder.DEFAULT_CAPABILITY_MANAGER;
@Nullable DefaultMessageMap defaultMessageMap = null;
Function<Client.WithManagement, ? extends EventManager> eventManager = DefaultBuilder.DEFAULT_EVENT_MANAGER;
List<EventListenerSupplier> eventListeners = DefaultBuilder.DEFAULT_EVENT_LISTENERS;
Function<Client.WithManagement, ? extends ISupportManager> iSupportManager = DefaultBuilder.DEFAULT_ISUPPORT_MANAGER;
Function<Client.WithManagement, ? extends MessageSendingQueue> messageSendingQueue = DefaultBuilder.DEFAULT_MESSAGE_SENDING_QUEUE;
Function<Client.WithManagement, ? extends MessageTagManager> messageTagManager = DefaultBuilder.DEFAULT_MESSAGE_TAG_MANAGER;
NetworkHandler networkHandler = NettyNetworkHandler.getInstance();
Function<Client.WithManagement, ? extends ServerInfo.WithManagement> serverInfo = DefaultBuilder.DEFAULT_SERVER_INFO;
@Nullable StsStorageManager stsStorageManager = null;

@Override
public @NonNull DefaultBuilder name(@NonNull String name) {
Expand Down Expand Up @@ -447,28 +447,8 @@ private class ManagementImpl implements Management {

@Override
public @NonNull Client build() {
if (this.stsStorageManager != null) {
Sanity.truthiness(!SslUtil.isInsecure(this.secureTrustManagerFactory), "Cannot use STS with an insecure trust manager.");
}

HostWithPort proxyAddress = null;
if ((this.proxyHost != null) && (this.proxyPort > 0)) {
proxyAddress = HostWithPort.of(this.proxyHost, this.proxyPort);
}
Client.WithManagement client = new DefaultClient();
client.initialize(this.name, this.networkHandler,
this.serverHostWithPort, this.serverPassword,
this.getInetSocketAddress(this.bindHost, this.bindPort),
proxyAddress, this.proxyType,
this.nick, this.userString, this.realName,
this.actorTracker.apply(client),
this.authManager.apply(client), this.capabilityManager.apply(client), this.eventManager.apply(client),
this.eventListeners, this.messageTagManager.apply(client),
this.iSupportManager.apply(client), this.defaultMessageMap, this.messageSendingQueue,
this.serverInfo, this.exceptionListener, this.inputListener, this.outputListener, this.secure,
this.secureKeyCertChain, this.secureKey, this.secureKeyPassword, this.secureTrustManagerFactory, this.stsStorageManager,
this.webircHost, this.webircIP, this.webircPassword, this.webircGateway
);
DefaultClient client = new DefaultClient(this);
client.initialize(this);

return client;
}
Expand All @@ -485,7 +465,7 @@ private class ManagementImpl implements Management {
return new ToStringer(this).toString();
}

private InetSocketAddress getInetSocketAddress(@Nullable String host, int port) {
InetSocketAddress getInetSocketAddress(@Nullable String host, int port) {
if (host != null) {
return new InetSocketAddress(host, port);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,69 +241,65 @@ protected void processElement(@NonNull String element) {

/**
* Creates a new default client.
*
* @param builder builder
*/
public DefaultClient() {

}

@Override
public void initialize(@NonNull String name, @NonNull NetworkHandler networkHandler,
@NonNull HostWithPort serverAddress, @Nullable String serverPassword,
@Nullable InetSocketAddress bindAddress,
@Nullable HostWithPort proxyAddress, @Nullable ProxyType proxyType,
@NonNull String nick, @NonNull String userString, @NonNull String realName, @NonNull ActorTracker actorTracker,
@NonNull AuthManager authManager, CapabilityManager.@NonNull WithManagement capabilityManager,
@NonNull EventManager eventManager, @NonNull List<EventListenerSupplier> listenerSuppliers,
@NonNull MessageTagManager messageTagManager,
@NonNull ISupportManager iSupportManager, @Nullable DefaultMessageMap defaultMessageMap,
@NonNull Function<Client.WithManagement, ? extends MessageSendingQueue> messageSendingQueue,
@NonNull Function<Client.WithManagement, ? extends ServerInfo.WithManagement> serverInfo,
@Nullable Consumer<Exception> exceptionListener, @Nullable Consumer<String> inputListener,
@Nullable Consumer<String> outputListener, boolean secure, @Nullable Path secureKeyCertChain,
@Nullable Path secureKey, @Nullable String secureKeyPassword, @Nullable TrustManagerFactory trustManagerFactory,
@Nullable StsStorageManager stsStorageManager, @Nullable String webircHost,
@Nullable InetAddress webircIP, @Nullable String webircPassword, @Nullable String webircGateway) {
this.name = name;
DefaultClient(DefaultBuilder builder) {
this.processor = new InputProcessor();
this.messageSendingImmediate = new QueueProcessingThreadSender(this, "Immediate");
this.networkHandler = networkHandler;
this.serverAddress = serverAddress;
this.proxyAddress = proxyAddress;
this.proxyType = proxyType;
this.serverPassword = serverPassword;
this.bindAddress = bindAddress;
this.currentNick = this.requestedNick = this.goalNick = nick;
this.userString = userString;
this.realName = realName;
this.actorTracker = actorTracker;
this.authManager = authManager;
this.capabilityManager = capabilityManager;
this.eventManager = eventManager;
this.messageTagManager = messageTagManager;
this.iSupportManager = iSupportManager;
this.defaultMessageMap = (defaultMessageMap == null) ? new SimpleDefaultMessageMap() : defaultMessageMap;
this.messageSendingQueueSupplier = messageSendingQueue;
this.serverInfoSupplier = serverInfo;
this.exceptionListener = new Listener<>(this, exceptionListener);
this.inputListener = new Listener<>(this, inputListener);
this.outputListener = new Listener<>(this, outputListener);
this.secure = secure;
this.secureKeyCertChain = secureKeyCertChain;
this.secureKey = secureKey;
this.secureKeyPassword = secureKeyPassword;
this.secureTrustManagerFactory = trustManagerFactory;
this.stsStorageManager = stsStorageManager;
this.webircHost = webircHost;
this.webircIP = webircIP;
this.webircPassword = webircPassword;
this.webircGateway = webircGateway;

for (EventListenerSupplier eventListenerSupplier : listenerSuppliers) {
this.eventManager.registerEventListener(eventListenerSupplier.getConstructingFunction().apply(this));
}

this.serverInfo = this.serverInfoSupplier.apply(this);
this.name = builder.name;

this.networkHandler = builder.networkHandler;
this.serverAddress = builder.serverHostWithPort;
this.serverPassword = builder.serverPassword;
this.bindAddress = builder.getInetSocketAddress(builder.bindHost, builder.bindPort);
this.proxyAddress = ((builder.proxyHost != null) && (builder.proxyPort > 0)) ? HostWithPort.of(builder.proxyHost, builder.proxyPort) : null;
this.proxyType = builder.proxyType;

this.currentNick = this.requestedNick = this.goalNick = builder.nick;
this.userString = builder.userString;
this.realName = builder.realName;

this.defaultMessageMap = (builder.defaultMessageMap == null) ? new SimpleDefaultMessageMap() : builder.defaultMessageMap;

this.messageSendingQueueSupplier = builder.messageSendingQueue;

this.serverInfoSupplier = builder.serverInfo;

this.exceptionListener = new Listener<>(this, builder.exceptionListener);
this.inputListener = new Listener<>(this, builder.inputListener);
this.outputListener = new Listener<>(this, builder.outputListener);

this.secure = builder.secure;
this.secureKeyCertChain = builder.secureKeyCertChain;
this.secureKey = builder.secureKey;
this.secureKeyPassword = builder.secureKeyPassword;
this.secureTrustManagerFactory = builder.secureTrustManagerFactory;
this.stsStorageManager = builder.stsStorageManager;

this.webircHost = builder.webircHost;
this.webircIP = builder.webircIP;
this.webircPassword = builder.webircPassword;
this.webircGateway = builder.webircGateway;
}

/**
* Call initialization methods that require a fully constructed Client.
*
* @param builder builder, same as sent to constructor
*/
void initialize(@NonNull DefaultBuilder builder) {
this.actorTracker = builder.actorTracker.apply(this);
this.authManager = builder.authManager.apply(this);
this.capabilityManager = builder.capabilityManager.apply(this);
this.eventManager = builder.eventManager.apply(this);
this.messageTagManager = builder.messageTagManager.apply(this);
this.iSupportManager = builder.iSupportManager.apply(this);
this.serverInfo = this.serverInfoSupplier.apply(this);
for (EventListenerSupplier eventListenerSupplier : builder.eventListeners) {
this.eventManager.registerEventListener(eventListenerSupplier.getConstructingFunction().apply(this));
}
if (this.stsStorageManager != null) {
this.configureSts();
} else if (!this.isSecureConnection()) {
Expand Down
Loading

0 comments on commit b15f931

Please sign in to comment.