Collage
0.6.1
|
00001 00002 /* Copyright (c) 2005-2012, Stefan Eilemann <eile@equalizergraphics.com> 00003 * 2010, Cedric Stalder <cedric.stalder@gmail.com> 00004 * 00005 * This library is free software; you can redistribute it and/or modify it under 00006 * the terms of the GNU Lesser General Public License version 2.1 as published 00007 * by the Free Software Foundation. 00008 * 00009 * This library is distributed in the hope that it will be useful, but WITHOUT 00010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00011 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00012 * details. 00013 * 00014 * You should have received a copy of the GNU Lesser General Public License 00015 * along with this library; if not, write to the Free Software Foundation, Inc., 00016 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00017 */ 00018 00019 #ifndef CO_NODE_H 00020 #define CO_NODE_H 00021 00022 #include <co/dispatcher.h> // base class 00023 #include <co/connection.h> // member - ConnectionPtr 00024 #include <co/nodeType.h> // for NODETYPE_CO_NODE enum 00025 #include <co/types.h> 00026 00027 #include <lunchbox/lockable.h> // member 00028 #include <lunchbox/spinLock.h> // member 00029 00030 namespace co 00031 { 00040 class Node : public Dispatcher, public lunchbox::Referenced 00041 { 00042 public: 00044 CO_API Node(); 00045 00048 bool operator == ( const Node* n ) const; 00049 00050 bool isConnected() const { return !isClosed();} 00051 bool isClosed() const { return _state == STATE_CLOSED; } 00052 bool isListening() const { return _state == STATE_LISTENING || 00053 _state == STATE_CLOSING; } 00055 00059 bool isLocal() const { return isListening(); } 00060 00066 CO_API void addConnectionDescription( ConnectionDescriptionPtr cd ); 00067 00075 CO_API bool removeConnectionDescription(ConnectionDescriptionPtr cd); 00076 00078 CO_API ConnectionDescriptions getConnectionDescriptions() const; 00079 00081 ConnectionPtr getConnection() const { return _outgoing; } 00082 00084 ConnectionPtr useMulticast(); 00086 00095 bool send( const Packet& packet ) 00096 { 00097 ConnectionPtr connection = _getConnection(); 00098 if( !connection ) 00099 return false; 00100 return connection->send( packet ); 00101 } 00102 00118 bool send( Packet& packet, const std::string& string ) 00119 { 00120 ConnectionPtr connection = _getConnection(); 00121 if( !connection ) 00122 return false; 00123 return connection->send( packet, string ); 00124 } 00125 00139 template< class T > 00140 bool send( Packet& packet, const std::vector<T>& data ) 00141 { 00142 ConnectionPtr connection = _getConnection(); 00143 if( !connection ) 00144 return false; 00145 return connection->send( packet, data ); 00146 } 00147 00164 bool send( Packet& packet, const void* data, const uint64_t size ) 00165 { 00166 ConnectionPtr connection = _getConnection(); 00167 if( !connection ) 00168 return false; 00169 return connection->send( packet, data, size ); 00170 } 00171 00178 bool multicast( const Packet& packet ) 00179 { 00180 ConnectionPtr connection = useMulticast(); 00181 if( !connection ) 00182 return false; 00183 return connection->send( packet ); 00184 } 00186 00187 const NodeID& getNodeID() const { return _id; } 00188 00190 CO_API std::string serialize() const; 00192 CO_API bool deserialize( std::string& data ); 00193 00195 int64_t getLastReceiveTime() const { return _lastReceive; } 00196 00198 virtual uint32_t getType() const { return NODETYPE_CO_NODE; } 00199 00200 protected: 00202 CO_API virtual ~Node(); 00203 00211 CO_API virtual NodePtr createNode( const uint32_t type ); 00212 00213 private: 00215 enum State 00216 { 00217 STATE_CLOSED, 00218 STATE_CONNECTED, 00219 STATE_LISTENING, 00220 STATE_CLOSING 00221 }; 00222 00223 friend CO_API std::ostream& operator << ( std::ostream& os, 00224 const Node& node ); 00225 friend CO_API std::ostream& operator << ( std::ostream&, 00226 const State ); 00227 friend class LocalNode; 00228 00230 NodeID _id; 00231 00233 State _state; 00234 00236 ConnectionPtr _outgoing; 00237 00239 lunchbox::Lockable< ConnectionPtr > _outMulticast; 00240 00241 struct MCData 00242 { 00243 ConnectionPtr connection; 00244 NodePtr node; 00245 }; 00246 typedef std::vector< MCData > MCDatas; 00247 00255 MCDatas _multicasts; 00256 00258 lunchbox::Lockable< ConnectionDescriptions, lunchbox::SpinLock > 00259 _connectionDescriptions; 00260 00262 int64_t _lastReceive; 00263 00265 ConnectionPtr _getConnection() 00266 { 00267 ConnectionPtr connection = _outgoing; 00268 if( _state != STATE_CLOSED ) 00269 return connection; 00270 LBUNREACHABLE; 00271 return 0; 00272 } 00273 }; 00274 00275 CO_API std::ostream& operator << ( std::ostream& os, const Node& node ); 00276 CO_API std::ostream& operator << ( std::ostream&, const Node::State ); 00277 } 00278 00279 #endif // CO_NODE_H