00001 /*!@file Corba/Objects/SceneRecServer.C control a SceneRec via corba */ 00002 00003 //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Lior Elazary <lelazary@yahoo.com> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Corba/Objects/SceneRecServer.C $ 00035 // $Id: SceneRecServer.C 9412 2008-03-10 23:10:15Z farhan $ 00036 // 00037 00038 #include <stdlib.h> 00039 #include <time.h> 00040 #include <iostream> 00041 #include <unistd.h> 00042 #include <fcntl.h> 00043 #include <sys/types.h> 00044 #include <sys/stat.h> 00045 #include <signal.h> 00046 #include "Image/ColorOps.H" 00047 #include "Image/MathOps.H" 00048 #include "Image/Image.H" 00049 #include "Image/ImageSet.H" 00050 #include "Image/Pixels.H" 00051 #include "Image/PyramidOps.H" 00052 #include "Image/ShapeOps.H" 00053 #include "Image/Transforms.H" 00054 #include "Image/fancynorm.H" 00055 #include "Util/Assert.H" 00056 #include "Util/Timer.H" 00057 #include "Util/Types.H" 00058 #include "Component/ModelManager.H" 00059 #include "Corba/Objects/SceneRecServerSK.hh" 00060 #include "Corba/ImageOrbUtil.H" 00061 #include "Corba/Objects/SceneRecServer.H" 00062 #include "Corba/CorbaUtil.H" 00063 00064 CORBA::ORB_var orb; 00065 CosNaming::Name objectName; 00066 00067 bool Debug = false; 00068 00069 //! Signal handler (e.g., for control-C) 00070 void terminate(int s) 00071 { 00072 LERROR("*** INTERRUPT ***"); 00073 unbindObject(orb, "saliency", "SceneRec", objectName); 00074 orb->shutdown(0); 00075 } 00076 00077 void SceneRecServer_i::shutdown() { 00078 // Shutdown the ORB 00079 unbindObject(orb, "saliency", "SceneRec", objectName); 00080 orb->shutdown(0); 00081 } 00082 00083 00084 SceneRecServer_i::SceneRecServer_i(OptionManager &mgr) : 00085 SceneRec(mgr) 00086 { 00087 } 00088 00089 00090 SceneRecServer_i::~SceneRecServer_i() { 00091 00092 } 00093 00094 00095 void SceneRecServer_i::newInput(const ImageOrb &img){ 00096 Image<PixRGB<byte> > image; 00097 orb2Image(img, image); 00098 SceneRec::newInput(image); 00099 00100 00101 } 00102 00103 short SceneRecServer_i::outputReady(){ 00104 00105 return SceneRec::outputReady(); 00106 } 00107 00108 short SceneRecServer_i::getLandmarkLoc(Point2DOrb &loc){ 00109 00110 Point2D<int> location; 00111 00112 short leg = SceneRec::getLandmarkLoc(location); 00113 loc.i = location.i; 00114 loc.j = location.j; 00115 00116 return leg; 00117 } 00118 00119 void SceneRecServer_i::trainFeature(const ImageOrb &img, const Point2DOrb &loc, 00120 const DimsOrb &window, const short leg){ 00121 00122 Image<PixRGB<byte> > image; 00123 orb2Image(img, image); 00124 00125 SceneRec::trainFeature(image, Point2D<int>(loc.i, loc.j), Dims(window.ww, window.hh), leg); 00126 } 00127 00128 00129 //start the class server 00130 int main(int argc, char **argv){ 00131 00132 MYLOGVERB = LOG_INFO; 00133 00134 // Instantiate a ModelManager: 00135 ModelManager manager("SceneRecServer Corba Object"); 00136 nub::ref<SceneRecServer_i> sceneRec(new SceneRecServer_i(manager)); 00137 manager.addSubComponent(sceneRec); 00138 00139 if (manager.parseCommandLine((const int)argc, (const char**)argv, "", 0, 0) == false) 00140 return(1); 00141 00142 if (manager.debugMode()){ 00143 Debug = true; 00144 } else { 00145 LINFO("Running as a daemon. Set --debug to see any errors."); 00146 //Become a daemon 00147 // fork off the parent process 00148 pid_t pid = fork(); 00149 if (pid < 0) 00150 LFATAL("Can not fork"); 00151 00152 if (pid > 0) 00153 exit(0); //exit the parent process 00154 00155 // Change the file mask 00156 umask(0); 00157 00158 //Create a new system id so that the kernel wont think we are an orphan. 00159 pid_t sid = setsid(); 00160 if (sid < 0) 00161 LFATAL("Can not become independent"); 00162 00163 fclose(stdin); 00164 fclose(stdout); 00165 fclose(stderr); 00166 00167 } 00168 // catch signals and redirect them to terminate for clean exit: 00169 signal(SIGHUP, terminate); signal(SIGINT, terminate); 00170 signal(SIGQUIT, terminate); signal(SIGTERM, terminate); 00171 signal(SIGALRM, terminate); 00172 00173 00174 //Create the object and run in 00175 orb = CORBA::ORB_init(argc, argv); 00176 00177 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA"); 00178 PortableServer::POA_var poa = PortableServer::POA::_narrow(obj); 00179 00180 SceneRecServer_i* myobj = sceneRec.get(); 00181 00182 PortableServer::ObjectId_var objID = poa->activate_object(myobj); 00183 00184 //get a ref string 00185 obj = myobj->_this(); 00186 CORBA::String_var sior(orb->object_to_string(obj)); 00187 std::cerr << "'" << (char*)sior << "'" << "\n"; 00188 00189 if( !bindObjectToName(orb, obj, "saliency", "SceneRec", "SceneRecServer", objectName) ) 00190 return 1; 00191 myobj->_remove_ref(); 00192 00193 PortableServer::POAManager_var pman = poa->the_POAManager(); 00194 pman->activate(); 00195 00196 00197 try { 00198 manager.start(); 00199 //run the object untill shutdown or killed 00200 orb->run(); 00201 } catch (...) { 00202 LINFO("Error starting server"); 00203 } 00204 00205 00206 LINFO("Shutting down"); 00207 unbindObject(orb, "saliency", "SceneRec", objectName); 00208 00209 manager.stop(); 00210 00211 return 0; 00212 } 00213