Skip to content

Commit

Permalink
Can toggle simulated and non-simulated without restarting now
Browse files Browse the repository at this point in the history
  • Loading branch information
vranki committed Oct 17, 2018
1 parent 901f7f3 commit e27c971
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 16 deletions.
15 changes: 14 additions & 1 deletion clients/extplane-client-qt/dataref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ DataRef::DataRef(QObject *parent) : QObject(parent)
, m_clientDataRef(nullptr)
, m_client(nullptr)
{
m_client = &ExtPlaneClient::instance();
setClient(&ExtPlaneClient::instance());
}

void DataRef::subscribeIfPossible()
{
if(m_clientDataRef)
return;

if(m_name.isEmpty())
return;

if(m_client && !m_name.isEmpty()) {
m_clientDataRef = m_client->subscribeDataRef(m_name, m_accuracy);
if(!m_clientDataRef) {
Expand Down Expand Up @@ -54,11 +60,18 @@ void DataRef::setClient(ExtPlaneClient *client)
if (m_client == client)
return;

disconnect(client, 0, this, 0);
m_client = client;
emit clientChanged(m_client);
connect(client, &ExtPlaneClient::datarefProviderChanged, this, &DataRef::setDataRefProvider);
subscribeIfPossible();
}

void DataRef::setDataRefProvider() {
m_clientDataRef = nullptr;
if(client()->datarefProvider()) subscribeIfPossible();
}

QStringList& DataRef::values() {
return m_clientDataRef ? m_clientDataRef->values() : m_emptyStringList;
}
Expand Down
3 changes: 2 additions & 1 deletion clients/extplane-client-qt/dataref.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class ClientDataRef;
class ExtPlaneClient;

class DataRefProvicer;

/**
* Use this class to create datarefs from QML code and wherever you
Expand Down Expand Up @@ -44,6 +44,7 @@ public slots:
void setValue(QString _newValue, int index=0); // Set value (from client)
void setValues(QStringList values); // Set full array (from client)
void setClient(ExtPlaneClient* client);
void setDataRefProvider();

private:
void subscribeIfPossible();
Expand Down
12 changes: 10 additions & 2 deletions clients/extplane-client-qt/extplaneclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ void ExtPlaneClient::createClient()
m_simulatedExtplaneConnection.startConnection();
} else {
m_extplaneConnection.registerClient(this);
m_extplaneConnection.startConnection();
}
m_connection = m_simulated ? &m_simulatedExtplaneConnection : &m_extplaneConnection;
}
Expand Down Expand Up @@ -89,7 +90,7 @@ void ExtPlaneClient::cdrChanged(ClientDataRef *ref) {
}

void ExtPlaneClient::unsubscribeDataRef(QString name) {
DEBUG << name;
// DEBUG << name;
for(ClientDataRef *ref : m_dataRefs) {
if(ref->name() == name) {
m_dataRefs.removeOne(ref);
Expand Down Expand Up @@ -168,18 +169,25 @@ void ExtPlaneClient::setSimulated(bool simulated)
if (m_simulated == simulated)
return;

while(!m_dataRefs.isEmpty())
unsubscribeDataRef(m_dataRefs.first()->name());

qDebug() << Q_FUNC_INFO << simulated;

m_simulated = simulated;
m_connection = nullptr;
emit datarefProviderChanged(m_connection);
if(m_simulated) {
m_extplaneConnection.stopConnection();
m_connection = &m_simulatedExtplaneConnection;
m_simulatedExtplaneConnection.startConnection();
} else {
m_simulatedExtplaneConnection.stopConnection();
m_connection = &m_extplaneConnection;
m_extplaneConnection.startConnection();
}
emit simulatedChanged(m_simulated);
emit datarefProviderChanged(m_connection);

}

void ExtPlaneClient::valueSet(ClientDataRef *ref) {
Expand Down
33 changes: 23 additions & 10 deletions clients/extplane-client-qt/extplaneconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,27 @@ ExtPlaneConnection::ExtPlaneConnection(QObject *parent) : BasicTcpClient(parent)
, server_ok(false)
, updateInterval(0.333)
{
connect(this, SIGNAL(connectedChanged(bool)), this, SLOT(tcpClientConnected()));
connect(this, SIGNAL(connectedChanged(bool)), this, SLOT(connectedChanged(bool)));
connect(this, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)));
connect(this, &BasicTcpClient::receivedLine, this, &ExtPlaneConnection::receivedLineSlot);
setHostName("localhost");
setPort(51000);
}

void ExtPlaneConnection::startConnection() {
DEBUG << "Starting real connection";
emit connectionMessage("Starting real connection");
BasicTcpClient::startConnection();
}

void ExtPlaneConnection::stopConnection() {
DEBUG << "Stopping real connection";
BasicTcpClient::disconnectFromHost();
qDeleteAll(dataRefs.values());
dataRefs.clear();
emit connectionMessage("Stopped real");
}

void ExtPlaneConnection::setUpdateInterval(double newInterval) {
updateInterval = newInterval;
if(server_ok & (updateInterval > 0)) {
Expand All @@ -24,9 +38,8 @@ void ExtPlaneConnection::setUpdateInterval(double newInterval) {
}
}


void ExtPlaneConnection::tcpClientConnected() {
if(connected()) {
void ExtPlaneConnection::connectedChanged(bool connected) {
if(connected) {
emit connectionMessage("Connected to ExtPlane, waiting for handshake");
} else {
emit connectionMessage("Socket disconnected");
Expand All @@ -46,7 +59,7 @@ void ExtPlaneConnection::registerClient(ExtPlaneClient* client) {
ClientDataRef *ExtPlaneConnection::subscribeDataRef(QString name, double accuracy) {
ClientDataRef *ref = dataRefs.value(name);
if(ref) {
// DEBUG << "Ref already subscribed";
DEBUG << "Ref already subscribed";
ref->setSubscribers(ref->subscribers()+1);
if(accuracy < ref->accuracy()) {
if(server_ok)
Expand All @@ -73,7 +86,7 @@ ClientDataRef *ExtPlaneConnection::createDataRef(QString name, double accuracy)
}

void ExtPlaneConnection::unsubscribeDataRef(ClientDataRef *ref) {
DEBUG << ref << ref->name() << ref->subscribers();
// DEBUG << ref << ref->name() << ref->subscribers();
ref->setSubscribers(ref->subscribers() - 1);
if(ref->subscribers() > 0) return;
// DEBUG << "Ref not subscribed by anyone anymore";
Expand All @@ -86,14 +99,14 @@ void ExtPlaneConnection::unsubscribeDataRef(ClientDataRef *ref) {
}

void ExtPlaneConnection::receivedLineSlot(QString & line) {
DEBUG << "Server says: " << line;
//DEBUG << line;
if(!server_ok) { // Waiting for handshake..
if(line=="EXTPLANE 1") {
server_ok = true;
emit connectionMessage("");
emit connectionMessage(QString("Connected to ExtPlane at %1:%2").arg(hostName()).arg(port()));
setUpdateInterval(updateInterval);
// Sub all refs
foreach(ClientDataRef *ref, dataRefs)
for(ClientDataRef *ref : dataRefs)
subRef(ref);
}
return;
Expand Down Expand Up @@ -126,7 +139,7 @@ void ExtPlaneConnection::receivedLineSlot(QString & line) {
}

void ExtPlaneConnection::subRef(ClientDataRef *ref) {
// DEBUG;
// DEBUG << ref->name();
Q_ASSERT(server_ok);
QString subLine = "sub " + ref->name();
if(ref->accuracy() != 0) {
Expand Down
4 changes: 3 additions & 1 deletion clients/extplane-client-qt/extplaneconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ public slots:
virtual void setValue(ClientDataRef *ref);
void setUpdateInterval(double newInterval);
void receivedLineSlot(QString &line);
virtual void startConnection();
virtual void stopConnection();

private slots:
void tcpClientConnected();
void connectedChanged(bool connected);
void socketError(QAbstractSocket::SocketError err);

protected:
Expand Down
11 changes: 11 additions & 0 deletions clients/extplane-client-qt/simulatedextplaneconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,22 @@ SimulatedExtPlaneConnection::SimulatedExtPlaneConnection(QObject *parent) : ExtP
}

void SimulatedExtPlaneConnection::startConnection() {
DEBUG << "Starting simulated connection";
server_ok = true;
emit connectionMessage("Connected to ExtPlane (simulated)");
tickTimer.start();
}

void SimulatedExtPlaneConnection::stopConnection() {
DEBUG << "Stopping simulated connection";
server_ok = false;
tickTimer.stop();

qDeleteAll(simulatedRefs);
simulatedRefs.clear();
emit connectionMessage("Stopped simulated");
}

ClientDataRef *SimulatedExtPlaneConnection::createDataRef(QString name, double accuracy) {
Q_UNUSED(accuracy);
SimulatedDataRef *simRef = nullptr;
Expand Down
2 changes: 2 additions & 0 deletions clients/extplane-client-qt/simulatedextplaneconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class SimulatedExtPlaneConnection : public ExtPlaneConnection {
public slots:
virtual void unsubscribeDataRef(ClientDataRef *ref);
virtual void startConnection();
virtual void stopConnection();

private slots:
void tickTime();
private:
Expand Down
2 changes: 2 additions & 0 deletions util/basictcpclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ void BasicTcpClient::setPort(int port)


void BasicTcpClient::tryReconnect() {
DEBUG << m_host;
if(m_host.isEmpty() || !m_port) {
emit connectionMessage("Please set host and port to connect");
return;
Expand All @@ -100,6 +101,7 @@ void BasicTcpClient::tryReconnect() {

void BasicTcpClient::socketStateChanged(QAbstractSocket::SocketState state)
{
// DEBUG << state;
if(connected()) {
reconnectTimer.stop();
m_networkError = "";
Expand Down
1 change: 0 additions & 1 deletion util/basictcpclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class BasicTcpClient : public QTcpSocket
QString hostName() const;
int port() const;
bool connected() const;

QString networkError() const;

public slots:
Expand Down

0 comments on commit e27c971

Please sign in to comment.