windows-torture-test-20060625
[openafs.git] / src / WINNT / tests / torture / StopStressTest / StopStressTest.c
1 // Stress.cpp : Defines the entry point for the console application.
2 //
3
4 #include <windows.h>
5 #include <stdio.h>
6 #include <time.h>
7 #include <stdio.h>
8 #include <io.h>
9
10 #define SUSPEND_PROCESSING  'p'
11 #define CONTINUE_PROCESSING 'c'
12 #define END_PROCESSING      'e'
13
14 void usage(void);
15 int  GetConsoleInput(HANDLE hStdin);
16 void ProcessRequest(char RequestType, HANDLE ShutDownEventHandle, HANDLE PauseEventHandle, HANDLE ContinueEventHandle);
17
18 int main(int argc, char* argv[])
19 {
20     char    Character;
21     HANDLE  ShutDownEventHandle;
22     HANDLE  PauseEventHandle;
23     HANDLE  ContinueEventHandle;
24     HANDLE  hStdin; 
25
26     hStdin = GetStdHandle(STD_INPUT_HANDLE);
27
28
29     ShutDownEventHandle = CreateEvent(NULL, TRUE, FALSE, "AfsShutdownEvent");
30     PauseEventHandle = CreateEvent(NULL, TRUE, FALSE, "AfsPauseEvent");
31     ContinueEventHandle = CreateEvent(NULL, TRUE, FALSE, "AfsContinueEvent");
32
33     while (1)
34     {
35         printf("\nType p to pause the stress test\n");
36         printf("Type c to continue the stress test\n");
37         printf("Type e to end the stress test\n");
38         printf("Type q to quit StopStressTest\n");
39         Character = (char)GetConsoleInput(hStdin);
40
41         if (Character == 'q')
42             break;
43         if ((Character == SUSPEND_PROCESSING) || (Character == END_PROCESSING) || (Character == CONTINUE_PROCESSING))
44             ProcessRequest(Character, ShutDownEventHandle, PauseEventHandle, ContinueEventHandle);
45         else
46             printf("\nInvalid selection....\n");
47
48     }
49
50     CloseHandle(ShutDownEventHandle);
51     CloseHandle(PauseEventHandle);
52     CloseHandle(ContinueEventHandle);
53         return(0);
54
55 }
56
57 int GetConsoleInput(HANDLE hStdin)
58 {
59     INPUT_RECORD    InputRecord[128];
60     DWORD           InputRecordCount;
61     BOOL            rc;
62     int             i;
63     int             RetCode;
64     char            ReadChar;
65
66     InputRecordCount = 0;
67     RetCode = 0;
68     while (1)
69     {
70         rc = ReadConsoleInput(hStdin, InputRecord, 1, &InputRecordCount);
71         if (InputRecord[0].EventType == KEY_EVENT)
72         {
73             if (InputRecord[0].Event.KeyEvent.bKeyDown)
74                 break;
75         }
76     }
77     for (i = 0; i < (int)InputRecordCount; i++) 
78     {
79         switch(InputRecord[i].EventType) 
80         { 
81             case KEY_EVENT:
82                 if (InputRecord[i].Event.KeyEvent.bKeyDown)
83                 {
84                     ReadChar = InputRecord[i].Event.KeyEvent.uChar.AsciiChar;
85                     if ((ReadChar == 'q') || (ReadChar == 'Q'))
86                     {
87                         printf("\nQuit request received\n");
88                         exit(0);
89                     }
90                     else if ((ReadChar == 'c') || (ReadChar == 'C'))
91                     {
92                         printf("\nContinue stress test request received\n");
93                         RetCode = CONTINUE_PROCESSING;
94                     }
95                     else if ((ReadChar == 'p') || (ReadChar == 'P'))
96                     {
97                         printf("\nPause stress test request received\n");
98                         RetCode = SUSPEND_PROCESSING;
99                     }
100                     else if ((ReadChar == 'e') || (ReadChar == 'E'))
101                     {
102                         RetCode = END_PROCESSING;
103                         printf("\nEnd stress test request received\n");
104                     }
105                 }
106                 break; 
107             default: 
108                 break; 
109         }
110     }
111     return(RetCode);
112 }
113
114 void ProcessRequest(char RequestType, HANDLE ShutDownEventHandle, HANDLE PauseEventHandle, HANDLE ContinueEventHandle)
115 {
116     static int LastRequest = 0;
117
118     if (RequestType == CONTINUE_PROCESSING)
119     {
120         if (LastRequest == END_PROCESSING)
121         {
122             printf("Invalid request, all process are currently ending\n");
123             return;
124         }
125         ResetEvent(PauseEventHandle);
126         SetEvent(ContinueEventHandle);
127         printf("Continue processing event has been set\n");
128         LastRequest = RequestType;
129     }
130     if (RequestType == SUSPEND_PROCESSING)
131     {
132         if (LastRequest == END_PROCESSING)
133         {
134             printf("Invalid request, all processes are currently ending\n");
135             return;
136         }
137         ResetEvent(ContinueEventHandle);
138         SetEvent(PauseEventHandle);
139         printf("Suspend processing event has been set\n");
140         LastRequest = RequestType;
141     }
142     else if (RequestType == END_PROCESSING)
143     {
144         SetEvent(ShutDownEventHandle);
145         ResetEvent(PauseEventHandle);
146         SetEvent(ContinueEventHandle);
147         printf("End processing event has been set\n");
148         LastRequest = RequestType;
149     }
150 }