| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2006 by Gerd v. Egidy * |
| 3 | * gve@intra2net.com * |
| 4 | * * |
| 5 | * This library is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU Lesser General Public License version * |
| 7 | * 2.1 as published by the Free Software Foundation. * |
| 8 | * * |
| 9 | * This library is distributed in the hope that it will be useful, * |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 12 | * GNU Lesser General Public License for more details. * |
| 13 | * * |
| 14 | * You should have received a copy of the GNU Lesser General Public * |
| 15 | * License along with this program; if not, write to the * |
| 16 | * Free Software Foundation, Inc., * |
| 17 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 18 | ***************************************************************************/ |
| 19 | #ifndef __LIBT2N_SERVER |
| 20 | #define __LIBT2N_SERVER |
| 21 | |
| 22 | #include <iostream> |
| 23 | #include <string> |
| 24 | #include <map> |
| 25 | |
| 26 | namespace libt2n |
| 27 | { |
| 28 | |
| 29 | /** |
| 30 | Basic connection class |
| 31 | */ |
| 32 | class connection |
| 33 | { |
| 34 | private: |
| 35 | int timeout; |
| 36 | int last_action_time; |
| 37 | bool closed; |
| 38 | std::string buffer; |
| 39 | |
| 40 | protected: |
| 41 | connection(int _timeout) |
| 42 | { |
| 43 | set_timeout(_timeout); |
| 44 | reset_timeout(); |
| 45 | closed=false; |
| 46 | } |
| 47 | |
| 48 | public: |
| 49 | ~connection() |
| 50 | { this->close(); } |
| 51 | |
| 52 | void check_timeout(); |
| 53 | void reset_timeout(); |
| 54 | void set_timeout(int _timeout) |
| 55 | { timeout=_timeout; } |
| 56 | |
| 57 | bool is_closed() |
| 58 | { return closed; } |
| 59 | |
| 60 | virtual void close() |
| 61 | { closed=true; } |
| 62 | |
| 63 | virtual void fill_buffer(void)=0; |
| 64 | |
| 65 | bool get_packet(std::string& data, unsigned int& conn_id); |
| 66 | virtual void write(const std::string& data)=0; |
| 67 | }; |
| 68 | |
| 69 | /** |
| 70 | Basic server class |
| 71 | */ |
| 72 | class server |
| 73 | { |
| 74 | public: |
| 75 | enum log_level_values { none=0, error=1, debug=2 }; |
| 76 | |
| 77 | private: |
| 78 | int default_timeout; |
| 79 | log_level_values log_level; |
| 80 | std::ostream *logstream; |
| 81 | |
| 82 | unsigned int next_id; |
| 83 | std::map<unsigned int, connection*> connections; |
| 84 | |
| 85 | protected: |
| 86 | server() |
| 87 | { |
| 88 | set_default_timeout(30); |
| 89 | set_logging(NULL,none); |
| 90 | next_id=0; |
| 91 | } |
| 92 | |
| 93 | public: |
| 94 | virtual ~server(); |
| 95 | |
| 96 | void set_default_timeout(int _default_timeout) |
| 97 | { default_timeout=_default_timeout; } |
| 98 | |
| 99 | void set_logging(std::ostream *_logstream, log_level_values _log_level) |
| 100 | { |
| 101 | log_level=_log_level; |
| 102 | logstream=_logstream; |
| 103 | } |
| 104 | |
| 105 | connection* get_connection(unsigned int conn_id); |
| 106 | |
| 107 | virtual void fill_buffer(long long usec_timeout=-1)=0; |
| 108 | void check_timeout(); |
| 109 | |
| 110 | bool get_packet(std::string& data, unsigned int& conn_id); |
| 111 | |
| 112 | void fill_connection_buffers(void); |
| 113 | |
| 114 | protected: |
| 115 | void log(log_level_values level, const std::string& message) |
| 116 | { log(level,message.c_str()); } |
| 117 | void log(log_level_values level, const char* message); |
| 118 | }; |
| 119 | |
| 120 | } |
| 121 | |
| 122 | #endif |