Equalizer 1.0
|
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 00051 { return (_state == STATE_CONNECTED || _state == STATE_LISTENING);} 00052 bool isClosed() const { return _state == STATE_CLOSED; } 00053 bool isListening() const { return _state == STATE_LISTENING; } 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 00197 protected: 00199 CO_API virtual ~Node(); 00200 00202 virtual uint32_t getType() const { return NODETYPE_CO_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 }; 00221 00222 friend CO_API std::ostream& operator << ( std::ostream& os, 00223 const Node& node ); 00224 friend CO_API std::ostream& operator << ( std::ostream&, 00225 const State ); 00226 friend class LocalNode; 00227 00229 NodeID _id; 00230 00232 State _state; 00233 00235 ConnectionPtr _outgoing; 00236 00238 base::Lockable< ConnectionPtr > _outMulticast; 00239 00240 struct MCData 00241 { 00242 ConnectionPtr connection; 00243 NodePtr node; 00244 }; 00245 typedef std::vector< MCData > MCDatas; 00246 00254 MCDatas _multicasts; 00255 00257 base::Lockable< ConnectionDescriptions, base::SpinLock > 00258 _connectionDescriptions; 00259 00261 ConnectionPtr _getConnection() 00262 { 00263 ConnectionPtr connection = _outgoing; 00264 if( _state == STATE_CONNECTED || _state == STATE_LISTENING ) 00265 return connection; 00266 EQUNREACHABLE; 00267 return 0; 00268 } 00269 }; 00270 00271 CO_API std::ostream& operator << ( std::ostream& os, const Node& node ); 00272 CO_API std::ostream& operator << ( std::ostream&, const Node::State ); 00273 } 00274 00275 #endif // CO_NODE_H