AsynchRequest.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (C) 2006-2010 Alfresco Software Limited.
  3. *
  4. * This file is part of Alfresco
  5. *
  6. * Alfresco is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Alfresco is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package org.alfresco.jlan.client;
  20. /**
  21. * Asynchronous Request Class
  22. *
  23. * <p>Abstract class used to track the details of an asynchronous SMB/CIFS request where the request is sent to the server but no
  24. * reply is received until a particular event occurs on the server, such as a directory change notification.
  25. *
  26. * @author gkspencer
  27. */
  28. public abstract class AsynchRequest {
  29. // Multiplex id that uniquely identifies this request
  30. private int m_id;
  31. // Request name
  32. private String m_name;
  33. // Asynchronous request completed flag
  34. private boolean m_completed;
  35. // Auto-reset flag, used for asynchronous requests that need to be setup again each they have completed
  36. private boolean m_autoReset;
  37. /**
  38. * Class constructor
  39. *
  40. * @param mid int
  41. */
  42. protected AsynchRequest(int mid) {
  43. m_id = mid;
  44. }
  45. /**
  46. * Class constructor
  47. *
  48. * @param mid int
  49. * @param name String
  50. */
  51. protected AsynchRequest(int mid, String name) {
  52. m_id = mid;
  53. m_name = name;
  54. }
  55. /**
  56. * Get the request id
  57. *
  58. * @return int
  59. */
  60. public final int getId() {
  61. return m_id;
  62. }
  63. /**
  64. * Return the request name
  65. *
  66. * @return String
  67. */
  68. public final String getName() {
  69. return m_name != null ? m_name : "";
  70. }
  71. /**
  72. * Check if the asynchronous request has completed
  73. *
  74. * @return boolean
  75. */
  76. public final boolean hasCompleted() {
  77. return m_completed;
  78. }
  79. /**
  80. * Check if the request should be automatically reset
  81. *
  82. * @return boolean
  83. */
  84. public final boolean hasAutoReset() {
  85. return m_autoReset;
  86. }
  87. /**
  88. * Enable/disable auto-reset of the request
  89. *
  90. * @param auto boolean
  91. */
  92. public final void setAutoReset(boolean auto) {
  93. m_autoReset = auto;
  94. }
  95. /**
  96. * Process the asynchronous response packet for this request
  97. *
  98. * @param sess Session
  99. * @param pkt SMBPacket
  100. */
  101. protected abstract void processResponse(Session sess, SMBPacket pkt);
  102. /**
  103. * Resubmit the request to the server
  104. *
  105. * @param sess Session
  106. * @param pkt SMBPacket
  107. * @return boolean
  108. */
  109. protected abstract boolean resubmitRequest(Session sess, SMBPacket pkt);
  110. /**
  111. * Set the asynchronous request completion status
  112. *
  113. * @param sts boolean
  114. */
  115. protected final void setCompleted(boolean sts) {
  116. m_completed = sts;
  117. }
  118. /**
  119. * Set the request id
  120. *
  121. * @param id int
  122. */
  123. protected final void setId(int id) {
  124. m_id = id;
  125. }
  126. /**
  127. * Set the request name
  128. *
  129. * @param name String
  130. */
  131. protected final void setName(String name) {
  132. m_name = name;
  133. }
  134. /**
  135. * Return the request as a string
  136. *
  137. * @return String
  138. */
  139. public String toString() {
  140. StringBuffer str = new StringBuffer();
  141. str.append("[");
  142. str.append(getId());
  143. str.append(":");
  144. str.append(getName());
  145. str.append(":");
  146. str.append(hasCompleted() ? "Completed" : "Pending");
  147. if ( hasAutoReset())
  148. str.append(",Auto");
  149. str.append("]");
  150. return str.toString();
  151. }
  152. /**
  153. * Compare objects for equality
  154. *
  155. * @return boolean
  156. */
  157. public boolean equals(Object obj) {
  158. // Check if the object is the same type
  159. if ( obj instanceof AsynchRequest) {
  160. // Compare the request id
  161. AsynchRequest ar = (AsynchRequest) obj;
  162. return ar.getId() == getId();
  163. }
  164. // Not the same object type
  165. return false;
  166. }
  167. /**
  168. * Return a hashcode for the request
  169. *
  170. * @return int
  171. */
  172. public int hashCode() {
  173. return getId();
  174. }
  175. }