Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

NetworkCommPort.cc

Go to the documentation of this file.
00001 #include "NetworkCommPort.h"
00002 #include "Shared/MarkScope.h"
00003 
00004 using namespace std;
00005 
00006 const std::string NetworkCommPort::autoRegisterNetworkCommPort = CommPort::getRegistry().registerType<NetworkCommPort>("NetworkCommPort");
00007 
00008 bool NetworkCommPort::isReadable() {
00009   sbuf.update_status();
00010   if(sbuf.is_open())
00011     return true;
00012   if(recon.isStarted())
00013     return false;
00014   if(openedCnt>0) {
00015     MarkScope l(getLock());
00016     if(recon.isStarted())
00017       return false;
00018     if(!doOpen(true)) {
00019       recon.start();
00020       return false;
00021     }
00022   }
00023   return sbuf.is_open();
00024 }
00025 
00026 bool NetworkCommPort::open() {
00027   MarkScope l(getLock());
00028   if(openedCnt++>0)
00029     return true;
00030   sbuf.setReconnect(block);
00031   block.addPrimitiveListener(dynamic_cast<plist::PrimitiveListener*>(this));
00032   host.addPrimitiveListener(dynamic_cast<plist::PrimitiveListener*>(this));
00033   port.addPrimitiveListener(dynamic_cast<plist::PrimitiveListener*>(this));
00034   transport.addPrimitiveListener(dynamic_cast<plist::PrimitiveListener*>(this));
00035   server.addPrimitiveListener(dynamic_cast<plist::PrimitiveListener*>(this));
00036   curaddr.set_name(host);
00037   curaddr.set_port(port);
00038   if(!doOpen(true)) {
00039     if(recon.isStarted())
00040       return false;
00041     recon.start();
00042     return false;
00043   }
00044   return true;
00045 }
00046 
00047 bool NetworkCommPort::doOpen(bool dispError) {
00048   MarkScope l(getLock());
00049   if(sbuf.is_open())
00050     return true;
00051   if(server) {
00052     if(block)
00053       cout << "Waiting for '" << instanceName << "' connection on port " << port << "... " << flush;
00054     if(!sbuf.listen(curaddr,transport==Config::UDP)) {
00055       Thread::testCurrentCancel();
00056       if(dispError) {
00057         stringstream ss;
00058         ss << "Could not listen '" << instanceName << "' on port " << port;
00059         connectionError(ss.str(),true);
00060       }
00061       return false;
00062     }
00063   } else {
00064     if(block)
00065       cout << "Waiting for '" << instanceName << "' connection to " << host << ':' << port << "... " << flush;
00066     if(!sbuf.open(curaddr,transport==Config::UDP)) {
00067       Thread::testCurrentCancel();
00068       if(dispError) {
00069         stringstream ss;
00070         ss << "Could not open '" << instanceName << "' connection to " << host << ":" << port;
00071         connectionError(ss.str(),true);
00072       }
00073       return false;
00074     }
00075   }
00076   if(block)
00077     cout << instanceName <<  " connected." << endl;
00078   sbuf.pubseekoff(0,ios_base::end,ios_base::in);
00079   opened();
00080   return true;
00081 }
00082 
00083 void NetworkCommPort::keepOpen() {
00084   while(!sbuf.is_open()) {
00085     sleep(1);
00086     Thread::testCurrentCancel();
00087     doOpen(false);
00088   }
00089 }
00090 
00091 bool NetworkCommPort::close() {
00092   MarkScope l(getLock());
00093   if(openedCnt==0)
00094     std::cerr << "Warning: NetworkCommPort close() without open()" << std::endl;
00095   if(--openedCnt>0)
00096     return false;
00097   sbuf.setReconnect(false);
00098   if(recon.isStarted())
00099     recon.stop().join();
00100   if(sbuf.is_open()) {
00101     closing();
00102     sbuf.close();
00103   }
00104   host.removePrimitiveListener(dynamic_cast<plist::PrimitiveListener*>(this));
00105   port.removePrimitiveListener(dynamic_cast<plist::PrimitiveListener*>(this));
00106   transport.removePrimitiveListener(dynamic_cast<plist::PrimitiveListener*>(this));
00107   server.removePrimitiveListener(dynamic_cast<plist::PrimitiveListener*>(this));
00108   return true;
00109 }
00110 
00111 void NetworkCommPort::plistValueChanged(const plist::PrimitiveBase& pl) {
00112   if(&pl==&host || &pl==&port || &pl==&transport) {
00113     MarkScope l(getLock());
00114     if(host!=curaddr.get_name() || port!=curaddr.get_port() || curtrans!=transport) {
00115       close();
00116       open();
00117     }
00118   } else if(&pl==&server) {
00119     MarkScope l(getLock());
00120     close();
00121     open();
00122   } else if(&pl==&verbose) {
00123     sbuf.setEcho(verbose);
00124   } else if(&pl==&block) {
00125     MarkScope l(getLock());
00126     if(recon.isStarted())
00127       recon.stop().join();
00128     sbuf.setReconnect(block);
00129   } else {
00130     std::cerr << "Unhandled value change in " << getClassName() << ": " << pl.get() << std::endl;
00131   }
00132 }
00133 
00134 /*! @file
00135  * @brief 
00136  * @author Ethan Tira-Thompson (ejt) (Creator)
00137  */

Tekkotsu Hardware Abstraction Layer 5.1CVS
Generated Mon May 9 05:01:39 2016 by Doxygen 1.6.3