Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / APG / Streams / CommandTask.cpp
blobb7b715925ce1cf9605149221273d0b36c5888f30
1 #include "CommandTask.h"
3 // Listing 01 code/ch18
4 CommandTask::CommandTask (int command)
5 : inherited (), command_(command)
6 { }
7 // Listing 01
9 // Listing 02 code/ch18
10 int CommandTask::open (void *)
12 return this->activate ();
14 // Listing 02
16 // Listing 03 code/ch18
17 int CommandTask::put (ACE_Message_Block *message,
18 ACE_Time_Value *timeout)
20 return this->putq (message, timeout);
22 // Listing 03
24 // Listing 04 code/ch18
25 int CommandTask::process (Command *)
27 ACE_TRACE ("CommandTask::process()");
28 return Command::RESULT_FAILURE;
30 // Listing 04
32 // Listing 05 code/ch18
33 int CommandTask::close (u_long flags)
35 int rval = 0;
36 if (flags == 1)
38 ACE_Message_Block *hangup = new ACE_Message_Block;
39 hangup->msg_type (ACE_Message_Block::MB_HANGUP);
40 if (this->putq (hangup->duplicate ()) == -1)
42 ACE_ERROR_RETURN ((LM_ERROR,
43 ACE_TEXT ("%p\n"),
44 ACE_TEXT ("Task::close() putq")),
45 -1);
48 hangup->release ();
49 rval = this->wait ();
52 return rval;
54 // Listing 05
56 // Listing 06 code/ch18
57 // Listing 061 code/ch18
58 int CommandTask::svc ()
60 ACE_Message_Block *message;
62 for (;;)
64 ACE_DEBUG ((LM_DEBUG,
65 ACE_TEXT ("CommandTask::svc() - ")
66 ACE_TEXT ("%s waiting for work\n"),
67 this->module ()->name ()));
69 if (this->getq (message) == -1)
70 ACE_ERROR_RETURN ((LM_ERROR,
71 ACE_TEXT ("%p\n"),
72 ACE_TEXT ("getq")),
73 -1);
75 if (message->msg_type () == ACE_Message_Block::MB_HANGUP)
77 if (this->putq (message->duplicate ()) == -1)
79 ACE_ERROR_RETURN ((LM_ERROR,
80 ACE_TEXT ("%p\n"),
81 ACE_TEXT ("Task::svc() putq")),
82 -1);
85 message->release ();
86 break;
88 // Listing 061
90 // Listing 062 code/ch18
91 Command *command = (Command *)message->data_block ();
93 ACE_DEBUG ((LM_DEBUG,
94 ACE_TEXT ("CommandTask::svc() - ")
95 ACE_TEXT ("%s got work request %d\n"),
96 this->module ()->name (),
97 command->command_));
99 if (command->command_ != this->command_)
101 this->put_next (message->duplicate ());
103 // Listing 062
104 // Listing 063 code/ch18
105 else
107 int result = this->process (command);
108 ACE_DEBUG ((LM_DEBUG,
109 ACE_TEXT ("CommandTask::svc() - ")
110 ACE_TEXT ("%s work request %d result is %d\n"),
111 this->module ()->name (),
112 command->command_,
113 result));
115 if (result == Command::RESULT_FAILURE)
117 command->numeric_result_ = -1;
119 // Listing 063
120 // Listing 064 code/ch18
121 else if (result == Command::RESULT_PASS)
123 this->put_next (message->duplicate ());
125 // Listing 064
126 // Listing 065 code/ch18
127 else // result == Command::RESULT_SUCCESS
129 if (this->is_writer ())
131 this->sibling ()->putq
132 (message->duplicate ());
134 // Listing 065
135 // Listing 066 code/ch18
136 else // this->is_reader ()
138 this->put_next (message->duplicate ());
140 // Listing 066
141 } // result == ...
142 } // command->command_ ? = this->command_
144 // Listing 067 code/ch18
145 message->release ();
146 } // for (;;)
148 return 0;
150 // Listing 067
151 // Listing 06