summaryrefslogtreecommitdiff
path: root/TAO/examples/Event_Comm/consumer.cpp
blob: 52af5c5c73167eb4cbbdbceb4cb7d8232f27b8be (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
// $Id$

#include "Consumer_Handler.h"
#include "Consumer_Input_Handler.h"
#include "ace/Argv_Type_Converter.h"

ACE_RCSID(Consumer, consumer, "$Id$")

class Consumer : public ACE_Event_Handler, public ShutdownCallback
{
  // = TITLE
  //    Consumer driver for the Publish/Subscribe example.
  //
  // = DESCRIPTION
  //    The Consumer holds the <Consumer_Input_Handler> and
  //    <Cosumer_Handler> objects.
public:
  // = Initialization and termination methods.
  Consumer (void);
  // Constructor.

  ~Consumer (void);
  // Destructor.

  int initialize (int argc, char *argv[]);
  // Initialization method.

  int run (void);
  // Execute the consumer;

  virtual void close (void);
  // Shutdown the consumer.

private:
  virtual int handle_signal (int signum, siginfo_t *, ucontext_t *);
  // Signal handler method.

  Consumer_Input_Handler ih_;
  // Handler for keyboard input.

  Consumer_Handler ch_;
  // Handler for CORBA Consumer.
};

Consumer::Consumer (void)
{
  // No-Op.
}

Consumer::~Consumer (void)
{
  // No-Op.
}

int
Consumer::handle_signal (int signum,
                         siginfo_t *,
                         ucontext_t *)
{
  ACE_DEBUG ((LM_DEBUG,
              "%S\n",
              signum));

  // Indicate that the consumer initiated the shutdown.
  this->ih_.consumer_initiated_shutdown (1);

  this->close ();

  return 0;
}

void
Consumer::close (void)
{
  // clean up the input handler.
  ih_.close ();
 // Shut down the ORB
  ch_.close ();
}

int
Consumer::run (void)
{
  // Run the <Consumer_Handler>'s ORB.
  return ch_.run ();
}

int
Consumer::initialize (int argc, char *argv[])
{
  // Initialize the <Consumer_Handler>.
  if (this->ch_.init (argc, argv, this) == -1)
     ACE_ERROR_RETURN ((LM_ERROR,
			"%p\n",
			"Consumer_Handler failed to initialize\n"),
                       -1);
   // Initialize the <Consumer_Input_Handler>.
  else if (this->ih_.initialize (&this->ch_) == -1)
     ACE_ERROR_RETURN ((LM_ERROR,
			"%p\n",
			"Consumer_Input_Handler failed to initialize\n"),
                       -1);
  else if (this->ch_.reactor ()->register_handler (SIGINT,
                                              this) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
		       "%p\n",
		       "register_handler"),
                      -1);
  else
    return 0;
}

int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
  ACE_Argv_Type_Converter convert (argc, argv);
  // Initialize the supplier and consumer object references.
  Consumer consumer;

  if (consumer.initialize (convert.get_argc(), convert.get_ASCII_argv()) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
		       "%p\n",
		       "Consumer init failed\n"),
		      1);

  // Loop forever handling events.
  if (consumer.run () == -1)
  ACE_ERROR_RETURN ((LM_ERROR,
		       "%p\n",
		       "Consumer run failed\n"),
		      1);

  return 0;
}