00001 // -*- C++ -*- 00002 // bidirconn.h - This package defines a bi-directional connection, from which 00003 // subclasses can be created for different types of bi-dir connections. 00004 // 00005 // ***** BEGIN LICENSE BLOCK ***** 00006 // Version: MPL 1.1/GPL 2.0/LGPL 2.1 00007 // 00008 // The contents of this file are subject to the Mozilla Public License 00009 // Version 1.1 (the "License"); you may not use this file except in 00010 // compliance with the License. You may obtain a copy of the License at 00011 // http://www.mozilla.org/MPL/ 00012 // 00013 // Software distributed under the License is distributed on an "AS IS" 00014 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 00015 // the License for the specific language governing rights and limitations 00016 // under the License. 00017 // 00018 // The Original Code is the consmgr network/serial-line monitoring package. 00019 // 00020 // The Initial Developer of the Original Code is Chris P. Ross. 00021 // Portions created by the Initial Developer are Copyright (C) 2000-2008 00022 // the Initial Developer. All Rights Reserved. 00023 // 00024 // Contributor(s): 00025 // 00026 // Alternatively, the contents of this file may be used under the terms of 00027 // either the GNU General Public License Version 2 or later (the "GPL"), or 00028 // the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 00029 // in which case the provisions of the GPL or the LGPL are applicable instead 00030 // of those above. If you wish to allow use of your version of this file only 00031 // under the terms of either the GPL or the LGPL, and not to allow others to 00032 // use your version of this file under the terms of the MPL, indicate your 00033 // decision by deleting the provisions above and replace them with the notice 00034 // and other provisions required by the GPL or the LGPL. If you do not delete 00035 // the provisions above, a recipient may use your version of this file under 00036 // the terms of any one of the MPL, the GPL or the LGPL. 00037 // 00038 // ***** END LICENSE BLOCK ***** 00039 00040 #ifndef __BIDIRCONN_H 00041 #define __BIDIRCONN_H 00042 00043 extern "C" { 00044 #include <pthread.h> 00045 #include <string.h> 00046 } 00047 00048 #include <deque> 00049 #include <set> 00050 #include <map> 00051 #include <functional> 00052 00053 #include <string> 00054 00055 #include "messager.h" 00056 #include "outlet.h" 00057 #include "connlist.h" 00058 #include "protocol.h" 00059 00063 class BiDirConn : public Outlet, public Messager { 00064 public: 00066 BiDirConn(ConnectorP&); 00068 virtual ~BiDirConn() throw(); 00069 00071 void add_encoder(Encoder*); 00072 00073 virtual std::string class_name(void) const { return("BiDirConn"); }; 00074 virtual std::string get_id(void) const; 00075 00076 protected: 00079 void start_engine(void); 00080 00081 private: 00083 static void *reader_thread(void *); 00085 void *reader(void); 00086 00088 pthread_t tid_reader; 00089 00090 // XXX - Should we have a wrapper class for this as well, to protect it 00091 // with a mutex? 00092 std::set<Encoder*> encoders; 00093 00094 protected: 00096 virtual void stop_engine(void); 00097 00098 #if SHOULD_NOT_BE_HERE 00100 void disconnect(void); 00101 #endif 00102 00105 /* XXX - This could be a MessageP, but as it's only looked at */ 00106 /* and returned, I can just pass the Data*, right? */ 00107 virtual void process_data(Data*); 00108 00109 #if 0 00111 void send_data_to_recipients(Blob *); 00112 #endif 00113 }; 00114 00115 00116 // 00117 // Misc functions that exist mostly so that ConnectionList::mutating_for_each() 00118 // can call their operator()'s... 00119 // 00120 00123 class invoke_queue_data { 00124 public: 00125 invoke_queue_data(MessageP &data) : m(data) { 00126 if (dynamic_cast<Data*>(MessageP::GetPointer(data)) == NULL) 00127 throw CMUnsupported("Cannot queue a non-data Message"); 00128 } 00129 void operator()(Outlet *o) const { 00130 o->queue_data(const_cast<MessageP&>(m)); 00131 } 00132 private: 00133 MessageP m; 00134 }; 00135 00136 #endif /* __BIDIRCONN_H */