Index: trunk/BNC/src/bncephuser.cpp
===================================================================
--- trunk/BNC/src/bncephuser.cpp	(revision 11215)
+++ trunk/BNC/src/bncephuser.cpp	(revision 11216)
@@ -144,4 +144,8 @@
   }
 
+  if (_voting && realTime) {
+    releaseHeld();
+  }
+
   // satellite outside the range supported by BNC (e.g. G39)
   if (eph->prn().toInt() == 0) {
@@ -196,4 +200,9 @@
       votes->_eph.append(clone(newEph));
       votes->_station[newEph->receptStaID()] = 0;
+      votes->_first = currentTime();
+      if (qq.size() > 1) {
+        const t_votes* prev = _votes.value(qq[qq.size()-2], 0);
+        votes->_released = !(prev && prev->_station.size() >= VOTE_AGREE);
+      }
       _votes[newEph] = votes;
     }
@@ -316,4 +325,97 @@
 }
 
+// Satellite in the vote messages, for Galileo with the navigation message
+// (I/NAV and F/NAV data sets are buffered and voted separately)
+////////////////////////////////////////////////////////////////////////////
+static QString voteSat(const t_eph* eph) {
+  QString sat(QString(eph->prn().toInternalString().c_str()).left(3));
+  if      (eph->type() == t_eph::INAV) sat += " INAV";
+  else if (eph->type() == t_eph::FNAV) sat += " FNAV";
+  return sat;
+}
+
+const double bncEphUser::VOTE_HOLD  = 40.0;  // SSR providers switch to a new IOD after 60 s
+const int    bncEphUser::VOTE_AGREE = 3;
+
+// Current GPS time (time of the input data when replaying a file)
+////////////////////////////////////////////////////////////////////////////
+bncTime bncEphUser::currentTime() {
+  return bncTime(currentDateAndTimeGPS().toString(Qt::ISODate).toStdString());
+}
+
+// Release the data sets held back longer than VOTE_HOLD seconds
+////////////////////////////////////////////////////////////////////////////
+void bncEphUser::releaseHeld() {
+  bncTime now = currentTime();
+  bool changed = false;
+  QStringList msgs;  // sorted: the QHash order differs between runs
+  QHashIterator<const t_eph*, t_votes*> it(_votes);
+  while (it.hasNext()) {
+    it.next();
+    t_votes* votes = it.value();
+    if (!votes->_released && now - votes->_first >= VOTE_HOLD) {
+      votes->_released = true;
+      changed = true;
+      const t_eph* eph = it.key();
+      QVector<int> count(votes->_content.size(), 0);
+      QMapIterator<QString, int> is(votes->_station);
+      while (is.hasNext()) {
+        is.next();
+        count[is.value()]++;
+      }
+      msgs << QString("ephemeris vote %1 %2 %3: copy of %4 uploaded after %5 s without %6 agreeing streams (%7 of %8 streams)")
+                        .arg(voteSat(eph))
+                        .arg(eph->TOC().datestr().c_str())
+                        .arg(eph->TOC().timestr(0).c_str())
+                        .arg(eph->receptStaID())
+                        .arg(int(VOTE_HOLD))
+                        .arg(VOTE_AGREE)
+                        .arg(count[votes->_current])
+                        .arg(votes->_station.size());
+    }
+  }
+  msgs.sort();
+  for (const QString& msg : msgs) {
+    BNC_CORE->message(msg.toLatin1(), false, "bce", t_logRecord::info);
+  }
+  if (changed) {
+    ephBufferChanged();
+  }
+}
+
+// Vote over the copies of a data set, check of the hold every second
+////////////////////////////////////////////////////////////////////////////
+void bncEphUser::enableVoting() {
+  _voting = true;
+  QTimer* timer = new QTimer(this);
+  connect(timer, SIGNAL(timeout()), this, SLOT(slotVoteTimer()));
+  timer->start(1000);
+}
+
+// Release of held data sets without new ephemerides; not when replaying a
+// file, where the time of the input data decides (reproducible results)
+////////////////////////////////////////////////////////////////////////////
+void bncEphUser::slotVoteTimer() {
+  if (BNC_CORE->dateAndTimeGPSSet()) {
+    return;
+  }
+  QMutexLocker locker(&_mutex);
+  releaseHeld();
+}
+
+// Data set to be uploaded
+////////////////////////////////////////////////////////////////////////////
+t_eph* bncEphUser::ephUpload(const QString& prn) {
+  if (!_eph.contains(prn)) {
+    return 0;
+  }
+  const deque<t_eph*>& qq = _eph[prn];
+  const t_votes* votes = _votes.value(qq.back(), 0);
+  if (votes && !votes->_released && qq.size() > 1) {
+    return qq[qq.size()-2];
+  }
+  return qq.back();
+}
+
 // Further copy of the buffered data set qq[index] received from a stream
 ////////////////////////////////////////////////////////////////////////////
@@ -345,5 +447,13 @@
     }
   }
+  bool released = false;
+  if (!votes->_released && count[iBest] >= VOTE_AGREE) {
+    votes->_released = true;
+    released = true;
+  }
   if (iBest == votes->_current) {
+    if (released) {
+      ephBufferChanged();
+    }
     return;
   }
@@ -353,4 +463,7 @@
   if (!radialDistanceOk(newEph)) {
     delete newEph;
+    if (released) {
+      ephBufferChanged();
+    }
     return;
   }
@@ -380,5 +493,5 @@
   }
   BNC_CORE->message(QString("ephemeris vote %1 %2 %3: copy of %4 replaced by that of %5 (%6 of %7 streams; %8; differing: %9)")
-                    .arg(prn.left(3))
+                    .arg(voteSat(oldEph))
                     .arg(oldEph->TOC().datestr().c_str())
                     .arg(oldEph->TOC().timestr(0).c_str())
