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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) 2006-2013 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.callbacks.CallbackInterface;
  24. import java.net.InetSocketAddress;
  25. import java.net.Proxy;
  26. import java.net.ProxySelector;
  27. import java.net.SocketAddress;
  28. import java.net.URI;
  29. import java.net.URISyntaxException;
  30. import java.util.HashMap;
  31. import java.util.List;
  32. import java.util.Map;
  33. /**
  34. * Implements common base functionality for parsers.<p>
  35. * Implementations of this class must be annotated with
  36. * {@link ChildImplementations} to define the implementations to use for
  37. * instances of {@link ClientInfo}, {@link ChannelInfo}, etc.
  38. */
  39. public abstract class BaseParser extends ThreadedParser {
  40. /** The URI that this parser was constructed for. */
  41. private final URI uri;
  42. /** The ignore list for this parser. */
  43. private IgnoreList ignoreList;
  44. /** The ping timer interval for this parser. */
  45. private long pingTimerInterval;
  46. /** The ping timer fraction for this parser. */
  47. private int pingTimerFraction;
  48. /** The cached name of the server this parser is connected to. */
  49. private String serverName;
  50. /** The IP that this parser should bind to. */
  51. private String bindIp;
  52. /** The URI of the proxy to use when connecting, if any. */
  53. private URI proxy;
  54. /** The callback manager to use for this parser. */
  55. private final CallbackManager callbackManager;
  56. /** A map for callers to use to store things for no sane reason. */
  57. private final Map<Object, Object> map = new HashMap<Object, Object>();
  58. /**
  59. * Creates a new base parser for the specified URI.
  60. *
  61. * @param uri The URI this parser will connect to.
  62. */
  63. public BaseParser(final URI uri) {
  64. this.uri = uri;
  65. final Map<Class<?>, Class<?>> implementations
  66. = new HashMap<Class<?>, Class<?>>();
  67. for (Class<?> child : this.getClass().getAnnotation(ChildImplementations.class).value()) {
  68. for (Class<?> iface : child.getInterfaces()) {
  69. implementations.put(iface, child);
  70. }
  71. }
  72. this.callbackManager = new CallbackManager(this, implementations);
  73. }
  74. /** {@inheritDoc} */
  75. @Override
  76. public void quit(final String reason) {
  77. disconnect(reason);
  78. }
  79. /** {@inheritDoc} */
  80. @Override
  81. public URI getURI() {
  82. return uri;
  83. }
  84. /** {@inheritDoc} */
  85. @Override
  86. public URI getProxy() {
  87. if (proxy == null && ProxySelector.getDefault() != null) {
  88. final List<Proxy> proxies = ProxySelector.getDefault().select(getURI());
  89. if (!proxies.isEmpty()) {
  90. final SocketAddress sock = proxies.get(0).address();
  91. if (sock instanceof InetSocketAddress) {
  92. final InetSocketAddress isa = (InetSocketAddress)sock;
  93. try {
  94. return new URI("proxy://", "", isa.getAddress().getHostAddress(), isa.getPort(), "", "", "");
  95. } catch (final URISyntaxException use) { /* Oh well... */ }
  96. }
  97. }
  98. } else {
  99. return proxy;
  100. }
  101. return null;
  102. }
  103. /** {@inheritDoc} */
  104. @Override
  105. public void setProxy(final URI proxy) {
  106. this.proxy = proxy;
  107. }
  108. /** {@inheritDoc} */
  109. @Override
  110. public IgnoreList getIgnoreList() {
  111. return ignoreList;
  112. }
  113. /** {@inheritDoc} */
  114. @Override
  115. public void setIgnoreList(final IgnoreList ignoreList) {
  116. this.ignoreList = ignoreList;
  117. }
  118. /** {@inheritDoc} */
  119. @Override
  120. public long getPingTimerInterval() {
  121. return pingTimerInterval;
  122. }
  123. /** {@inheritDoc} */
  124. @Override
  125. public void setPingTimerInterval(final long newValue) {
  126. pingTimerInterval = newValue;
  127. }
  128. /** {@inheritDoc} */
  129. @Override
  130. public int getPingTimerFraction() {
  131. return pingTimerFraction;
  132. }
  133. /** {@inheritDoc} */
  134. @Override
  135. public void setPingTimerFraction(final int newValue) {
  136. pingTimerFraction = newValue;
  137. }
  138. /** {@inheritDoc} */
  139. @Override
  140. public CallbackManager getCallbackManager() {
  141. return callbackManager;
  142. }
  143. /**
  144. * Gets a proxy object which can be used to despatch callbacks of the
  145. * specified type. Callers may pass <code>null</code> for the first two
  146. * arguments of any callback, and these will automatically be replaced
  147. * by the relevant Parser instance and the current date.
  148. *
  149. * @param <T> The type of the callback to retrieve
  150. * @param callback The callback to retrieve
  151. * @return A proxy object which can be used to call the specified callback
  152. */
  153. protected <T extends CallbackInterface> T getCallback(final Class<T> callback) {
  154. return callbackManager.getCallback(callback);
  155. }
  156. /** {@inheritDoc} */
  157. @Override
  158. public Map<Object, Object> getMap() {
  159. return map;
  160. }
  161. /** {@inheritDoc} */
  162. @Override
  163. public void joinChannel(final String channel) {
  164. joinChannels(new ChannelJoinRequest(channel));
  165. }
  166. /** {@inheritDoc} */
  167. @Override
  168. public void joinChannel(final String channel, final String key) {
  169. joinChannels(new ChannelJoinRequest(channel, key));
  170. }
  171. /** {@inheritDoc} */
  172. @Override
  173. public String getServerName() {
  174. return serverName;
  175. }
  176. /**
  177. * Sets the name of this parser's server.
  178. *
  179. * @param serverName The new name for this parser's server
  180. */
  181. protected void setServerName(final String serverName) {
  182. this.serverName = serverName;
  183. }
  184. /** {@inheritDoc} */
  185. @Override
  186. public String getBindIP() {
  187. return bindIp;
  188. }
  189. /** {@inheritDoc} */
  190. @Override
  191. public void setBindIP(final String ip) {
  192. this.bindIp = ip;
  193. }
  194. }