Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / APG / Streams / BasicTask.h
blobb172099b0d1858828016af7efee667a38af67ec9
1 /* -*- C++ -*- */
2 #ifndef BASIC_TASK_H
3 #define BASIC_TASK_H
5 #include "ace/Task_T.h"
6 #include "ace/ace_wchar.h"
8 // Listing 100 code/ch18
9 class BasicTask : public ACE_Task<ACE_MT_SYNCH>
11 public:
12 typedef ACE_Task<ACE_MT_SYNCH> inherited;
14 BasicTask () : inherited()
15 { }
17 virtual int open (void * = 0)
19 ACE_DEBUG ((LM_DEBUG,
20 ACE_TEXT ("BasicTask::open() starting ")
21 ACE_TEXT ("%d threads\n"),
22 this->desired_threads ()));
24 return this->activate (THR_NEW_LWP | THR_JOINABLE,
25 this->desired_threads ());
27 // Listing 100
29 // Listing 101 code/ch18
30 int put (ACE_Message_Block *message,
31 ACE_Time_Value *timeout)
33 return this->putq (message, timeout);
35 // Listing 101
37 // Listing 1020 code/ch18
38 virtual int svc ()
40 for (ACE_Message_Block *message = 0; ; )
42 ACE_DEBUG ((LM_DEBUG,
43 ACE_TEXT ("BasicTask::svc() - ")
44 ACE_TEXT ("waiting for work\n" )));
46 if (this->getq (message) == -1)
47 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"),
48 ACE_TEXT ("getq")),
49 -1);
50 // Listing 1020
52 // Listing 1021 code/ch18
53 if (message->msg_type () == ACE_Message_Block::MB_HANGUP)
55 if (this->putq (message) == -1)
57 ACE_ERROR ((LM_ERROR,
58 ACE_TEXT ("%p\n"),
59 ACE_TEXT ("Task::svc() putq")));
60 message->release ();
62 break;
64 // Listing 1021
66 // Listing 1022 code/ch18
67 Message *recordedMessage =
68 (Message *)message->rd_ptr ();
70 if (this->process (recordedMessage) == -1)
72 message->release ();
73 ACE_ERROR_RETURN ((LM_ERROR,
74 ACE_TEXT ("%p\n"),
75 ACE_TEXT ("process")),
76 -1);
78 // Listing 1022
80 // Listing 1023 code/ch18
81 ACE_DEBUG ((LM_DEBUG,
82 ACE_TEXT ("BasicTask::svc() - ")
83 ACE_TEXT ("Continue to next stage\n" )));
84 if (this->next_step (message) < 0)
86 ACE_ERROR ((LM_ERROR,
87 ACE_TEXT ("%p\n"),
88 ACE_TEXT ("put_next failed")));
89 message->release ();
90 break;
92 // Listing 1023
95 return 0;
98 // Listing 103 code/ch18
99 virtual int close (u_long flags)
101 int rval = 0;
103 if (flags == 1)
105 ACE_Message_Block *hangup = new ACE_Message_Block ();
106 hangup->msg_type (ACE_Message_Block::MB_HANGUP);
107 if (this->putq (hangup) == -1)
109 hangup->release ();
110 ACE_ERROR_RETURN ((LM_ERROR,
111 ACE_TEXT ("%p\n"),
112 ACE_TEXT ("Task::close() putq")),
113 -1);
116 rval = this->wait ();
119 return rval;
121 // Listing 103
123 // Listing 105 code/ch18
124 protected:
125 virtual int next_step (ACE_Message_Block *message_block)
127 return this->put_next (message_block);
129 // Listing 105
131 // Listing 104 code/ch18
132 virtual int process (Message *message) = 0;
134 virtual int desired_threads ()
136 return 1;
139 // Listing 104
141 #endif /* BASIC_TASK_H */