PLearn 0.1
RemotePLearnServer.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // RemotePLearnServer.cc
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.cc 9130 2008-06-16 18:21:54Z saintmlx $ 
00038  ******************************************************* */
00039 
00040 // Authors: Pascal Vincent
00041 
00045 #include "RemotePLearnServer.h"
00046 #include "PLearnService.h"
00047 #include <plearn/io/pl_log.h>
00048 
00049 namespace PLearn {
00050 using namespace std;
00051 
00052 RemotePLearnServer::RemotePLearnServer(const PStream& serverio)
00053     :io(serverio)
00054 {
00055     io.remote_plearn_comm= true;
00056 }
00057   
00058 void RemotePLearnServer::clearMaps()
00059 {
00060     io.clearInOutMaps();
00061     // copy local object map to stream's out map
00062     for(ObjMap::iterator it= objmap.begin(); it != objmap.end(); ++it)
00063         io.copies_map_out[it->second]= it->first;
00064 }
00065   
00066 void RemotePLearnServer::link(unsigned int objid, void* obj)
00067 { 
00068     io.copies_map_out[obj]= objid;
00069     objmap[objid]= obj; 
00070     rev_objmap[obj]= objid;
00071 
00072     DBG_LOG << "copies map: " << objmap << endl;
00073 }
00074 
00075 void RemotePLearnServer::unlink(unsigned int objid)
00076 { 
00077     ObjMap::iterator it= objmap.find(objid);
00078     if(it == objmap.end())
00079         PLERROR("in RemotePLearnServer::unlink : cannot unlink an object which is not linked");
00080     objmap.erase(it);
00081     rev_objmap.erase(it->second);
00082     io.copies_map_out.erase(it->second);
00083 }
00084 
00085 void RemotePLearnServer::unlink(void* obj)
00086 { 
00087     ReverseObjMap::iterator it= rev_objmap.find(obj);
00088     if(it == rev_objmap.end())
00089         PLERROR("in RemotePLearnServer::unlink : cannot unlink an object which is not linked");
00090     rev_objmap.erase(it);
00091     objmap.erase(it->second);
00092     io.copies_map_out.erase(obj);
00093 }
00094 
00095 
00096 void RemotePLearnServer::newObject(int objid, const Object& model)
00097 { 
00098     clearMaps();
00099     io.write("!N "); io << objid << model << endl;
00100     expectResults(0);
00101 }
00102 
00103 void RemotePLearnServer::newObject(int objid, PP<Object> model)
00104 {
00105     if(model.isNull())
00106         PLERROR("In RemotePLearnServer::newObject model is a Null pointer");
00107     newObject(objid, *model);
00108 }
00109 
00110 void RemotePLearnServer::newObject(int objid, const string& description)
00111 { 
00112     clearMaps();
00113     io.write("!N "); io << objid; io.put(' ');
00114     io.write(description);
00115     io << endl;
00116     expectResults(0);
00117 }
00118 
00119 int RemotePLearnServer::newObject(const Object& model)
00120 { 
00121     clearMaps();
00122     io.write("!O "); io << model << endl;
00123     int objid;
00124     getResults(objid);
00125     return objid;
00126 }
00127 
00128 int RemotePLearnServer::newObject(PP<Object> model)
00129 {
00130     if(model.isNull())
00131         PLERROR("In RemotePLearnServer::newObject model is a Null pointer");
00132     return newObject(*model);
00133 }
00134 
00135 int RemotePLearnServer::newObject(const string& description)
00136 { 
00137     clearMaps();
00138     io.write("!O "); 
00139     io.write(description);
00140     io << endl;
00141     int objid;
00142     getResults(objid);
00143     return objid;
00144 }
00145 
00146 
00147 void RemotePLearnServer::newObjectAsync(int objid, const Object& model)
00148 { 
00149     clearMaps();
00150     io.write("!N "); io << objid << model << endl;
00151 }
00152 
00153 void RemotePLearnServer::newObjectAsync(int objid, PP<Object> model)
00154 {
00155     if(model.isNull())
00156         PLERROR("In RemotePLearnServer::newObject model is a Null pointer");
00157     newObjectAsync(objid, *model);
00158 }
00159 
00160 void RemotePLearnServer::newObjectAsync(int objid, const string& description)
00161 { 
00162     clearMaps();
00163     io.write("!N "); io << objid; io.put(' ');
00164     io.write(description);
00165     io << endl;
00166 }
00167 
00168 void RemotePLearnServer::newObjectAsync(const Object& model)
00169 { 
00170     clearMaps();
00171     io.write("!O "); io << model << endl;
00172 }
00173 
00174 void RemotePLearnServer::newObjectAsync(const PP<Object>& model)
00175 {
00176     if(model.isNull())
00177         PLERROR("In RemotePLearnServer::newObject model is a Null pointer");
00178     newObjectAsync(*model);
00179 }
00180 
00181 void RemotePLearnServer::newObjectAsync(const string& description)
00182 { 
00183     clearMaps();
00184     io.write("!O "); 
00185     io.write(description);
00186     io << endl;
00187 }
00188 
00189 
00190 
00191 void RemotePLearnServer::deleteObject(int objid)
00192 {
00193     deleteObjectAsync(objid);
00194     expectResults(0);
00195 }
00196 
00197 void RemotePLearnServer::deleteObjectAsync(int objid)
00198 {
00199     io.write("!D "); io << objid << endl;
00200 }
00201 
00202 void RemotePLearnServer::deleteAllObjects()
00203 {
00204     deleteAllObjectsAsync();
00205     getResults();
00206 }
00207 
00208 void RemotePLearnServer::deleteAllObjectsAsync()
00209 {
00210     if(io.good())
00211     {
00212         io.write("!Z "); 
00213         io << endl;
00214     }
00215     else
00216         DBG_LOG << "in RemotePLearnServer::deleteAllObjectsAsync() : stream not good." << endl;
00217 }
00218 
00219 
00220 void RemotePLearnServer::expectResults(int nargs_expected)
00221 {
00222     PLearnService& service= PLearnService::instance();
00223     service.waitForResultFrom(this);
00224 
00225     //DBG_LOG << "RemotePLearnServer entering expectResults" << endl;
00226     io.skipBlanksAndComments();
00227     int headchar = io.get();
00228     if(headchar!='!')
00229         PLERROR(" Answers from plearn server are expected to start with a !, but I received a %c",headchar);
00230     int command = io.get();
00231     //DBG_LOG << "RemotePLearnServer expectResults received command: " << (char)command << endl;
00232     int nreturned;
00233     string msg;
00234     switch(command)
00235     {
00236     case 'R':
00237         io >> nreturned;
00238         //DBG_LOG << "RemotePLearnServer expectResults nreturned= " << nreturned << endl;
00239         if(nreturned!=nargs_expected)
00240             PLERROR("RemotePLearnServer: expected %d return arguments, but read R %d",nargs_expected,nreturned);
00241         break;
00242     case 'E':
00243         io >> msg;
00244         {
00245             pair<string, int> id= PLearnService::getId(this);
00246             PLERROR("From server %s %d : %s", id.first.c_str(), id.second, 
00247                     msg.c_str());
00248         }
00249         break;
00250     default:
00251         PLERROR("RemotePLearnServer: expected R (return command), but read %c ????",command);
00252     }
00253 }
00254 
00255 RemotePLearnServer::~RemotePLearnServer()
00256 {
00257     // The PLearnService is responsible for most of RemotePLearnServer destruction 
00258     DBG_LOG << "ENTERING RemotePLearnServer destructor" << endl;
00259     io.remote_plearn_comm= false;
00260     DBG_LOG << "LEAVING RemotePLearnServer destructor" << endl;
00261 }
00262 
00263 
00264 } // end of namespace PLearn
00265 
00266 
00267 /*
00268   Local Variables:
00269   mode:c++
00270   c-basic-offset:4
00271   c-file-style:"stroustrup"
00272   c-file-offsets:((innamespace . 0)(inline-open . 0))
00273   indent-tabs-mode:nil
00274   fill-column:79
00275   End:
00276 */
00277 // 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