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.

DCCChat.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2006-2010 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. socketClosed();
  66. }
  67. }
  68. /**
  69. * Called when the socket is closed, before the thread terminates.
  70. */
  71. @Override
  72. protected void socketClosed() {
  73. out = null;
  74. in = null;
  75. if (handler != null) {
  76. handler.socketClosed(this);
  77. }
  78. }
  79. /**
  80. * Handle the socket.
  81. *
  82. * @return false when socket is closed, true will cause the method to be
  83. * called again.
  84. */
  85. @Override
  86. protected boolean handleSocket() {
  87. if (out == null || in == null) {
  88. return false;
  89. }
  90. final String inLine;
  91. try {
  92. inLine = in.readLine();
  93. if (inLine == null) {
  94. return false;
  95. } else {
  96. if (handler != null) {
  97. handler.handleChatMessage(this, inLine);
  98. }
  99. return true;
  100. }
  101. } catch (IOException e) {
  102. return false;
  103. }
  104. }
  105. /**
  106. * Check if this socket can be written to.
  107. */
  108. @Override
  109. public boolean isWriteable() {
  110. return out != null;
  111. }
  112. /**
  113. * Send a line out the socket.
  114. *
  115. * @param line The line to be sent
  116. */
  117. public void sendLine(final String line) {
  118. if (out != null) {
  119. out.printf("%s\r\n", line);
  120. }
  121. }
  122. }