PageRenderTime 75ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/core/ri/src/main/java/org/jredis/ri/alphazero/connection/SynchConnection.java

https://github.com/parag/jredis
Java | 178 lines | 78 code | 23 blank | 77 comment | 5 complexity | 54bcc5e9e0d80edd73fff0c36bad3ec9 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright 2009 Joubin Houshyar
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.jredis.ri.alphazero.connection;
  17. import org.jredis.ClientRuntimeException;
  18. import org.jredis.ProviderException;
  19. import org.jredis.RedisException;
  20. import org.jredis.connector.Connection;
  21. import org.jredis.connector.ConnectionSpec;
  22. import org.jredis.connector.NotConnectedException;
  23. import org.jredis.protocol.Command;
  24. import org.jredis.protocol.Protocol;
  25. import org.jredis.protocol.Request;
  26. import org.jredis.protocol.Response;
  27. import org.jredis.protocol.ResponseStatus;
  28. import org.jredis.ri.alphazero.RedisVersion;
  29. import org.jredis.ri.alphazero.support.Assert;
  30. import org.jredis.ri.alphazero.support.Log;
  31. /**
  32. * [TODO: document me!]
  33. *
  34. * @author Joubin Houshyar (alphazero@sensesay.net)
  35. * @version alpha.0, Apr 10, 2009
  36. * @since alpha.0
  37. *
  38. */
  39. public class SynchConnection extends ConnectionBase implements Connection {
  40. // ------------------------------------------------------------------------
  41. // Properties
  42. // ------------------------------------------------------------------------
  43. // ------------------------------------------------------------------------
  44. // Constructors
  45. // ------------------------------------------------------------------------
  46. /**
  47. * This constructor will pass the connection spec to the super class constructor and
  48. * create and install the {@link Protocol} handler delegate instance for this {@link SynchConnection}
  49. * for the {@link RedisVersion#current_revision}.
  50. *
  51. * @param connectionSpec
  52. * @throws ClientRuntimeException
  53. * @throws ProviderException
  54. */
  55. public SynchConnection (
  56. ConnectionSpec connectionSpec,
  57. boolean isShared
  58. )
  59. throws ClientRuntimeException, ProviderException
  60. {
  61. this(connectionSpec, isShared, RedisVersion.current_revision);
  62. }
  63. /**
  64. * All the constructors in this class delegate to this constructor and are
  65. * effectively provided for convenience.
  66. * <p>
  67. * This constructor will pass the connection spec to the super class constructor and
  68. * create and install the {@link Protocol} handler delegate instance for this {@link SynchConnection}.
  69. * If you definitely need to specify the redis server version, and the protocol implementation for that
  70. * version exists, you should use this constructor. Otherwise, it is recommended that the
  71. * {@link SynchConnection#SynchConnection(ConnectionSpec)}be used.
  72. * <p>
  73. * This constructor will open the socket connection immediately.
  74. *
  75. * @param connectionSpec
  76. * @param redisversion
  77. * @throws ClientRuntimeException due to either dns (host connectivity) or any IO issues related to establishing
  78. * the socket connection to the specified server.
  79. * @throws ProviderException if the version specified is not supported.
  80. */
  81. public SynchConnection (
  82. ConnectionSpec connectionSpec,
  83. boolean isShared,
  84. RedisVersion redisversion
  85. )
  86. throws ClientRuntimeException, ProviderException
  87. {
  88. super (connectionSpec);
  89. }
  90. // ------------------------------------------------------------------------
  91. // Interface
  92. // ======================================================= ProtocolHandler
  93. // ------------------------------------------------------------------------
  94. /* (non-Javadoc)
  95. * @see org.jredis.connector.Connection#getModality()
  96. */
  97. // @Override
  98. public final Modality getModality() {
  99. return Connection.Modality.Synchronous;
  100. }
  101. /* (non-Javadoc)
  102. * @see org.jredis.ri.alphazero.connection.ConnectionBase#serviceRequest(org.jredis.protocol.Command, byte[][])
  103. */
  104. // TODO: not happy about the performance hit synchronized has caused ... but its required for heartbeat.
  105. // is it worth it?
  106. public synchronized Response serviceRequest (Command cmd, byte[]... args)
  107. throws RedisException
  108. {
  109. if(!isConnected()) throw new NotConnectedException ("Not connected!");
  110. Request request = null;
  111. Response response = null;
  112. ResponseStatus status = null;
  113. try {
  114. // 1 - Request
  115. // Log.log("RedisConnection - requesting ..." + cmd.code);
  116. request = Assert.notNull(protocol.createRequest (cmd, args), "request object from handler", ProviderException.class);
  117. request.write(super.getOutputStream());
  118. // 2 - response
  119. // Log.log("RedisConnection - read response ..." + cmd.code);
  120. response = Assert.notNull(protocol.createResponse(cmd), "response object from handler", ProviderException.class);
  121. response.read(super.getInputStream());
  122. // break;
  123. }
  124. catch (ProviderException bug){
  125. Log.bug ("serviceRequest() -- ProviderException: " + bug.getLocalizedMessage());
  126. Log.log ("serviceRequest() -- closing connection ...");
  127. disconnect();
  128. throw bug;
  129. }
  130. catch (ClientRuntimeException cre) {
  131. Log.problem ("serviceRequest() -- ClientRuntimeException => " + cre.getLocalizedMessage());
  132. reconnect();
  133. throw new ConnectionResetException ("Connection re-established but last request not processed: " + cre.getCause().getLocalizedMessage());
  134. }
  135. catch (RuntimeException e){
  136. e.printStackTrace();
  137. Log.bug ("serviceRequest() -- *unexpected* RuntimeException: " + e.getLocalizedMessage());
  138. Log.log ("serviceRequest() -- closing connection ...");
  139. disconnect();
  140. throw new ClientRuntimeException("unexpected runtime exeption: " + e.getLocalizedMessage(), e);
  141. }
  142. // 3 - Status
  143. //
  144. status = Assert.notNull (response.getStatus(), "status from response object", ProviderException.class);
  145. if(status.isError()) {
  146. Log.error ("Error response for " + cmd.code + " => " + status.message());
  147. throw new RedisException(cmd, status.message());
  148. }
  149. else if(status.code() == ResponseStatus.Code.CIAO) {
  150. // normal for quit and shutdown commands. we disconnect too.
  151. disconnect();
  152. }
  153. return response;
  154. }
  155. }