rstpserver.h

00001 /* libradar_stp
00002  * <http://www.k9nl.net/radar/> <radar@k9nl.net>
00003  * 
00004  * rstpserver.h - RSTP server class
00005  *
00006  * THIS FILE IS NOT MEANT FOR LIBRARY CLIENT USE
00007  * It is included in case it's helpful
00008  *
00009  * Copyright (C) 2007 Nicholas J. Luther
00010  *
00011  * This program is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU General Public License,
00013  * version 2, as published by the Free Software Foundation.
00014  * 
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023  * You may contact the author by writing to Nick Luther, 1655 South
00024  * Westhaven Drive, Oshkosh, Wisconsin 54904, USA.
00025  * 
00026  */
00027  
00028 #ifndef _LIBRSTP_INC_RSTPSERVER_H
00029 #define _LIBRSTP_INC_RSTPSERVER_H
00030  
00031 #include <string>
00032 #include <set>
00033 #include <queue>
00034 #include <list>
00035 #include <map>
00036 #include <vector>
00037 
00038 #include "lplproj.h"
00039 #include "thread/thread.h"
00040 #include "thread/mutex.h"
00041 #include "socket/tcpsocket.h"
00042 #include "filter.h"
00043 #include "loghandler.h"
00044 
00045 
00046 
00047 namespace libradar_stp {
00048 
00049 // CAUTION: be careful copying RSTPServers
00050 class 
00051 RSTPServer : public LPORTLIB_NAMESPACE::thread::Thread {
00052 public:
00053   
00054   RSTPServer();
00055   ~RSTPServer();
00056   
00057   bool listen( const std::string& local_addr, unsigned short port, int backlog = 10 );
00058   bool listen( unsigned short port );
00059   
00060   bool notifyData( const char* data, unsigned int size );
00061   
00062   void quit();
00063   
00064   void setAuthRequired();
00065   void setAuthCredential( const std::string& login, const std::string& pass );
00066   void setAuthPermission( const std::string& login, const std::string& stream );
00067   void clearAuthSettings();
00068   void setMaxClients( unsigned int max_clients = 0 );
00069   
00070   void setFilterStream( const std::string& stream, const std::string& file );
00071   void setPassStream( const std::string& stream );
00072   void setPipeThrough( const std::string& stream, const std::string& cmd );
00073   
00074   void setStreamCache( const std::string& stream, int cache_size, const std::string& file );
00075   
00076   typedef void (*STRCB)( void*, const std::string& );
00077   void setStreamAddCallback( STRCB func, void* arg = 0 );
00078   void setStreamDropCallback( STRCB func, void* arg = 0 );
00079   
00080   static std::string pipeThroughCmd( const std::string& data,
00081       const std::string& cmd );
00082   static std::string getTempPath( unsigned int seed = 0 );
00083   
00084   void addBroadcastData( const std::string& data );
00085   void addBroadcastFile( const std::string& file );
00086   void setBroadcastInterval( unsigned int interval );
00087   
00088   void setLogging( const std::string& file, int level = 1, std::string progid = "" );
00089   void setStatusFile( const std::string& file );
00090   
00091 protected:
00092 
00093   void beginThread();
00094   
00095 private:
00096 
00097   void cleanup();
00098 
00099   bool m_quit_request;
00100   std::queue< std::string > m_data_queue;
00101   LPORTLIB_NAMESPACE::thread::Mutex m_data_queue_mutex;
00102   LPORTLIB_NAMESPACE::socket::TCPSocket m_sock;
00103   
00104   bool m_auth_required;
00105   std::map< std::string, std::string > m_auth_db;
00106   LPORTLIB_NAMESPACE::thread::Mutex m_auth_db_mutex;
00107   std::map< std::string, std::set<std::string> > m_auth_streams;
00108   LPORTLIB_NAMESPACE::thread::Mutex m_auth_streams_mutex;
00109   
00110   std::set< std::string > m_pass_streams;
00111   std::map< std::string, Filter > m_filter_streams;
00112   std::map< std::string, std::string > m_pipe_streams;
00113   
00114   unsigned int m_max_clients;
00115   
00116   class ClientHelper : public LPORTLIB_NAMESPACE::thread::Thread {
00117   public:
00118     ClientHelper( RSTPServer* parent, const LPORTLIB_NAMESPACE::socket::TCPSocket& sock );
00119     ~ClientHelper();
00120     void notifyData( const std::set<std::string>& streams, const char* data, unsigned int size );
00121     void quit();
00122     void join();
00123     LogHandler m_log;
00124   protected:
00125     void beginThread();
00126   private:
00127     struct queue_datum { std::string m_stream; std::string m_data; };
00128     //std::queue<std::string> m_data_queue;
00129     std::queue<queue_datum> m_data_queue;
00130     LPORTLIB_NAMESPACE::thread::Mutex m_data_queue_mutex;
00131     std::set<std::string> m_streams;
00132     LPORTLIB_NAMESPACE::thread::Mutex m_streams_mutex;
00133     LPORTLIB_NAMESPACE::socket::TCPSocket m_sock;
00134     std::string m_login;
00135     bool m_quit_request;
00136     unsigned int m_lh;
00137     bool m_nop;
00138     RSTPServer* m_parent;
00139     bool m_authenticated;
00140     bool m_supp_bzip2;
00141     bool m_supp_zlib;
00142   }; // class ClientHelper
00143   friend class ClientHelper;
00144   
00145   std::set<ClientHelper*> m_clients;
00146   LPORTLIB_NAMESPACE::thread::Mutex m_clients_mutex;
00147   std::queue<ClientHelper*> m_close_queue;
00148   LPORTLIB_NAMESPACE::thread::Mutex m_close_queue_mutex;
00149   
00150   void notifyClose( ClientHelper* helper );
00151   
00152   std::map<std::string,unsigned int> m_stream_count;
00153   LPORTLIB_NAMESPACE::thread::Mutex m_stream_count_mutex;
00154   
00155   void notifyStreamAdd( const std::string& stream, ClientHelper* ch = 0 );
00156   void notifyStreamDrop( const std::string& stream );
00157   
00158   STRCB m_stream_add_cb;
00159   STRCB m_stream_drop_cb;
00160   void* m_stream_add_arg;
00161   void* m_stream_drop_arg;
00162   
00163   std::vector<std::string> m_broadcast_data;
00164   std::vector<std::string> m_broadcast_files;
00165   unsigned int m_broadcast_time;
00166   unsigned int m_last_broadcast;
00167   void processBroadcastFile( const std::string& file );
00168   
00169   LogHandler m_log;
00170   
00171   struct ConnStatus {
00172     string m_addr;
00173     string m_conn_time;
00174     string m_login;
00175     string m_streams;
00176   }; // struct
00177   std::map<int,ConnStatus> m_status;
00178   std::string              m_status_file;
00179   void reportConnect( int id, string addr );
00180   void reportLogin( int id, string login );
00181   void reportStreams( int id, const std::set<std::string>& streams );
00182   void reportDisconnect( int id );
00183   void writeReport();
00184   void reportMinLen( std::string& s, int len ) const;
00185   LPORTLIB_NAMESPACE::thread::Mutex m_status_mutex;
00186   
00187   
00188   // a thread-safe class!
00189   class CacheCtrl {
00190     public:
00191       CacheCtrl();
00192       ~CacheCtrl();
00193       
00194       bool ok() const;
00195       
00196       void setFilter( const Filter& filter );
00197       Filter getFilter();
00198       
00199       void setMaxSize( int max_size );
00200       int getMaxSize() const;
00201       
00202       void setStream( std::string stream );
00203       std::string getStream();
00204       
00205       void notifyData( const char* data, unsigned int size );
00206       void pushCacheData( ClientHelper* ch );
00207       
00208     private:
00209       int m_max_size;
00210       std::string m_stream;
00211       Filter m_filter;
00212       std::map< std::string, std::list< std::string > > m_cache;
00213       LPORTLIB_NAMESPACE::thread::Mutex m_mutex;
00214   }; // class CacheCtrl
00215   friend class CacheCtrl;
00216   std::map< std::string, CacheCtrl > m_cache_ctrl;
00217 
00218 }; // class
00219 
00220 
00221 };// namespace
00222 
00223 #endif
00224 
00225 

Generated on Tue Jul 1 20:25:26 2008 for libradar_stp by  doxygen 1.4.7