Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / orbsvcs / DevGuideExamples / NotifyService / SupplierSideNC / Messenger_i.cpp
blobea1a56d9e66809fe94d9647fd6d93bcad32b1cdd
1 #include "orbsvcs/CosNotifyChannelAdminC.h"
2 #include "orbsvcs/CosNotifyCommC.h"
3 #include "orbsvcs/CosNamingC.h"
4 #include "orbsvcs/Notify/Notify_EventChannelFactory_i.h"
6 // The static initialization trick doesn't work with static builds.
7 // On SunOS 5.8 and MacOS X, the static initialization trick used
8 // in the CosNotification_Serv library does not work. Including the
9 // initializer class here works around the problem.
10 #if defined (TAO_AS_STATIC_LIBS) || defined (sun) || defined (__APPLE__)
11 #include "orbsvcs/Notify/CosNotify_Initializer.h"
12 #endif /* sun || __APPLE__ */
14 #include "Messenger_i.h"
15 #include "StructuredEventSupplier_i.h"
16 #include <iostream>
18 Messenger_i::Messenger_i (CORBA::ORB_ptr orb)
19 : orb_ (CORBA::ORB::_duplicate(orb))
22 try
24 CORBA::Object_var poa_obj = orb->resolve_initial_references("RootPOA");
25 PortableServer::POA_var poa = PortableServer::POA::_narrow(poa_obj.in());
27 CORBA::Object_var naming_obj =
28 orb_->resolve_initial_references ("NameService");
30 if (CORBA::is_nil(naming_obj.in())) {
31 std::cerr << "Unable to find naming service" << std::endl;
34 CosNaming::NamingContext_var naming_context =
35 CosNaming::NamingContext::_narrow(naming_obj.in());
39 // Create an instance of TAO's notification event channel
42 CosNotifyChannelAdmin::EventChannelFactory_var notify_factory =
43 TAO_Notify_EventChannelFactory_i::create(poa.in());
45 if (CORBA::is_nil (notify_factory.in ())) {
46 std::cerr << "Unable to create the notify event channel" << std::endl;
47 return;
50 CosNotifyChannelAdmin::ChannelID id;
51 CosNotification::QoSProperties initial_qos;
52 CosNotification::AdminProperties initial_admin;
54 CosNotifyChannelAdmin::EventChannel_var ec =
55 notify_factory->create_channel (initial_qos,
56 initial_admin,
57 id);
59 if (CORBA::is_nil (ec.in())) {
60 std::cerr << "Unable to create event channel" << std::endl;
61 return;
65 CosNaming::Name name(1);
66 name.length(1);
67 name[0].id = CORBA::string_dup("MyEventChannel");
69 naming_context->rebind(name, ec.in());
71 CosNotifyChannelAdmin::AdminID adminid;
72 CosNotifyChannelAdmin::InterFilterGroupOperator ifgop =
73 CosNotifyChannelAdmin::AND_OP;
75 CosNotifyChannelAdmin::SupplierAdmin_var supplier_admin =
76 ec->new_for_suppliers (ifgop, adminid);
78 if (CORBA::is_nil (supplier_admin.in())) {
79 std::cerr << "Unable to find supplier admin" << std::endl;
82 CosNotifyChannelAdmin::ProxyID supplieradmin_proxy_id;
84 CosNotifyChannelAdmin::ProxyConsumer_var proxy_consumer =
85 supplier_admin->obtain_notification_push_consumer(
86 CosNotifyChannelAdmin::STRUCTURED_EVENT,
87 supplieradmin_proxy_id);
89 StructuredEventSupplier_i *servant =
90 new StructuredEventSupplier_i(orb_.in());
92 PortableServer::ObjectId_var oid = poa->activate_object(servant);
93 CORBA::Object_var supplier_obj = poa->id_to_reference(oid.in());
94 CosNotifyComm::StructuredPushSupplier_var supplier =
95 CosNotifyComm::StructuredPushSupplier::_narrow(supplier_obj.in());
97 consumer_proxy_ =
98 CosNotifyChannelAdmin::StructuredProxyPushConsumer::
99 _narrow(proxy_consumer.in());
101 if (CORBA::is_nil (consumer_proxy_.in())) {
102 std::cerr << "Unable to find structured proxy push consumer" << std::endl;
105 consumer_proxy_->connect_structured_push_supplier(supplier.in());
108 catch(const CORBA::Exception& ex) {
109 std::cerr << ex << std::endl;
115 // Implementation skeleton destructor
116 Messenger_i::~Messenger_i (void)
120 CORBA::Boolean Messenger_i::send_message (const char * user_name,
121 const char * subject,
122 char *& message)
124 std::cout << "Message from: " << user_name << std::endl;
125 std::cout << "Subject: " << subject << std::endl;
126 std::cout << "Message: " << message << std::endl;
131 // Event Definition
132 CosNotification::StructuredEvent event;
134 event.header.fixed_header.event_type.domain_name =
135 CORBA::string_dup("OCI_TAO");
136 // string
137 event.header.fixed_header.event_type.type_name =
138 CORBA::string_dup("examples");
139 // string
140 event.header.fixed_header.event_name =
141 CORBA::string_dup("myevent");
143 // sequence<Property>: string name, any value
144 event.filterable_data.length (1);
145 event.filterable_data[0].name = CORBA::string_dup("From");
146 event.filterable_data[0].value <<= (const char *)user_name;
147 event.filterable_data.length (2);
148 event.filterable_data[1].name = CORBA::string_dup("Subject");
149 event.filterable_data[1].value <<= (const char *)subject;
150 event.filterable_data.length (3);
151 event.filterable_data[2].name = CORBA::string_dup("Message");
152 event.filterable_data[2].value <<= (const char *)message;
154 consumer_proxy_->push_structured_event(event);
157 catch(const CosNotifyComm::InvalidEventType&) {
158 std::cerr << "Invalid Event Type Exception " << std::endl;
159 return 1;
162 catch(const CORBA::Exception& ex) {
163 std::cerr << ex << std::endl;
164 return 1;
166 return 0;