~ubuntu-branches/ubuntu/lucid/sip-tester/lucid

« back to all changes in this revision

Viewing changes to screen.cpp

  • Committer: Bazaar Package Importer
  • Author(s): ARAKI Yasuhiro
  • Date: 2005-04-11 11:53:53 UTC
  • Revision ID: james.westby@ubuntu.com-20050411115353-o33qn3noyjwjgnpd
Tags: upstream-1.1rc1
ImportĀ upstreamĀ versionĀ 1.1rc1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  This program is free software; you can redistribute it and/or modify
 
3
 *  it under the terms of the GNU General Public License as published by
 
4
 *  the Free Software Foundation; either version 2 of the License, or
 
5
 *  (at your option) any later version.
 
6
 *
 
7
 *  This program is distributed in the hope that it will be useful,
 
8
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
 *  GNU General Public License for more details.
 
11
 *
 
12
 *  You should have received a copy of the GNU General Public License
 
13
 *  along with this program; if not, write to the Free Software
 
14
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
 *
 
16
 *  Author : Richard GAYRAUD - 04 Nov 2003
 
17
 *           From Hewlett Packard Company.
 
18
 */
 
19
 
 
20
/****
 
21
 * Screen.cpp : Simple curses & logfile encapsulation 
 
22
 */
 
23
 
 
24
#include "stat.hpp"
 
25
 
 
26
#include <curses.h>
 
27
#include <stdio.h>
 
28
#include <stdlib.h>
 
29
#include <string.h>
 
30
#include <signal.h>
 
31
#include <screen.hpp>
 
32
 
 
33
 
 
34
#ifdef __3PCC__
 
35
#include <unistd.h>
 
36
extern int           twinSippSocket;
 
37
extern int           localTwinSippSocket;
 
38
#endif // __3PCC__ //
 
39
 
 
40
unsigned long screen_errors;
 
41
char          screen_last_error[32768];
 
42
char          _screen_err[32768];
 
43
FILE        * screen_errorf = 0;
 
44
int           screen_inited = 0;
 
45
char          screen_logfile[255];
 
46
char          screen_exename[255];
 
47
extern void   releaseGlobalAllocations();
 
48
extern bool   backgroundMode;
 
49
 
 
50
void (*screen_exit_handler)();
 
51
 
 
52
/* Clock must be a pointer to struct timeval */
 
53
#define GET_TIME(clock)       \
 
54
{                             \
 
55
  struct timezone tzp;        \
 
56
  gettimeofday (clock, &tzp); \
 
57
}
 
58
 
 
59
int  screen_readkey()
 
60
{
 
61
  return getch();
 
62
}
 
63
 
 
64
void screen_exit(int rc)
 
65
{
 
66
  unsigned long counter_value=0;
 
67
 
 
68
  /* Some signals may be delivered twice during exit() execution,
 
69
   * and we must prevent all this from beeing done twice */
 
70
  
 
71
  {
 
72
    static int already_exited = 0;
 
73
    if(already_exited) {
 
74
      return;
 
75
    }
 
76
    already_exited = 1;
 
77
  }
 
78
  
 
79
  if( backgroundMode == false ) 
 
80
  endwin();
 
81
  
 
82
  if(screen_exit_handler) {
 
83
    screen_exit_handler();
 
84
  }
 
85
 
 
86
  if(screen_errors) {
 
87
    fprintf(stderr, "%s", screen_last_error);
 
88
    if(screen_errors > 1) {
 
89
      if (screen_logfile[0] != (char)0) {
 
90
      fprintf(stderr, 
 
91
              "%s: There were more errors, see scenarioname_ppid_errors.log file\n",
 
92
              screen_exename);
 
93
      } else {
 
94
          fprintf(stderr, 
 
95
              "%s: There were more errors, enable -trace_err to log them.\n",
 
96
              screen_exename);
 
97
      }
 
98
    }
 
99
    fflush(stderr);
 
100
  }
 
101
 
 
102
#ifdef __3PCC__
 
103
  if(twinSippSocket) {
 
104
    close(twinSippSocket);
 
105
  }
 
106
 
 
107
  if(localTwinSippSocket) {
 
108
    close(localTwinSippSocket);
 
109
         }
 
110
#endif //__3PCC__
 
111
 
 
112
  // Get failed calls counter value before releasing objects
 
113
  counter_value = CStat::instance()->GetStat (CStat::CPT_C_FailedCall);
 
114
 
 
115
  releaseGlobalAllocations();
 
116
 
 
117
  if (rc != EXIT_TEST_RES_UNKNOWN) {
 
118
    // Exit is not a normal exit. Just use the passed exit code.
 
119
    exit(rc);
 
120
  } else {
 
121
    // Normal exit: we need to determine if the calls were all
 
122
    // successful or not.
 
123
    // In order to compute the return code, get the counter
 
124
    // of failed calls. If there is 0 failed calls, then everything is OK!
 
125
    if (counter_value == 0) {
 
126
      exit(EXIT_TEST_OK);
 
127
    } else {
 
128
      exit(EXIT_TEST_FAILED);
 
129
    }
 
130
  }
 
131
}
 
132
 
 
133
/* Exit handler for Curses */
 
134
 
 
135
void screen_quit()
 
136
{
 
137
  screen_exit(EXIT_TEST_RES_UNKNOWN);
 
138
}
 
139
 
 
140
void screen_clear() 
 
141
{
 
142
  printf("\033[2J");
 
143
}
 
144
 
 
145
void screen_set_exename(char * exe_name)
 
146
{
 
147
  strcpy(screen_exename, exe_name);
 
148
}
 
149
 
 
150
void screen_init(char *logfile_name, void (*exit_handler)())
 
151
{
 
152
  struct sigaction action_quit;
 
153
  
 
154
  screen_inited = 1;
 
155
  if (logfile_name == NULL) {
 
156
    screen_logfile[0] = (char)0;
 
157
  } else {
 
158
  strcpy(screen_logfile, logfile_name);
 
159
  }
 
160
  screen_exit_handler = exit_handler;
 
161
 
 
162
  /* Initializes curses and signals */
 
163
  initscr();
 
164
  /* Enhance performances and display */
 
165
  noecho();
 
166
  
 
167
  /* Map exit handlers to curses reset procedure */
 
168
  memset(&action_quit, 0, sizeof(action_quit));
 
169
  (*(void **)(&(action_quit.sa_handler)))=(void *)screen_quit;
 
170
  sigaction(SIGTERM, &action_quit, NULL);
 
171
  sigaction(SIGINT, &action_quit, NULL);
 
172
  sigaction(SIGKILL, &action_quit, NULL);  
 
173
 
 
174
  printf("\033[2J");
 
175
}
 
176
 
 
177
void _screen_error(char *s, int fatal)
 
178
{
 
179
  FILE * output;
 
180
  char * c = screen_last_error;
 
181
  struct timeval currentTime;
 
182
 
 
183
  GET_TIME (&currentTime);
 
184
  
 
185
  c+= sprintf(c, "%s: ", CStat::instance()->formatTime(&currentTime));
 
186
  c+= sprintf(c, "%s", s);
 
187
  c+= sprintf(c, ".\n");
 
188
  screen_errors++;
 
189
 
 
190
  if(screen_inited && (!screen_errorf) && screen_logfile[0] != (char)0) {
 
191
    screen_errorf = fopen(screen_logfile, "w");
 
192
    if(!screen_errorf) {
 
193
      c += sprintf(c, "%s: Unable to create '%s'.\n",
 
194
                   screen_exename, screen_logfile);
 
195
      screen_exit(EXIT_FATAL_ERROR);
 
196
    } else {
 
197
      fprintf(screen_errorf, "%s: The following events occured:\n",
 
198
              screen_exename);
 
199
      fflush(screen_errorf);
 
200
    }
 
201
  }
 
202
 
 
203
  if(screen_errorf) {
 
204
    output = screen_errorf;
 
205
    fprintf(output, "%s", screen_last_error);
 
206
    fflush(output);
 
207
  } else if (fatal) {
 
208
    output = stderr;
 
209
  fprintf(output, "%s", screen_last_error);
 
210
  fflush(output);
 
211
  }
 
212
 
 
213
  if(fatal) {
 
214
    if(!screen_inited) {
 
215
      exit(EXIT_FATAL_ERROR);
 
216
    } else {
 
217
      screen_exit(EXIT_FATAL_ERROR);
 
218
    }
 
219
  }
 
220
}