- /* Copyright (C) 1998, 1999 State University of New York at Stony Brook
- Author: Andrew V. Shuvalov ( andrew@ecsl.cs.sunysb.edu )
- Software license is located in file "COPYING"
- */
- #ifndef protocol_thread_h
- #define protocol_thread_h
- #include "protocol_stream.h"
- #include "../../pusher/src/sockaddr.h"
- #include "../../pusher/src/timeval.h"
- #include "mutex.h"
- class protocol_cmd
- {
- public:
- typedef enum { } commandT;
- private:
- commandT command;
- public:
- protocol_cmd( commandT cmd );
- };
- struct text_data;
- class protocol_thread
- {
- /** connection status is determined by checking this to be NULL */
- protocol_stream *protocolStream;
- Sockaddr dbServerAddr;
- Timeval whenTryToRetryConnection;
- /** received from handshake */
- int acquisition_server_id;
- bool shutdown_now;
- /** commands from other threads are queued in this queue. */
- std::queue< protocol_cmd * > commands;
- /** abstract log destination */
- ostream *log_output;
- /** mutex to lock log out stream */
- mutex lock_log;
- /** queue of push servers where we successfully connected - to report to
- the db server */
- std::queue < int > connected_push_servers;
- /** lock connected_push_servers
- */
- mutex lock_various_cmdstacks;
- /** queue of text data to be sent to the database server */
- std::queue< text_data * > new_text;
- /** access to the queue is shared */
- mutex lock_new_text;
- /** logging level */
- int verbose_level;
- static const int retryConnectionInterval;
- static const int setupConnectionTimeout;
- static const char register_acquisition_server[];
- static const char cmd_request_broadcast_parameters[];
- static const char cmd_push_server_ok[];
- static const char cmd_add_caption_noid[];
- public:
- /** only set-up initial values */
- protocol_thread( int argc, char **argv );
- /** static thread - entry function. When it ends - thread ends
- gets the address and port of database video server from command line,
- -a address_ip_or_text -p portnum
- try to connect. If connection fails - set up timeout and reconnect
- again
- */
- static void protocol_thread_entry_func( void *instance );
- /** close sockets, etc. */
- void cleanup();
- /** this may be called several times after timeout */
- void open_connection();
- /** deallocate data associated with connection - but not to shutdown!
- only to reconnect later. Call cleanup if want to shutdown after call */
- void cleanup_connection();
- /** handshake with server */
- void handshake();
- void request_broadcast_parameters();
- /** never exit, work until some exception */
- void loop();
- /** we have something on input... */
- void process_unknown_input();
- private:
- /** sleep for spec num of seconds */
- void sleep( int seconds );
- /** look in command queue for commands from others threads
- */
- void lookup_cmd_stack();
- /** acquisition server do not knows whether this channel is recorded now
- or not. It just send the text - database server will drop it if
- it don't need it */
- void check_new_text();
- public:
- // ------- outside ------------
- /** call from outside thread
- */
- void shutdown_requested();
- /** report to db server that push server id is operating fine
- */
- void report_push_serv_ok( int push_id );
- /** pass new text data to protocol thread. This call should never be
- blocked by any I/O - only place pointer into queue. The queue is
- protected by mutex and mutex should never lock the queue while any
- I/O on the way */
- void new_text_data( text_data *data );
- /** take log from old input stream messages */
- void dump_log();
- /** log this message */
- void log( int level, const char *s, ... );
- };
- extern protocol_thread *protocolThread;
- #endif // protocol_thread_h