summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/Logger/svr.cpp
blob: 40c7f382e30f651864b059b32af6df95e0d10b12 (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
// $Id$

// ============================================================================
//
// = LIBRARY
//    TAO/orbsvcs/tests/Logger
//
// = FILENAME
//    svr.cpp
//
// = DESCRIPTION 
//    This program is an implementation of a simple logger service.  
//    Whatever is sent to it through its interface is displayed on stdout.
//    It uses the Logger_Factory server to create logger objects.  
//
// = AUTHORS
//    Sergio Flores-Gaitan <sergio@cs.wustl.edu>
//
// ============================================================================

#include <iostream.h>
#include "orbsvcs/CosNamingC.h"
#include "loggerS.h"
#include "logger_i.h"

int
main (int argc, char ** argv)
{
  CORBA::Environment env;
  char *orb_name = "logger";

  CORBA::ORB_ptr orb_ptr = CORBA::ORB_init (argc, argv, orb_name, env);

  if (env.exception () != 0)
    {
      env.print_exception ("ORB init");
      return 1;
    }

  // Initialize the Object Adapter
  CORBA::POA_ptr oa_ptr = orb_ptr->POA_init (argc, argv, "POA");

  if (oa_ptr == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
		       " (%P|%t) Unable to initialize the POA.\n"),
		      1);

  // create a factory implementation
  Logger_Factory_ptr f;

  ACE_NEW_RETURN (f, Logger_Factory_i ("factory"), -1);

  // Stringify the objref we'll be implementing, and print it to
  // stdout.  Someone will take that string and give it to a
  // client.  Then release the object.
  
  CORBA::String str;
  
  str = orb_ptr->object_to_string (f, env);
  
  if (env.exception () != 0)
    {
      env.print_exception ("object2string");
      return 1;
    }
  
  ACE_DEBUG ((LM_DEBUG, "listening as object '%s'\n", str)); 

  int name_service_flag = 0;
  CosNaming::NamingContext_ptr naming_service = 0;
  CosNaming::Name n(1);

  do {
    CORBA::Object_ptr  obj_ptr = 
      orb_ptr->resolve_initial_references ("NameService");
  
    // Use the naming service to advertise us, if we could successfully get a reference.
    if (CORBA::is_nil (obj_ptr) == CORBA::B_TRUE)
      {
	ACE_ERROR ((LM_ERROR, "resolve_initial_references failed.\n"));
	break;
      }
    
    // resolve the naming service
    naming_service = CosNaming::NamingContext::_narrow (obj_ptr, env);

    if (CORBA::is_nil (naming_service) == CORBA::B_TRUE)
      {
	ACE_ERROR ((LM_ERROR, "_narrow failed\n"));
	break;
      }

    // The name of the logger factory in the naming service.
    n.length (1);
    n[0].id = CORBA::string_dup ("logger_factory");
    
    // bind the logger factory to a name using the naming service.
    naming_service->bind (n, f, env);
    
    // check for errors!
    if (env.exception () != 0)
      {
	env.print_exception ("name_service->bind()");
	break;
      }
    else
      {
	ACE_DEBUG ((LM_DEBUG, "Success using the naming service!! to bind the logger factory!\n"));
	name_service_flag = 1;
      }

  } while (0);

  if (name_service_flag == 0)
    {
      ACE_ERROR ((LM_ERROR, "Unable to use the naming service.\n"));
    }

  // Handle requests for this object until we're killed, or one of the
  // methods asks us to exit.
  if (orb_ptr->run () == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "run"), -1);

  // unbind the logger factory name 
  naming_service->unbind (n, env);

  // check for errors!
  if (env.exception () != 0)
    {
      env.print_exception ("name_service->unbind()");
    }

  // release the object reference for the naming service.
  CORBA::release (naming_service);
  
  cout << "Server logger_factory is terminating" << endl;
  return 0;
}