summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/Notify/Structured_Filter/Structured_Supplier.cpp
blob: fba45d19f79d3b9524d2c396a48a86ac03b26fdf (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// $Id$

// ******************************************************************
// Include Section
// ******************************************************************

#include "orbsvcs/orbsvcs/CosNotifyChannelAdminS.h"
#include "orbsvcs/orbsvcs/CosNotifyCommC.h"
#include "orbsvcs/orbsvcs/CosNamingC.h"
#include "Notify_StructuredPushSupplier.h"
#include "controlS.h"
#include "Notify_Test_Client.h"
#include "tao/debug.h"

// ******************************************************************
// Data Section
// ******************************************************************

static CORBA::Boolean go = 0;
static CORBA::Short n_events = 1;
static const char* ior_file = "supplier.ior";
static TAO_Notify_Tests_StructuredPushSupplier* supplier = 0;

// ******************************************************************
// Subroutine Section
// ******************************************************************

class supplier_ctrl_i : public POA_supplier_ctrl
{
public:
  void ctrl (CORBA::Boolean flag,
             CORBA::Short num_events
             ACE_ENV_ARG_DECL)
    ACE_THROW_SPEC ((CORBA::SystemException));
};


void
supplier_ctrl_i::ctrl (CORBA::Boolean flag,
                       CORBA::Short num_events
                       ACE_ENV_ARG_DECL_NOT_USED /*ACE_ENV_SINGLE_ARG_PARAMETER*/)
  ACE_THROW_SPEC ((CORBA::SystemException))
{
  go = flag;
  n_events = num_events;
}


static CosNotifyChannelAdmin::SupplierAdmin_ptr
create_supplieradmin (CosNotifyChannelAdmin::EventChannel_ptr ec
                      ACE_ENV_ARG_DECL)
{
  CosNotifyChannelAdmin::AdminID adminid = 0;
  CosNotifyChannelAdmin::SupplierAdmin_var admin =
    ec->new_for_suppliers (CosNotifyChannelAdmin::AND_OP,
                           adminid
                           ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (0);

  return CosNotifyChannelAdmin::SupplierAdmin::_duplicate (admin.in ());
}

int
setup_event ()
{
  static int count = 0;

  CosNotification::StructuredEvent event;

   event.header.fixed_header.event_type.domain_name =
     CORBA::string_dup ("TAO Test Suite");
   event.header.fixed_header.event_type.type_name =
     CORBA::string_dup ("Filtered Structured Event Notification Svc test");

   if (count % 3 == 0)
    {
      event.header.fixed_header.event_name = CORBA::string_dup ("String test event");
      event.header.variable_header.length (1);

      event.filterable_data.length (2);
      event.filterable_data[0].name = CORBA::string_dup ("String");
      event.filterable_data[0].value <<= "One Hundred";
      event.filterable_data[1].name = CORBA::string_dup ("seq");
      event.filterable_data[1].value <<= (CORBA::Short) count;
    }
   else if (count % 3 == 1)
    {
      event.header.fixed_header.event_name = CORBA::string_dup ("Short test event");
      event.header.variable_header.length (1);

      event.filterable_data.length (2);
      event.filterable_data[0].name = CORBA::string_dup ("Number");
      event.filterable_data[0].value <<= (CORBA::Short) 100;
      event.filterable_data[1].name = CORBA::string_dup ("seq");
      event.filterable_data[1].value <<= (CORBA::Short) count;
    }
   else
    {
      event.header.fixed_header.event_name = CORBA::string_dup ("Short test event");
      event.header.variable_header.length (1);

      event.filterable_data.length (2);
      event.filterable_data[0].name = CORBA::string_dup ("Number");
      event.filterable_data[0].value <<= (CORBA::Short) 50;
      event.filterable_data[1].name = CORBA::string_dup ("seq");
      event.filterable_data[1].value <<= (CORBA::Short) count;
    }

  count++;

  ACE_TRY_NEW_ENV
    {
      if (TAO_debug_level)
        ACE_DEBUG ((LM_DEBUG, "%d sent \n", count));

      supplier->send_event (event ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;
    }
  ACE_CATCH (CORBA::Exception, e)
    {
      ACE_PRINT_EXCEPTION (e, "Supplier Signal Exception: ");
    }
  ACE_ENDTRY;

  return 0;
}

static void create_supplier (CosNotifyChannelAdmin::SupplierAdmin_ptr admin,
                             PortableServer::POA_ptr poa
                             ACE_ENV_ARG_DECL)
{
  ACE_NEW_THROW_EX (supplier,
                    TAO_Notify_Tests_StructuredPushSupplier (),
                    CORBA::NO_MEMORY ());

  supplier->init (poa ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  supplier->connect (admin ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;
}

// ******************************************************************
// Main Section
// ******************************************************************

int main (int argc, char * argv[])
{
  int status = 0;

  ACE_TRY_NEW_ENV;
    {
      Notify_Test_Client client;
      client.init (argc, argv ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      PortableServer::POA_ptr poa = client.root_poa ();

      CosNotifyChannelAdmin::EventChannel_var ec =
               client.create_event_channel ("NotifyEventChannelFactory",
                                            0
                                            ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // register control with naming service
      supplier_ctrl_i controller;

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

      PortableServer::ObjectId_var oid =
        poa->activate_object (&controller);

      CosNaming::NamingContext_ptr naming_context =
                                      client.naming_context ();

      CORBA::Object_var controller_obj = poa->id_to_reference (oid.in ());
      naming_context->bind (ctrl_name, controller_obj.in ());

      int done = 0;

      CosNotifyChannelAdmin::SupplierAdmin_var admin =
        create_supplieradmin (ec.in () ACE_ENV_ARG_PARAMETER);

      if (!CORBA::is_nil (admin.in ()))
        {
          create_supplier (admin.in (), client.root_poa () ACE_ENV_ARG_PARAMETER);

          // Ready. So Write ior to file
          CORBA::ORB_ptr orb = client.orb ();
          CORBA::String_var ior = orb->object_to_string (controller_obj.in ());
          FILE *fd = ACE_OS::fopen (ior_file, "w");
          if (fd == 0)
            ACE_ERROR_RETURN ((LM_ERROR,
                               "Cannot open output file for "
                               "writing IOR: %s",
                               ior_file),
                               1);
          ACE_OS::fprintf (fd, "%s", ior.in ());
          ACE_OS::fclose (fd);

          if (TAO_debug_level)
            ACE_DEBUG ((LM_DEBUG, "Supplier Ready.\n"));

          while ((!done) && (n_events > 0))
            {
              if (go)
                {
                  done = setup_event ();
                  n_events--;
                }

              if (orb->work_pending ())
                orb->perform_work ();
            }

          orb->destroy ();
        }
    }
  ACE_CATCH (CORBA::Exception, e)
    {
      ACE_PRINT_EXCEPTION (e, "Supplier Exception: ");
      status =  1;
    }
  ACE_ENDTRY;

 return status;
}