Equalizer
1.2.1
|
00001 00002 /* Copyright (c) 2005-2011, 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 <co/base/lockable.h> // member 00028 #include <co/base/spinLock.h> // member 00029 00030 namespace co 00031 { 00040 class Node : public Dispatcher, public base::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; } 00054 00056 00062 bool isLocal() const { return isListening(); } 00063 00069 CO_API void addConnectionDescription( ConnectionDescriptionPtr cd ); 00070 00078 CO_API bool removeConnectionDescription(ConnectionDescriptionPtr cd); 00079 00081 CO_API ConnectionDescriptions getConnectionDescriptions() const; 00082 00084 ConnectionPtr getConnection() const { return _outgoing; } 00085 00087 ConnectionPtr getMulticast(); 00089 00098 bool send( const Packet& packet ) 00099 { 00100 ConnectionPtr connection = _getConnection(); 00101 if( !connection ) 00102 return false; 00103 return connection->send( packet ); 00104 } 00105 00121 bool send( Packet& packet, const std::string& string ) 00122 { 00123 ConnectionPtr connection = _getConnection(); 00124 if( !connection ) 00125 return false; 00126 return connection->send( packet, string ); 00127 } 00128 00142 template< class T > 00143 bool send( Packet& packet, const std::vector<T>& data ) 00144 { 00145 ConnectionPtr connection = _getConnection(); 00146 if( !connection ) 00147 return false; 00148 return connection->send( packet, data ); 00149 } 00150 00167 bool send( Packet& packet, const void* data, const uint64_t size ) 00168 { 00169 ConnectionPtr connection = _getConnection(); 00170 if( !connection ) 00171 return false; 00172 return connection->send( packet, data, size ); 00173 } 00174 00181 bool multicast( const Packet& packet ) 00182 { 00183 ConnectionPtr connection = getMulticast(); 00184 if( !connection ) 00185 return false; 00186 return connection->send( packet ); 00187 } 00189 00190 const NodeID& getNodeID() const { return _id; } 00191 00193 CO_API std::string serialize() const; 00195 CO_API bool deserialize( std::string& data ); 00196 00198 int64_t getLastReceiveTime() const { return _lastReceive; } 00199 00201 virtual uint32_t getType() const { return NODETYPE_CO_NODE; } 00202 00203 protected: 00205 CO_API virtual ~Node(); 00206 00214 CO_API virtual NodePtr createNode( const uint32_t type ); 00215 00216 private: 00218 enum State 00219 { 00220 STATE_CLOSED, 00221 STATE_CONNECTED, 00222 STATE_LISTENING, 00223 STATE_CLOSING 00224 }; 00225 00226 friend CO_API std::ostream& operator << ( std::ostream& os, 00227 const Node& node ); 00228 friend CO_API std::ostream& operator << ( std::ostream&, 00229 const State ); 00230 friend class LocalNode; 00231 00233 NodeID _id; 00234 00236 State _state; 00237 00239 ConnectionPtr _outgoing; 00240 00242 base::Lockable< ConnectionPtr > _outMulticast; 00243 00244 struct MCData 00245 { 00246 ConnectionPtr connection; 00247 NodePtr node; 00248 }; 00249 typedef std::vector< MCData > MCDatas; 00250 00258 MCDatas _multicasts; 00259 00261 base::Lockable< ConnectionDescriptions, base::SpinLock > 00262 _connectionDescriptions; 00263 00265 ConnectionPtr _getConnection() 00266 { 00267 ConnectionPtr connection = _outgoing; 00268 if( _state != STATE_CLOSED ) 00269 return connection; 00270 EQUNREACHABLE; 00271 return 0; 00272 } 00273 00275 int64_t _lastReceive; 00276 }; 00277 00278 CO_API std::ostream& operator << ( std::ostream& os, const Node& node ); 00279 CO_API std::ostream& operator << ( std::ostream&, const Node::State ); 00280 } 00281 00282 #endif // CO_NODE_H