00001 // vim:ts=8 sts=4 sw=4 00002 00011 // ***** BEGIN LICENSE BLOCK ***** 00012 // Version: MPL 1.1/GPL 2.0/LGPL 2.1 00013 // 00014 // The contents of this file are subject to the Mozilla Public License 00015 // Version 1.1 (the "License"); you may not use this file except in 00016 // compliance with the License. You may obtain a copy of the License at 00017 // http://www.mozilla.org/MPL/ 00018 // 00019 // Software distributed under the License is distributed on an "AS IS" 00020 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 00021 // the License for the specific language governing rights and limitations 00022 // under the License. 00023 // 00024 // The Original Code is the consmgr network/serial-line monitoring package. 00025 // 00026 // The Initial Developer of the Original Code is Chris P. Ross. 00027 // Portions created by the Initial Developer are Copyright (C) 2000-2008 00028 // the Initial Developer. All Rights Reserved. 00029 // 00030 // Contributor(s): 00031 // 00032 // Alternatively, the contents of this file may be used under the terms of 00033 // either the GNU General Public License Version 2 or later (the "GPL"), or 00034 // the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 00035 // in which case the provisions of the GPL or the LGPL are applicable instead 00036 // of those above. If you wish to allow use of your version of this file only 00037 // under the terms of either the GPL or the LGPL, and not to allow others to 00038 // use your version of this file under the terms of the MPL, indicate your 00039 // decision by deleting the provisions above and replace them with the notice 00040 // and other provisions required by the GPL or the LGPL. If you do not delete 00041 // the provisions above, a recipient may use your version of this file under 00042 // the terms of any one of the MPL, the GPL or the LGPL. 00043 // 00044 // ***** END LICENSE BLOCK ***** 00045 00046 #ifndef __CONNECTION_H 00047 #define __CONNECTION_H 00048 00049 #include "messageable.h" 00050 #include "messager.h" 00051 #include "connector.h" 00052 00089 class Connection : public Messageable 00090 { 00091 public: 00095 Connection(ConnectorP&); 00096 virtual ~Connection() throw(); 00097 00098 private: 00108 static void *manager_thread(void*); 00109 00110 // 00111 // Useful methods 00112 // 00113 00114 private: 00119 void manager(void); 00120 00127 #if SHOULD_NOT_BE_HERE 00128 00130 void initiate_connection(void); 00131 #endif 00132 public: // Perhaps shutdown() should be a pure virtual of Messageable? 00135 void shutdown(void); 00136 00137 00138 // 00139 // Virtual methods for functionality that needs to be implemented 00140 // in a connection-type-dependant way. 00141 // 00142 00143 protected: 00146 virtual void start_engine(void) = 0; 00148 virtual void stop_engine(void) = 0; 00149 00150 00151 public: 00152 // 00153 // Inquires 00154 // 00155 #if MAY_NOT_WANT_THIS 00157 bool connected(void); 00158 #endif 00160 virtual std::string class_name(void) const { return "Connection"; } 00161 virtual std::string get_id(void) const = 0; 00162 00163 // 00164 // Data members 00165 // 00166 00167 protected: 00168 ConnectorP base; 00169 00170 void note_disconnect(void); 00171 00175 bool please_exit; 00176 00177 private: 00179 pthread_mutex_t mutex; 00180 00181 #if MAY_NOT_WANT_THIS 00183 pthread_cond_t state_change; // Signal change in connection state 00184 bool conn_state; 00185 #endif 00186 00188 pthread_t tid_manager; 00189 }; 00190 00193 // This is a class who's function operator can be passed to a for_each 00194 // call. It will remove the given Messageable from the recipient list 00195 // of all Connections. 00196 class invalidate_messageable { 00197 public: 00198 invalidate_messageable(Messageable *m) : recipient(m) {} 00199 void operator()(const Connection *conn) const { 00200 // This dynamic cast will only produce a non-NULL pointer 00201 // if only conn is a Messager. If it's some non-messaging 00202 // other subclass of Connection, it will return NULL. 00203 Messager *sender = const_cast<Messager*>( 00204 dynamic_cast<const Messager*>(conn)); 00205 if (sender) 00206 sender->del_recipient(recipient); 00207 } 00208 private: 00209 Messageable *recipient; 00210 }; 00211 00212 #endif /* __CONNECTION_H */