SimEventsUtils.H

00001 /*!@file Ice/IceStormUtils.H Utilities to interact with ice storm */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the 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
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Ice/SimEventsUtils.H $
00035 // $Id: SimEventsUtils.H 10794 2009-02-08 06:21:09Z itti $
00036 //
00037 //
00038 #ifndef SIMEVENTSUTILS
00039 #define SIMEVENTSUTILS
00040 
00041 #include <IceStorm/IceStorm.h>
00042 #include <Ice/Ice.h>
00043 
00044 namespace SimEventsUtils
00045 {
00046 
00047   struct TopicInfo
00048   {
00049     TopicInfo(const char* _name, IceStorm::TopicPrx& prx) :
00050       name(_name), topicPrx(prx)
00051     {}
00052     std::string name;
00053     IceStorm::TopicPrx topicPrx;
00054   };
00055 
00056   void initSimEvents(Ice::CommunicatorPtr icPtr, Ice::ObjectPrx objectPrx,
00057       std::vector<TopicInfo>& topicsSubscriptions)
00058   {
00059     //Get the IceStorm object
00060     Ice::ObjectPrx obj = icPtr->stringToProxy("SimEvents/TopicManager:tcp -p 11111");
00061     IceStorm::TopicManagerPrx topicManager =
00062       IceStorm::TopicManagerPrx::checkedCast(obj);
00063 
00064 
00065     //subscribe  to the topics
00066     for(uint i=0; i<topicsSubscriptions.size();  i++)
00067     {
00068       try {
00069         IceStorm::QoS qos;
00070         topicsSubscriptions[i].topicPrx =
00071           topicManager->retrieve(topicsSubscriptions[i].name.c_str()); //Get the
00072         topicsSubscriptions[i].topicPrx->subscribeAndGetPublisher(qos, objectPrx); //Subscribe to the retina topic
00073       } catch (const IceStorm::NoSuchTopic&) {
00074         LFATAL("Error! No %s topic found!", topicsSubscriptions[i].name.c_str());
00075       } catch (const IceStorm::AlreadySubscribed&) {
00076         LDEBUG("Skipping already subscribed topic");
00077       }
00078     }
00079   }
00080 
00081   Ice::ObjectPrx getPublisher(Ice::CommunicatorPtr icPtr, std::string topicName)
00082   {
00083     //Get the IceStorm object
00084     Ice::ObjectPrx obj = icPtr->stringToProxy("SimEvents/TopicManager:tcp -p 11111");
00085     IceStorm::TopicManagerPrx topicManager =
00086       IceStorm::TopicManagerPrx::checkedCast(obj);
00087 
00088     ////Create a PrefrontalCortex Topic
00089     IceStorm::TopicPrx topic;
00090     try {
00091       topic = topicManager->retrieve(topicName.c_str()); //check if the topic exists
00092     } catch (const IceStorm::NoSuchTopic&) {
00093       topic = topicManager->create(topicName.c_str()); //The topic does not exists, create
00094     }
00095 
00096     //Make a one way visualCortex message publisher for efficency
00097     return topic->getPublisher()->ice_oneway();
00098   }
00099 
00100   void createTopic(Ice::CommunicatorPtr icPtr, std::string topicName)
00101   {
00102     //Get the IceStorm object
00103     Ice::ObjectPrx obj = icPtr->stringToProxy("SimEvents/TopicManager:tcp -p 11111");
00104     IceStorm::TopicManagerPrx topicManager =
00105       IceStorm::TopicManagerPrx::checkedCast(obj);
00106 
00107     ////Create a PrefrontalCortex Topic
00108     IceStorm::TopicPrx topic;
00109     try {
00110       topic = topicManager->retrieve(topicName.c_str()); //check if the topic exists
00111     } catch (const IceStorm::NoSuchTopic&) {
00112       topic = topicManager->create(topicName.c_str()); //The topic does not exists, create
00113     }
00114 
00115   }
00116 
00117 
00118 
00119   void unsubscribeSimEvents(
00120       std::vector<TopicInfo>& topicsSubscriptions,
00121       Ice::ObjectPrx objectPrx)
00122   {
00123     //Unsubscribe from all the topics we are registerd to
00124     for(uint i=0; i<topicsSubscriptions.size();  i++)
00125     {
00126       topicsSubscriptions[i].topicPrx->unsubscribe(objectPrx);
00127     }
00128   }
00129 
00130 }
00131 
00132 #endif
Generated on Sun May 8 08:40:48 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3