Exemple #1
0
 public void writeComplete(int rc, long ledgerId, long entryId, Object ctx) {
   Cnxn src = (Cnxn) ctx;
   ByteBuffer bb = ByteBuffer.allocate(24);
   bb.putInt(BookieProtocol.ADDENTRY);
   bb.putInt(rc);
   bb.putLong(ledgerId);
   bb.putLong(entryId);
   bb.flip();
   if (LOG.isTraceEnabled()) {
     LOG.trace("Add entry rc = " + rc + " for " + entryId + "@" + ledgerId);
   }
   src.sendResponse(new ByteBuffer[] {bb});
 }
Exemple #2
0
  public void processPacket(ByteBuffer packet, Cnxn src) {
    int type = packet.getInt();
    switch (type) {
      case BookieProtocol.ADDENTRY:
        try {
          byte[] masterKey = new byte[20];
          packet.get(masterKey, 0, 20);
          // LOG.debug("Master key: " + new String(masterKey));
          bookie.addEntry(packet.slice(), this, src, masterKey);
        } catch (IOException e) {
          if (LOG.isTraceEnabled()) {
            ByteBuffer bb = packet.duplicate();

            long ledgerId = bb.getLong();
            long entryId = bb.getLong();
            LOG.trace("Error reading " + entryId + "@" + ledgerId, e);
          }
          ByteBuffer eio = ByteBuffer.allocate(8);
          eio.putInt(type);
          eio.putInt(BookieProtocol.EIO);
          eio.flip();
          src.sendResponse(new ByteBuffer[] {eio});
        } catch (BookieException e) {
          ByteBuffer bb = packet.duplicate();
          long ledgerId = bb.getLong();

          LOG.error("Unauthorized access to ledger " + ledgerId);

          ByteBuffer eio = ByteBuffer.allocate(8);
          eio.putInt(type);
          eio.putInt(BookieProtocol.EUA);
          eio.flip();
          src.sendResponse(new ByteBuffer[] {eio});
        }
        break;
      case BookieProtocol.READENTRY:
        ByteBuffer[] rsp = new ByteBuffer[2];
        ByteBuffer rc = ByteBuffer.allocate(8 + 8 + 8);
        rsp[0] = rc;
        rc.putInt(type);

        long ledgerId = packet.getLong();
        long entryId = packet.getLong();
        LOG.debug("Received new read request: " + ledgerId + ", " + entryId);
        try {
          rsp[1] = bookie.readEntry(ledgerId, entryId);
          LOG.debug("##### Read entry ##### " + rsp[1].remaining());
          rc.putInt(BookieProtocol.EOK);
        } catch (Bookie.NoLedgerException e) {
          if (LOG.isTraceEnabled()) {
            LOG.error("Error reading " + entryId + "@" + ledgerId, e);
          }
          rc.putInt(BookieProtocol.ENOLEDGER);
        } catch (Bookie.NoEntryException e) {
          if (LOG.isTraceEnabled()) {
            LOG.error("Error reading " + entryId + "@" + ledgerId, e);
          }
          rc.putInt(BookieProtocol.ENOENTRY);
        } catch (IOException e) {
          if (LOG.isTraceEnabled()) {
            LOG.error("Error reading " + entryId + "@" + ledgerId, e);
          }
          rc.putInt(BookieProtocol.EIO);
        }
        rc.putLong(ledgerId);
        rc.putLong(entryId);
        rc.flip();
        if (LOG.isTraceEnabled()) {
          int rcCode = rc.getInt();
          rc.rewind();
          LOG.trace("Read entry rc = " + rcCode + " for " + entryId + "@" + ledgerId);
        }
        if (rsp[1] == null) {
          // We haven't filled in entry data, so we have to send back
          // the ledger and entry ids here
          rsp[1] = ByteBuffer.allocate(16);
          rsp[1].putLong(ledgerId);
          rsp[1].putLong(entryId);
          rsp[1].flip();
        }
        LOG.debug("Sending response for: " + entryId + ", " + new String(rsp[1].array()));
        src.sendResponse(rsp);
        break;
      default:
        ByteBuffer badType = ByteBuffer.allocate(8);
        badType.putInt(type);
        badType.putInt(BookieProtocol.EBADREQ);
        badType.flip();
        src.sendResponse(new ByteBuffer[] {packet});
    }
  }