Skip to content

Commit

Permalink
Merge pull request #31 from vranki/bugfix/no_initial_value_on_second_sub
Browse files Browse the repository at this point in the history
Fix issue #30 second subscriber not getting initial value
  • Loading branch information
vranki authored Jun 20, 2018
2 parents 4523f7d + 0300394 commit 7a0e1fe
Showing 3 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions extplane-server/datarefs/datadataref.cpp
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@ void DataDataRef::updateValue() {
}

void DataDataRef::setValue(QByteArray &newValue) {
Q_UNUSED(newValue);
//TODO: @dankrusi: finish this implementation and test
qFatal("Writing of Data DataRefs is not yet supported");
/*
21 changes: 16 additions & 5 deletions extplane-server/tcpclient.cpp
Original file line number Diff line number Diff line change
@@ -56,9 +56,9 @@ void TcpClient::disconnectClient() {
void TcpClient::readClient() {
while(_socket->canReadLine()) {
QByteArray lineBA = _socket->readLine();

QString line = QString(lineBA).trimmed();
DEBUG << "Client says: " << line;

// Split the command in strings
QStringList subLine = line.split(" ", QString::SkipEmptyParts);
QString command = subLine.value(0);
@@ -96,7 +96,7 @@ void TcpClient::readClient() {
}
INFO << "Subscribed to " << ref->name() << ", accuracy " << accuracy << ", type " << ref->typeString();
if(ref->isValid()) {
emit ref->changed(ref); // Force update to all clients
sendRef(ref); // Force update
}
if(command == "get") ref->setUnsubscribeAfterChange();
} else {
@@ -196,7 +196,7 @@ void TcpClient::readClient() {
}
} else if(command == "sit"){
if(subLine.size() == 2) {
_refProvider->loadSituation(subLine.value(1));
_refProvider->loadSituation(subLine.value(1));
} else {
INFO << "Invalid sit command";
}
@@ -210,6 +210,7 @@ void TcpClient::refChanged(DataRef *ref) {
Q_ASSERT(_subscribedRefs.contains(ref));
Q_ASSERT(ref->isValid()); // Never send invalid values.

// Check if the ref has changed enough to be worth sending
if(ref->type()== extplaneRefTypeFloat) {
FloatDataRef *refF = qobject_cast<FloatDataRef*>(ref);
if(qAbs(refF->value() - _refValueF[ref]) < ref->accuracy())
@@ -282,6 +283,18 @@ void TcpClient::refChanged(DataRef *ref) {
INFO << "Ref type " << ref->type() << " not supported (this should not happen!)";
return;
}

// Send the ref value if we got this far..
sendRef(ref);

if(ref->shouldUnsubscribeAfterChange())
unsubscribeRef(ref->name());
}

void TcpClient::sendRef(DataRef *ref) {
Q_ASSERT(ref);
Q_ASSERT(ref->isValid());

QByteArray block;
QTextStream out(&block, QIODevice::WriteOnly);
out << "u" << ref->typeString() << " " << ref->name() << " " << ref->valueString() << "\n";
@@ -291,8 +304,6 @@ void TcpClient::refChanged(DataRef *ref) {
_socket->write(block);
// _socket->flush(); Not really needed and may mess up performance
}
if(ref->shouldUnsubscribeAfterChange())
unsubscribeRef(ref->name());
}

QStringList TcpClient::listRefs() {
2 changes: 2 additions & 0 deletions extplane-server/tcpclient.h
Original file line number Diff line number Diff line change
@@ -33,6 +33,8 @@ public slots:
private:
DataRef *getSubscribedRef(const QString &name);
void unsubscribeRef(const QString &name);
void sendRef(DataRef *ref); // Sends the ref value to the client

QTcpSocket *_socket;
QSet<DataRef*> _subscribedRefs;
QMap<DataRef*, double> _refValueD;

0 comments on commit 7a0e1fe

Please sign in to comment.