summaryrefslogtreecommitdiff
path: root/examples/Simple/chat/Server_i.cpp
blob: c23bb8ab738f9be38f612f0d2ca4aad5837b9678 (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

//=============================================================================
/**
 *  @file    Server_i.cpp
 *
 *  $Id$
 *
 *  Implementation of the Chat Server_i class.
 *
 *
 *  @author Pradeep Gore <pradeep@cs.wustl.edu>
 */
//=============================================================================


#include "Server_i.h"
#include "ace/Get_Opt.h"
#include "ace/OS_NS_stdio.h"

Server_i::Server_i ()
  : ior_file_name_ (ACE_TEXT ("chat.ior"))
{
  Broadcaster_i *tmp = 0;
  ACE_NEW_THROW_EX (tmp,
                    Broadcaster_i (),
                    CORBA::NO_MEMORY ());
  this->broadcaster_i_ = tmp;
}

Server_i::~Server_i (void)
{
  // NO Op.
}

int
Server_i::parse_args (int argc, ACE_TCHAR *argv[])
{
  ACE_Get_Opt get_opts (argc, argv, ACE_TEXT ("o:"));
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'o':  // get the file name to write to
        this->ior_file_name_ = get_opts.opt_arg ();
        break;

      case '?':  // display help for use of the server.
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("usage:  %s")
                           ACE_TEXT (" [-o] <ior_output_file>")
                           ACE_TEXT ("\n"),
                           argv [0]),
                          -1);
      }

  return 0;
}

int
Server_i::init (int argc,
                ACE_TCHAR *argv[])
{
  // Parse the command line options.
  if (this->parse_args(argc, argv) == -1)
    return -1;

  if (this->orb_manager_.init (argc,
                               argv) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("%p\n"),
                       ACE_TEXT ("orb manager init failed")),
                      -1);

  CORBA::ORB_var orb = this->orb_manager_.orb ();

  // Activate the servant.
  CORBA::String_var str =
    this->orb_manager_.activate (this->broadcaster_i_.in ());

  // Write the IOR to a file.
  this->write_IOR (str.in ());
  return 0;
}

int
Server_i::run (void)
{
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("Running chat server...\n")));

  // Run the main event loop for the ORB.
  int ret = this->orb_manager_.run ();
  if (ret == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("Server_i::run")),
                      -1);
  return 0;
}

int
Server_i::write_IOR (const char* ior)
{
  FILE* ior_output_file_ =
    ACE_OS::fopen (this->ior_file_name_, "w");

  if (ior_output_file_)
    {
      ACE_OS::fprintf (ior_output_file_,
                       ACE_TEXT ("%s"),
                       ior);
      ACE_OS::fclose (ior_output_file_);
    }

  return 0;
}