Example #1
0
    public boolean equals(Object other) {
      if (!super.equals(other)) {
        return false;
      }

      Persistent2 otherPersistent2 = (Persistent2) other;

      if (int2 != otherPersistent2.int2) {
        System.out.println("int2 failed");
        return false;
      }

      if (string2 != otherPersistent2.string2
          && (string2 != null
              && !string2.equals(otherPersistent2.string2)
              && otherPersistent2.string2 != null)) {
        System.out.println("string2 failed");
        return false;
      }

      if (buffer2 != otherPersistent2.buffer2
          && (buffer2 != null
              && !buffer2.equals(otherPersistent2.buffer2)
              && otherPersistent2.buffer2 != null)) {
        System.out.println("buffer2 failed");
        return false;
      }

      return true;
    }
Example #2
0
    public void encode(Encoder encoder) {
      super.encode(encoder);

      encoder.encodeInt(int2);
      encoder.encodeString(string2);
      encoder.encodeInt(buffer2.remaining());
      encoder.encodeKnownLengthBuffer(buffer2);
    }
Example #3
0
  private synchronized DBMessage go(DBMessage msg, ByteDecoder decoder) throws IOException {

    if (_sock == null) _open();

    {
      ByteBuffer out = msg.prepare();
      while (out.remaining() > 0) _sock.write(out);
    }

    if (_pool != null) _pool._everWorked = true;

    if (decoder == null) return null;

    ByteBuffer response = decoder._buf;

    if (response.position() != 0) throw new IllegalArgumentException();

    int read = 0;
    while (read < DBMessage.HEADER_LENGTH) read += _read(response);

    int len = response.getInt(0);
    if (len <= DBMessage.HEADER_LENGTH)
      throw new IllegalArgumentException("db sent invalid length: " + len);

    if (len > response.capacity())
      throw new IllegalArgumentException(
          "db message size is too big (" + len + ") " + "max is (" + response.capacity() + ")");

    response.limit(len);
    while (read < len) read += _read(response);

    if (read != len) throw new RuntimeException("something is wrong");

    response.flip();
    return new DBMessage(response);
  }
Example #4
0
 private int _read(ByteBuffer buf) throws IOException {
   int x = _in.read(buf.array(), buf.position(), buf.remaining());
   if (x < 0) throw new IOException("connection to server closed unexpectedly");
   buf.position(buf.position() + x);
   return x;
 }
Example #5
0
/**
 * A reactor that selects on some stuff and then notifies some Communicators that things happened
 */
public class Overlord {
  private Selector selector;
  private Pipe pipe;
  private static final Logger log = Logger.getLogger("Overlord");
  private ConcurrentLinkedQueue<Communicator> queue;

  // This is just used to read the one byte off of pipes informing us that
  // there is data on some queue.
  ByteBuffer ignored = ByteBuffer.allocate(10);

  public Overlord() {
    try {
      selector = Selector.open();
      queue = new ConcurrentLinkedQueue<Communicator>();

      // open the pipe and register it with our selector
      pipe = Pipe.open();
      pipe.sink().configureBlocking(false);
      pipe.source().configureBlocking(false);
      pipe.source().register(selector, SelectionKey.OP_READ);
    } catch (IOException e) {
      throw new RuntimeException("select() failed");
    }
  }

  /** Selects on sockets and informs their Communicator when there is something to do. */
  public void communicate(int timeout) {

    try {
      selector.select(timeout);
    } catch (IOException e) {
      // Not really sure why/when this happens yet
      return;
    }

    Iterator<SelectionKey> keys = selector.selectedKeys().iterator();

    while (keys.hasNext()) {
      SelectionKey key = keys.next();
      keys.remove();
      if (!key.isValid()) continue; // WHY
      Communicator communicator = (Communicator) key.attachment();

      if (key.isReadable()) communicator.onReadable();
      if (key.isWritable()) communicator.onWritable();
      if (key.isAcceptable()) communicator.onAcceptable();
    }

    // Go through the queue and handle each communicator
    while (!queue.isEmpty()) {
      Communicator c = queue.poll();
      c.onMemo();
    }
  }

  public void offer(Communicator c) {
    queue.offer(c);
  }

  /** Registers a SelectableChannel */
  public boolean register(SelectableChannel sc, Communicator communicator) {
    try {
      sc.register(selector, sc.validOps(), communicator);

      return true;
    } catch (Exception e) {
      return false;
    }
  }

  /** If the selector is waiting, wake it up */
  public void interrupt() {
    selector.wakeup();
  }

  /** Registers a SelectableQueue */
  public boolean register(SelectableQueue sq, Communicator communicator) {
    try {
      // Register the new pipe with the queue. It will write a byte to this
      // pipe when the queue is hot, and it will offer its communicator to our
      // queue.
      sq.register(this, communicator);

      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
}
Example #6
0
  public void testByteBufferCoder() {
    Persistent3 three =
        new Persistent3(
            10,
            (float) 4.444,
            555555555555L,
            true,
            "this is the fourth persistent string",
            new Date(),
            new byte[] {10, 20, 30, 40, 50, 60, 70, 80, 90, 100},
            null);

    Persistent2 two =
        new Persistent2(
            1,
            (float) 2.222,
            333333333333L,
            false,
            "this is the first persistent string",
            new Date(),
            new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
            three,
            4,
            "this is the second persistent string",
            (ByteBuffer) ByteBuffer.allocateDirect(17).putInt(1234).rewind());

    Persistent1 one =
        new Persistent1(
            2,
            (float) 3.333,
            4444444444444L,
            true,
            "this is the third persistent string",
            new Date(),
            new byte[] {10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
            two);

    ByteBuffer buffer = ByteBuffer.allocateDirect(8 * 1024);
    ByteBufferCoder encoder = new ByteBufferCoder(buffer, true);

    encoder.encodeCodable(one);
    buffer.rewind();

    ByteBufferCoder decoder = new ByteBufferCoder(buffer, true, new Delegate());
    Persistent1 anotherOne = (Persistent1) decoder.decodeCodable();

    if (!one.equals(anotherOne)) {
      fail("unarchived object with type checking does not equal archived one");
    }

    buffer.clear();
    encoder = new ByteBufferCoder(buffer, false);

    encoder.encodeCodable(one);
    buffer.rewind();

    decoder = new ByteBufferCoder(buffer, false, new Delegate());
    anotherOne = (Persistent1) decoder.decodeCodable();

    if (!one.equals(anotherOne)) {
      fail("unarchived object without type checking does not equal archived one");
    }
  }