Esempio n. 1
0
int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
  try
  {
    PortableServer::POAManager_var poa_manager;

    CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);

    CORBA::Object_var poa_obj = orb->resolve_initial_references("RootPOA");
    PortableServer::POA_var root_poa = PortableServer::POA::_narrow(poa_obj.in());

    poa_manager = root_poa->the_POAManager();

    if (parse_args (argc, argv) != 0)
      return 1;

    poa_manager->activate();

    /*Get event_channel*/
    std::cout << "Get event_channel now"  << std::endl;
    CosNotifyChannelAdmin::EventChannel_var ec = get_event_channel(orb.in());

    //Instanciating the Consumer
    CosNotifyComm::StructuredPushConsumer_var spc =
      CosNotifyComm::StructuredPushConsumer::_nil();

    CosNotifyCommImpl::StructuredPushConsumer *pImpl_spc = new CosNotifyCommImpl::StructuredPushConsumer;
    spc = pImpl_spc->_this();

    //Obtain a Consumer Admin
    CosNotifyChannelAdmin::AdminID adminid = 0;
    CosNotifyChannelAdmin::ConsumerAdmin_var ca =
      ec->new_for_consumers (CosNotifyChannelAdmin::AND_OP, adminid);

    if( ca.in() == CosNotifyChannelAdmin::ConsumerAdmin::_nil() ){
      std::cerr << "ca is nil!" << std::endl;
      return 1;
    }

    //Obtain a Proxy Consumer
    CosNotifyChannelAdmin::ProxyID proxy_id;
    CosNotifyChannelAdmin::ClientType ctype = CosNotifyChannelAdmin::STRUCTURED_EVENT;

    CosNotifyChannelAdmin::ProxySupplier_var proxySupplier_obj;
    try
    {
      proxySupplier_obj = ca->obtain_notification_push_supplier(ctype, proxy_id);
    }
    catch(CosNotifyChannelAdmin::AdminLimitExceeded err)
    {
      std::cerr << "CosNotifyChannelAdmin::AdminLimitExceeded Exception!" << std::endl;
      throw;
    }

    CosNotifyChannelAdmin::StructuredProxyPushSupplier_var pps =
      CosNotifyChannelAdmin::StructuredProxyPushSupplier::_narrow(proxySupplier_obj.in());


    //Attaching a filter to pps
    CosNotifyFilter::FilterFactory_var dff =
      ec->default_filter_factory();

    ACE_ASSERT(!CORBA::is_nil(dff.in()));

    CosNotifyFilter::Filter_var filter = dff->create_filter("EXTENDED_TCL");

    CosNotification::EventTypeSeq event_types(1);
    event_types.length(2);

    event_types[0].domain_name = CORBA::string_dup("DomainA");
    event_types[0].type_name = CORBA::string_dup("TypeA");
    event_types[1].domain_name = CORBA::string_dup("DomainB");
    event_types[1].type_name = CORBA::string_dup("TypeB");

    CosNotifyFilter::ConstraintExpSeq constraints(1);
    constraints.length(1);

    constraints[0].event_types = event_types;
    constraints[0].constraint_expr = CORBA::string_dup(
      "");

    filter->add_constraints(constraints);

    pps->add_filter(filter.in());

    std::cout << "Attached a filter to ProxyPushSupplier" << std::endl;
    std::cout << "The filter's event_types[0].domain_name=" <<  event_types[0].domain_name << std::endl;
    std::cout << "The filter's event_types[0].type_name=" <<  event_types[0].type_name << std::endl;
    std::cout << "The filter's event_types[1].domain_name=" <<  event_types[1].domain_name << std::endl;
    std::cout << "The filter's event_types[1].type_name=" <<  event_types[1].type_name << std::endl;


    //Connecting a Supplier to a Proxy Consumer
    try
    {
      pps->connect_structured_push_consumer(spc.in());
    }
    catch (CosEventChannelAdmin::AlreadyConnected ac)
    {
      std::cerr << "CosEventChannelAdmin::AlreadyConnected" << std::endl;
      throw;
    }
    catch (const CORBA::SystemException& se)
    {
      std::cerr << "System exception occurred during connect: " <<
        se << std::endl;
      throw;
    }


    ACE_Time_Value tv (runtime);
    orb->run (tv);
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Consumer done.\n")));

    if (pImpl_spc->received_events ())
    {
      //Consumer should not receive any events as it's filtered with event type.
      std::cerr << "Test failed - received test events." << std::endl;
      return 1;
    }
    else
    {
      std::cerr << "Test passed - did not receive test events as expected." << std::endl;
    }
  }
  catch(...)
  {
    std::cerr << "Consumer: Some exceptions was caught!" << std::endl;
    return 1;
  }


  return 0;
}
Esempio n. 2
0
int main(int argc, char* argv[]){

  char aux1[1000];


  try{
    //
    // Inicializacion del ORB 
    //
    CORBA::ORB_var orb = CORBA::ORB_init(argc, argv, "");

    CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
    PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
    PortableServer::POAManager_var manager = root_poa->the_POAManager();


    //
    // Obtain NamincContext reference ...
    //
    CORBA::Object_var naming_context_object;
    CosNaming::NamingContext_ptr naming_context;
    try {
       naming_context_object = 
         orb->string_to_object("corbaloc:iiop:[email protected]:2809/NameService");
       naming_context =
          CosNaming::NamingContext::_narrow (naming_context_object.in ());
    } catch (...) {
       cerr << "Error: cannot obtain naming service initial reference" << endl;
       throw; 
    }

    //
    // Crea un POA para el supplier: PERSISTENT LifespanPolicy, USER_ID policy
    // 

    PortableServer::LifespanPolicy_var lifespan =
      root_poa->create_lifespan_policy(PortableServer::PERSISTENT);
    
    PortableServer::IdAssignmentPolicy_var idAssignment =
      root_poa->create_id_assignment_policy(PortableServer::USER_ID);
    
    CORBA::PolicyList policies(2);  
    policies.length(2);
    policies[0] = PortableServer::IdAssignmentPolicy::_duplicate(idAssignment);
    policies[1] = PortableServer::LifespanPolicy::_duplicate(lifespan);
    
    PortableServer::POA_var consumer_poa =
      root_poa->create_POA("consumerPOA", manager.in(), policies);

    PortableServer::POAManager_var consumer_poa_manager = 
      consumer_poa->the_POAManager();

    idAssignment->destroy();
    lifespan->destroy();

    // Activa el POAManager
    manager->activate();


    //
    // Crea y activa el servant del consumer
    // 

    PortableServer::ObjectId_var oid = 
      PortableServer::string_to_ObjectId("MyConsumer");

    MyConsumerImpl servant_consumer;

    try{
      consumer_poa->activate_object_with_id(oid.in(), &servant_consumer);
    }
    catch(...){
      cerr << "[supplier] ERROR: activating servant_supplier " << endl;
    }
    CORBA::Object_var consumer_obj = consumer_poa->id_to_reference(oid.in());


    CosEventComm::PushConsumer_var consumer =
      CosEventComm::PushConsumer::_narrow(consumer_obj);


    //
    // Recoge la referencia al canal
    //
    // readChannelIOR();
    // obj = orb->string_to_object(ChannelIOR);
    CosNaming::NamingContext_var naming_context_notif = NULL;
    CosNaming::Name name_notif(2);
    name_notif.length (2);
    name_notif[0].id = CORBA::string_dup ("MyNotif");
    name_notif[0].kind = CORBA::string_dup ("");
    name_notif[1].id = CORBA::string_dup ("channel");
    name_notif[1].kind = CORBA::string_dup ("");
    obj = naming_context->resolve(name_notif);

    CosNotifyChannelAdmin::EventChannel_var channel;
    channel = CosNotifyChannelAdmin::EventChannel::_narrow(obj);
    if (CORBA::is_nil(channel)) { 
      cerr << "[consumer] ERROR: canal nulo " << endl;
      return -1; 
    } 
    // cerr << "[consumer] ChannelId " << channel->get_channelID() << endl;


    // 
    // Obtenemos el manager 
    // 
    CosNotifyChannelAdmin::ConsumerAdmin_var consumerAdmin;
    CosNotifyChannelAdmin::AdminID adminid;
    
    try{
      consumerAdmin = channel->default_consumer_admin();   
    }
    catch(CORBA::Exception &ce){
      fprintf(stderr,"(%s)%i CORBA::Exception - %s\n",
              __FILE__,__LINE__,ce._name());
    }
    catch(...){
      fprintf(stderr,"(%s)%i - Unexpected exception!",
              __FILE__,__LINE__);
    }


    //
    // Obtenemos el proxy
    // 
    CosNotifyChannelAdmin::ProxyPushSupplier_var pushSupplier;
    CosNotifyChannelAdmin::ProxyID proxy_id;
    CosNotifyChannelAdmin::ClientType ctype = CosNotifyChannelAdmin::ANY_EVENT;

    try{
      CosNotifyChannelAdmin::ProxySupplier_var obj =
        consumerAdmin->obtain_notification_push_supplier(ctype, proxy_id);
      
      pushSupplier = CosNotifyChannelAdmin::ProxyPushSupplier::_narrow(obj);
      
      // Conecta proxy
      pushSupplier->connect_any_push_consumer(consumer);

    }
    catch(CosEventChannelAdmin::AlreadyConnected &ac){
      fprintf(stderr,"(%s)%i CosEventChannelAdmin::AlreadyConnected\n",
              __FILE__,__LINE__);
    }
    catch(CORBA::SystemException& se){
      fprintf(stderr,"(%s)%i CORBA::SystemException\n",
              __FILE__,__LINE__);
    }
    catch(CosNotifyChannelAdmin::AdminLimitExceeded err){
      fprintf(stderr,"(%s)%i CosNotifyChannelAdmin::AdminLimitExceeded\n",
              __FILE__,__LINE__);
    }
    catch(CORBA::Exception &ce){
      fprintf(stderr,"(%s)%i CORBA::Exception - %s\n",
              __FILE__,__LINE__,ce._name());
    }
    catch(...){
      fprintf(stderr,"Unexpected exception!\n");
    }

//     // 
//     // Guarda la referencia en el Servicio de Nombres
//     // 

//     CosNaming::NamingContext_var naming_context_notif = NULL;
//     CosNaming::Name name_notif(1);
//     name_notif.length (1);
//     name_notif[0].id = CORBA::string_dup ("MyNotif");
//     name_notif[0].kind = CORBA::string_dup ("");

//     naming_context_notif = naming_context->bind_new_context(name_notif);

//     CosNaming::Name name_consumer(1);
//     name_consumer.length (1);
//     name_consumer[0].id = CORBA::string_dup ("consumer");
//     name_consumer[0].kind = CORBA::string_dup ("");
//     naming_context_notif->bind(name_consumer, consumer_obj);



    cerr << "[consumer] Esperando eventos.... " << endl;


    orb->run();  

    cerr << "[consumer] disconnect .... " << endl;
//     supplier->disconnect_push_supplier();
//     consumeradmin->destroy();

    cerr << "[consumer] Terminando " << endl;
    orb->shutdown(true);
    orb->destroy();



  } catch(CORBA::Exception& exc) {
    cerr << "[consumer] Excepcion: " << exc << endl;
    return 1;
  }

}