summaryrefslogtreecommitdiff
path: root/TAO/tests/Cubit/TAO/DII_Cubit/svr.cpp
blob: 1151df9e0fde6da6aadf7c3dbb2b5cac90de758a (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
143
144
145
146
147
148
149
150
151
// @(#)svr.cpp	05/14/97
// Copyright 1994-1995 by Sun Microsystems Inc.
// All Rights Reserved
//
// TEST:	simple IIOP server for "cubit.idl" interface.
//
// Starts up, builds an objref, prints its string in debug mode,
// listens for messages, responds to them.
//
// Modified version of Cubit Example written by Sun Microsystems Inc.
// Modified by: Brian Mendel

#include "ace/Get_Opt.h"
#include "ace/Log_Msg.h"

#include "tao/corba.h"

#include "cubit_i.h"

// Global Variables
static CORBA::String key = (CORBA::String) "key0";

static int num_of_objs = 1;

// Parses the command line arguments and returns an error status

static int 
parse_args (int argc, char *argv[])
{
  ACE_Get_Opt opts (argc, argv, "dk:n:");
  int			c;
   
  while ((c = opts ()) != -1)
    switch (c) 
      {
      case 'd':  // debug flag
	TAO_debug_level++;
	break;
      case 'k':			// key (str)
	key = (CORBA::String) opts.optarg;
	break;
      case 'n':			// idle seconds b4 exit
	num_of_objs = ACE_OS::atoi (opts.optarg);
	break;
      case '?':
      default:
	ACE_ERROR_RETURN ((LM_ERROR,
			   "usage:  %s"
			   " [-d]"
			   " [-k {object_key}]"
			   "\n", argv [0]), 1);
      }
   
  return 0;  // Indicates successful parsing of command line
}

// Standard command line parsing utilities used.

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

  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 Basic Object Adapter
  CORBA::POA_ptr oa_ptr = orb_ptr->POA_init (argc, argv);

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

  // Parse remaining command line and verify parameters.

  parse_args (argc, argv);
  
  // Create implementation object with user specified key.
  Cubit_i_ptr *my_cubit = new Cubit_i_ptr[num_of_objs];

  for (int i = 0; i < num_of_objs; i++)
    {
      CORBA::String obj_str = CORBA::string_alloc (ACE_OS::strlen ((char *) key)+2);

      ::sprintf (obj_str, "%s%d", (char *) key, i);

      my_cubit[i] = new Cubit_i (obj_str);

      if (my_cubit[i] == 0)
        ACE_ERROR_RETURN ((LM_ERROR,
			   " (%P|%t) Unable to create implementation object&d\n",
			   i),
			  2);

      if (TAO_debug_level > 0)
	{
	  // Why are we getting the POA_ptr from here when we've
	  // already got it above?

	  CORBA::OctetSeq obj_key;
	  obj_key.buffer = (CORBA::Octet *) obj_str;
	  obj_key.length = obj_key.maximum = ACE_OS::strlen (obj_str);
     
#if 0
	  CORBA::Object_ptr obj = 0;

	  if (oa_ptr->find (obj_key, obj) == -1)
	    ACE_ERROR_RETURN ((LM_ERROR,
			       " (%P|%t) Unable to locate object with key '%s', %p\n", 
			       key),
			      3);
#endif
     
 	  // Stringify the objref we'll be implementing, and print it
	  // to stdout.  Someone will take that string and give it to
	  // some client.  Then release the object.

	  CORBA::String str;
         
	  str = orb_ptr->object_to_string (my_cubit[i], env);

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

	  ACE_OS::puts ((char *) str);
	  ACE_OS::fflush (stdout);
	  //dmsg1 ("Object Created at: '%ul'", obj);
	  dmsg1 ("listening as object '%s'", str);
	}

      CORBA::string_free (obj_str);  
    }

  // 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);

  // Free resources
  CORBA::release (orb_ptr);

  return 0;
}