示例#1
0
/**
 * @brief BTRecordFile::readRecordFromDiskTest
 * @param pDisk
 * @param pRecordID
 * Hace la lectura de un registro en la RAM
 */
void BTRecordFile::readRecordFromDiskTest(Disk pDisk, unsigned short pRecordID)
{
    const char *padre = pDisk.read(this->_metadataPtr->getFirstRecordPos(), 7);
    const char *hizq = pDisk.read(this->_metadataPtr->getFirstRecordPos() + 8, 7);
    const char *hder = pDisk.read(this->_metadataPtr->getFirstRecordPos() + 16, 7);
    std::string father(padre);          // obtiene el padre
    std::string HI(hizq);               // obtiene el hijo izq
    std::string HD(hder);               // obtiene el hijo der
    unsigned short _sizeCounter = this->_metadataPtr->getFirstRecordPos() + 24;       // inicio de la data
    DLL<IRecordDataType*> *tmp1 = _metadataPtr->getRecordStruct();
    DLLNode<IRecordDataType*> *tmp = tmp1->getHeadPtr();
    const char *data;

    cout << "Binario " << father << " " << HI << " " << HD << endl;
    std::string P = _conversion->binaryToDecimal(father);
    std::string PHI = _conversion->binaryToDecimal(HI);
    std::string PHD = _conversion->binaryToDecimal(HD);
    cout << P << " " << PHI << " " << PHD << " ";
    while (tmp != nullptr) {
        data = (dynamic_cast<RecordDataType<char>*>(tmp->getData()))->getDataPtr();
        const char *DATO = pDisk.read(_sizeCounter, 7);
        std::string DATOSTR(DATO);       // obtiene el padre
        _sizeCounter += 8;
        cout << sortUserDataFromDisk(DATOSTR, *data) << endl;
        tmp = tmp->getNextPtr();
    }
}
示例#2
0
int RPC::dispatch(Disk& disk)
{
  char buf[NDISK_BLOCK_SIZE];
  char packet[NDISK_BLOCK_SIZE + 8];

  size_t len = NDISK_BLOCK_SIZE + 8;
  char *msg = packet;

  if (disk.block_size() != NDISK_BLOCK_SIZE)
    return DISK_ERROR;

  // Read the packet (with 8-byte header)
  do
  {
    ssize_t nread = recv(sock, msg, len, 0);

    if (nread <= 0)
      return DISK_ERROR;

    len -= nread;
    msg += nread;
  }
  while (len > 0);

  // Get the command from the packet's first four bytes
  char command[5];
  memcpy(command, packet, 4);
  command[4] = '\0';

  // Get the block number parameter from the packet
  int block_num = ston(packet + 4);

  if (!strcmp(command, "NUMB"))
  {
    // Write num blocks into packet data
    ntos(packet + 8, disk.num_blocks());
  }
  else if (!strcmp(command, "FRMT"))
  {
    disk.format(block_num);
  }
  else if (!strcmp(command, "READ"))
  {
    disk.read(block_num, buf);
    memcpy(packet + 8, buf, NDISK_BLOCK_SIZE);
  }
  else if (!strcmp(command, "WRTE"))
  {
    memcpy(buf, packet + 8, NDISK_BLOCK_SIZE);
    disk.write(block_num, buf);
  }
  else
  {
    message1("Unknown RPC command %s\n", command);
  }

  message2("RPC command %s(%d)\n", command, block_num);

  len = NDISK_BLOCK_SIZE + 8;
  msg = packet;

  // Write the packet (with 8-byte header)
  do
  {
    ssize_t nwritten = send(sock, msg, len, 0);

    if (nwritten < 0)
      return DISK_ERROR;

    len -= nwritten;
    msg += nwritten;
  }
  while (len > 0);

  return DISK_OK;
}