You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BaseParser.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.parser.common;
  23. import com.dmdirc.parser.interfaces.ChannelInfo;
  24. import com.dmdirc.parser.interfaces.ClientInfo;
  25. import com.dmdirc.parser.interfaces.callbacks.CallbackInterface;
  26. import java.net.InetSocketAddress;
  27. import java.net.Proxy;
  28. import java.net.ProxySelector;
  29. import java.net.SocketAddress;
  30. import java.net.URI;
  31. import java.net.URISyntaxException;
  32. import java.util.HashMap;
  33. import java.util.List;
  34. import java.util.Map;
  35. /**
  36. * Implements common base functionality for parsers.
  37. * <p>
  38. * Implementations of this class must be annotated with {@link ChildImplementations} to define
  39. * the implementations to use for instances of {@link ClientInfo}, {@link ChannelInfo}, etc.
  40. */
  41. public abstract class BaseParser extends ThreadedParser {
  42. /** The URI that this parser was constructed for. */
  43. private final URI uri;
  44. /** The ignore list for this parser. */
  45. private IgnoreList ignoreList;
  46. /** The ping timer interval for this parser. */
  47. private long pingTimerInterval;
  48. /** The ping timer fraction for this parser. */
  49. private int pingTimerFraction;
  50. /** The cached name of the server this parser is connected to. */
  51. private String serverName;
  52. /** The IP that this parser should bind to. */
  53. private String bindIp;
  54. /** The IPv6 IP that this parser should bind to. */
  55. private String bindIpv6;
  56. /** The URI of the proxy to use when connecting, if any. */
  57. private URI proxy;
  58. /** The callback manager to use for this parser. */
  59. private final CallbackManager callbackManager;
  60. /**
  61. * Creates a new base parser for the specified URI.
  62. *
  63. * @param uri The URI this parser will connect to.
  64. */
  65. public BaseParser(final URI uri) {
  66. this.uri = uri;
  67. final Map<Class<?>, Class<?>> implementations = new HashMap<>();
  68. for (Class<?> child : getClass().getAnnotation(ChildImplementations.class).value()) {
  69. for (Class<?> iface : child.getInterfaces()) {
  70. implementations.put(iface, child);
  71. }
  72. }
  73. callbackManager = new CallbackManager(implementations);
  74. callbackManager.initialise(this);
  75. }
  76. @Override
  77. public void quit(final String reason) {
  78. disconnect(reason);
  79. }
  80. @Override
  81. public URI getURI() {
  82. return uri;
  83. }
  84. @Override
  85. public URI getProxy() {
  86. if (proxy == null && ProxySelector.getDefault() != null) {
  87. final List<Proxy> proxies = ProxySelector.getDefault().select(getURI());
  88. if (!proxies.isEmpty()) {
  89. final SocketAddress sock = proxies.get(0).address();
  90. if (sock instanceof InetSocketAddress) {
  91. final InetSocketAddress isa = (InetSocketAddress)sock;
  92. try {
  93. return new URI("proxy://", "", isa.getAddress().getHostAddress(), isa.getPort(), "", "", "");
  94. } catch (final URISyntaxException use) { /* Oh well... */ }
  95. }
  96. }
  97. } else {
  98. return proxy;
  99. }
  100. return null;
  101. }
  102. @Override
  103. public void setProxy(final URI proxy) {
  104. this.proxy = proxy;
  105. }
  106. @Override
  107. public IgnoreList getIgnoreList() {
  108. return ignoreList;
  109. }
  110. @Override
  111. public void setIgnoreList(final IgnoreList ignoreList) {
  112. this.ignoreList = ignoreList;
  113. }
  114. @Override
  115. public long getPingTimerInterval() {
  116. return pingTimerInterval;
  117. }
  118. @Override
  119. public void setPingTimerInterval(final long newValue) {
  120. pingTimerInterval = newValue;
  121. }
  122. @Override
  123. public int getPingTimerFraction() {
  124. return pingTimerFraction;
  125. }
  126. @Override
  127. public void setPingTimerFraction(final int newValue) {
  128. pingTimerFraction = newValue;
  129. }
  130. @Override
  131. public CallbackManager getCallbackManager() {
  132. return callbackManager;
  133. }
  134. /**
  135. * Gets a proxy object which can be used to despatch callbacks of the
  136. * specified type. Callers may pass <code>null</code> for the first two
  137. * arguments of any callback, and these will automatically be replaced
  138. * by the relevant Parser instance and the current date.
  139. *
  140. * @param <T> The type of the callback to retrieve
  141. * @param callback The callback to retrieve
  142. * @return A proxy object which can be used to call the specified callback
  143. */
  144. protected <T extends CallbackInterface> T getCallback(final Class<T> callback) {
  145. return callbackManager.getCallback(callback);
  146. }
  147. @Override
  148. public void joinChannel(final String channel) {
  149. joinChannels(new ChannelJoinRequest(channel));
  150. }
  151. @Override
  152. public void joinChannel(final String channel, final String key) {
  153. joinChannels(new ChannelJoinRequest(channel, key));
  154. }
  155. @Override
  156. public String getServerName() {
  157. return serverName;
  158. }
  159. /**
  160. * Sets the name of this parser's server.
  161. *
  162. * @param serverName The new name for this parser's server
  163. */
  164. protected void setServerName(final String serverName) {
  165. this.serverName = serverName;
  166. }
  167. @Override
  168. public String getBindIP() {
  169. return bindIp;
  170. }
  171. @Override
  172. public void setBindIP(final String ip) {
  173. this.bindIp = ip;
  174. }
  175. @Override
  176. public String getBindIPv6() {
  177. return bindIpv6;
  178. }
  179. @Override
  180. public void setBindIPv6(final String ip) {
  181. this.bindIpv6 = ip;
  182. }
  183. }