| 1 | // protocol.hxx -- High level protocol class |
| 2 | // |
| 3 | // Written by Curtis Olson, started November 1999. |
| 4 | // |
| 5 | // Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt |
| 6 | // |
| 7 | // This program is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU General Public License as |
| 9 | // published by the Free Software Foundation; either version 2 of the |
| 10 | // License, or (at your option) any later version. |
| 11 | // |
| 12 | // This program is distributed in the hope that it will be useful, but |
| 13 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | // General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License |
| 18 | // along with this program; if not, write to the Free Software |
| 19 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | // |
| 21 | // $Id$ |
| 22 | |
| 23 | |
| 24 | #pragma once |
| 25 | |
| 26 | #include <simgear/compiler.h> |
| 27 | #include <simgear/io/iochannel.hxx> |
| 28 | |
| 29 | #include <string> |
| 30 | #include <vector> |
| 31 | |
| 32 | |
| 33 | #define FG_MAX_MSG_SIZE 16384 |
| 34 | |
| 35 | class FGProtocol { |
| 36 | |
| 37 | private: |
| 38 | |
| 39 | double hz; |
| 40 | double count_down; |
| 41 | unsigned long count; |
| 42 | |
| 43 | SGProtocolDir dir; |
| 44 | |
| 45 | // std::string protocol_str; |
| 46 | |
| 47 | // char buf[FG_MAX_MSG_SIZE]; |
| 48 | // int length; |
| 49 | |
| 50 | bool enabled; |
| 51 | |
| 52 | SGIOChannel *io; |
| 53 | std::string m_name; |
| 54 | |
| 55 | public: |
| 56 | |
| 57 | FGProtocol(); |
| 58 | virtual ~FGProtocol(); |
| 59 | |
| 60 | virtual bool open(); |
| 61 | virtual bool process(); |
| 62 | virtual bool close(); |
| 63 | virtual void reinit(); |
| 64 | |
| 65 | inline SGProtocolDir get_direction() const { return dir; } |
| 66 | void set_direction( const std::string& d ); |
| 67 | |
| 68 | inline double get_hz() const { return hz; } |
| 69 | inline void set_hz( double t ) { hz = t; } |
| 70 | inline double get_count_down() const { return count_down; } |
| 71 | inline void set_count_down( double c ) { count_down = c; } |
| 72 | inline void inc_count_down( double amt ) { count_down += amt; } |
| 73 | inline void dec_count_down( double amt ) { count_down -= amt; } |
| 74 | inline void inc_count() { count++; } |
| 75 | inline unsigned long get_count() { return count; } |
| 76 | |
| 77 | virtual bool gen_message(); |
| 78 | virtual bool parse_message(); |
| 79 | |
| 80 | // inline std::string get_protocol() const { return protocol_str; } |
| 81 | // inline void set_protocol( const std::string& str ) { protocol_str = str; } |
| 82 | |
| 83 | // inline char *get_buf() { return buf; } |
| 84 | // inline int get_length() const { return length; } |
| 85 | // inline void set_length( int l ) { length = l; } |
| 86 | |
| 87 | inline bool is_enabled() const { return enabled; } |
| 88 | inline void set_enabled( const bool b ) { enabled = b; } |
| 89 | |
| 90 | inline SGIOChannel *get_io_channel() const { return io; } |
| 91 | inline void set_io_channel( SGIOChannel *c ) { io = c; } |
| 92 | |
| 93 | // allow storing an identifying name on the Protocol instance |
| 94 | void set_name(const std::string& n); |
| 95 | std::string get_name() const { return m_name; } |
| 96 | }; |
| 97 | |
| 98 | |
| 99 | typedef std::vector < FGProtocol * > io_container; |
| 100 | typedef io_container::iterator io_iterator; |
| 101 | typedef io_container::const_iterator const_io_iterator; |
| 102 | |
| 103 | #include <stdexcept> |
| 104 | |
| 105 | //namespace flightgear { namespace network { |
| 106 | class FGProtocolConfigError : public std::invalid_argument |
| 107 | { |
| 108 | public: |
| 109 | FGProtocolConfigError( const std::string& what_string ) |
| 110 | : std::invalid_argument(what_string) {} |
| 111 | }; |
| 112 | //}} // end namespace flightgear::network |