1 //
2 // The contents of this file are subject to the Mozilla Public
3 // License Version 1.1 (the "License"); you may not use this file
4 // except in compliance with the License. You may obtain a copy of
5 // the License at http://www.mozilla.org/MPL/
6 //
7 // Software distributed under the License is distributed on an "AS
8 // IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 // implied. See the License for the specific language governing
10 // rights and limitations under the License.
11 //
12 // The Original Code is State Machine Compiler (SMC).
13 //
14 // The Initial Developer of the Original Code is Charles W. Rapp.
15 // Portions created by Charles W. Rapp are
16 // Copyright (C) 2000 - 2003 Charles W. Rapp.
17 // All Rights Reserved.
18 //
19 // Contributor(s):
20 //
21 // Class
22 // AppClass
23 //
24 // Member Functions
25 // AppClass() - Default constructor.
26 // Run() - Start the system running.
27 // ReceiveRequest(const char *) - Process a request.
28 // CheckForRequest() - See if a request has arrived.
29 // DoRequest() - Do the actual request processing here.
30 // ProcessingCompleted() - All done.
31 //
32 // RCS ID
33 // $Id: AppClass.cpp,v 1.5 2005/06/08 11:09:12 cwrapp Exp $
34 //
35 // CHANGE LOG
36 // $Log: AppClass.cpp,v $
37 // Revision 1.5 2005/06/08 11:09:12 cwrapp
38 // + Updated Python code generator to place "pass" in methods with empty
39 // bodies.
40 // + Corrected FSM errors in Python example 7.
41 // + Removed unnecessary includes from C++ examples.
42 // + Corrected errors in top-level makefile's distribution build.
43 //
44 // Revision 1.4 2005/05/28 13:31:18 cwrapp
45 // Updated C++ examples.
46 //
47 // Revision 1.0 2003/12/14 19:29:32 charlesr
48 // Initial revision
49 //
50
51 #ifdef WIN32
52 #pragma warning(disable: 4355)
53 #endif
54
55 #if !defined(WIN32)
56 #include <unistd.h>
57 #include <sys/time.h>
58 #endif
59 #include <memory.h>
60 #include "AppClass.h"
61
62 using namespace std;
63
64 const static char _rcs_id[] = "$Id: AppClass.cpp,v 1.5 2005/06/08 11:09:12 cwrapp Exp $";
65
AppClass()66 AppClass::AppClass()
67 : _fsm(*this),
68 _number_of_requests(0),
69 _continue_running(1)
70 {
71 // Uncomment following line to see debug output.
72 // _fsm.setDebugFlag(true);
73 }
74
Run()75 void AppClass::Run()
76 {
77 #ifdef WIN32
78 DWORD SleepTime;
79 #endif
80
81 while (_continue_running == 1)
82 {
83 #ifdef WIN32
84 // Sleep for half a second at a time.
85 // This will allow for timely receipt of
86 // SIGINTs.
87 for (SleepTime = 5000; SleepTime > 0; SleepTime -= 500)
88 {
89 Sleep(500);
90 }
91
92 ProcessingCompleted();
93 #else
94 pause();
95 #endif
96 }
97
98 return;
99 } // end of AppClass::Run()
100
ReceiveRequest(const char * message)101 void AppClass::ReceiveRequest(const char *message)
102 {
103 if (strcmp(message, "stop") == 0)
104 {
105 // Stop processing messages.
106 _continue_running = 0;
107 }
108 else
109 {
110 // Increment the request count.
111 ++_number_of_requests;
112
113 // Process this message.
114 _fsm.RequestReceived();
115 }
116
117 return;
118 } // end of AppClass::ReceiveRequest(const char*)
119
CheckForRequest()120 void AppClass::CheckForRequest()
121 {
122 if (_number_of_requests > 0)
123 {
124 _fsm.ProcessRequest();
125 }
126 else if (_number_of_requests < 0)
127 {
128 cout << "The number of outstanding requests is less than zero (";
129 cout << _number_of_requests << "); resetting to zero." << endl;
130 _number_of_requests = 0;
131 }
132
133 return;
134 } // end of AppClass::CheckForRequest()
135
DoRequest()136 void AppClass::DoRequest()
137 {
138 // Decrement the request count.
139 --_number_of_requests;
140
141 #ifdef WIN32
142 cout << "Processing request ..." << endl;
143 #else
144 // Local variable decalarations.
145 itimerval nextTimeout;
146
147 // Sleep on this request.
148 (void) memset((char *) &nextTimeout, 0, sizeof(nextTimeout));
149 nextTimeout.it_value.tv_sec = 5;
150 if (setitimer(ITIMER_REAL, &nextTimeout, (itimerval *) NULL) < 0)
151 {
152 // Failed to start process timer - quit.
153 _continue_running = 0;
154 }
155 else
156 {
157 cout << "Processing request ..." << endl;
158 }
159 #endif
160
161 return;
162 } // end of AppClass::DoRequest()
163
ProcessingCompleted()164 void AppClass::ProcessingCompleted()
165 {
166 cout << "... Processing completed." << endl;
167
168 _fsm.ProcessingDone();
169 } // end of AppClass::ProcessingCompleted()
170