Package net.rubyeye.xmemcached.command

Examples of net.rubyeye.xmemcached.command.Command


      final AtomicBoolean done = new AtomicBoolean(false);
      byte[] response = saslClient.hasInitialResponse() ? saslClient
          .evaluateChallenge(EMPTY_BYTES) : EMPTY_BYTES;
      CountDownLatch latch = new CountDownLatch(1);
      Command command = this.commandFactory.createAuthStartCommand(
          saslClient.getMechanismName(), latch, response);
      if (!this.memcachedTCPSession.isClosed())
        this.memcachedTCPSession.write(command);
      else {
        log
            .error("Authentication fail,because the connection has been closed");
        throw new RuntimeException(
            "Authentication fai,connection has been close");
      }

      while (!done.get()) {
        try {
          latch.await();
        } catch (InterruptedException e) {
          Thread.currentThread().interrupt();
          done.set(true);
        }
        ResponseStatus responseStatus = ((BaseBinaryCommand) command)
            .getResponseStatus();
        switch (responseStatus) {
        case NO_ERROR:
          done.set(true);
          log.info("Authentication to "
              + this.memcachedTCPSession.getRemoteSocketAddress()
              + " successfully");
          break;
        case AUTH_REQUIRED:
          log
              .error("Authentication failed to "
                  + this.memcachedTCPSession
                      .getRemoteSocketAddress());
          log.warn("Reopen connection to "
              + this.memcachedTCPSession.getRemoteSocketAddress()
              + ",beacause auth fail");
          this.memcachedTCPSession.setAuthFailed(true);

          // It it is not first time,try to sleep 1 second
          if (!this.authInfo.isFirstTime()) {
            Thread.sleep(1000);
          }
          this.memcachedTCPSession.close();
          done.set(true);
          break;
        case FUTHER_AUTH_REQUIRED:
          System.out.println(command.getResult());
          String result = String.valueOf(command.getResult());
          response = saslClient.evaluateChallenge(ByteUtils
              .getBytes(result));
          latch = new CountDownLatch(1);
          command = commandFactory.createAuthStepCommand(saslClient
              .getMechanismName(), latch, response);
View Full Code Here


public class BinaryTouchCommandUnitTest extends BaseBinaryCommandUnitTest {
  String key = "hello";
  byte[] keyBytes = ByteUtils.getBytes(this.key);

  public void testEncodeDecode() {
    Command command = this.commandFactory.createTouchCommand(key, keyBytes,
        null, 10, false);
    command.encode();
    ByteBuffer encodeBuffer = command.getIoBuffer().buf();
    assertNotNull(encodeBuffer);
    assertEquals(24 + 4 + 5, encodeBuffer.capacity());

    byte opCode = encodeBuffer.get(1);
    assertEquals(OpCode.TOUCH.fieldValue(), opCode);

    ByteBuffer buffer = constructResponse(OpCode.TOUCH.fieldValue(),
        (short) 0, (byte) 0, (byte) 0, (short) 0, 0, 0, 1L, null, null,
        null);

    assertTrue(command.decode(null, buffer));
    assertTrue((Boolean) command.getResult());
    assertEquals(0, buffer.remaining());

    buffer = constructResponse(OpCode.TOUCH.fieldValue(), (short) 0,
        (byte) 0, (byte) 0, (short) 0x0005, 0, 0, 1L, null, null, null);
    command = this.commandFactory.createTouchCommand(key, keyBytes, null,
        10, false);
    assertTrue(command.decode(null, buffer));
    assertFalse((Boolean) command.getResult());
    assertEquals(0, buffer.remaining());
  }
View Full Code Here

  String value = "!";
  boolean noreply = false;

  public void testAppendEncodeAndDecode() {

    Command command = this.commandFactory.createAppendCommand(this.key,
        this.keyBytes, this.value, this.noreply, this.transcoder);
    command.encode();
    ByteBuffer encodeBuffer = command.getIoBuffer().buf();
    assertNotNull(encodeBuffer);
    assertEquals(30, encodeBuffer.capacity());

    byte opCode = encodeBuffer.get(1);
    assertEquals(OpCode.APPEND.fieldValue(), opCode);

    ByteBuffer buffer = constructResponse(OpCode.APPEND.fieldValue(),
        (short) 0, (byte) 0, (byte) 0, (short) 0, 0, 0, 1L, null, null,
        null);

    assertTrue(command.decode(null, buffer));
    assertTrue((Boolean) command.getResult());
    assertEquals(0, buffer.remaining());

    buffer = constructResponse(OpCode.APPEND.fieldValue(), (short) 0,
        (byte) 0, (byte) 0, (short) 0x0005, 0, 0, 1L, null, null, null);
    command = this.commandFactory.createAppendCommand(this.key, this.keyBytes, this.value,
        this.noreply, this.transcoder);
    assertTrue(command.decode(null, buffer));
    assertFalse((Boolean) command.getResult());
    assertEquals(0, buffer.remaining());
  }
View Full Code Here

    assertEquals(0, buffer.remaining());
  }

  public void testPrependEncodeAndDecode() {

    Command command = this.commandFactory.createPrependCommand(this.key,
        this.keyBytes, this.value, this.noreply, this.transcoder);
    command.encode();
    ByteBuffer encodeBuffer = command.getIoBuffer().buf();
    assertNotNull(encodeBuffer);
    assertEquals(30, encodeBuffer.capacity());

    byte opCode = encodeBuffer.get(1);
    assertEquals(OpCode.PREPEND.fieldValue(), opCode);

    ByteBuffer buffer = constructResponse(OpCode.PREPEND.fieldValue(),
        (short) 0, (byte) 0, (byte) 0, (short) 0, 0, 0, 1L, null, null,
        null);

    assertTrue(command.decode(null, buffer));
    assertTrue((Boolean) command.getResult());
    assertEquals(0, buffer.remaining());

    buffer = constructResponse(OpCode.PREPEND.fieldValue(), (short) 0,
        (byte) 0, (byte) 0, (short) 0x0005, 0, 0, 1L, null, null, null);
    command = this.commandFactory.createPrependCommand(this.key, this.keyBytes,
        this.value, this.noreply, this.transcoder);
    assertTrue(command.decode(null, buffer));
    assertFalse((Boolean) command.getResult());
    assertEquals(0, buffer.remaining());
  }
View Full Code Here

  String value = "world";
  boolean noreply = false;

  public void testAddEncodeAndDecode() {

    Command command = this.commandFactory.createCASCommand(this.key, this.keyBytes,
        0, this.value, 9L, this.noreply, this.transcoder);

    command.encode();
    ByteBuffer encodeBuffer = command.getIoBuffer().buf();
    assertNotNull(encodeBuffer);
    assertEquals(42, encodeBuffer.capacity());

    byte opCode = encodeBuffer.get(1);
    // cas use set command
    assertEquals(OpCode.SET.fieldValue(), opCode);

    ByteBuffer buffer = constructResponse(OpCode.SET.fieldValue(),
        (short) 0, (byte) 0, (byte) 0, (short) 0, 0, 0, 10L, null,
        null, null);

    assertTrue(command.decode(null, buffer));
    assertTrue((Boolean) command.getResult());
    assertEquals(0, buffer.remaining());

    buffer = constructResponse(OpCode.SET.fieldValue(), (short) 0,
        (byte) 0, (byte) 0, (short) 0x0005, 0, 0, 0L, null, null, null);
    command = this.commandFactory.createCASCommand(this.key, this.keyBytes, 0, this.value,
        9L, this.noreply, this.transcoder);
    assertTrue(command.decode(null, buffer));
    assertFalse((Boolean) command.getResult());
    assertEquals(0, buffer.remaining());
  }
View Full Code Here

import net.rubyeye.xmemcached.command.CommandType;
import net.rubyeye.xmemcached.transcoders.CachedData;

public class TextGetOneCommandUnitTest extends BaseTextCommandUnitTest {
  public void testGetOneEncode() {
    Command command = this.commandFactory.createGetCommand("test", "test"
        .getBytes(), CommandType.GET_ONE, null);
    assertNull(command.getIoBuffer());
    command.encode();
    checkByteBufferEquals(command, "get test\r\n");
  }
View Full Code Here

  @SuppressWarnings("unchecked")
  private final <T> Object fetch0(final String key, final byte[] keyBytes,
      final CommandType cmdType, final long timeout,
      Transcoder<T> transcoder) throws InterruptedException,
      TimeoutException, MemcachedException, MemcachedException {
    final Command command = this.commandFactory.createGetCommand(key,
        keyBytes, cmdType, this.transcoder);
    this.latchWait(command, timeout, this.sendCommand(command));
    command.getIoBuffer().free(); // free buffer
    this.checkException(command);
    CachedData data = (CachedData) command.getResult();
    if (data == null) {
      return null;
    }
    if (transcoder == null) {
      transcoder = this.transcoder;
View Full Code Here

  private final <T> Command sendGetMultiCommand(
      final Collection<String> keys, final CountDownLatch latch,
      final CommandType cmdType, final Transcoder<T> transcoder)
      throws InterruptedException, TimeoutException, MemcachedException {
    final Command command = this.commandFactory.createGetMultiCommand(keys,
        latch, cmdType, transcoder);
    this.sendCommand(command);
    return command;
  }
View Full Code Here

      boolean noreply, long opTimeout) throws MemcachedException,
      InterruptedException, TimeoutException {
    key = this.preProcessKey(key);
    final byte[] keyBytes = ByteUtils.getBytes(key);
    ByteUtils.checkKey(keyBytes);
    final Command command = this.commandFactory.createDeleteCommand(key,
        keyBytes, time, cas, noreply);
    final Session session = this.sendCommand(command);
    if (!command.isNoreply()) {
      this.latchWait(command, opTimeout, session);
      command.getIoBuffer().free();
      this.checkException(command);
      if (command.getResult() == null) {
        throw new MemcachedException(
            "Operation fail,may be caused by networking or timeout");
      }
    } else {
      return false;
    }
    return (Boolean) command.getResult();
  }
View Full Code Here

      throws TimeoutException, InterruptedException, MemcachedException {
    key = this.preProcessKey(key);
    final byte[] keyBytes = ByteUtils.getBytes(key);
    ByteUtils.checkKey(keyBytes);
    CountDownLatch latch = new CountDownLatch(1);
    final Command command = this.commandFactory.createTouchCommand(key,
        keyBytes, latch, exp, false);
    this.latchWait(command, opTimeout, this.sendCommand(command));
    command.getIoBuffer().free();
    this.checkException(command);
    if (command.getResult() == null) {
      throw new MemcachedException(
          "Operation fail,may be caused by networking or timeout");
    }
    return (Boolean) command.getResult();
  }
View Full Code Here

TOP

Related Classes of net.rubyeye.xmemcached.command.Command

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.