protocol_thread.h
上传用户:psq1974
上传日期:2007-01-06
资源大小:1195k
文件大小:4k
源码类别:

mpeg/mp3

开发平台:

C/C++

  1. /* Copyright (C) 1998, 1999 State University of New York at Stony Brook
  2.    Author: Andrew V. Shuvalov ( andrew@ecsl.cs.sunysb.edu )
  3.    Software license is located in file "COPYING"
  4. */
  5. #ifndef protocol_thread_h
  6. #define protocol_thread_h
  7. #include "protocol_stream.h"
  8. #include "../../pusher/src/sockaddr.h"
  9. #include "../../pusher/src/timeval.h"
  10. #include "mutex.h"
  11. class protocol_cmd 
  12. {
  13. public:
  14.   typedef enum { } commandT;
  15. private:
  16.   commandT command;
  17. public:
  18.   protocol_cmd( commandT cmd );
  19. };
  20. struct text_data;
  21. class protocol_thread
  22. {
  23.   /** connection status is determined by checking this to be NULL */
  24.   protocol_stream *protocolStream;
  25.   Sockaddr dbServerAddr;
  26.   Timeval whenTryToRetryConnection;
  27.   /** received from handshake */
  28.   int acquisition_server_id;
  29.   bool shutdown_now;
  30.   /** commands from other threads are queued in this queue. */
  31.   std::queue< protocol_cmd * > commands;
  32.   /** abstract log destination */
  33.   ostream *log_output;
  34.   /** mutex to lock log out stream */
  35.   mutex lock_log;
  36.   /** queue of push servers where we successfully connected - to report to
  37.       the db server */
  38.   std::queue < int > connected_push_servers;
  39.   /** lock connected_push_servers
  40.    */
  41.   mutex lock_various_cmdstacks;
  42.   /** queue of text data to be sent to the database server */
  43.   std::queue< text_data * > new_text;
  44.   /** access to the queue is shared */
  45.   mutex lock_new_text;
  46.   
  47.   /** logging level */
  48.   int verbose_level;
  49.   static const int retryConnectionInterval;
  50.   static const int setupConnectionTimeout;
  51.   static const char register_acquisition_server[];
  52.   static const char cmd_request_broadcast_parameters[];
  53.   static const char cmd_push_server_ok[];
  54.   static const char cmd_add_caption_noid[];
  55. public:
  56.   /** only set-up initial values */
  57.   protocol_thread( int argc, char **argv );
  58.   /** static thread - entry function. When it ends - thread ends 
  59.       gets the address and port of database video server from command line,
  60.       -a address_ip_or_text -p portnum
  61.       try to connect. If connection fails - set up timeout and reconnect
  62.       again 
  63.    */
  64.   static void protocol_thread_entry_func( void *instance );
  65.   /** close sockets, etc. */
  66.   void cleanup();
  67.   /** this may be called several times after timeout */
  68.   void open_connection();
  69.   /** deallocate data associated with connection - but not to shutdown! 
  70.       only to reconnect later. Call cleanup if want to shutdown after call */
  71.   void cleanup_connection();
  72.   /** handshake with server */
  73.   void handshake();
  74.   void request_broadcast_parameters();
  75.   /** never exit, work until some exception */
  76.   void loop();
  77.   /** we have something on input... */
  78.   void process_unknown_input();
  79. private:
  80.   /** sleep for spec num of seconds */
  81.   void sleep( int seconds );
  82.   /** look in command queue for commands from others threads
  83.    */
  84.   void lookup_cmd_stack();
  85.   /** acquisition server do not knows whether this channel is recorded now 
  86.       or not. It just send the text - database server will drop it if 
  87.       it don't need it */
  88.   void check_new_text();
  89. public:
  90.   // ------- outside ------------
  91.   /** call from outside thread
  92.    */
  93.   void shutdown_requested();
  94.   /** report to db server that push server id is operating fine
  95.    */
  96.   void report_push_serv_ok( int push_id );
  97.   /** pass new text data to protocol thread. This call should never be 
  98.       blocked by any I/O - only place pointer into queue. The queue is
  99.       protected by mutex and mutex should never lock the queue while any
  100.       I/O on the way */
  101.   void new_text_data( text_data *data );
  102.   /** take log from old input stream messages */
  103.   void dump_log();
  104.   /** log this message */
  105.   void log( int level, const char *s, ... );
  106. };
  107. extern protocol_thread *protocolThread;
  108. #endif  //  protocol_thread_h