summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/DevGuideExamples/NotifyService/SupplierSideNC/Messenger_i.cpp
blob: 2def116b05a85c18e50bb74138b837d41d4ca1d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "orbsvcs/CosNotifyChannelAdminC.h"
#include "orbsvcs/CosNotifyCommC.h"
#include "orbsvcs/CosNamingC.h"
#include "orbsvcs/Notify/Notify_EventChannelFactory_i.h"

// The static initialization trick doesn't work with static builds.
// On SunOS 5.8 and MacOS X, the static initialization trick used
// in the CosNotification_Serv library does not work.  Including the
// initializer class here works around the problem.
#if defined (TAO_AS_STATIC_LIBS) || defined (sun) || defined (__APPLE__)
#include "orbsvcs/Notify/CosNotify_Initializer.h"
#endif /* sun || __APPLE__ */

#include "Messenger_i.h"
#include "StructuredEventSupplier_i.h"
#include <iostream>

Messenger_i::Messenger_i (CORBA::ORB_ptr orb)
: orb_ (CORBA::ORB::_duplicate(orb))

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

    CORBA::Object_var naming_obj =
      orb_->resolve_initial_references ("NameService");

    if (CORBA::is_nil(naming_obj.in())) {
      std::cerr << "Unable to find naming service" << std::endl;
    }

    CosNaming::NamingContext_var naming_context =
      CosNaming::NamingContext::_narrow(naming_obj.in());


    //
    //  Create an instance of TAO's notification event channel
    //

    CosNotifyChannelAdmin::EventChannelFactory_var notify_factory =
      TAO_Notify_EventChannelFactory_i::create(poa.in());

    if (CORBA::is_nil (notify_factory.in ())) {
      std::cerr << "Unable to create the notify event channel" << std::endl;
      return;
    }

    CosNotifyChannelAdmin::ChannelID id;
    CosNotification::QoSProperties initial_qos;
    CosNotification::AdminProperties initial_admin;

    CosNotifyChannelAdmin::EventChannel_var ec =
      notify_factory->create_channel (initial_qos,
      initial_admin,
      id);

    if (CORBA::is_nil (ec.in())) {
      std::cerr << "Unable to create event channel" << std::endl;
      return;
    }


    CosNaming::Name name(1);
    name.length(1);
    name[0].id = CORBA::string_dup("MyEventChannel");

    naming_context->rebind(name, ec.in());

    CosNotifyChannelAdmin::AdminID adminid;
    CosNotifyChannelAdmin::InterFilterGroupOperator ifgop =
      CosNotifyChannelAdmin::AND_OP;

    CosNotifyChannelAdmin::SupplierAdmin_var supplier_admin =
      ec->new_for_suppliers (ifgop, adminid);

    if (CORBA::is_nil (supplier_admin.in())) {
      std::cerr << "Unable to find supplier admin" << std::endl;
    }

    CosNotifyChannelAdmin::ProxyID supplieradmin_proxy_id;

    CosNotifyChannelAdmin::ProxyConsumer_var proxy_consumer =
      supplier_admin->obtain_notification_push_consumer(
      CosNotifyChannelAdmin::STRUCTURED_EVENT,
      supplieradmin_proxy_id);

    StructuredEventSupplier_i *servant =
      new StructuredEventSupplier_i(orb_.in());

    PortableServer::ObjectId_var oid = poa->activate_object(servant);
    CORBA::Object_var supplier_obj = poa->id_to_reference(oid.in());
    CosNotifyComm::StructuredPushSupplier_var supplier =
      CosNotifyComm::StructuredPushSupplier::_narrow(supplier_obj.in());

    consumer_proxy_ =
      CosNotifyChannelAdmin::StructuredProxyPushConsumer::
      _narrow(proxy_consumer.in());

    if (CORBA::is_nil (consumer_proxy_.in())) {
      std::cerr << "Unable to find structured proxy push consumer" << std::endl;
    }

    consumer_proxy_->connect_structured_push_supplier(supplier.in());

  }
  catch(const CORBA::Exception& ex) {
    std::cerr << ex << std::endl;
  }

}


// Implementation skeleton destructor
Messenger_i::~Messenger_i (void)
{
}

CORBA::Boolean Messenger_i::send_message (
                                          const char * user_name,
                                          const char * subject,
                                          char *& message
                                          )
{

  std::cout << "Message from: " << user_name << std::endl;
  std::cout << "Subject:      " << subject << std::endl;
  std::cout << "Message:      " << message << std::endl;

  try
  {

    // Event Definition
    CosNotification::StructuredEvent event;

    event.header.fixed_header.event_type.domain_name =
      CORBA::string_dup("OCI_TAO");
    // string
    event.header.fixed_header.event_type.type_name =
      CORBA::string_dup("examples");
    // string
    event.header.fixed_header.event_name =
      CORBA::string_dup("myevent");

    // sequence<Property>: string name, any value
    event.filterable_data.length (1);
    event.filterable_data[0].name = CORBA::string_dup("From");
    event.filterable_data[0].value <<= (const char *)user_name;
    event.filterable_data.length (2);
    event.filterable_data[1].name = CORBA::string_dup("Subject");
    event.filterable_data[1].value <<= (const char *)subject;
    event.filterable_data.length (3);
    event.filterable_data[2].name = CORBA::string_dup("Message");
    event.filterable_data[2].value <<= (const char *)message;

    consumer_proxy_->push_structured_event(event);
  }

  catch(const CosNotifyComm::InvalidEventType&) {
    std::cerr << "Invalid Event Type Exception " << std::endl;
    return 1;
  }

  catch(const CORBA::Exception& ex) {
    std::cerr << ex << std::endl;
    return 1;
  }
  return 0;
}