conexus/endpoint.h

00001 /***************************************************************************
00002  *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
00003  *   rvinyard@cs.nmsu.edu                                                  *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation version 2.1.                *
00008  *                                                                         *
00009  *   This program 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.  See the         *
00012  *   GNU General Public License for more details.                          *
00013  *                                                                         *
00014  *   You should have received a copy of the GNU Lesser General Public      *
00015  *   License along with this library; if not, write to the                 *
00016  *   Free Software Foundation, Inc.,                                       *
00017  *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
00018  ***************************************************************************/
00019 #ifndef CONEXUSIO_H
00020 #define CONEXUSIO_H
00021 
00022 #include <set>
00023 #include <queue>
00024 
00025 #include <sys/select.h>
00026 
00027 #include <glibmm/dispatcher.h>
00028 #include <glibmm/thread.h>
00029 
00030 #include <conexus/pointer.h>
00031 #include <conexus/enums.h>
00032 #include <conexus/except.h>
00033 #include <conexus/data.h>
00034 
00230 namespace Conexus
00231   {
00232 
00250   class Endpoint
00251     {
00252       public:
00253 
00255       typedef ConexusPointer<Endpoint> pointer;
00256 
00264       Endpoint(bool close_on_destruct=true);
00265 
00270       virtual ~Endpoint();
00271 
00283       virtual void start( );
00284 
00289       virtual void start( bool use_dispatcher );
00290 
00295       virtual void stop( );
00296 
00298       virtual bool is_running();
00299 
00301       virtual bool timestamp_received_data();
00302 
00304       virtual void set_timestamp_received_data(bool t=true);
00305 
00306       virtual void set_blocking_write(bool block=true);
00307 
00308       virtual bool is_blocking_write();
00309 
00315       sigc::signal<void, const Data> signal_data();
00316 
00322       sigc::signal<void, size_t> signal_data_received();
00323 
00329       sigc::signal<void, size_t> signal_data_transmitted();
00330 
00331       long int read_timeout();
00332 
00333       void set_read_timeout( long int t );
00334 
00335       long int write_timeout();
00336 
00337       void set_write_timeout( long int t );
00338 
00339       bool read_terminate_immediate();
00340 
00341       void set_read_terminate_immediate( bool i=true );
00342 
00343       bool write_terminate_immediate();
00344 
00345       void set_write_terminate_immediate( bool i=true );
00346 
00350       virtual void open() throw (open_exception) = 0;
00351 
00354       virtual void close(bool force=false) throw (close_exception) = 0;
00355 
00365       ssize_t write(const void* data, size_t size) throw (write_exception);
00366 
00371       ssize_t write(const Data data) throw (write_exception);
00372 
00392       Data read(size_t s = 0) throw (read_exception);
00393 
00394       virtual ssize_t input_available();
00395 
00410       virtual void change_state(long new_state) throw (state_exception);
00411 
00416       long state();
00417 
00419       void set_close_on_destruction(bool value);
00420 
00422       bool close_on_destruction() const;
00423 
00432       void close_and_reopen(long state = ENDPOINT_UNCHANGED);
00433 
00435       sigc::signal<void> signal_opened();
00436 
00438       sigc::signal<void> signal_closed();
00439 
00441       bool is_open();
00442 
00444       bool is_closed();
00445 
00446     protected:
00447       Glib::Thread *m_read_thread;
00448       Glib::Thread *m_write_thread;
00449       Glib::Thread *m_read_delivery_thread;
00450       bool m_read_terminate;
00451       bool m_read_terminate_immediate;
00452       bool m_write_terminate;
00453       bool m_write_terminate_immediate;
00454 
00455       long int m_read_timeout;
00456       long int m_write_timeout;
00457       bool m_timestamp;
00458 
00459       Glib::Mutex m_read_queue_lock;
00460       Glib::Mutex m_write_queue_lock;
00461 
00462       Glib::Cond m_read_delivery_conditional;
00463       Glib::Cond m_write_conditional;
00464 
00465       Glib::Dispatcher* m_dispatcher;
00466       sigc::connection m_dispatcher_connection;
00467 
00468       bool m_use_dispatcher;
00469 
00470       sigc::signal<void, const Data> m_signal_data;
00471       sigc::signal<void, size_t> m_signal_data_received;
00472       sigc::signal<void, size_t> m_signal_data_transmitted;
00473       std::queue<Data> m_read_queue;
00474       std::queue<Data> m_write_queue;
00475 
00476       virtual void read_thread_main();
00477       virtual void read_thread_cleanup();
00478 
00479       virtual void read_delivery_thread_main();
00480       virtual void read_delivery_thread_cleanup();
00481 
00482       virtual void write_thread_main();
00483       virtual void write_thread_cleanup();
00484 
00485       void queue_received_data( const Data d );
00486       void queue_write_data( const Data d );
00487       void emit_received_data( );
00488 
00489 
00490       bool m_close_on_destruction;
00491       long m_state;
00492       bool m_readable;
00493       bool m_writable;
00494 
00495       virtual void set_state_opened();
00496       virtual void set_state_closed();
00497 
00498       sigc::signal<void> m_signal_opened;
00499       sigc::signal<void> m_signal_closed;
00500 
00501       virtual ssize_t write_data(long int timeout, const Data data) throw (write_exception) = 0;
00502 
00503       virtual Data read_data(long int timeout, size_t s = 0) throw (read_exception) = 0;
00504 
00505     };
00506 
00507 }
00508 
00509 Conexus::Endpoint& operator<<(Conexus::Endpoint& io, const Conexus::Data d);
00510 Conexus::Endpoint::pointer operator<<(Conexus::Endpoint::pointer io, const Conexus::Data d);
00511 Conexus::Endpoint& operator>>(Conexus::Endpoint& io, Conexus::Data d);
00512 Conexus::Endpoint::pointer operator>>(Conexus::Endpoint::pointer io, Conexus::Data d);
00513 
00514 #endif

Generated on Tue Mar 13 19:54:43 2007 by  doxygen 1.5.1