bool DiffWrapperBase<T, TDiff>::GetElementDiffs(
    google::protobuf::RepeatedPtrField<ElementDiff> *dst,
    const google::protobuf::RepeatedPtrField<Element> &old_elements,
    const google::protobuf::RepeatedPtrField<Element> &new_elements) {
  bool differs = false;
  assert(dst != nullptr);
  int i = 0;
  int j = 0;
  while (i < old_elements.size() && j < new_elements.size()) {
    const Element &old_element = old_elements.Get(i);
    const Element &new_element = new_elements.Get(i);

    if (Diff(old_element, new_element)) {
      ElementDiff *diff = dst->Add();
      Element *old_elementp = nullptr;
      Element *new_elementp = nullptr;
      if (!diff || !(old_elementp = diff->mutable_old()) ||
          !(new_elementp = diff->mutable_new_())) {
        llvm::errs() << "Failed to add diff element\n";
        ::exit(1);
      }
      *old_elementp = old_element;
      *new_elementp = new_element;
      diff->set_index(i);
      differs = true;
    }
    i++;
    j++;
  }
  if (old_elements.size() != new_elements.size()) {
    GetExtraElementDiffs(dst, i, j, old_elements, new_elements);
    differs = true;
  }
  return differs;
}
void DiffWrapperBase<T, TDiff>::GetExtraElementDiffs(
    google::protobuf::RepeatedPtrField<ElementDiff> *dst, int i, int j,
    const google::protobuf::RepeatedPtrField<Element> &old_elements,
    const google::protobuf::RepeatedPtrField<Element> &new_elements) {
 assert(dst != nullptr);
 while (i < old_elements.size()) {
    const Element &old_element = old_elements.Get(i);
    ElementDiff *diff = dst->Add();
    Element *old_elementp = nullptr;
    if (!diff || !(old_elementp = diff->mutable_old())) {
      llvm::errs() << "Failed to add diff element\n";
      ::exit(1);
    }
    *old_elementp = old_element;
    diff->set_index(i);
    i++;
 }
 while (j < new_elements.size()) {
    const Element &new_element = new_elements.Get(j);
    ElementDiff *diff = dst->Add();
    Element *new_elementp = nullptr;
    if (!diff || !(new_elementp = diff->mutable_new_())) {
      llvm::errs() << "Failed to add diff element\n";
      ::exit(1);
    }
    *new_elementp = new_element;
    diff->set_index(j);
    j++;
 }
}
bool EventStreamHandler<ButtonsType>::InitializeQueues(
    const google::protobuf::RepeatedPtrField<StartGamePB::ConnectedPortPB>&
        ports) {
  if (ports.size() > 4) {
    LOG(ERROR) << "Too many ports. Expected 0 to 4, saw " << ports.size();
    return false;
  }

  std::set<Port> handled_ports;

  int local_ports_connected = 0;

  for (const StartGamePB::ConnectedPortPB connected_port : ports) {
    Port port_id = connected_port.port();

    if (port_id == PORT_ANY) {
      LOG(ERROR)
          << "Server returned connected port with unexpected value PORT_ANY.";
      return false;
    }

    if (handled_ports.find(port_id) != handled_ports.end()) {
      LOG(ERROR) << "Duplicate port returned from server: "
                 << Port_Name(port_id);
      return false;
    }

    // Actually initialize the port as either local or remote.
    if (local_ports_.find(port_id) != local_ports_.end()) {
      VLOG(3) << "Inserting local queue for connected port:\n"
              << connected_port.DebugString();
      input_queues_.emplace(port_id, std::unique_ptr<ButtonsInputQueue>(
                                         ButtonsInputQueue::MakeLocalQueue(
                                             connected_port.delay_frames())));
      ++local_ports_connected;
    } else {
      VLOG(3) << "Inserting remote queue for connected port:\n"
              << connected_port.DebugString();
      input_queues_.emplace(port_id, std::unique_ptr<ButtonsInputQueue>(
                                         ButtonsInputQueue::MakeRemoteQueue(
                                             connected_port.delay_frames())));
    }

    handled_ports.insert(port_id);
  }

  // Make sure all local ports were connected.
  if (local_ports_.size() != local_ports_connected) {
    LOG(ERROR) << "Failed to connect local ports: ";
    for (const Port local_port : local_ports_) {
      if (handled_ports.find(local_port) == handled_ports.end()) {
        LOG(ERROR) << Port_Name(local_port);
      }
    }

    return false;
  }

  return true;
}
Ejemplo n.º 4
0
bool operator==(const ::google::protobuf::RepeatedPtrField<T>& a, const ::google::protobuf::RepeatedPtrField<T>& b) {
	if (a.size() != b.size())
		return false;
	for (auto aiter=a.begin(), biter=b.begin(); aiter != a.end(); aiter++, biter++ ) {
		if (*aiter != *biter)
			return false;
	}

	return true;
}
Ejemplo n.º 5
0
std::vector<T> convert(const google::protobuf::RepeatedPtrField<T>& items)
{
  std::vector<T> result;
  for (int i = 0; i < items.size(); i++) {
    result.push_back(items.Get(i));
  }

  return result;
}
Ejemplo n.º 6
0
void zina::deserializeVectorClock(const google::protobuf::RepeatedPtrField<VClock> &protoVc, vectorclock::VectorClock<string> *vc)
{
    int32_t numClocks = protoVc.size();

    for (int i = 0; i < numClocks; i++) {
        const VClock &vcData = protoVc.Get(i);
        vc->insertNodeWithValue(vcData.device_id(), vcData.value());
    }
}
Ejemplo n.º 7
0
void SpotifyService::FillPlaylist(
    QStandardItem* item,
    const google::protobuf::RepeatedPtrField<pb::spotify::Track>& tracks) {
  if (item->hasChildren()) item->removeRows(0, item->rowCount());

  for (int i = 0; i < tracks.size(); ++i) {
    Song song;
    SongFromProtobuf(tracks.Get(i), &song);

    QStandardItem* child = CreateSongItem(song);

    item->appendRow(child);
  }
}
Ejemplo n.º 8
0
bool UserManager::init(const ::google::protobuf::RepeatedPtrField<UserOptions>& user_options)
{
	int o_size = user_options.size();
	for (int i = 0; i < o_size; ++i) {
		const UserOptions& user = user_options.Get(i);
		UserInfo* uinfo = new UserInfo(user.name(), user.file_name());
		uinfo->pending_events = new PendingEvents(user.file_name());
		if (!uinfo->pending_events->init()) {
			LOG(ERROR, "Failed to init user \"%s\"", user.name().c_str());
			return false;
		}
		_users[user.name()] = uinfo;
	}
	return true;
}
Ejemplo n.º 9
0
void PeerListModel::updatePeers(const google::protobuf::RepeatedPtrField<Protos::GUI::State::Peer>& peers, const QSet<Common::Hash>& peersDownloadingOurData)
{
   Common::SortedArray<Peer*> peersToRemove = this->orderedPeers;
   QList<Peer*> peersToAdd;

   for (int i = 0; i < peers.size(); i++)
   {
      const Common::Hash peerID(peers.Get(i).peer_id().hash());
      const QString& nick = Common::ProtoHelper::getStr(peers.Get(i), &Protos::GUI::State::Peer::nick);
      const QString& coreVersion = Common::ProtoHelper::getStr(peers.Get(i), &Protos::GUI::State::Peer::core_version);
      const quint64 sharingAmount(peers.Get(i).sharing_amount());
      const TransferInformation transferInformation { peers.Get(i).download_rate(), peers.Get(i).upload_rate(),  peersDownloadingOurData.contains(peerID) };
      const QHostAddress ip =
         peers.Get(i).has_ip() ?
            Common::ProtoHelper::getIP(peers.Get(i).ip()) :
            QHostAddress();

      Peer* peer = this->indexedPeers[peerID];
      int j = this->orderedPeers.indexOf(peer);
      if (j != -1)
      {
         peersToRemove.remove(peer);
         if (peer->nick != nick)
         {
            if (this->currentSortType == Protos::GUI::Settings::BY_NICK)
            {
               this->beginRemoveRows(QModelIndex(), j, j);
               this->orderedPeers.remove(peer);
               this->endRemoveRows();
               peer->nick = nick;
               peersToAdd << peer;
            }
            else
            {
               peer->nick = nick;
               emit dataChanged(this->createIndex(j, 1), this->createIndex(j, 1));
            }
         }
         if (peer->sharingAmount != sharingAmount)
         {
            if (this->currentSortType == Protos::GUI::Settings::BY_SHARING_AMOUNT)
            {
               this->beginRemoveRows(QModelIndex(), j, j);
               this->orderedPeers.remove(peer);
               this->endRemoveRows();
               peer->sharingAmount = sharingAmount;
               peersToAdd << peer;
            }
            else
            {
               peer->sharingAmount = sharingAmount;
               emit dataChanged(this->createIndex(j, 1), this->createIndex(j, 1));
            }
         }
         if (peer->transferInformation != transferInformation)
         {
            peer->transferInformation = transferInformation;
            emit dataChanged(this->createIndex(j, 0), this->createIndex(j, 0));
         }

         peer->ip = ip;
         peer->coreVersion = coreVersion;
      }
      else
      {
         peersToAdd << new Peer(peerID, nick, coreVersion, sharingAmount, ip, transferInformation);
      }
   }

   QList<Common::Hash> peerIDsRemoved;
   for (Common::SortedArray<Peer*>::Iterator i(peersToRemove); i.hasNext();)
   {
      Peer* const peer = i.next();
      peerIDsRemoved << peer->peerID;
      int j = this->orderedPeers.indexOf(peer);
      if (j != -1)
      {
         this->beginRemoveRows(QModelIndex(), j, j);
         this->indexedPeers.remove(peer->peerID);
         this->orderedPeers.remove(peer);
         delete peer;
         this->endRemoveRows();
      }
   }

   if (!peerIDsRemoved.isEmpty())
      emit peersRemoved(peerIDsRemoved);

   for (QListIterator<Peer*> i(peersToAdd); i.hasNext();)
   {
      Peer* const peer = i.next();
      int pos = this->orderedPeers.insert(peer);
      this->beginInsertRows(QModelIndex(), pos, pos);
      this->indexedPeers.insert(peer->peerID, peer);
      this->endInsertRows();
   }
}