Changeset 13294
- Timestamp:
- 03/30/12 22:29:56 (13 years ago)
- Location:
- trunk/FACT++/src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/ConnectionUSB.cc
r12540 r13294 69 69 // ------------------------ close -------------------------- 70 70 // close from another thread 71 void ConnectionUSB::CloseImp( bool restart)71 void ConnectionUSB::CloseImp(int64_t delay) 72 72 { 73 73 if (IsConnected()) 74 { 75 ostringstream str; 76 str << "Closing connection to " << URL() << "."; 77 Info(str); 78 } 74 Info("Closing connection to "+URL()+"."); 79 75 80 76 // Close possible open connections 81 77 bs::error_code ec; 82 78 cancel(ec); 83 if (ec) 84 Error("Cancel async requests on "+URL()+": "+ec.message()); 79 if (ec && ec!=ba::error::basic_errors::bad_descriptor) 80 { 81 ostringstream msg; 82 msg << "Cancel async requests on " << URL() << ": " << ec.message() << " (" << ec << ")"; 83 Error(msg); 84 } 85 85 86 86 if (IsConnected()) … … 88 88 close(ec); 89 89 if (ec) 90 Error("Closing "+URL()+": "+ec.message()); 90 { 91 ostringstream msg; 92 msg << "Closing " << URL() << ": " << ec.message() << " (" << ec << ")"; 93 Error(msg); 94 } 91 95 else 92 Info("C onnection closedsuccesfully.");96 Info("Closed connection to "+URL()+" succesfully."); 93 97 } 94 98 … … 96 100 fInTimeout.cancel(); 97 101 fOutTimeout.cancel(); 102 fConnectTimeout.cancel(); 98 103 99 104 // Reset the connection status … … 112 117 #endif 113 118 114 if ( !restart|| IsConnecting())119 if (delay<0 || IsConnecting()) 115 120 return; 116 121 117 122 // We need some timeout before reconnecting! 118 123 // And we have to check if we are alreayd trying to connect 119 // We shoudl wait until all operations in progress were canceled 124 // We should wait until all operations in progress were canceled 125 fConnectTimeout.expires_from_now(boost::posix_time::seconds(delay)); 126 fConnectTimeout.async_wait(boost::bind(&ConnectionUSB::HandleReconnectTimeout, this, dummy::error)); 127 } 128 129 void ConnectionUSB::PostClose(int64_t delay) 130 { 131 get_io_service().post(boost::bind(&ConnectionUSB::CloseImp, this, delay)); 132 } 133 134 void ConnectionUSB::HandleReconnectTimeout(const bs::error_code &error) 135 { 136 if (error==ba::error::basic_errors::operation_aborted) 137 return; 138 139 // 125: Operation canceled (bs::error_code(125, bs::system_category)) 140 if (error) 141 { 142 ostringstream str; 143 str << "Reconnect timeout of " << URL() << ": " << error.message() << " (" << error << ")";// << endl; 144 Error(str); 145 146 CloseImp(-1); 147 return; 148 } 149 150 151 if (is_open()) 152 { 153 Error("HandleReconnectTimeout - "+URL()+" is already open."); 154 return; 155 } 156 157 // Check whether the deadline has passed. We compare the deadline 158 // against the current time since a new asynchronous operation 159 // may have moved the deadline before this actor had a chance 160 // to run. 161 if (fConnectTimeout.expires_at() > ba::deadline_timer::traits_type::now()) 162 return; 120 163 121 164 // Start trying to reconnect … … 123 166 } 124 167 125 void ConnectionUSB::PostClose(bool restart)126 {127 get_io_service().post(boost::bind(&ConnectionUSB::CloseImp, this, restart));128 }129 168 130 169 // ------------------------ write -------------------------- … … 141 180 Error(str); 142 181 143 CloseImp( false);182 CloseImp(-1); 144 183 return; 145 184 } … … 161 200 Error("fOutTimeout has expired, writing data to "+URL()); 162 201 163 CloseImp( false);202 CloseImp(-1); 164 203 } 165 204 … … 172 211 Error(str); 173 212 174 CloseImp( false);213 CloseImp(-1); 175 214 return; 176 215 } … … 329 368 fCharacterSize(8), fParity(parity::none), fStopBits(stop_bits::one), 330 369 fFlowControl(flow_control::hardware), 331 fInTimeout(ioservice), fOutTimeout(ioservice), 370 fInTimeout(ioservice), fOutTimeout(ioservice), fConnectTimeout(ioservice), 332 371 fConnectionStatus(kDisconnected) 333 372 { -
trunk/FACT++/src/ConnectionUSB.h
r12236 r13294 36 36 private: 37 37 boost::asio::deadline_timer fOutTimeout; 38 boost::asio::deadline_timer fConnectTimeout; 38 39 std::deque<std::vector<uint8_t>> fOutQueue; 39 40 … … 52 53 53 54 protected: 54 void CloseImp( bool restart=true);55 void CloseImp(int64_t delay=0); 55 56 56 57 private: … … 60 61 void HandleWriteTimeout(const boost::system::error_code &error); 61 62 void HandleSentData(const boost::system::error_code& error, size_t); 63 void HandleReconnectTimeout(const boost::system::error_code &error); 62 64 63 65 int Write(const Time &t, const std::string &txt, int qos=kInfo); … … 75 77 76 78 // ------------------------ close -------------------------- 77 void PostClose( bool restart=true);79 void PostClose(int64_t delay=0); 78 80 79 81 // ------------------------ write -------------------------- -
trunk/FACT++/src/biasctrl.cc
r13258 r13294 55 55 uint32_t fUpdateTime; 56 56 uint16_t fSyncTime; 57 uint32_t fReconnectDelay; 57 58 58 59 int fIsInitializing; … … 61 62 62 63 vector<uint64_t> fCounter; 64 65 Time fLastConnect; 63 66 64 67 protected: … … 274 277 275 278 private: 279 void DelayedReconnect() 280 { 281 const Time now; 282 283 // If we have been connected without a diconnect for at least 60s 284 // we can reset the delay. 285 if (now-fLastConnect>boost::posix_time::seconds(60)) 286 fReconnectDelay = 1; 287 288 ostringstream msg; 289 msg << "Automatic reconnect in " << fReconnectDelay << "s after being connected for "; 290 msg << (now-fLastConnect).seconds() << "s"; 291 Info(msg); 292 293 CloseImp(fReconnectDelay); 294 fReconnectDelay *= 2; 295 } 296 276 297 void HandleReceivedData(const vector<uint8_t> &buf, size_t bytes_received, int command, int send_counter) 277 298 { … … 312 333 (cmd==kExpertChannelSet && !CheckMessageLength(bytes_received, 3, "CmdExpertChannelSet"))) 313 334 { 314 CloseImp( false);335 CloseImp(-1); 315 336 return; 316 337 } … … 321 342 if (!EvalAnswer(buf.data(), i, command)) 322 343 { 323 CloseImp(false);344 DelayedReconnect(); 324 345 return; 325 346 } … … 345 366 msg << "Corrupted answer: received wrap counter " << fWrapCounter << " is not send counter " << send_counter << "%8."; 346 367 Error(msg); 347 CloseImp(false); 368 369 DelayedReconnect(); 348 370 } 349 371 … … 418 440 if (command==kCmdGlobalSet) 419 441 fCounter[7]++; 420 421 442 } 422 443 … … 443 464 Error(str); 444 465 } 445 CloseImp( false);//err!=ba::error::basic_errors::operation_aborted);466 CloseImp(-1);//err!=ba::error::basic_errors::operation_aborted); 446 467 return; 447 468 } … … 452 473 { 453 474 Error("Number of received bytes not a multiple of 3, can't read data."); 454 CloseImp( false);475 CloseImp(-1); 455 476 return; 456 477 } … … 517 538 Error(str); 518 539 519 CloseImp( false);540 CloseImp(-1); 520 541 return; 521 542 } … … 538 559 { 539 560 Error("Synchronization attempt timed out."); 540 CloseImp( false);561 CloseImp(-1); 541 562 return; 542 563 } … … 576 597 fIsRamping = false; 577 598 599 fLastConnect = Time(); 600 578 601 // Send a single 0 (and possible two consecutive 0's 579 602 // to make sure we are in sync with the device) … … 603 626 Error(str); 604 627 605 PostClose(false);628 CloseImp(-1); 606 629 return; 607 630 } … … 763 786 Error(str); 764 787 765 PostClose(false);766 788 fIsRamping = false; 789 CloseImp(-1); 767 790 return; 768 791 } … … 813 836 fUpdateTime(3000), 814 837 fSyncTime(333), 838 fReconnectDelay(1), 815 839 fIsRamping(false), 816 840 fWaitingForAnswer(-1), … … 860 884 ostringstream msg; 861 885 msg << "CheckChannelVoltage - Set voltage " << volt << "V of channel " << ch << " exeeds absolute limit of " << fVoltageMaxAbs << "V."; 862 Error(msg);886 Warn(msg); 863 887 return false; 864 888 } … … 1448 1472 return BIAS::kVoltageOn; 1449 1473 } 1474 1475 void SetReconnectDelay(uint32_t delay=1) 1476 { 1477 fReconnectDelay = delay; 1478 } 1450 1479 }; 1451 1480 … … 1745 1774 { 1746 1775 // Close all connections 1747 fBias.PostClose( false);1776 fBias.PostClose(-1); 1748 1777 1749 1778 /* … … 1759 1788 { 1760 1789 // Close all connections to supress the warning in SetEndpoint 1761 fBias.PostClose( false);1790 fBias.PostClose(-1); 1762 1791 1763 1792 // Now wait until all connection have been closed and … … 1769 1798 1770 1799 // Now we can reopen the connection 1771 fBias.PostClose(true); 1800 fBias.SetReconnectDelay(); 1801 fBias.PostClose(0); 1772 1802 1773 1803 return T::GetCurrentState();
Note:
See TracChangeset
for help on using the changeset viewer.