summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/Trading_Service/Offer_Exporter.cpp
blob: 78253463e77b7763c99ac424a280d6de7a2be1dc (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// $Id$
#include "Offer_Exporter.h"

template<class T>
class Simple_DP_Evaluation_Handler
  : public TAO_DP_Evaluation_Handler
{
public:
  Simple_DP_Evaluation_Handler (T dp);
  
  virtual CORBA::Any* evalDP (const CORBA::Any& extra_info,
			      CORBA::Environment& _env)
    TAO_THROW_SPEC ((CosTradingDynamic::DPEvalFailure));

private:

  T dp_;
};


template <class T>
Simple_DP_Evaluation_Handler<T>::Simple_DP_Evaluation_Handler (T dp)
  : dp_ (dp)
{  
}

template <class T> CORBA::Any*
Simple_DP_Evaluation_Handler<T>::evalDP (const CORBA::Any& extra_info,
					 CORBA::Environment& _env)
  TAO_THROW_SPEC ((CosTradingDynamic::DPEvalFailure))
{
  CORBA::Any* return_value = 0;

  ACE_NEW_RETURN (return_value, CORBA::Any, return_value);

  (*return_value) <<= this->dp_;
  return return_value;
}

TAO_Offer_Exporter::
TAO_Offer_Exporter (CosTrading::Register_ptr register_if,
		    CORBA::Environment& _env)
  TAO_THROW_SPEC ((CORBA::SystemException))
  : register_ (register_if)
{
  this->create_offers ();
  this->admin_ = register_if->admin_if (_env);
  TAO_CHECK_ENV_RETURN (_env,);
}

TAO_Offer_Exporter::~TAO_Offer_Exporter (void)
{
  for (int i = 0; i < NUM_OFFERS; i++)
    {
      /*
      delete dp_plotters_[i].remove_handler
	(TT_Info::PLOTTER_PROPERTY_NAMES[TT_Info::PLOTTER_USER_QUEUE]);
      delete dp_plotters_[i].remove_handler
	(TT_Info::PLOTTER_PROPERTY_NAMES[TT_Info::PLOTTER_FILE_SIZES_PENDING]);
      delete dp_printers_[i].remove_handler
	(TT_Info::PRINTER_PROPERTY_NAMES[TT_Info::PRINTER_USER_QUEUE]);
      delete dp_printers_[i].remove_handler
	(TT_Info::PRINTER_PROPERTY_NAMES[TT_Info::PRINTER_FILE_SIZES_PENDING]);
      delete dp_fs_[i].remove_handler
	(TT_Info::FILESYSTEM_PROPERTY_NAMES[TT_Info::SPACE_REMAINING]);
	*/
    }
}

void
TAO_Offer_Exporter::export_offers (CORBA::Environment& _env)
{
  ACE_DEBUG ((LM_DEBUG, "Exporting offers.\n"));
  
  TAO_TRY
    {
      for (int i = 0; i < NUM_OFFERS; i++)
	{
	  this->register_->export (this->plotter_[i]._this (TAO_TRY_ENV),
				   TT_Info::INTERFACE_NAMES[1],
				   this->props_plotters_[i],
				   TAO_TRY_ENV);
	  TAO_CHECK_ENV;

	  this->register_->export (this->printer_[i]._this (TAO_TRY_ENV),
				   TT_Info::INTERFACE_NAMES[2],
				   this->props_printers_[i],
				   TAO_TRY_ENV);
	  TAO_CHECK_ENV;
	  
	  this->register_->export (this->fs_[i]._this (TAO_TRY_ENV),
				   TT_Info::INTERFACE_NAMES[3],
				   this->props_fs_[i],
				   TAO_TRY_ENV);
	  TAO_CHECK_ENV;
	}
    }
  TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("TAO_Offer_Exporter::export_offers");
      TAO_RETHROW;
    }
  TAO_ENDTRY;
}

void
TAO_Offer_Exporter::withdraw_offers (CORBA::Environment& _env)
  TAO_THROW_SPEC ((CORBA::SystemException, 
		   CosTrading::IllegalOfferId, 
		   CosTrading::UnknownOfferId, 
		   CosTrading::Register::ProxyOfferId))
{
  ACE_DEBUG ((LM_DEBUG, "Withdrawing all offers.\n"));
  
  TAO_TRY
    {
      CORBA::ULong length;
      CORBA::ULong amount = NUM_OFFERS;

      CosTrading::OfferIdSeq_var offer_id_seq = this->grab_offerids (_env);
      TAO_CHECK_ENV;
      
      if (offer_id_seq.ptr () != 0)
	{
	  length = offer_id_seq->length ();
	  for (int i = 0; i < length; i++)
	    {
	      this->register_->withdraw (offer_id_seq[i], TAO_TRY_ENV);
	      TAO_CHECK_ENV;
	    }
	}
    }
  TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("TAO_Offer_Exporter::export_offers");
      TAO_RETHROW;
    }
  TAO_ENDTRY;
}

void
TAO_Offer_Exporter::describe_offers (CORBA::Environment& _env)
  TAO_THROW_SPEC ((CORBA::SystemException, 
		   CosTrading::IllegalOfferId, 
		   CosTrading::UnknownOfferId, 
		   CosTrading::Register::ProxyOfferId))
{
  ACE_DEBUG ((LM_DEBUG, "Describing all offers.\n"));
  
  TAO_TRY
    {
      CORBA::ULong length;
      CORBA::ULong amount = NUM_OFFERS;

      CosTrading::OfferIdSeq_var offer_id_seq = this->grab_offerids (_env);
      TAO_CHECK_ENV;

      if (offer_id_seq.ptr () != 0)
	{
	  length = offer_id_seq->length ();
	  for (int i = 0; i < length; i++)
	    {
	      CosTrading::Register::OfferInfo_var offer_info =
		this->register_->describe (offer_id_seq[i], TAO_TRY_ENV);
	      TAO_CHECK_ENV;

	      ACE_DEBUG ((LM_DEBUG, "Offer Id: %s\n", (const char *) offer_id_seq[i]));
	      ACE_DEBUG ((LM_DEBUG, "Service Type: %s\n", offer_info->type.in ()));	      
	      TT_Info::dump_properties (offer_info->properties);
	      ACE_DEBUG ((LM_DEBUG, "------------------------------\n"));
	    }
	}
    }
  TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("TAO_Offer_Exporter::export_offers");
      TAO_RETHROW;
    }
  TAO_ENDTRY;
}

void
TAO_Offer_Exporter::modify_offers (CORBA::Environment& _env)
  TAO_THROW_SPEC ((CORBA::SystemException, 
		   CosTrading::NotImplemented, 
		   CosTrading::IllegalOfferId, 
		   CosTrading::UnknownOfferId, 
		   CosTrading::Register::ProxyOfferId, 
		   CosTrading::IllegalPropertyName, 
		   CosTrading::Register::UnknownPropertyName, 
		   CosTrading::PropertyTypeMismatch, 
		   CosTrading::ReadonlyDynamicProperty, 
		   CosTrading::Register::MandatoryProperty, 
		   CosTrading::Register::ReadonlyProperty, 
		   CosTrading::DuplicatePropertyName))

{
}

void
TAO_Offer_Exporter::
withdraw_offers_using_constraints (CORBA::Environment& _env)
  TAO_THROW_SPEC ((CORBA::SystemException, 
		   CosTrading::IllegalServiceType, 
		   CosTrading::UnknownServiceType, 
		   CosTrading::IllegalConstraint, 
		   CosTrading::Register::NoMatchingOffers))
{
}

CosTrading::OfferIdSeq*
TAO_Offer_Exporter::grab_offerids (CORBA::Environment& _env)
  TAO_THROW_SPEC ((CORBA::SystemException,
		   CosTrading::NotImplemented))
{
  ACE_DEBUG ((LM_DEBUG, "Grabbing all offer ids.\n"));

  CosTrading::OfferIdSeq_ptr offer_id_seq;
  TAO_TRY
    {
      CORBA::ULong length = NUM_OFFERS;
      CosTrading::OfferIdIterator_ptr offer_id_iter;

      this->admin_->list_offers (NUM_OFFERS,
				 CosTrading::OfferIdSeq_out (offer_id_seq),
				 CosTrading::OfferIdIterator_out (offer_id_iter),
				 TAO_TRY_ENV);
      TAO_CHECK_ENV;

      if ((! CORBA::is_nil (offer_id_iter)) && offer_id_seq != 0)
	{
	  CORBA::Boolean any_left = CORBA::B_FALSE;
	  CosTrading::OfferIdSeq_ptr id_seq;
	  CosTrading::OfferIdIterator_var offer_id_iter_var (offer_id_iter);
	  
	  do
	    {
	      any_left =
		offer_id_iter->next_n (length,
				       CosTrading::OfferIdSeq_out (id_seq),
				       TAO_TRY_ENV);
	      TAO_CHECK_ENV;

	      int offers = id_seq->length ();
	      int old_length = offer_id_seq->length ();
	      offer_id_seq->length (old_length + offers);

	      for (int i = 0; i < offers; i++)
		(*offer_id_seq)[i + old_length] = (*id_seq)[i];

	      delete id_seq;
	    } while (any_left);	      
	}

      ACE_DEBUG ((LM_DEBUG, "The following offer ids are registered:\n"));
      for (int len = offer_id_seq->length (), j = 0; j < len; j++)
	ACE_DEBUG ((LM_DEBUG, "Offer Id: %s\n", (const char *)(*offer_id_seq)[j]));
      
      return offer_id_seq;
    }
  TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("TAO_Offer_Exporter::grab_offerids");
      TAO_RETHROW_RETURN (offer_id_seq);
    }
  TAO_ENDTRY;
}

void
TAO_Offer_Exporter::create_offers (void)
{
  int counter = 0;
  char name[BUFSIZ];
  char description[BUFSIZ];
  CORBA::Any extra_info;
  TAO_Sequences::StringSeq string_seq (4);
  TAO_Sequences::ULongSeq ulong_seq (4);
  CosTradingDynamic::DynamicProp_var dp_user_queue;
  CosTradingDynamic::DynamicProp_var dp_file_queue;

  // Initialize plotters
  for (int i = 0; i < NUM_OFFERS; i++)
    {      
      ACE_OS::sprintf (name, "Plotter #%d", i);
      ACE_OS::sprintf (description,
		       "%s is a plotter. It plots stuff. Like charts.",
		       name);

      for (int j = 0; j < 4; j++, counter = (counter + 1) % NUM_OFFERS)
	{
	  string_seq[j] = TT_Info::USERS [counter];
	  ulong_seq[j] = counter * 10000;
	}
      /*
      dp_user_queue =
	this->dp_plotters_[i].register_handler
	(TT_Info::PLOTTER_PROPERTY_NAMES[TT_Info::PLOTTER_USER_QUEUE],
	TT_Info::PLOTTER_PROPERTY_TYPES[TT_Info::PLOTTER_USER_QUEUE],
	 extra_info,
	 new Simple_DP_Evaluation_Handler<TAO_Sequences::StringSeq> (string_seq));

      dp_file_queue =
	this->dp_plotters_[i].register_handler
	(TT_Info::PLOTTER_PROPERTY_NAMES[TT_Info::PLOTTER_FILE_SIZES_PENDING],
	 TT_Info::PLOTTER_PROPERTY_TYPES[TT_Info::PLOTTER_FILE_SIZES_PENDING],
	 extra_info,
	 new Simple_DP_Evaluation_Handler<TAO_Sequences::ULongSeq> (ulong_seq));
      */      
      this->props_plotters_[i].length (7);
      this->props_plotters_[i][0].name = CORBA::string_dup (TT_Info::REMOTE_IO_PROPERTY_NAMES[TT_Info::NAME]);
      this->props_plotters_[i][0].value <<= CORBA::string_dup (name);
      this->props_plotters_[i][1].name = CORBA::string_dup (TT_Info::REMOTE_IO_PROPERTY_NAMES[TT_Info::LOCATION]);
      this->props_plotters_[i][1].value <<= TT_Info::LOCATIONS[i];
      this->props_plotters_[i][2].name = CORBA::string_dup (TT_Info::REMOTE_IO_PROPERTY_NAMES[TT_Info::DESCRIPTION]);
      this->props_plotters_[i][2].value <<= CORBA::string_dup (description);
      this->props_plotters_[i][3].name = CORBA::string_dup (TT_Info::PLOTTER_PROPERTY_NAMES[TT_Info::PLOTTER_NUM_COLORS]);
      this->props_plotters_[i][3].value <<= (i * 2);
      this->props_plotters_[i][4].name = CORBA::string_dup (TT_Info::PLOTTER_PROPERTY_NAMES[TT_Info::PLOTTER_AUTO_LOADING]);
      this->props_plotters_[i][4].value <<= CORBA::Any::from_boolean ((CORBA::Boolean) (i % 2));
      this->props_plotters_[i][5].name = CORBA::string_dup (TT_Info::PLOTTER_PROPERTY_NAMES[TT_Info::PLOTTER_COST_PER_PAGE]);
      this->props_plotters_[i][5].value <<= (CORBA::Float) i;
      this->props_plotters_[i][6].name = CORBA::string_dup (TT_Info::PLOTTER_PROPERTY_NAMES[TT_Info::PLOTTER_MODEL_NUMBER]);
      this->props_plotters_[i][6].value <<= CORBA::string_dup (TT_Info::MODEL_NUMBERS[i]);
      //      this->props_plotters_[i][7].name = TT_Info::PLOTTER_PROPERTY_NAMES[TT_Info::PLOTTER_USER_QUEUE];
      //      this->props_plotters_[i][7].value <<= dp_user_queue.in ();
      //      this->props_plotters_[i][8].name = TT_Info::PLOTTER_PROPERTY_NAMES[TT_Info::PLOTTER_FILE_SIZES_PENDING];
      //      this->props_plotters_[i][8].value <<= dp_file_queue.in ();
    }

  // Initialize printers
  for (i = 0; i < NUM_OFFERS; i++)
    {
      ACE_OS::sprintf (name, "Printer #%d", i);
      ACE_OS::sprintf (description,
		       "%s is a printer. It prints stuff. Like reports.",
		       name);

      for (int j = 0; j < 4; j++, counter = (counter + 1) % NUM_OFFERS)
	{
	  string_seq[j] = TT_Info::USERS [counter];
	  ulong_seq[j] = counter * 10000;
	}
      /*
      dp_user_queue =
	this->dp_printers_[i].register_handler
	(TT_Info::PRINTER_PROPERTY_NAMES[TT_Info::PRINTER_USER_QUEUE],
	 TT_Info::PRINTER_PROPERTY_TYPES[TT_Info::PRINTER_USER_QUEUE],
	 extra_info,
	 new Simple_DP_Evaluation_Handler<TAO_Sequences::StringSeq> (string_seq));

      dp_file_queue =
	this->dp_printers_[i].register_handler
	(TT_Info::PRINTER_PROPERTY_NAMES[TT_Info::PRINTER_FILE_SIZES_PENDING],
	 TT_Info::PRINTER_PROPERTY_TYPES[TT_Info::PRINTER_FILE_SIZES_PENDING],
	 extra_info,
	 new Simple_DP_Evaluation_Handler<TAO_Sequences::ULongSeq> (ulong_seq));
	 */      
      this->props_printers_[i].length (8);
      this->props_printers_[i][0].name = CORBA::string_dup (TT_Info::REMOTE_IO_PROPERTY_NAMES[TT_Info::NAME]);
      this->props_printers_[i][0].value <<= CORBA::string_dup (name);
      this->props_printers_[i][1].name = CORBA::string_dup (TT_Info::REMOTE_IO_PROPERTY_NAMES[TT_Info::LOCATION]);
      this->props_printers_[i][1].value <<= TT_Info::LOCATIONS[i];
      this->props_printers_[i][2].name = CORBA::string_dup (TT_Info::REMOTE_IO_PROPERTY_NAMES[TT_Info::DESCRIPTION]);
      this->props_printers_[i][2].value <<= CORBA::string_dup (description);
      this->props_printers_[i][3].name = CORBA::string_dup (TT_Info::PRINTER_PROPERTY_NAMES[TT_Info::PRINTER_COLOR]);
      this->props_printers_[i][3].value <<= CORBA::Any::from_boolean ((CORBA::Boolean) (i % 2));
      this->props_printers_[i][4].name = CORBA::string_dup (TT_Info::PRINTER_PROPERTY_NAMES[TT_Info::PRINTER_DOUBLE_SIDED]);
      this->props_printers_[i][4].value <<= CORBA::Any::from_boolean ((CORBA::Boolean) ((i + 1) % 2));
      this->props_printers_[i][5].name = CORBA::string_dup (TT_Info::PRINTER_PROPERTY_NAMES[TT_Info::PRINTER_COST_PER_PAGE]);
      this->props_printers_[i][5].value <<= (CORBA::Float) i;
      this->props_printers_[i][6].name = CORBA::string_dup (TT_Info::PRINTER_PROPERTY_NAMES[TT_Info::PRINTER_MODEL_NUMBER]);
      this->props_printers_[i][6].value <<= CORBA::string_dup (TT_Info::MODEL_NUMBERS[i]);
      this->props_printers_[i][7].name = CORBA::string_dup (TT_Info::PRINTER_PROPERTY_NAMES[TT_Info::PRINTER_PAGES_PER_SEC]);
      this->props_printers_[i][7].value <<= (CORBA::UShort) i;
      //      this->props_printers_[i][8].name = CORBA::string_dup (TT_Info::PRINTER_PROPERTY_NAMES[TT_Info::PRINTER_USER_QUEUE];
      //      this->props_printers_[i][8].value <<= dp_user_queue.in ();
      //      this->props_printers_[i][9].name = CORBA::string_dup (TT_Info::PRINTER_PROPERTY_NAMES[TT_Info::PRINTER_FILE_SIZES_PENDING];
      //      this->props_printers_[i][9].value <<= dp_file_queue.in ();
    }

  // Initialize FileSystem
  for (i = 0; i < NUM_OFFERS; i++)
    {
      CosTradingDynamic::DynamicProp_var dp_space_left;
      
      ACE_OS::sprintf (name, "File System #%d", i);
      ACE_OS::sprintf (description,
		       "%s is a File System. It stores stuff. Like files.",
		       name);
      /*
      dp_space_left =
	this->dp_fs_[i].register_handler
	(TT_Info::FILESYSTEM_PROPERTY_NAMES[TT_Info::SPACE_REMAINING],
	 TT_Info::FILESYSTEM_PROPERTY_TYPES[TT_Info::SPACE_REMAINING],
	 extra_info,
	 new Simple_DP_Evaluation_Handler<CORBA::ULong> (i * 4434343));
	 */      
      this->props_fs_[i].length (5);
      this->props_fs_[i][0].name = CORBA::string_dup (TT_Info::REMOTE_IO_PROPERTY_NAMES[TT_Info::NAME]);
      this->props_fs_[i][0].value <<= CORBA::string_dup (name);
      this->props_fs_[i][1].name = CORBA::string_dup (TT_Info::REMOTE_IO_PROPERTY_NAMES[TT_Info::LOCATION]);
      this->props_fs_[i][1].value <<= CORBA::string_dup (TT_Info::LOCATIONS[i]);
      this->props_fs_[i][2].name = CORBA::string_dup (TT_Info::REMOTE_IO_PROPERTY_NAMES[TT_Info::DESCRIPTION]);
      this->props_fs_[i][2].value <<= CORBA::string_dup (description);
      this->props_fs_[i][3].name = CORBA::string_dup (TT_Info::FILESYSTEM_PROPERTY_NAMES[TT_Info::DISK_SIZE]);
      this->props_fs_[i][3].value <<= (CORBA::ULong) (i * 2000000);
      this->props_fs_[i][4].name = CORBA::string_dup (TT_Info::FILESYSTEM_PROPERTY_NAMES[TT_Info::PERMISSION_LEVEL]);
      this->props_fs_[i][4].value <<= (CORBA::UShort) (i + 1);
      //      this->props_fs_[i][6].name = TT_Info::FILESYSTEM_PROPERTY_NAMES[TT_Info::SPACE_REMAINING];
      //      this->props_fs_[i][6].value <<= dp_space_left.in ();
    }
}