PLearn 0.1
RemotePLearnServer.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // RemotePLearnServer.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: RemotePLearnServer.h 7042 2007-05-09 23:44:20Z saintmlx $ 
00038  ******************************************************* */
00039 
00040 // Authors: Pascal Vincent
00041 
00045 #ifndef RemotePLearnServer_INC
00046 #define RemotePLearnServer_INC
00047 
00048 #include <plearn/base/Object.h>
00049 #include <plearn/base/PP.h>
00050 #include <plearn/io/PStream.h>
00051 #include <plearn/sys/Popen.h>
00052 #include <plearn/io/pl_log.h>
00053 
00054 namespace PLearn {
00055 
00056 
00057 class RemotePLearnServer: public PPointable
00058 {    
00059 private:
00060     friend class PLearnService;
00061     PStream io; // io communication channel with remote PLearnServer
00062     RemotePLearnServer(const PStream& serverio);
00063 
00064     typedef map<unsigned int, void*> ObjMap;
00065     ObjMap objmap;
00066     typedef map<void*, unsigned int> ReverseObjMap;
00067     ReverseObjMap rev_objmap;
00068 
00069 public:
00070 
00071     void killServer() { io << "!K " << endl; }
00072     
00075     void newObject(int objid, const Object& model);
00076 
00077     void newObject(int objid, PP<Object> model);
00078 
00081     void newObject(int objid, const string& description);  
00082 
00085     int newObject(const Object& model);
00086     int newObject(PP<Object> model);
00089     int newObject(const string& description);  
00090     
00093     void newObjectAsync(int objid, const Object& model);
00094     void newObjectAsync(int objid, PP<Object> model);
00097     void newObjectAsync(int objid, const string& description);  
00098 
00101     void newObjectAsync(const Object& model);
00102     void newObjectAsync(const PP<Object>& model);
00105     void newObjectAsync(const string& description);  
00106 
00108     void deleteObject(int objid);
00109 
00111     void deleteObjectAsync(int objid);
00112 
00114     void deleteAllObjects();
00115 
00117     void deleteAllObjectsAsync();
00118 
00119     // object map related methods
00120     void clearMaps();
00121     // Should link/unlink be called automatically in newObject/deleteObject ? -xsm
00122     void link(unsigned int objid, void* obj);
00123     void unlink(unsigned int objid);
00124     void unlink(void* obj);
00125 
00127     inline void sendFunctionCallHeader(const string& function_name, int nargs)
00128     { 
00129         clearMaps();
00130         io.write("!F "); io << function_name << nargs; 
00131     }
00132 
00134     inline void sendMethodCallHeader(int objid, const string& method_name, int nargs)
00135     { 
00136         clearMaps();
00137         io.write("!M "); io << objid << method_name << nargs; 
00138     }
00139     
00141 
00145     void expectResults(int nargs_expected);
00146 
00148     inline void callFunction(const string& name)
00149     {
00150         sendFunctionCallHeader(name, 0);
00151         io << endl;
00152     }
00154     template<class Arg1>
00155     inline void callFunction(const string& name, const Arg1& arg1)
00156     {
00157         sendFunctionCallHeader(name, 1);
00158         io << arg1 << endl;
00159     }
00161     template<class Arg1, class Arg2>
00162     inline void callFunction(const string& name, const Arg1& arg1, const Arg2& arg2)
00163     {
00164         sendFunctionCallHeader(name, 2);
00165         io << arg1 << arg2 << endl;
00166     }
00168     template<class Arg1, class Arg2, class Arg3>
00169     inline void callFunction(const string& name, const Arg1& arg1, const Arg2& arg2, const Arg3& arg3)
00170     {
00171         sendFunctionCallHeader(name, 3);
00172         io << arg1 << arg2 << arg3 << endl;
00173     }
00175     template<class Arg1, class Arg2, class Arg3, class Arg4>
00176     inline void callFunction(const string& name, const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4)
00177     {
00178         sendFunctionCallHeader(name, 4);
00179         io << arg1 << arg2 << arg3 << arg4 << endl;
00180     }
00182     template<class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
00183     inline void callFunction(const string& name, const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4, const Arg5& arg5)
00184     {
00185         sendFunctionCallHeader(name, 5);
00186         io << arg1 << arg2 << arg3 << arg4 << arg5 << endl;
00187     }
00189     template<class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
00190     inline void callFunction(const string& name, const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4, const Arg5& arg5, const Arg6& arg6)
00191     {
00192         sendFunctionCallHeader(name, 6);
00193         io << arg1 << arg2 << arg3 << arg4 << arg5 << arg6 << endl;
00194     }
00195 
00197     inline void callMethod(int objid, const string& name)
00198     {
00199         sendMethodCallHeader(objid, name, 0);
00200         io << endl;
00201     }
00203     template<class Arg1>
00204     inline void callMethod(int objid, const string& name, const Arg1& arg1)
00205     {
00206         sendMethodCallHeader(objid, name, 1);
00207         io << arg1 << endl;
00208     }
00210     template<class Arg1, class Arg2>
00211     inline void callMethod(int objid, const string& name, const Arg1& arg1, const Arg2& arg2)
00212     {
00213         sendMethodCallHeader(objid, name, 2);
00214         io << arg1 << arg2 << endl;
00215     }
00217     template<class Arg1, class Arg2, class Arg3>
00218     inline void callMethod(int objid, const string& name, const Arg1& arg1, const Arg2& arg2, const Arg3& arg3)
00219     {
00220         sendMethodCallHeader(objid, name, 3);
00221         io << arg1 << arg2 << arg3 << endl;
00222     }
00224     template<class Arg1, class Arg2, class Arg3, class Arg4>
00225     inline void callMethod(int objid, const string& name, const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4)
00226     {
00227         sendMethodCallHeader(objid, name, 4);
00228         io << arg1 << arg2 << arg3 << arg4 << endl;
00229     }
00231     template<class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
00232     inline void callMethod(int objid, const string& name, const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4, const Arg5& arg5)
00233     {
00234         sendMethodCallHeader(objid, name, 5);
00235         io << arg1 << arg2 << arg3 << arg4 << arg5 << endl;
00236     }
00238     template<class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
00239     inline void callMethod(int objid, const string& name, const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4, const Arg5& arg5, const Arg6& arg6)
00240     {
00241         sendMethodCallHeader(objid, name, 6);
00242         io << arg1 << arg2 << arg3 << arg4 << arg5 << arg6 << endl;
00243     }
00244 
00247     inline void getResults()
00248     { expectResults(0); };
00249 
00252     template<class Arg1>
00253     inline void getResults(Arg1& arg1)
00254     {
00255         expectResults(1);
00256         //DBG_LOG << "RemotePLearnServer getResults(Arg1& arg1)" << endl;
00257         io >> arg1;
00258         //DBG_LOG << "RemotePLearnServer getResults got arg1" << endl;
00259     }
00262     template<class Arg1, class Arg2>
00263     inline void getResults(Arg1& arg1, Arg2& arg2)
00264     {
00265         expectResults(2);
00266         io >> arg1 >> arg2;
00267     }
00270     template<class Arg1, class Arg2, class Arg3>
00271     inline void getResults(Arg1& arg1, Arg2& arg2, Arg3& arg3)
00272     {
00273         expectResults(3);
00274         io >> arg1 >> arg2 >> arg3;
00275     }
00278     template<class Arg1, class Arg2, class Arg3, class Arg4>
00279     inline void getResults(Arg1& arg1, Arg2& arg2, Arg3& arg3, Arg4& arg4)
00280     {
00281         expectResults(4);
00282         io >> arg1 >> arg2 >> arg3 >> arg4;
00283     }
00286     template<class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
00287     inline void getResults(Arg1& arg1, Arg2& arg2, Arg3& arg3, Arg4& arg4, Arg5& arg5)
00288     {
00289         expectResults(5);
00290         io >> arg1 >> arg2 >> arg3 >> arg4 >> arg5;
00291     }
00294     template<class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
00295     inline void getResults(Arg1& arg1, Arg2& arg2, Arg3& arg3, Arg4& arg4, Arg5& arg5, Arg6& arg6)
00296     {
00297         expectResults(6);
00298         io >> arg1 >> arg2 >> arg3 >> arg4 >> arg5 >> arg6;
00299     }  
00300 
00301     ~RemotePLearnServer();
00302     
00303 };
00304 
00305 
00306 } // end of namespace PLearn
00307 
00308 #endif
00309 
00310 
00311 /*
00312   Local Variables:
00313   mode:c++
00314   c-basic-offset:4
00315   c-file-style:"stroustrup"
00316   c-file-offsets:((innamespace . 0)(inline-open . 0))
00317   indent-tabs-mode:nil
00318   fill-column:79
00319   End:
00320 */
00321 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines