00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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/enums.h>
00031 #include <conexus/except.h>
00032 #include <conexus/object.h>
00033 #include <conexus/data.h>
00034
00230 namespace Conexus
00231 {
00232
00250 class Endpoint: public Object
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, Data::const_pointer> signal_data();
00316
00322 sigc::signal<void, size_t> signal_data_received();
00323
00329 sigc::signal<void, size_t> signal_data_transmitted();
00330
00342
00343
00344 long int read_timeout();
00345
00346 void set_read_timeout( long int t );
00347
00348 long int write_timeout();
00349
00350 void set_write_timeout( long int t );
00351
00352 bool read_terminate_immediate();
00353
00354 void set_read_terminate_immediate( bool i=true );
00355
00356 bool write_terminate_immediate();
00357
00358 void set_write_terminate_immediate( bool i=true );
00359
00363 virtual void open() throw (open_exception) = 0;
00364
00367 virtual void close(bool force=false) throw (close_exception) = 0;
00368
00378 ssize_t write(const void* data, size_t size) throw (write_exception);
00379
00384 ssize_t write(Data::const_pointer data) throw (write_exception);
00385
00405 Data::pointer read(size_t s = 0) throw (read_exception);
00406
00407 virtual ssize_t input_available();
00408
00423 virtual void change_state(long new_state) throw (state_exception);
00424
00429 long state();
00430
00432 void set_close_on_destruction(bool value);
00433
00435 bool close_on_destruction() const;
00436
00445 void close_and_reopen(long state = ENDPOINT_UNCHANGED);
00446
00448 sigc::signal<void> signal_opened();
00449
00451 sigc::signal<void> signal_closed();
00452
00454 bool is_open();
00455
00457 bool is_closed();
00458
00460 virtual const std::string& object_type() { static std::string s("Conexus::Endpoint"); return s; }
00461
00462 protected:
00463 Glib::Thread *m_read_thread;
00464 Glib::Thread *m_write_thread;
00465 Glib::Thread *m_read_delivery_thread;
00466 bool m_read_terminate;
00467 bool m_read_terminate_immediate;
00468 bool m_write_terminate;
00469 bool m_write_terminate_immediate;
00470
00471 long int m_read_timeout;
00472 long int m_write_timeout;
00473 bool m_timestamp;
00474
00475
00476 Glib::Mutex m_read_queue_lock;
00477 Glib::Mutex m_write_queue_lock;
00478
00479 Glib::Cond m_read_delivery_conditional;
00480 Glib::Cond m_write_conditional;
00481
00482 Glib::Dispatcher* m_dispatcher;
00483 sigc::connection m_dispatcher_connection;
00484
00485 bool m_use_dispatcher;
00486
00487 sigc::signal<void, Data::const_pointer> m_signal_data;
00488 sigc::signal<void, size_t> m_signal_data_received;
00489 sigc::signal<void, size_t> m_signal_data_transmitted;
00490 std::queue<Data::const_pointer> m_read_queue;
00491 std::queue<Data::const_pointer> m_write_queue;
00492
00493 virtual void read_thread_main();
00494 virtual void read_thread_cleanup();
00495
00496 virtual void read_delivery_thread_main();
00497 virtual void read_delivery_thread_cleanup();
00498
00499 virtual void write_thread_main();
00500 virtual void write_thread_cleanup();
00501
00502 void queue_received_data( Data::const_pointer d );
00503 void queue_write_data( Data::const_pointer d );
00504 void emit_received_data( );
00505
00506
00507 bool m_close_on_destruction;
00508 long m_state;
00509 bool m_readable;
00510 bool m_writable;
00511
00512 virtual void set_state_opened();
00513 virtual void set_state_closed();
00514
00515 sigc::signal<void> m_signal_opened;
00516 sigc::signal<void> m_signal_closed;
00517
00518 virtual ssize_t write_data(long int timeout, Data::const_pointer data) throw (write_exception) = 0;
00519
00520 virtual Data::pointer read_data(long int timeout, size_t s = 0) throw (read_exception) = 0;
00521
00522 };
00523
00524 }
00525
00526 Conexus::Endpoint& operator<<(Conexus::Endpoint& io, const Conexus::Data::pointer d);
00527 Conexus::Endpoint& operator>>(Conexus::Endpoint& io, Conexus::Data::pointer d);
00528
00529 #endif