RetinaService.C
Go to the documentation of this file.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/RetinaService.H"
00039
00040 RetinaI::RetinaI(ModelManager& mgr,
00041 nub::ref<InputFrameSeries> ifs,
00042 const std::string& descrName,
00043 const std::string& tagName
00044 ) :
00045 ModelComponent(mgr, descrName, tagName),
00046 itsIfs(ifs)
00047 {
00048 }
00049
00050 void RetinaI::initSimEvents(const Ice::CommunicatorPtr icPtr, const Ice::ObjectPrx objectPrx)
00051 {
00052
00053 Ice::ObjectPrx obj = icPtr->stringToProxy("SimEvents/TopicManager:tcp -p 11111");
00054 IceStorm::TopicManagerPrx topicManager =
00055 IceStorm::TopicManagerPrx::checkedCast(obj);
00056 IceStorm::TopicPrx topic;
00057 try {
00058 topic = topicManager->retrieve("RetinaTopic");
00059 } catch (const IceStorm::NoSuchTopic&) {
00060 topic = topicManager->create("RetinaTopic");
00061 }
00062
00063
00064 Ice::ObjectPrx pub = topic->getPublisher()->ice_oneway();
00065 itsEventsPub = SimEvents::EventsPrx::uncheckedCast(pub);
00066 }
00067
00068
00069 void RetinaI::evolve(const SimEvents::EventMessagePtr& eMsg,
00070 const Ice::Current&)
00071 {
00072 }
00073
00074
00075 void RetinaI::run()
00076 {
00077 while(1)
00078 {
00079 getFrame();
00080 if (itsCurrentImg.initialized())
00081 {
00082 SimEvents::RetinaMessagePtr retinaMsg = new SimEvents::RetinaMessage;
00083 retinaMsg->img = Image2Ice(itsCurrentImg);
00084 itsEventsPub->evolve(retinaMsg);
00085 }
00086 usleep(10000);
00087 }
00088 }
00089
00090
00091 void RetinaI::getFrame()
00092 {
00093 itsCurrentImg.clear();
00094 const FrameState is = itsIfs->updateNext();
00095 if (is == FRAME_COMPLETE) return;
00096
00097 GenericFrame input = itsIfs->readFrame();
00098 if (!input.initialized()) return;
00099 itsCurrentImg = input.asRgb();
00100 }
00101
00102
00103 ImageIceMod::ImageIce RetinaI::getOutput(const Ice::Current&)
00104 {
00105 return Image2Ice(itsCurrentImg);
00106 }
00107
00108
00109 class RetinaService : public Ice::Service {
00110 protected:
00111 virtual bool start(int, char* argv[]);
00112 virtual bool stop();
00113 private:
00114 Ice::ObjectAdapterPtr itsAdapter;
00115 ModelManager *itsMgr;
00116 };
00117
00118 bool RetinaService::stop()
00119 {
00120 if (itsMgr)
00121 delete itsMgr;
00122 return true;
00123 }
00124
00125 bool RetinaService::start(int argc, char* argv[])
00126 {
00127
00128 itsMgr = new ModelManager("RetinaService");
00129
00130 nub::ref<InputFrameSeries> ifs(new InputFrameSeries(*itsMgr));
00131 itsMgr->addSubComponent(ifs);
00132
00133 nub::ref<RetinaI> retina(new RetinaI(*itsMgr, ifs));
00134 itsMgr->addSubComponent(retina);
00135
00136 itsMgr->parseCommandLine((const int)argc, (const char**)argv, "", 0, 0);
00137
00138 char adapterStr[255];
00139 sprintf(adapterStr, "default -p %i", BrainObjects::RetinaPort);
00140 itsAdapter = communicator()->createObjectAdapterWithEndpoints("RetinaAdapter", adapterStr);
00141
00142 Ice::ObjectPtr object = retina.get();
00143 Ice::ObjectPrx objectPrx = itsAdapter->add(object, communicator()->stringToIdentity("Retina"));
00144 retina->initSimEvents(communicator(), objectPrx);
00145 itsAdapter->activate();
00146
00147 itsMgr->start();
00148
00149
00150 IceUtil::ThreadPtr retinaThread = retina.get();
00151 retinaThread->start();
00152
00153 return true;
00154 }
00155
00156
00157 int main(int argc, char** argv) {
00158
00159 RetinaService svc;
00160 return svc.main(argc, argv);
00161 }
00162