Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

DCCChat.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.io.BufferedReader;
  24. import java.io.IOException;
  25. import java.io.InputStreamReader;
  26. import java.io.PrintWriter;
  27. /**
  28. * This class handles a DCC Chat
  29. *
  30. * @author Shane 'Dataforce' McCormack
  31. */
  32. public class DCCChat extends DCC {
  33. /** The handler for this DCCChat. */
  34. private DCCChatInterface handler = null;
  35. /** Used to send data out the socket. */
  36. private PrintWriter out;
  37. /** Used to read data from the socket. */
  38. private BufferedReader in;
  39. /**
  40. * Creates a new instance of DCCChat.
  41. */
  42. public DCCChat() {
  43. super();
  44. }
  45. /**
  46. * Change the handler for this DCC Chat.
  47. *
  48. * @param handler A class implementing DCCChatInterface
  49. */
  50. public void setHandler(final DCCChatInterface handler) {
  51. this.handler = handler;
  52. }
  53. /**
  54. * Called when the socket is first opened, before any data is handled.
  55. */
  56. @Override
  57. protected void socketOpened() {
  58. try {
  59. out = new PrintWriter(socket.getOutputStream(), true);
  60. in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  61. if (handler != null) {
  62. handler.socketOpened(this);
  63. }
  64. } catch (IOException ioe) { }
  65. }
  66. /**
  67. * Called when the socket is closed, before the thread terminates.
  68. */
  69. @Override
  70. protected void socketClosed() {
  71. out = null;
  72. in = null;
  73. if (handler != null) {
  74. handler.socketClosed(this);
  75. }
  76. }
  77. /**
  78. * Handle the socket.
  79. *
  80. * @return false when socket is closed, true will cause the method to be
  81. * called again.
  82. */
  83. @Override
  84. protected boolean handleSocket() {
  85. if (out == null || in == null) { return false; }
  86. final String inLine;
  87. try {
  88. inLine = in.readLine();
  89. if (inLine == null) {
  90. return false;
  91. } else {
  92. if (handler != null) {
  93. handler.handleChatMessage(this, inLine);
  94. }
  95. return true;
  96. }
  97. } catch (IOException e) {
  98. return false;
  99. }
  100. }
  101. /**
  102. * Check if this socket can be written to.
  103. */
  104. @Override
  105. public boolean isWriteable() {
  106. return out != null;
  107. }
  108. /**
  109. * Send a line out the socket.
  110. *
  111. * @param line The line to be sent
  112. */
  113. public void sendLine(final String line) {
  114. if (out != null) {
  115. out.printf("%s\r\n", line);
  116. }
  117. }
  118. }