|
| 1 | +package org.arkecosystem.client; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +public class ClientManager { |
| 7 | + private final Map<String, ArkClient> clients; |
| 8 | + private String defaultClient = "main"; |
| 9 | + |
| 10 | + public ClientManager() { |
| 11 | + this.clients = new HashMap<>(); |
| 12 | + } |
| 13 | + |
| 14 | + /** |
| 15 | + * Get the default client name. |
| 16 | + * |
| 17 | + * @return The name of the default client. |
| 18 | + */ |
| 19 | + public String getDefaultClient() { |
| 20 | + return this.defaultClient; |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Set the default client name. |
| 25 | + * |
| 26 | + * @param name The name of the default client to set. |
| 27 | + */ |
| 28 | + public void setDefaultClient(String name) { |
| 29 | + this.defaultClient = name; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Return all created clients. |
| 34 | + * |
| 35 | + * @return A map of all created clients. |
| 36 | + */ |
| 37 | + public Map<String, ArkClient> getClients() { |
| 38 | + return this.clients; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Connect to a given client. |
| 43 | + * |
| 44 | + * @param host The host URL for the client. |
| 45 | + * @param name The name to assign to this client instance. |
| 46 | + * @return The newly created ArkClient instance. |
| 47 | + */ |
| 48 | + public ArkClient connect(String host, String name) { |
| 49 | + if (this.clients.containsKey(name)) { |
| 50 | + throw new IllegalArgumentException("Client [" + name + "] is already configured."); |
| 51 | + } |
| 52 | + |
| 53 | + this.clients.put(name, new ArkClient(host)); |
| 54 | + |
| 55 | + return this.clients.get(name); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Connect to the default client. |
| 60 | + * |
| 61 | + * @param host The host URL for the client. |
| 62 | + * @return The newly created ArkClient instance. |
| 63 | + */ |
| 64 | + public ArkClient connect(String host) { |
| 65 | + return connect(host, "main"); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Disconnect from a given client. |
| 70 | + * |
| 71 | + * @param name The name of the client to disconnect. |
| 72 | + */ |
| 73 | + public void disconnect(String name) { |
| 74 | + if (name == null || name.isEmpty()) { |
| 75 | + name = getDefaultClient(); |
| 76 | + } |
| 77 | + |
| 78 | + this.clients.remove(name); |
| 79 | + } |
| 80 | + |
| 81 | + /** Disconnect from the default client. */ |
| 82 | + public void disconnect() { |
| 83 | + disconnect(null); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Get a client instance by name. |
| 88 | + * |
| 89 | + * @param name The name of the client to retrieve. |
| 90 | + * @return The corresponding ArkClient instance. |
| 91 | + */ |
| 92 | + public ArkClient client(String name) { |
| 93 | + if (name == null || name.isEmpty()) { |
| 94 | + name = getDefaultClient(); |
| 95 | + } |
| 96 | + |
| 97 | + if (!this.clients.containsKey(name)) { |
| 98 | + throw new IllegalArgumentException("Client [" + name + "] not configured."); |
| 99 | + } |
| 100 | + |
| 101 | + return this.clients.get(name); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Get the default client instance. |
| 106 | + * |
| 107 | + * @return The default ArkClient instance. |
| 108 | + */ |
| 109 | + public ArkClient client() { |
| 110 | + return client(null); |
| 111 | + } |
| 112 | +} |
0 commit comments