Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members   File Members  

radiosched.h

Go to the documentation of this file.
00001 /* MuSE - Multiple Streaming Engine
00002  * (c) Copyright 2001 Eugen Melinte <ame01@gmx.net>
00003  *
00004  * This source code is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Public License as published 
00006  * by the Free Software Foundation; either version 2 of the License,
00007  * or (at your option) any later version.
00008  *
00009  * This source code is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00012  * Please refer to the GNU Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Public License along with
00015  * this source code; if not, write to:
00016  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 
00018  $Id: radiosched.h,v 1.3 2004/06/02 21:20:15 eugene Exp $
00019  
00020  */
00021 /*
00022   Content:
00023      -generic/abstract Basic_scheduler class, threaded with code 
00024           ripped from the Channel class.
00025      -Scheduler_text class - not used anymore
00026      -Scheduler_xml class 
00027  */
00028 
00029 
00037 #ifndef __CRADIOSCHED_H
00038 #define __CRADIOSCHED_H
00039 
00040 #include <math.h>
00041 #include <pthread.h>
00042 #include <sys/time.h>
00043 
00044 #include <inchannels.h>
00045 #include <outchannels.h>
00046 #include <gui.h>
00047 
00048 #include <generic.h>
00049 
00050 
00051 /*
00052  * We can have many types of schedulers: using a cron-like format, an SQL db, 
00053  * xml, etc.  Usage:
00054  * To init (like a Channel) - @see muse.cpp : 
00055  *    sched = new MyBasic_scheduler( sched_data );
00056  *    mixer->register_sched( sched );
00057  *    sched->lock();
00058  *    sched->start();
00059  *    sched->wait();
00060  *    sched->unlock();
00061  * To use:
00062  *        sched->set_schedule( ... ); //only if different from sched_data
00063  *        sched->play();
00064  *        sched->stop();
00065  *        sched->play(); //etc..
00066  *        sched->stop();
00067  * To kill thread:
00068  *    sched->quit = true;
00069  */
00070 #define SCHEDFILE       "schedule.xml" //under $HOME/.muse
00071 const char *sched_file_path(void);
00072 
00073 
00074 class Basic_scheduler {
00075 
00076  public:
00077   Basic_scheduler( const void *sched ); 
00078   virtual ~Basic_scheduler();
00079   
00080   void register_mixer(Stream_mixer *mix) {mixer=mix;};
00081 
00082   virtual void run();
00083   virtual bool stop();
00084   virtual bool play();
00085   virtual void on_wakeup();
00086   virtual void load_schedule() { on_load_schedule();};
00088   virtual void on_load_schedule() =0; 
00089   
00090   void set_schedule( const void *s ) {_schedule = s;};
00091 
00092   bool on;
00093   bool running;
00094   bool quit;
00095   Channel *channel;
00096 
00097   /* pthread stuff */
00098   void start() {
00099     pthread_create(&_thread, &_attr, &kickoff, this); };
00100   void lock() { pthread_mutex_lock(&_mutex); };
00101   void unlock() { pthread_mutex_unlock(&_mutex); };
00102   void wait() { pthread_cond_wait(&_cond,&_mutex); };
00103   void signal() { pthread_cond_signal(&_cond); };
00104   /* ------------- */
00105 
00106  protected:
00107   static void* kickoff(void *arg) { ((Basic_scheduler*) arg)->run(); return NULL; };
00108   
00109   /* Match the left string with the right number.  */
00110   bool match( const char *left, int right ); 
00111   void stop_channel(void);
00112   bool play_code; 
00113 
00114   bool start_channel( Url *rec ); 
00115   
00116   const void *_schedule; 
00117   Playlist *playlist;
00118   void dump(); 
00119   time_t prev_time;
00120   //schedule_record *head, *rec_ptr; /* crontab entry pointers       */
00121   /* Schedule record corresponding to the current playing Basic_connection */
00122   Url  *playing; 
00123   
00124   void stop_inner_channel(void);
00125   bool start_inner_channel( Url *rec ); 
00126   // start/stop mixer channels
00127   Stream_mixer *mixer;
00128   void stop_mixer_channel( Url *rec );
00129   bool start_mixer_channel( Url *rec ); 
00130 
00131  private:
00132   /* pthread stuff */
00133   void _thread_init();
00134   void _thread_destroy();
00135   pthread_t _thread;
00136   pthread_attr_t _attr;
00137   pthread_mutex_t _mutex;
00138   pthread_cond_t _cond;
00139   /* ------------- */
00140 
00141 };
00142 
00143 
00144 extern Basic_scheduler *rscheduler;
00145 
00146 /*
00147  * Scheduler_text uses a crontab(5) like text-file format:
00148  * Mn  Hr Day Mth WDay MnPlay Url comment
00149  */
00150 #define CRONSIZE        (8192*2)
00151 #define TSEPARATOR       " \t"
00152 class Scheduler_text : public Basic_scheduler {
00153  public:
00154   Scheduler_text(const void *sched) : Basic_scheduler(sched) 
00155       {memset(crontab, '\0', CRONSIZE);};
00156   virtual ~Scheduler_text() {};
00157   void on_load_schedule();
00158   
00159  private:
00160   char crontab[CRONSIZE];  /* memory for the entries       */
00161   void load_crontab( const char *cronf ); 
00162   void addentry( char *line ); 
00163   
00164 };
00165 
00166 
00167 /*
00168  * Schedule record chain.  The strings can contain wildcards:
00169  *      *               @match 1 for any number
00170  *      x,y [,z, ...]   @match 1 for any number given 
00171  *      x-y             @match 1 for any number within the range of x thru y 
00172  * Format might change in the future, as scheduler gets more mature. 
00173  * @see Url class also.
00174  */
00175 typedef struct  _sched_rec {
00176         const char *src;
00177         const char *comment;
00178         const char *wkd;      
00179         const char *month;    
00180         const char *day;      
00181         const char *stime;    
00182         const char *etime;    
00183         const char *secs;     
00184 } sched_rec;
00185 
00186 /*
00187  * xml format scheduler 
00188  */
00189 class Scheduler_xml : public Basic_scheduler {
00190  public:
00191   Scheduler_xml(const void *sched) : Basic_scheduler(sched) {};
00192   void on_load_schedule();
00193   virtual ~Scheduler_xml() {};
00194   
00195  private:
00196   void load_crontab( const char *cronf ); 
00197   static int addentry( void *instance, sched_rec *sr ); 
00198   
00199 };
00200 
00201 
00202 /*
00203  * xml schedule parser - to be used by GUIs, etc. 
00204  */
00205 typedef int (*sched_rec_callb)(void*, sched_rec*); 
00208 int parse_xml_sched_file( sched_rec_callb callb, void *udata, sched_rec *data );
00210 int create_xml_schedule_file(void);
00213 int write_xml_schedule_file(const char *content); 
00215 char *format_xml_sched_rec(const sched_rec *rec);
00216 
00217 #endif /*__CRADIOSCHED_H*/
00218 

Generated on Thu Dec 16 12:28:21 2004 for MuSE by doxygen1.3