00001 // vim:shiftwidth=4 sts=4 00002 // 00003 // ConnectionList 00007 // 00008 // $Id$ 00009 00010 // ***** BEGIN LICENSE BLOCK ***** 00011 // Version: MPL 1.1/GPL 2.0/LGPL 2.1 00012 // 00013 // The contents of this file are subject to the Mozilla Public License 00014 // Version 1.1 (the "License"); you may not use this file except in 00015 // compliance with the License. You may obtain a copy of the License at 00016 // http://www.mozilla.org/MPL/ 00017 // 00018 // Software distributed under the License is distributed on an "AS IS" 00019 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 00020 // the License for the specific language governing rights and limitations 00021 // under the License. 00022 // 00023 // The Original Code is the consmgr network/serial-line monitoring package. 00024 // 00025 // The Initial Developer of the Original Code is Chris P. Ross. 00026 // Portions created by the Initial Developer are Copyright (C) 2000-2008 00027 // the Initial Developer. All Rights Reserved. 00028 // 00029 // Contributor(s): 00030 // 00031 // Alternatively, the contents of this file may be used under the terms of 00032 // either the GNU General Public License Version 2 or later (the "GPL"), or 00033 // the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 00034 // in which case the provisions of the GPL or the LGPL are applicable instead 00035 // of those above. If you wish to allow use of your version of this file only 00036 // under the terms of either the GPL or the LGPL, and not to allow others to 00037 // use your version of this file under the terms of the MPL, indicate your 00038 // decision by deleting the provisions above and replace them with the notice 00039 // and other provisions required by the GPL or the LGPL. If you do not delete 00040 // the provisions above, a recipient may use your version of this file under 00041 // the terms of any one of the MPL, the GPL or the LGPL. 00042 // 00043 // ***** END LICENSE BLOCK ***** 00044 00045 #ifndef __CONNLIST_H 00046 #define __CONNLIST_H 00047 00048 #include <pthread.h> 00049 00050 #include <list> 00051 00052 #include "connection.h" 00053 #include "outlet.h" 00054 00055 // This is meant to provide a list of Connections. This is not implemented 00056 // as a subclass of std::list<> so as to avoid having to protect from 00057 // parent class methods acting without mutex protection. 00058 // XXX - At some point in the future perhaps this should provide an interface 00059 // wrapping std::list<> ? Or at least providing an STL-compliant iterator? 00060 class ConnectionList 00061 { 00062 public: 00063 ConnectionList(void); 00064 ~ConnectionList(void); 00065 00066 void add(const Connection*); 00067 void remove(const Connection*); 00068 00069 template <class Func> 00070 void mutexed_for_each(const Func &f); 00071 00072 private: 00073 std::list<Connection*> list; 00074 pthread_mutex_t mutex; 00075 }; 00076 00082 template <class Func> 00083 void ConnectionList::mutexed_for_each(const Func &f) { 00084 int ret; 00085 Outlet *o; 00086 00087 ret = pthread_mutex_lock(&mutex); 00088 PTHREAD_CHECK_AND_THROW(ret, "mutex_lock(OutletList::mutex)"); 00089 00090 std::list<Connection*>::iterator i = list.begin(), e = list.end(); 00091 for ( ; i != e ; ++i) { 00092 o = dynamic_cast<Outlet*>(*i); 00093 00094 if (o == NULL) { 00095 std::cerr << "Unpectedly pulled a non-Outlet from a ConnectionList!" << endl; 00096 continue; 00097 } 00098 std::clog << "for_each'ing '" << o->get_id() << "(" << o << ")'" 00099 << std::endl; 00100 f(o); 00101 std::clog << "done for_each on '" << o->get_id() << "(" << o << ")'" 00102 << std::endl; 00103 } 00104 00105 ret = pthread_mutex_unlock(&mutex); 00106 PTHREAD_CHECK_AND_THROW(ret, "mutex_unlock(OutletList::mutex)"); 00107 00108 std::clog << "Done processing ConnList..." << std::endl; 00109 return; 00110 } 00111 00112 00113 #endif /* __CONNLIST_H */