summaryrefslogtreecommitdiff
path: root/apps/Orbix-Examples/Logger/client.cpp
blob: 6a0104bd07e59bf424d0a4bbe862dc22da1456cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// A client for the distributed logger example.  This program reads
// $Id$

// from either stdin or from a redirected file and sends all the
// contents to the logging server.  It also computes how long it takes
// to send this stuff. 


#include "Logger.h"

// maximum message size
static size_t max_message_size = BUFSIZ;

// Default behavior is to use the locator service.
static char *hostname = 0;

// Should we prompt the user?
static int user_prompt;

static void
parse_args (int argc, char *argv[])
{
  extern char *optarg;
  extern int  optind;
  int c;

  ACE_LOG_MSG->open (argv[0]);

  // If a file has been redirected, don't activate user prompts
  if (ACE_OS::isatty (0))
    user_prompt = 1;
  else
    user_prompt = 0;

  while ((c = ACE_OS::getopt (argc, argv, "m:h:")) != -1)
    switch (c)
      {
      case 'm':
        max_message_size = ACE_OS::atoi (optarg) * BUFSIZ;
        break;
      case 'h':
	hostname = optarg;
	break;
      default:
        ACE_ERROR ((LM_ERROR, "%n: -h host -m max_message_size (in kbytes)\n%a", 1));
	/* NOTREACHED */
      }
}

// Enable/disable verbose logging.

static int
toggle_verbose (Logger &logger)
{
  int verbose_value;

  verbose_value = logger.verbose ();
  logger.verbose (!verbose_value);
  return 0;
}

// Transmit messages to the server.

int
transmit (Logger &logger, char buf[], ACE_HANDLE handle = 0)
{
  if (user_prompt)
    cout << "\nEnter message ('Q':quit,'V':toggle verbose):\n" << flush;

  ssize_t nbytes = ACE_OS::read (handle, buf, max_message_size);

  if (nbytes <= 0)
    return nbytes; // End of file or error.
  buf[nbytes] = '\0';

  if (user_prompt)
    {
      if (buf[0] == 'Q' || buf[0] == 'q')
	return 0;
      // toggle verbose?
      else if (buf[0] == 'V' || buf[0] == 'v')
	toggle_verbose (logger);
    }

  // send the message to the logger
  if (logger.log (logger::LM_DEBUG, buf, nbytes) == -1)
    return -1;
  else
    return nbytes;
}

// Print the results of the tests.

void
report_results (profile_logger::Elapsed_Time &et, size_t total_bytes)
{
  ACE_DEBUG ((LM_DEBUG, 
	     "real time = %8.2f\n"
	     "user time = %8.2f\n"
	     "sys time = %8.2f\n"
	     "mbits sec = %8.2f\n",
	     et.real_time, et.user_time, et.system_time, 
	     (total_bytes / et.real_time) * 8.0 / 1024.0 / 1024.0));
}

int
main (int argc, char **argv) 
{
  parse_args (argc,argv);

  // Pointer to the logger object that will be used.
  Logger logger (hostname, max_message_size);
  char	 *buf = new char [max_message_size];
  size_t total_bytes = 0;
  size_t nbytes = 0;

  logger.start_timer ();

  // Transmit logging records until user quits.

  for (int done = 0; done == 0;)
    switch (nbytes = transmit (logger, buf))
      {
      case -1:
	ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "transmit"), -1);
	/* NOTREACHED */
      case 0:
	done = 1;
	break;
      default:
	total_bytes += nbytes;
	break;
      }

  profile_logger::Elapsed_Time et;

  if (logger.stop_timer (et) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "stop timer"), -1);

  report_results (et, total_bytes);
  return 0;
}