Added logic to reconnect after connection failure;

Implemented option to stop receiving the proto replies;


Former-commit-id: d8dfc3f36e81d727c4b7d449888f781b7b57c99e
This commit is contained in:
johan 2014-01-26 13:16:58 +01:00
parent c96be1cf38
commit 8cf39a9f6f
2 changed files with 71 additions and 55 deletions

View File

@ -17,21 +17,18 @@ ProtoConnection::ProtoConnection(const std::string & a) :
{ {
throw std::runtime_error(QString("Wrong address: unable to parse address (%1)").arg(address).toStdString()); throw std::runtime_error(QString("Wrong address: unable to parse address (%1)").arg(address).toStdString());
} }
_host = parts[0];
bool ok; bool ok;
uint16_t port = parts[1].toUShort(&ok); _port = parts[1].toUShort(&ok);
if (!ok) if (!ok)
{ {
throw std::runtime_error(QString("Wrong address: Unable to parse the port number (%1)").arg(parts[1]).toStdString()); throw std::runtime_error(QString("Wrong address: Unable to parse the port number (%1)").arg(parts[1]).toStdString());
} }
_socket.connectToHost(parts[0], port); // try to connect to host
if (!_socket.waitForConnected()) std::cout << "Connecting to Hyperion: " << _host.toStdString() << ":" << _port << std::endl;
{ connectToHost();
throw std::runtime_error("Unable to connect to host");
}
std::cout << "Connected to " << a << std::endl;
} }
ProtoConnection::~ProtoConnection() ProtoConnection::~ProtoConnection()
@ -54,10 +51,7 @@ void ProtoConnection::setColor(const ColorRgb & color, int priority, int duratio
colorRequest->set_duration(duration); colorRequest->set_duration(duration);
// send command message // send command message
proto::HyperionReply reply = sendMessage(request); sendMessage(request);
// parse reply message
parseReply(reply);
} }
void ProtoConnection::setImage(const Image<ColorRgb> &image, int priority, int duration) void ProtoConnection::setImage(const Image<ColorRgb> &image, int priority, int duration)
@ -72,10 +66,7 @@ void ProtoConnection::setImage(const Image<ColorRgb> &image, int priority, int d
imageRequest->set_duration(duration); imageRequest->set_duration(duration);
// send command message // send command message
proto::HyperionReply reply = sendMessage(request); sendMessage(request);
// parse reply message
// parseReply(reply);
} }
void ProtoConnection::clear(int priority) void ProtoConnection::clear(int priority)
@ -86,10 +77,7 @@ void ProtoConnection::clear(int priority)
clearRequest->set_priority(priority); clearRequest->set_priority(priority);
// send command message // send command message
proto::HyperionReply reply = sendMessage(request); sendMessage(request);
// parse reply message
parseReply(reply);
} }
void ProtoConnection::clearAll() void ProtoConnection::clearAll()
@ -98,14 +86,32 @@ void ProtoConnection::clearAll()
request.set_command(proto::HyperionRequest::CLEARALL); request.set_command(proto::HyperionRequest::CLEARALL);
// send command message // send command message
proto::HyperionReply reply = sendMessage(request); sendMessage(request);
// parse reply message
parseReply(reply);
} }
proto::HyperionReply ProtoConnection::sendMessage(const proto::HyperionRequest &message) void ProtoConnection::connectToHost()
{ {
_socket.connectToHost(_host, _port);
if (_socket.waitForConnected()) {
std::cout << "Connected to Hyperion host" << std::endl;
}
}
void ProtoConnection::sendMessage(const proto::HyperionRequest &message)
{
if (_socket.state() == QAbstractSocket::UnconnectedState)
{
std::cout << "Currently disconnected: trying to connect to host" << std::endl;
connectToHost();
}
if (_socket.state() != QAbstractSocket::ConnectedState)
{
return;
}
// We only get here if we are connected
// serialize message (FastWriter already appends a newline) // serialize message (FastWriter already appends a newline)
std::string serializedMessage = message.SerializeAsString(); std::string serializedMessage = message.SerializeAsString();
@ -122,9 +128,12 @@ proto::HyperionReply ProtoConnection::sendMessage(const proto::HyperionRequest &
count += _socket.write(reinterpret_cast<const char *>(serializedMessage.data()), length); count += _socket.write(reinterpret_cast<const char *>(serializedMessage.data()), length);
if (!_socket.waitForBytesWritten()) if (!_socket.waitForBytesWritten())
{ {
throw std::runtime_error("Error while writing data to host"); std::cerr << "Error while writing data to host" << std::endl;
return;
} }
if (!_skipReply)
{
// read reply data // read reply data
QByteArray serializedReply; QByteArray serializedReply;
length = -1; length = -1;
@ -133,7 +142,8 @@ proto::HyperionReply ProtoConnection::sendMessage(const proto::HyperionRequest &
// receive reply // receive reply
if (!_socket.waitForReadyRead()) if (!_socket.waitForReadyRead())
{ {
throw std::runtime_error("Error while reading data from host"); std::cerr << "Error while reading data from host" << std::endl;
return;
} }
serializedReply += _socket.readAll(); serializedReply += _socket.readAll();
@ -153,10 +163,9 @@ proto::HyperionReply ProtoConnection::sendMessage(const proto::HyperionRequest &
proto::HyperionReply reply; proto::HyperionReply reply;
reply.ParseFromArray(serializedReply.constData()+4, length); reply.ParseFromArray(serializedReply.constData()+4, length);
// remove data from receive buffer // parse reply message
serializedReply = serializedReply.mid(length+4); parseReply(reply);
}
return reply;
} }
bool ProtoConnection::parseReply(const proto::HyperionReply &reply) bool ProtoConnection::parseReply(const proto::HyperionReply &reply)

View File

@ -68,14 +68,15 @@ public:
void clearAll(); void clearAll();
private: private:
/// Try to connect to the Hyperion host
void connectToHost();
/// ///
/// Send a command message and receive its reply /// Send a command message and receive its reply
/// ///
/// @param message The message to send /// @param message The message to send
/// ///
/// @return The returned reply void sendMessage(const proto::HyperionRequest & message);
///
proto::HyperionReply sendMessage(const proto::HyperionRequest & message);
/// ///
/// Parse a reply message /// Parse a reply message
@ -90,6 +91,12 @@ private:
/// The TCP-Socket with the connection to the server /// The TCP-Socket with the connection to the server
QTcpSocket _socket; QTcpSocket _socket;
/// Host address
QString _host;
/// Host port
uint16_t _port;
/// Skip receiving reply messages from Hyperion if set /// Skip receiving reply messages from Hyperion if set
bool _skipReply; bool _skipReply;
}; };