Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

DCC.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.addons.dcc;
  23. import java.net.Socket;
  24. import java.net.ServerSocket;
  25. import java.net.InetAddress;
  26. import java.net.UnknownHostException;
  27. import java.io.IOException;
  28. /**
  29. * This class handles the main "grunt work" of DCC, subclasses process the data
  30. * received by this class.
  31. *
  32. * @author Shane 'Dataforce' McCormack
  33. * @version $Id: DCC.java 969 2007-04-30 18:38:20Z ShaneMcC $
  34. */
  35. public abstract class DCC implements Runnable {
  36. /** Address */
  37. protected long address = 0;
  38. /** Port */
  39. protected int port = 0;
  40. /** Socket used to communicate with */
  41. protected Socket socket;
  42. /** The Thread in use for this */
  43. private volatile Thread myThread = null;
  44. /** The current socket in use is this is a listen socket */
  45. private ServerSocket serverSocket;
  46. /** Are we already running? */
  47. protected boolean running = false;
  48. /** Are we a listen socket? */
  49. protected boolean listen = false;
  50. /**
  51. * Creates a new instance of DCC.
  52. */
  53. public DCC() {
  54. super();
  55. }
  56. /**
  57. * Connect this dcc.
  58. */
  59. public void connect() {
  60. try {
  61. if (listen) {
  62. // serverSocket = new ServerSocket(0, 1, InetAddress.getByName(longToIP(bindIP)));
  63. serverSocket = new ServerSocket(0, 1, InetAddress.getLocalHost());
  64. address = ipToLong(InetAddress.getLocalHost().getHostAddress());
  65. port = serverSocket.getLocalPort();
  66. } else {
  67. // socket = new Socket(longToIP(address), port, bindIP, 0);
  68. socket = new Socket(longToIP(address), port);
  69. socketOpened();
  70. }
  71. } catch (UnknownHostException uhe) {
  72. return;
  73. } catch (IOException uhe) {
  74. return;
  75. }
  76. myThread = new Thread(this);
  77. myThread.start();
  78. }
  79. /**
  80. * Start a listen socket rather than a connect socket.
  81. */
  82. public void listen() {
  83. listen = true;
  84. connect();
  85. }
  86. /**
  87. * This handles the socket to keep it out of the main thread
  88. */
  89. public void run() {
  90. if (running) { return; }
  91. running = true;
  92. // handleSocket is implemented by sub classes, and should return false
  93. // when the socket is closed.
  94. Thread thisThread = Thread.currentThread();
  95. while (myThread == thisThread) {
  96. if (serverSocket != null) {
  97. try {
  98. socket = serverSocket.accept();
  99. serverSocket.close();
  100. socketOpened();
  101. } catch (IOException ioe) {
  102. break;
  103. }
  104. serverSocket = null;
  105. } else {
  106. if (!handleSocket()) {
  107. close();
  108. break;
  109. }
  110. }
  111. }
  112. // Socket closed
  113. thisThread = null;
  114. running = false;
  115. }
  116. /**
  117. * Called to close the socket
  118. */
  119. protected void close() {
  120. if (serverSocket != null) {
  121. try {
  122. if (!serverSocket.isClosed()) {
  123. serverSocket.close();
  124. }
  125. } catch (IOException ioe) { }
  126. serverSocket = null;
  127. }
  128. if (socket != null) {
  129. try {
  130. if (!socket.isClosed()) {
  131. socket.close();
  132. }
  133. } catch (IOException ioe) { }
  134. socketClosed();
  135. socket = null;
  136. }
  137. }
  138. /**
  139. * Called when the socket is first opened, before any data is handled.
  140. */
  141. protected void socketOpened() { }
  142. /**
  143. * Called when the socket is closed, before the thread terminates.
  144. */
  145. protected void socketClosed() { }
  146. /**
  147. * Check if this socket can be written to
  148. */
  149. public boolean isWriteable() {
  150. return false;
  151. }
  152. /**
  153. * Handle the socket.
  154. *
  155. * @return false when socket is closed, true will cause the method to be
  156. * called again.
  157. */
  158. protected abstract boolean handleSocket();
  159. /**
  160. * Set the address to connect to for this DCC
  161. *
  162. * @param address Address as an int (Network Byte Order, as specified in the DCC CTCP)
  163. * @param port Port to connect to
  164. */
  165. public void setAddress(final long address, final int port) {
  166. this.address = address;
  167. this.port = port;
  168. }
  169. /**
  170. * Is this a listening socket
  171. *
  172. * @return True if this is a listening socket
  173. */
  174. public boolean isListenSocket() {
  175. return listen;
  176. }
  177. /**
  178. * Get the host this socket is listening on/connecting to
  179. *
  180. * @return The IP that this socket is listening on/connecting to.
  181. */
  182. public String getHost() {
  183. return longToIP(address);
  184. }
  185. /**
  186. * Get the port this socket is listening on/connecting to
  187. *
  188. * @return The port that this socket is listening on/connecting to.
  189. */
  190. public int getPort() {
  191. return port;
  192. }
  193. /**
  194. * Convert the given IP Address to a long
  195. *
  196. * @param ip Input IP Address
  197. * @return ip as a long
  198. */
  199. public static long ipToLong(final String ip) {
  200. String bits[] = ip.split("\\.");
  201. if (bits.length > 3) {
  202. return (Long.parseLong(bits[0]) << 24) + (Long.parseLong(bits[1]) << 16) + (Long.parseLong(bits[2]) << 8) + Long.parseLong(bits[3]);
  203. }
  204. return 0;
  205. }
  206. /**
  207. * Convert the given long to an IP Address
  208. *
  209. * @param in Input long
  210. * @return long as an IP
  211. */
  212. public static String longToIP(final long in) {
  213. return ((in & 0xff000000) >> 24)+"."+((in & 0x00ff0000) >> 16)+"."+((in & 0x0000ff00) >> 8)+"."+(in & 0x000000ff);
  214. }
  215. }