PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // PLearnService.h 00004 // 00005 // Copyright (C) 2005 Pascal Vincent 00006 // Copyright (C) 2007 Xavier Saint-Mleux, ApSTAT Technologies inc. 00007 // 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 /* ******************************************************* 00037 * $Id: PLearnService.h 9630 2008-10-30 22:28:50Z saintmlx $ 00038 ******************************************************* */ 00039 00040 // Authors: Pascal Vincent 00041 00045 #ifndef PLearnService_INC 00046 #define PLearnService_INC 00047 00048 #include <plearn/misc/RemotePLearnServer.h> 00049 #include <plearn/base/PP.h> 00050 #include <plearn/io/PPath.h> 00051 #include <plearn/io/PStream.h> 00052 #include <plearn/math/TVec.h> 00053 #include <map> 00054 #include <set> 00055 #include <string> 00056 00057 namespace PLearn { 00058 00059 // class RemotePLearnServer; 00060 00061 class PLearnService: public PPointable 00062 { 00063 private: 00064 PLearnService(); 00065 00066 // TVec<PStream> serversio; 00067 // TVec<int> available_servers; 00068 // std::map<RemotePLearnServer*,int> reserved_servers; 00069 00070 TVec< PP<RemotePLearnServer> > available_servers; 00071 std::set< PP<RemotePLearnServer> > reserved_servers; 00072 00073 typedef map<RemotePLearnServer*, map<unsigned int, PP<ProgressBar> > > progress_bars_t; 00074 static progress_bars_t progress_bars; 00075 static map<RemotePLearnServer*, pair<string,int> > servers_ids; 00076 00077 public: 00078 friend class RemotePLearnServer; 00079 00080 // static void remoteLaunchServers(PPath serverfile, int nservers, int tcpport, const string& launch_command); 00081 00082 // Returns the unique static instance of class PLearnService 00083 static PLearnService& instance(); 00084 00086 void connectToServers(TVec< pair<string,int> > server_names_and_port); 00087 00090 void connectToServers(PPath serversfile); 00091 00092 void disconnectFromServers(); 00093 00094 void disconnectFromServer(PP<RemotePLearnServer> server); 00095 00097 int availableServers() const; 00098 00102 PP<RemotePLearnServer> reserveServer(); 00103 00105 TVec< PP<RemotePLearnServer> > reserveServers(int nservers); 00106 00109 void freeServer(PP<RemotePLearnServer> server); 00110 00112 void freeServers(TVec< PP<RemotePLearnServer> > servers); 00113 00114 int watchServers(TVec< PP<RemotePLearnServer> > servers, int timeout=0); 00115 00116 typedef void (*log_callback_t)(PP<RemotePLearnServer> server, const string& module_name, int vlevel, const string& msg); 00117 typedef void (*progress_callback_t)(PP<RemotePLearnServer> server, unsigned int pbar, char action, 00118 unsigned int pos, const string& title); 00119 00120 static void log_callback(PP<RemotePLearnServer> server, const string& module_name, int vlevel, const string& msg); 00121 static void progress_callback(PP<RemotePLearnServer> server, unsigned int pbar, char action, 00122 unsigned int pos= 0, const string& title= ""); 00123 00124 int watchServers(TVec< PP<RemotePLearnServer> > servers, 00125 log_callback_t the_log_callback, 00126 progress_callback_t the_progress_callback); 00127 00128 PP<RemotePLearnServer> waitForResult(TVec< PP<RemotePLearnServer> > servers= TVec< PP<RemotePLearnServer> >(), 00129 log_callback_t the_log_callback = log_callback, 00130 progress_callback_t the_progress_callback = progress_callback); 00131 00132 void waitForResultFrom(PP<RemotePLearnServer> from, 00133 log_callback_t the_log_callback = log_callback, 00134 progress_callback_t the_progress_callback = progress_callback); 00135 00136 static pair<string, int> getId(RemotePLearnServer* server) { return servers_ids[server]; } 00137 00138 ~PLearnService(); 00139 }; 00140 00141 00142 /* global function exported to python: 00143 Connect the global PLearnService instance to the listed servers. 00144 */ 00145 void globalConnectToServers(TVec< pair<string,int> > hostname_and_port); 00146 00147 } // end of namespace PLearn 00148 00149 #endif 00150 00151 00152 /* 00153 Local Variables: 00154 mode:c++ 00155 c-basic-offset:4 00156 c-file-style:"stroustrup" 00157 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00158 indent-tabs-mode:nil 00159 fill-column:79 00160 End: 00161 */ 00162 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :