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::open() {
00009   if(opened++>0)
00010     return true;
00011   sbuf.setReconnect(block);
00012   block.addPrimitiveListener(dynamic_cast<plist::PrimitiveListener*>(this));
00013   host.addPrimitiveListener(dynamic_cast<plist::PrimitiveListener*>(this));
00014   port.addPrimitiveListener(dynamic_cast<plist::PrimitiveListener*>(this));
00015   transport.addPrimitiveListener(dynamic_cast<plist::PrimitiveListener*>(this));
00016   server.addPrimitiveListener(dynamic_cast<plist::PrimitiveListener*>(this));
00017   curaddr.set_name(host);
00018   curaddr.set_port(port);
00019   return doOpen(true);
00020 }
00021 
00022 bool NetworkCommPort::doOpen(bool dispError) {
00023   if(lastAttempt.Age()<.75)
00024     return false;
00025   lastAttempt.Set();
00026   if(server) {
00027     if(block)
00028       cout << "Waiting for '" << instanceName << "' connection on port " << port << "... " << flush;
00029     if(!sbuf.listen(curaddr,transport==Config::UDP)) {
00030       if(dispError) {
00031         stringstream ss;
00032         ss << "Could not listen on port " << port;
00033         connectionError(ss.str(),true);
00034       }
00035       return false;
00036     }
00037   } else {
00038     if(block)
00039       cout << "Waiting for '" << instanceName << "' connection to " << host << ':' << port << "... " << flush;
00040     if(!sbuf.open(curaddr,transport==Config::UDP)) {
00041       if(dispError) {
00042         stringstream ss;
00043         ss << "Could not open connection to " << host << ":" << port;
00044         connectionError(ss.str(),true);
00045       }
00046       return false;
00047     }
00048   }
00049   if(block)
00050     cout << instanceName <<  " connected." << endl;
00051   return true;
00052 }
00053 
00054 bool NetworkCommPort::close() {
00055   MarkScope l(getLock());
00056   if(opened==0)
00057     std::cerr << "Warning: NetworkCommPort close() without open()" << std::endl;
00058   if(--opened>0)
00059     return false;
00060   sbuf.setReconnect(false);
00061   sbuf.close();
00062   host.removePrimitiveListener(dynamic_cast<plist::PrimitiveListener*>(this));
00063   port.removePrimitiveListener(dynamic_cast<plist::PrimitiveListener*>(this));
00064   transport.removePrimitiveListener(dynamic_cast<plist::PrimitiveListener*>(this));
00065   server.removePrimitiveListener(dynamic_cast<plist::PrimitiveListener*>(this));
00066   return true;
00067 }
00068 
00069 void NetworkCommPort::plistValueChanged(const plist::PrimitiveBase& pl) {
00070   if(&pl==&host || &pl==&port || &pl==&transport) {
00071     MarkScope l(getLock());
00072     if(host!=curaddr.get_name() || port!=curaddr.get_port() || curtrans!=transport) {
00073       close();
00074       open();
00075     }
00076   } else if(&pl==&server) {
00077     MarkScope l(getLock());
00078     close();
00079     open();
00080   } else if(&pl==&verbose) {
00081     sbuf.setEcho(verbose);
00082   } else if(&pl==&block) {
00083     sbuf.setReconnect(block);
00084   } else {
00085     std::cerr << "Unhandled value change in " << getClassName() << ": " << pl.get() << std::endl;
00086   }
00087 }
00088 
00089 /*! @file
00090  * @brief 
00091  * @author Ethan Tira-Thompson (ejt) (Creator)
00092  *
00093  * $Author: ejt $
00094  * $Name: tekkotsu-4_0 $
00095  * $Revision: 1.5 $
00096  * $State: Exp $
00097  * $Date: 2007/11/11 23:57:29 $
00098  */

Tekkotsu Hardware Abstraction Layer 4.0
Generated Thu Nov 22 01:00:53 2007 by Doxygen 1.5.4