From bc0fdcf1d0127e5f5bdf4754e985a4292e004d78 Mon Sep 17 00:00:00 2001 From: Bill Somerville Date: Mon, 1 Sep 2014 17:33:05 +0000 Subject: [PATCH] Add retries to HRD send command mechanism. It appears that the HRD logbook interferes with the HRD IP server causing it to fail to respond to pending commands. This enhancement adds a number of retries to the send command to HRD operation. git-svn-id: svn+ssh://svn.code.sf.net/p/wsjt/wsjt/branches/wsjtx@4241 ab8295b8-cf94-4d9e-aec4-7959e3be5d79 --- HRDTransceiver.cpp | 97 +++++++++++++++++++++++++++++++--------------- 1 file changed, 65 insertions(+), 32 deletions(-) diff --git a/HRDTransceiver.cpp b/HRDTransceiver.cpp index d346833a7..997862b31 100644 --- a/HRDTransceiver.cpp +++ b/HRDTransceiver.cpp @@ -701,48 +701,75 @@ QString HRDTransceiver::send_command (QString const& cmd, bool no_debug, bool pr auto context = '[' + QString::number (current_radio_) + "] "; - int bytes_to_send; - int bytes_sent; - if (v4 == protocol_) + unsigned retries {5}; + bool replied {false}; + while (!replied && --retries) { - auto message = ((prepend_context ? context + cmd : cmd) + "\r").toLocal8Bit (); - bytes_to_send = message.size (); - bytes_sent = hrd_->write (message.data (), bytes_to_send); + int bytes_to_send; + int bytes_sent; + if (v4 == protocol_) + { + auto message = ((prepend_context ? context + cmd : cmd) + "\r").toLocal8Bit (); + bytes_to_send = message.size (); + bytes_sent = hrd_->write (message.data (), bytes_to_send); - if (!no_debug) + if (!no_debug) + { +#if WSJT_TRACE_CAT + qDebug () << "HRDTransceiver::send_command:" << message; +#endif + } + } + else + { + auto string = prepend_context ? context + cmd : cmd; + QScopedPointer message (new (string) HRDMessage); + bytes_to_send = message->size_; + bytes_sent = hrd_->write (reinterpret_cast (message.data ()), bytes_to_send); + } + + if (bytes_sent < bytes_to_send + || !hrd_->waitForBytesWritten (socket_wait_time) + || QTcpSocket::ConnectedState != hrd_->state ()) { #if WSJT_TRACE_CAT - qDebug () << "HRDTransceiver::send_command:" << message; + qDebug () << "HRDTransceiver::send_command \"" << cmd << "\" failed" << hrd_->errorString (); #endif + + throw error { + tr ("Ham Radio Deluxe send command \"%1\" failed %2\n") + .arg (cmd) + .arg (hrd_->errorString ()) + }; + } + + replied = hrd_->waitForReadyRead (socket_wait_time); + if (!replied && hrd_->error () != hrd_->SocketTimeoutError) + { +#if WSJT_TRACE_CAT + qDebug () << "HRDTransceiver::send_command \"" << cmd << "\" failed to reply" << hrd_->errorString (); +#endif + + throw error { + tr ("Ham Radio Deluxe failed to reply to command \"%1\" %2\n") + .arg (cmd) + .arg (hrd_->errorString ()) + }; } } - else - { - auto string = prepend_context ? context + cmd : cmd; - QScopedPointer message (new (string) HRDMessage); - bytes_to_send = message->size_; - bytes_sent = hrd_->write (reinterpret_cast (message.data ()), bytes_to_send); - } - if (bytes_sent < bytes_to_send - || !hrd_->waitForBytesWritten (socket_wait_time) - || QTcpSocket::ConnectedState != hrd_->state ()) + if (!replied) { #if WSJT_TRACE_CAT - qDebug () << "HRDTransceiver::send_command failed" << hrd_->errorString (); + qDebug () << "HRDTransceiver::send_command \"" << cmd << "\" retries exhausted"; #endif - throw error {tr ("Ham Radio Deluxe send command failed\n") + hrd_->errorString ()}; + throw error { + tr ("Ham Radio Deluxe retries exhausted sending command \"%1\"") + .arg (cmd) + }; } - if (!hrd_->waitForReadyRead (socket_wait_time)) - { -#if WSJT_TRACE_CAT - qDebug () << "HRDTransceiver::send_command failed to reply" << hrd_->errorString (); -#endif - - throw error {tr ("Ham Radio Deluxe failed to reply to command\n") + hrd_->errorString ()}; - } QByteArray buffer (hrd_->readAll ()); if (!no_debug) { @@ -762,10 +789,13 @@ QString HRDTransceiver::send_command (QString const& cmd, bool no_debug, bool pr if (reply->magic_1_value_ != reply->magic_1_ && reply->magic_2_value_ != reply->magic_2_) { #if WSJT_TRACE_CAT - qDebug () << "HRDTransceiver::send_command invalid reply"; + qDebug () << "HRDTransceiver::send_command \"" << cmd << "\" invalid reply"; #endif - throw error {tr ("Ham Radio Deluxe sent an invalid reply to our command")}; + throw error { + tr ("Ham Radio Deluxe sent an invalid reply to our command \"%1\"") + .arg (cmd) + }; } result = QString {reply->payload_}; // this is not a memory leak (honest!) @@ -786,9 +816,12 @@ void HRDTransceiver::send_simple_command (QString const& command, bool no_debug) if ("OK" != send_command (command, no_debug)) { #if WSJT_TRACE_CAT - qDebug () << "HRDTransceiver::send_simple_command unexpected response"; + qDebug () << "HRDTransceiver::send_simple_command \"" << command << "\" unexpected response"; #endif - throw error {tr ("Ham Radio Deluxe didn't respond as expected")}; + throw error { + tr ("Ham Radio Deluxe didn't respond to command \"%1\" as expected") + .arg (command) + }; } }