CORBAModule.cxx
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
00033
00034
00035 #ifdef USE_CORBA
00036
00037
00038 #include "../tool/disable4786.h"
00039 #include "CORBAModule.h"
00040 #include "CORBASource.h"
00041 #include "CORBASink.h"
00042
00043 #include "../tool/OT_ACE_Log.h"
00044 #include <OT_CORBA.hh>
00045
00046 #include <stdio.h>
00047 #include <stdlib.h>
00048 #include <iostream>
00049 #include <string>
00050
00051 #include <ace/Log_Msg.h>
00052
00053
00054
00055
00056
00057 namespace ot {
00058
00059
00060 void CORBAModule::destroyORB()
00061 {
00062 try {
00063
00064 orb->destroy();
00065 }
00066 catch(CORBA::SystemException&) {
00067 cerr << "Caught CORBA::SystemException." << endl;
00068 }
00069 catch(CORBA::Exception&) {
00070 cerr << "Caught CORBA::Exception." << endl;
00071 }
00072 catch(omniORB::fatalException& fe) {
00073 cerr << "Caught omniORB::fatalException:" << endl;
00074 cerr << " file: " << fe.file() << endl;
00075 cerr << " line: " << fe.line() << endl;
00076 cerr << " mesg: " << fe.errmsg() << endl;
00077 }
00078 catch(...) {
00079 cerr << "Caught unknown exception." << endl;
00080 }
00081 }
00082
00083
00084
00085 void CORBAModule::initializeORB(int argc, char **argv)
00086 {
00087 try {
00088
00089 orb = CORBA::ORB_init(argc, argv);
00090
00091
00092 objref = orb->resolve_initial_references("RootPOA");
00093 root_poa = PortableServer::POA::_narrow(objref);
00094 pman = root_poa->the_POAManager();
00095 pman->activate();
00096
00097 if (persistent) {
00098
00099 CORBA::PolicyList pl;
00100 pl.length(2);
00101 pl[0] = root_poa->create_lifespan_policy(PortableServer::PERSISTENT);
00102 pl[1] = root_poa->create_id_assignment_policy(PortableServer::USER_ID);
00103
00104 poa = root_poa->create_POA("persistent poa", pman, pl);
00105 } else {
00106 poa = PortableServer::POA::_duplicate(root_poa);
00107 }
00108 }
00109 catch(CORBA::SystemException&) {
00110
00111 cerr << "Caught CORBA::SystemException." << endl;
00112 }
00113 catch(CORBA::Exception&) {
00114 cerr << "Caught CORBA::Exception." << endl;
00115 }
00116 catch(omniORB::fatalException& fe) {
00117 cerr << "Caught omniORB::fatalException:" << endl;
00118 cerr << " file: " << fe.file() << endl;
00119 cerr << " line: " << fe.line() << endl;
00120 cerr << " mesg: " << fe.errmsg() << endl;
00121 }
00122 catch(...) {
00123 cerr << "Caught unknown exception." << endl;
00124 }
00125 }
00126
00127
00128
00129 CORBAModule::~CORBAModule()
00130 {
00131 sinks.clear();
00132 destroyORB();
00133 }
00134
00135 void CORBAModule::init(StringTable& attributes, ConfigNode * localTree)
00136 {
00137 Module::init( attributes, localTree );
00138 if( attributes.containsKey("endPoint")) {
00139 persistent = true;
00140
00141 std::string endpoint = attributes.get("endPoint");
00142 char *av[4]; int ac = 4;
00143 char arg1[] = "opentracker";
00144 char arg2[] = "-ORBendPoint";
00145 char* arg3 = new char[endpoint.length()+1];
00146 endpoint.copy(arg3, std::string::npos);
00147 char arg4[] = "\0";
00148 char arg5 = (char) 0;
00149 av[0] = arg1;
00150 av[1] = arg2;
00151 av[2] = arg3;
00152 av[3] = arg4;
00153 av[4] = &arg5;
00154 initializeORB(ac, av);
00155 delete arg3;
00156 } else {
00157 persistent = false;
00158
00159 char *av[3]; int ac = 2;
00160 char arg1[] = "opentracker";
00161 char arg2[] = "\0";
00162 char arg3 = (char) 0;
00163 av[0] = arg1;
00164 av[1] = arg2;
00165 av[2] = &arg3;
00166 initializeORB(ac, av);
00167 }
00168 }
00169
00170
00171
00172
00173 void CORBAModule::clear()
00174 {
00175 for( CORBASinkVector::iterator it = sinks.begin(); it != sinks.end(); it++ )
00176 {
00177 delete *it;
00178 }
00179 sinks.clear();
00180 }
00181
00182
00183
00184 Node * CORBAModule::createNode( const std::string& name, StringTable& attributes)
00185 {
00186 if( name.compare("CORBASink") == 0 )
00187 {
00188 int frequency;
00189 int num = sscanf(attributes.get("frequency").c_str(), " %i", &frequency );
00190 if( num == 0 ){
00191 frequency = 1;
00192 }
00193 CosNaming::NamingContextExt::StringName_var string_name = CORBA::string_dup((const char*) attributes.get("name").c_str());
00194 CORBA::Object_var obj = CORBAUtils::getObjectReference(orb, string_name);
00195 if (CORBA::is_nil(obj)) {
00196 LOG_ACE_INFO("Could not obtain a reference to object supposedly bound to %s.\nExiting....\n", (const char*) string_name);
00197 exit(-1);
00198 }
00199
00200 OT_CORBA::Node_var sink_ref = OT_CORBA::Node::_narrow(obj);
00201
00202 CORBASink * sink = new CORBASink( sink_ref , frequency );
00203 sinks.push_back( sink );
00204 return sink;
00205 }
00206 else if (name.compare("CORBASource") == 0 )
00207 {
00208 CosNaming::NamingContextExt::StringName_var name = CORBA::string_dup((const char*) attributes.get("name").c_str());
00209 CORBASource * source_impl = new CORBASource( );
00210
00211 POA_OT_CORBA::OTSource_tie<CORBASource>* corba_source = new POA_OT_CORBA::OTSource_tie<CORBASource>(source_impl);
00212 PortableServer::ObjectId_var corba_source_id;
00213 if (persistent)
00214 {
00215 corba_source_id = CORBAUtils::getObjectId(orb, name);
00216 poa->activate_object_with_id(corba_source_id, corba_source);
00217 }
00218 else {
00219 corba_source_id = poa->activate_object(corba_source);
00220 }
00221 OT_CORBA::OTSource_var corba_source_ref = corba_source->_this();
00222 CORBA::Object_var obj = CORBA::Object::_narrow(corba_source_ref);
00223 CORBAUtils::bindObjectReferenceToName(orb, obj, name);
00224
00225 sources[source_impl] = corba_source_ref;
00226
00227 ACE_DEBUG((LM_DEBUG, ACE_TEXT("ot:Build CORBASource node\n")));
00228 return source_impl;
00229 }
00230 return NULL;
00231 }
00232
00233
00234 void CORBAModule::pushEvent()
00235 {
00236 for (SourceNodeMap::const_iterator source_it=sources.begin(); source_it!=sources.end(); ++source_it)
00237 {
00238 try {
00239 source_it->second->push();
00240 } catch (CORBA::TRANSIENT) {
00241 cerr << "Caught CORBA::TRANSIENT" << endl;
00242 }
00243 }
00244 }
00245
00246 void CORBASource::push()
00247 {
00248 lock();
00249 if (modified) {
00250 updateObservers( event );
00251 modified = false;
00252 }
00253 unlock();
00254 }
00255
00256 void CORBASource::setEvent(const OT_CORBA::Event& new_event)
00257 {
00258 lock();
00259 modified = true;
00260 CORBAUtils::convertFromCORBAEvent(event, new_event);
00261 unlock();
00262 }
00263
00264 }
00265 #endif //USE_CORBA