RTFX  0.3
Real time special effects collaborative visualization and production library.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
RTFXPeer.h
Go to the documentation of this file.
1 #ifndef __RTFX_PEER__
2 #define __RTFX_PEER__
3 
25 #include "RTFXPeerPostOffice.h"
26 #include "RTFXPeerSession.h"
27 #include "RTFXBoostIncludes.h"
28 
29 namespace RTFX
30 {
31 
32  typedef boost::shared_ptr<RTFXPeerSession> RTFXPeerSessionPtr;
33 
34  class RTFXAPI RTFXPeer
35  {
37  public:
38 
40  RTFXPeer();
41 
43  ~RTFXPeer();
44 
48  void Start( string _hostname, string _connectionHostname, unsigned int _port = 0 );
49 
51  bool HasBeenStarted() { return hasBeenStarted; }
52 
54  void ConnectToPeer( string _hostname );
55 
57  void ConnectToPeer( boost::asio::io_service &_ioService, tcp::resolver::iterator &_endpointIterator );
58 
60  void DisconnectFromPeers();
61 
63  void Close();
64 
66  void StartAccepting();
67 
69  void HandleAccept( RTFXPeerSessionPtr _session, const boost::system::error_code &_error );
70 
72  RTFXData * GetData() { return mailbox->GetPointerToMailbox(); };
73 
75  void SendUpdates();
76 
78  void Send( RTFXData * _ptr );
79 
81  void Send( string &_data, bool _isDataRTFX );
82 
84  bool IsConnected() { return isConnected; }
85 
87  bool CheckForUpdates();
88 
90  void SetServerKeepData( bool _keepData )
91  {
92  keepDataAtServer = _keepData;
93  }
94 
96  bool GetServerKeepData()
97  {
98  return keepDataAtServer;
99  }
100 
102  void SetHostname( string _hostname )
103  {
104  hostname = _hostname;
105  }
106 
108  string GetHostname()
109  {
110  return hostname;
111  }
112 
114  void AddHostname( string _hostname )
115  {
116  debug( ostringstream().flush() << "RTFXPeer::AddHostname: adding hostname " << _hostname << endl );
117  if ( connectionList.find( _hostname ) == connectionList.end() )
118  connectionList[ _hostname ] = 1;
119  else
120  connectionList[ _hostname ] = connectionList[ _hostname ] + 1;
121  }
122 
124  void DeleteHostname( string _hostname )
125  {
126  debug( ostringstream().flush() << "RTFXPeer::DeleteHostname: deleting hostname " << _hostname << endl );
127 
128  if ( connectionList.find( _hostname ) != connectionList.end() )
129  connectionList[ _hostname ] --;
130  }
131 
133  void SetConnectionHostname( string _chostname )
134  {
135  connectionHostname = _chostname;
136  }
137 
139  string GetConnectionHostname()
140  {
141  return connectionHostname;
142  }
143 
145  string GetHostnameData()
146  {
147  string hostnameData_ = "";
148  stringstream ss_( stringstream::in | stringstream:: out );
149 
150  map<string, int>::iterator iter_ = connectionList.begin();
151  while ( iter_ != connectionList.end() )
152  {
153  ss_ << (*iter_).first << "," << (*iter_).second << "\n";
154  iter_ ++;
155  }
156 
157  hostnameData_ = ss_.str();
158  return hostnameData_;
159  }
160 
162  bool HasNewDataArrived()
163  {
164  return isNewDataAvailable;
165  }
166 
168  void SetNewDataArrived( bool _value )
169  {
170  isNewDataAvailable = _value;
171  }
172 
174  void DeleteObjectEverywhere( RTFXObject * _obj );
175 
177  bool IsCustomDataAvailable()
178  {
179  if ( mailbox->GetCustomDataCount() > 0 ) return true;
180  return false;
181  }
182 
184  void SetDefaultCompressionType( COMPRESSIONTYPE _tmp )
185  {
186  compressionType = _tmp;
187  mailbox->SetDefaultCompressionType( _tmp );
188  postOffice.SetDefaultCompressionType( _tmp );
189  }
190 
192  COMPRESSIONTYPE GetDefaultCompressionType() { return compressionType; }
193 
195  void SetArchiveType( bool _isBinaryData )
196  {
197  isBinaryData = _isBinaryData;
198  }
199 
201  bool GetArchiveType()
202  {
203  return isBinaryData;
204  }
205 
206  private:
207 
209  void StartServer( unsigned int _port );
210 
212  void ConnectToPeer( string _connectionHostname, string _port );
213 
215  int CountKnownConnections()
216  {
217  map<string, int>::iterator iter_ = connectionList.begin();
218  int count_ = 0;
219 
220  while ( iter_ != connectionList.end() )
221  {
222  count_ += (*iter_).second;
223  iter_ ++;
224  }
225  return count_;
226  }
227 
229  void ResetKnownConnections()
230  {
231  connectionList.clear();
232  {
233  map<string,int> tmp_;
234  tmp_.swap( connectionList );
235  }
236  }
237 
238 
240  boost::thread * serverThread;
241  boost::thread * clientThread;
242 
244  map<string, int> connectionList;
245 
247  bool isClosing;
248 
251 
254 
256  void PeerHandleReadHeader( const boost::system::error_code &_error );
257 
259  string hostname;
260 
263 
267 
270 
272  boost::mutex peerLock;
273 
275  void PeerHandleConnect( const boost::system::error_code &_error, tcp::resolver::iterator _endpointIterator );
276 
278  void PeerHandleReadBody( const boost::system::error_code &_error );
279 
281  void DoWrite( RTFXParcel _msg );
282 
284  void PeerHandleWrite( const boost::system::error_code &_error );
285 
287  void DoClose();
288 
290  void Write( const RTFXParcel& _msg );
291 
294  std::map<boost::uuids::uuid, vector<RTFXParcel> > parcelCollection;
295 
297  boost::asio::io_service ioService;
298 
300  boost::asio::io_service clientIoService;
301 
303  tcp::acceptor * tcpAcceptor;
304 
307 
309  tcp::socket socket;
310 
313 
316 
319 
321  boost::uuids::uuid id;
322 
324  char * mailboxName;
325 
327  AXISFRAME localFrame;
328 
332 
336 
339 
343 
346 
349 
351  unsigned int port;
352  string portString;
353 
357 
360 
364  };
365 }
366 
367 #endif