RTCompConnect.h

説明を見る。
00001 //=================================================================================================
00002 //  RT コンポーネント接続クラス
00003 //
00004 //  date   : 20080720 first
00005 //  date   : 20080721 update
00006 //
00007 //  Author : Takashi KOJIMA, Chuo University
00008 //
00009 //=================================================================================================
00010 #ifndef INTERFACE_CONNECT_RT_COMPONENT_CLASS
00011 #define INTERFACE_CONNECT_RT_COMPONENT_CLASS
00012 
00013 #include "RTCompControl.h"
00014 
00016 /*
00017 *RT port 周りを集めた構造体
00018 */
00019 struct RTCompSet
00020 {
00021         RTC::CorbaConsumer<RTC::RTObject> con;       
00022         int                               numOfPort; 
00023   RTC::PortList_var                 pList;     
00024         
00026         RTCompSet():con(),numOfPort(-1),pList(){};
00028         RTCompSet(const RTCompSet& src):con(src.con),numOfPort(src.numOfPort),pList(src.pList){};
00030         ~RTCompSet(){};
00032         RTCompSet& operator=(const RTCompSet& right)
00033         {
00034                 con       = right.con;
00035                 numOfPort = right.numOfPort;
00036                 pList     = right.pList;
00037                 return *this;
00038         };
00039 };
00040 
00042 
00052 class RTCompConnect : public ORBvarManager
00053 {
00054         public:
00056         RTCompConnect(
00057                  std::string CorbaServeName   
00058                 ,long        CorbaPort=-1     
00059         );
00060 
00062         RTCompConnect(
00063                  const RTCompConnect& src     
00064         );
00065         
00067         virtual ~RTCompConnect();
00068 
00070 
00074         void SetNameServer(std::string ServerName ,long OmniPort=-1);
00075 
00077 
00082         void SetNameServer(const int num, std::string ServerName, long OmniPort=-1);
00083 
00085 
00090         bool SetPort( const int num, std::string ComponentName, std::string PortName    );
00091 
00093 
00098         bool SetPort( const int num,std::string ComponentName,int PortNumber);
00099 
00101 
00106         bool SetPort(const int num, RTC::PortList_var &plist, std::string PortName);
00107 
00109 
00114         bool SetPort(const int num, RTC::PortList_var &plist,int PortNumber);
00115 
00117 
00122         bool Connect( std::string ConnectionName, std::string subs_type="Flush",std::string period   ="" );
00123 
00125         inline bool DisConnect(
00126                 std::string ConnectionName      
00127         );
00128 
00130         RTCompConnect& operator=(
00131                 const RTCompConnect& right      
00132         );
00133 
00134         private:
00135         RTC::CorbaNaming       m_nameserver0;               
00136         RTC::CorbaNaming       m_nameserver1;               
00137         RTCompSet              m_port[2];                   
00138         std::string            m_connect_id;                
00139 
00140         protected:
00142 
00145         RTC::CorbaNaming* GetNameServer(const int num);
00146 
00148 
00153         void SetPortList  (
00154                  RTCompSet *port
00155                 ,std::string &ComponentName
00156                 ,RTC::CorbaNaming *NameServer
00157         );
00158 
00160 
00164         bool SetPortName  (RTCompSet *port, std::string &PortName);
00165 
00167 
00171         bool SetPortNumber(RTCompSet *port, int &PortNumber);
00172 
00174 
00179         std::string NameAndPort2str(std::string name, long port)
00180         {
00181                 if(port!=-1)
00182                 {
00183                         char PortStr[6];
00184                         sprintf(PortStr,":%ld",port);
00185                         name += PortStr;
00186                 }
00187                 return name;
00188         };
00189 
00190 };
00191 
00192 
00194 
00195 RTCompConnect::RTCompConnect(
00196                  std::string CorbaServerName   
00197                 ,long        CorbaPort        
00198 )
00199 :m_nameserver0(s_orb,NameAndPort2str(CorbaServerName, CorbaPort).c_str())
00200 ,m_nameserver1(s_orb,NameAndPort2str(CorbaServerName, CorbaPort).c_str())
00201 ,m_connect_id("")
00202 {
00203         SetNameServer(CorbaServerName,CorbaPort);
00204 }
00205 
00206 RTCompConnect::RTCompConnect
00207 (
00208         const RTCompConnect& src
00209 ):m_nameserver0(src.m_nameserver0)
00210 ,m_nameserver1(src.m_nameserver1)
00211 ,m_connect_id(src.m_connect_id)
00212 {
00213         m_port[0] = src.m_port[0];
00214         m_port[1] = src.m_port[1];
00215 }
00216 
00217 RTCompConnect::~RTCompConnect()
00218 {
00219 
00220 }
00221 
00222 void RTCompConnect::SetNameServer(std::string ServerName, long OmniPort)
00223 {
00224         ServerName = NameAndPort2str(ServerName, OmniPort);
00225         try{
00226                 m_nameserver0.init(ServerName.c_str());
00227                 m_nameserver1.init(ServerName.c_str());
00228         }
00229         catch(...)
00230         {
00231                 std::cout << "Error happen. Check what Corbaname server is running." << std::endl;
00232                 exit(1);
00233         }
00234 }
00235 void RTCompConnect::SetNameServer(const int num, std::string ServerName, long OmniPort)
00236 {
00237         ServerName = NameAndPort2str(ServerName, OmniPort);
00238         try{
00239                 GetNameServer(num)->init(ServerName.c_str());
00240         }
00241         catch(...)
00242         {
00243                 std::cout << "Error happen. Check what Corbaname server is running." << std::endl;
00244                 exit(1);
00245         }
00246 }
00247 
00248 RTC::CorbaNaming* RTCompConnect::GetNameServer(const int num)
00249 {
00250         switch(num)
00251         {
00252                 case 0:
00253                 return &m_nameserver0;
00254 
00255                 case 1:
00256                 return &m_nameserver1;
00257 
00258                 default:
00259                 std::cout << "Don't exist number" << std::endl;
00260         }
00261         return 0;
00262 }
00263 
00264 //push_back_port
00265 bool RTCompConnect::SetPort(const int num, std::string ComponentName,  std::string PortName)
00266 {
00267         if(num != 0 && num != 1)return false;
00269         SetPortList(&m_port[num],       ComponentName, GetNameServer(num));
00271         return SetPortName(&m_port[num],PortName);
00272 
00273 }
00274 //push_back_port
00275 bool RTCompConnect::SetPort(const int num, std::string ComponentName,  int PortNumber)
00276 {
00277         if(num != 0 && num != 1)return false;
00279         SetPortList(&m_port[num],       ComponentName, GetNameServer(num));
00281         return SetPortNumber(&m_port[num],PortNumber);
00282 }
00283 //push_back_port
00284 bool RTCompConnect::SetPort(const int num, RTC::PortList_var &plist,  std::string PortName)
00285 {
00286         if(num != 0 && num != 1)return false;
00288         m_port[num].pList = plist;
00290         return SetPortName(&m_port[num],PortName);
00291 }
00292 
00293 //push_back_port
00294 bool RTCompConnect::SetPort(const int num, RTC::PortList_var &plist,  int PortNumber)
00295 {
00296         if(num != 0 && num != 1)return false;
00298         m_port[num].pList = plist;
00300         return SetPortNumber(&m_port[num],PortNumber);
00301 }
00302 
00303 // Connect
00304 bool RTCompConnect::Connect(std::string ConnectionName,std::string subs_type, std::string period)
00305 {
00306         unsigned int i;
00308   RTC::ConnectorProfile prof;
00309   prof.connector_id = "";
00310   prof.name = CORBA::string_dup(ConnectionName.c_str());
00311 
00312   prof.ports.length(2);
00313         for(i=0;i<2;i++)
00314         {
00315                 prof.ports[i] = m_port[i].pList[ m_port[i].numOfPort ];
00316         }
00317 
00319   CORBA_SeqUtil::push_back(prof.properties, NVUtil::newNV("dataport.interface_type", "CORBA_Any"));
00320   CORBA_SeqUtil::push_back(prof.properties, NVUtil::newNV("dataport.dataflow_type", "Push"));
00321   CORBA_SeqUtil::push_back(prof.properties, NVUtil::newNV("dataport.subscription_type", subs_type.c_str()));
00322   if (period != ""){
00323                 CORBA_SeqUtil::push_back(prof.properties, NVUtil::newNV("dataport.push_interval",                period.c_str()));
00324         }
00325 
00327         DisConnect(m_connect_id);
00328 
00330   RTC::ReturnCode_t ret;
00331   ret = m_port[0].pList[m_port[0].numOfPort]->connect(prof);
00332   assert(ret == RTC::RTC_OK);
00333 
00335         if(ret == RTC::RTC_OK)std::cout << "=== Connection Success ===" <<std::endl;
00336         else                  std::cout << "=== Connection Failure ===" <<std::endl;
00337   std::cout << "Connector ID: " << prof.connector_id << std::endl;
00338   NVUtil::dump(prof.properties);
00339         std::cout << "==========================" << std::endl;
00340         m_connect_id = prof.connector_id;
00341         return (ret == RTC::RTC_OK);
00342 }
00343 
00344 //Disconnect
00345 bool RTCompConnect::DisConnect(
00346         std::string ConnectionName     
00347 )
00348 {
00349         if(m_connect_id=="")return true;
00350         m_port[0].pList[m_port[0].numOfPort]->disconnect(m_connect_id.c_str());
00351         m_connect_id = "";
00352         //切断処理を実装すべし
00353         return true;
00354 }
00355 
00356 // operator
00357 RTCompConnect& RTCompConnect::operator=(const RTCompConnect& right)
00358 {
00359         m_nameserver0 = right.m_nameserver0;
00360         m_nameserver1 = right.m_nameserver1;
00361         m_port[0]     = right.m_port[0];
00362         m_port[1]     = right.m_port[1];
00363         return *this;
00364 }
00365 
00367 void RTCompConnect::SetPortList  (RTCompSet *port, std::string &ComponentName, RTC::CorbaNaming *NameServer)
00368 {
00369         port->con.setObject(NameServer->resolve(ComponentName.c_str()));
00370   port->pList = port->con->get_ports();
00371 }
00372 
00374 bool RTCompConnect::SetPortName  (RTCompSet *port, std::string &PortName)
00375 {
00376         std::string oneOfPortName;
00377         port->numOfPort = -1;
00378         for(unsigned int i(0);i<port->pList->length();i++)
00379         {
00380                 oneOfPortName = port->pList[i]->get_port_profile()->name;
00381                 if( oneOfPortName == PortName )
00382                 {
00383                         port->numOfPort = i;
00384                         return true;
00385                 }
00386         }
00387         return false;
00388 }
00389 
00391 bool RTCompConnect::SetPortNumber(RTCompSet *port, int &PortNumber)
00392 {
00393         if(port->pList->length()<1)return false;
00394         else
00395         {
00396                 port->numOfPort = PortNumber;
00397                 return true;
00398         }
00399 }
00400 
00401 #endif

RTComponentControl に対してMon Jul 21 11:37:23 2008に生成されました。  doxygen 1.5.1