VisualCortexService.C
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "NeovisionII/VisualCortexService.H"
00039 #include "Image/ColorOps.H"
00040 #include "GUI/DebugWin.H"
00041
00042 VisualCortexI::VisualCortexI(OptionManager& mgr,
00043 const std::string& descrName,
00044 const std::string& tagName ) :
00045 ModelComponent(mgr, descrName, tagName)
00046 {
00047 itsEvc = nub::soft_ref<EnvVisualCortex>(new EnvVisualCortex(mgr));
00048 addSubComponent(itsEvc);
00049
00050
00051 IceStorm::TopicPrx topicPrx;
00052 TopicInfo tInfo("RetinaTopic", topicPrx);
00053 itsTopicsSubscriptions.push_back(tInfo);
00054
00055 }
00056
00057 VisualCortexI::~VisualCortexI()
00058 {
00059 unsubscribeSimEvents();
00060 }
00061
00062 void VisualCortexI::initSimEvents(Ice::CommunicatorPtr icPtr, Ice::ObjectPrx objectPrx)
00063 {
00064
00065 Ice::ObjectPrx obj = icPtr->stringToProxy("SimEvents/TopicManager:tcp -p 11111");
00066 IceStorm::TopicManagerPrx topicManager =
00067 IceStorm::TopicManagerPrx::checkedCast(obj);
00068
00069
00070 IceStorm::TopicPrx topic;
00071 try {
00072 topic = topicManager->retrieve("VisualCortexTopic");
00073 } catch (const IceStorm::NoSuchTopic&) {
00074 topic = topicManager->create("VisualCortexTopic");
00075 }
00076
00077 Ice::ObjectPrx pub = topic->getPublisher()->ice_oneway();
00078 itsEventsPub = SimEvents::EventsPrx::uncheckedCast(pub);
00079
00080
00081 itsObjectPrx = objectPrx;
00082
00083 for(uint i=0; i<itsTopicsSubscriptions.size(); i++)
00084 {
00085 try {
00086 IceStorm::QoS qos;
00087 itsTopicsSubscriptions[i].topicPrx =
00088 topicManager->retrieve(itsTopicsSubscriptions[i].name.c_str());
00089 itsTopicsSubscriptions[i].topicPrx->subscribeAndGetPublisher(qos, itsObjectPrx);
00090 } catch (const IceStorm::NoSuchTopic&) {
00091 LFATAL("Error! No %s topic found!", itsTopicsSubscriptions[i].name.c_str());
00092 } catch (const char* msg) {
00093 LINFO("Error %s", msg);
00094 } catch (const Ice::Exception& e) {
00095 cerr << e << endl;
00096 }
00097
00098 }
00099 }
00100
00101 void VisualCortexI::unsubscribeSimEvents()
00102 {
00103
00104 for(uint i=0; i<itsTopicsSubscriptions.size(); i++)
00105 {
00106 LINFO("Unsubscribe from %s", itsTopicsSubscriptions[i].name.c_str());
00107 itsTopicsSubscriptions[i].topicPrx->unsubscribe(itsObjectPrx);
00108 }
00109 }
00110
00111
00112
00113 void VisualCortexI::evolve(const SimEvents::EventMessagePtr& eMsg,
00114 const Ice::Current&)
00115 {
00116 if(eMsg->ice_isA("::SimEvents::RetinaMessage")){
00117 SimEvents::RetinaMessagePtr msg = SimEvents::RetinaMessagePtr::dynamicCast(eMsg);
00118 Image<PixRGB<byte> > retinaImg = Ice2Image<PixRGB<byte> > (msg->img);
00119
00120 itsEvc->input(retinaImg);
00121 Image<byte> smap = itsEvc->getVCXmap();
00122
00123
00124 SimEvents::VisualCortexMessagePtr vcMsg = new SimEvents::VisualCortexMessage;
00125 vcMsg->vco = Image2Ice(smap);
00126 try {
00127 itsEventsPub->evolve(vcMsg);
00128 } catch(const IceStorm::NoSuchTopic&) {
00129 }
00130 }
00131 }
00132
00133
00134 class VisualCortexService : public Ice::Service {
00135 protected:
00136 virtual bool start(int, char* argv[]);
00137 virtual bool stop() {
00138 if (itsMgr)
00139 delete itsMgr;
00140 return true;
00141 }
00142
00143 private:
00144 Ice::ObjectAdapterPtr itsAdapter;
00145 ModelManager *itsMgr;
00146 };
00147
00148 bool VisualCortexService::start(int argc, char* argv[])
00149 {
00150
00151 itsMgr = new ModelManager("VisualCortexService");
00152
00153 nub::ref<VisualCortexI> vc(new VisualCortexI(*itsMgr));
00154 itsMgr->addSubComponent(vc);
00155
00156 itsMgr->parseCommandLine((const int)argc, (const char**)argv, "", 0, 0);
00157
00158 char adapterStr[255];
00159 sprintf(adapterStr, "default -p %i", BrainObjects::VisualCortexPort);
00160 itsAdapter = communicator()->createObjectAdapterWithEndpoints("VisualCortexAdapter",
00161 adapterStr);
00162
00163 Ice::ObjectPtr object = vc.get();
00164 Ice::ObjectPrx objectPrx = itsAdapter->add(object, communicator()->stringToIdentity("VisualCortex"));
00165 vc->initSimEvents(communicator(), objectPrx);
00166 itsAdapter->activate();
00167
00168 itsMgr->start();
00169
00170 return true;
00171 }
00172
00173
00174 int main(int argc, char** argv) {
00175
00176 VisualCortexService svc;
00177 return svc.main(argc, argv);
00178 }
00179
00180