summaryrefslogtreecommitdiff
path: root/examples/Reactor/Multicast/Log_Wrapper.cpp
blob: b3f84071a323edad9c11972788bf280235654f40 (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
// $Id$

// client.C

#include "Log_Wrapper.h"
#include "ace/OS_NS_unistd.h"
#include "ace/OS_NS_sys_utsname.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_netdb.h"

ACE_RCSID(Multicast, Log_Wrapper, "$Id$")

Log_Wrapper::Log_Wrapper (void)
{
  sequence_number_ = 0;
  this->log_msg_.app_id = ACE_OS::getpid ();
}

Log_Wrapper::~Log_Wrapper (void)
{
}

// Set the log_msg_ host address.

int
Log_Wrapper::open (const int port, const char *mcast_addr)
{
  struct hostent *host_info;
  ACE_utsname host_data;

  if (ACE_OS::uname (&host_data) < 0)
    return -1;

#if defined (ACE_LACKS_UTSNAME_T)
  if ((host_info = ACE_OS::gethostbyname
       (ACE_TEXT_TO_CHAR_IN(host_data.nodename))) == NULL)
#else
  if ((host_info = ACE_OS::gethostbyname (host_data.nodename)) == NULL)
#endif
    return -1;
  else
    ACE_OS::memcpy ((char *) &this->log_msg_.host,
                    (char *) host_info->h_addr,
                    host_info->h_length);

  // This starts out initialized to all zeros!
  server_ = ACE_INET_Addr (port, mcast_addr);

  if (logger_.subscribe (server_) == -1)
      perror("can't subscribe to multicast group"), exit(1);

  // success.
  return 0;
}

// Send the message to a logger object.
// This wrapper fills in all the log_record info for you.
// uses iovector stuff to make contiguous header and message.

int
Log_Wrapper::log_message (Log_Priority type, char *message)
{
  sequence_number_++;

  this->log_msg_.type = type;
  this->log_msg_.time = time (0);
  this->log_msg_.msg_length = ACE_OS::strlen(message)+1;
  this->log_msg_.sequence_number = htonl(sequence_number_);

  iovec iovp[2];
  iovp[0].iov_base = reinterpret_cast<char*> (&log_msg_);
  iovp[0].iov_len  = sizeof (log_msg_);
  iovp[1].iov_base = message;
  iovp[1].iov_len  = log_msg_.msg_length;

  logger_.send (iovp, 2);

  // success.
  return 0;
}