summaryrefslogtreecommitdiff
path: root/TAO/tests/Cubit/COOL/MT_Cubit/server.cpp
blob: e19d39e7badda9bb05f402d3efc3dcdcc293b4c5 (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
152
153
154
155
156
157
158
159
160
// $Id$

// ============================================================================
//
// = LIBRARY
//    TAO/tests
//
// = FILENAME
//    server.cpp
//
// = AUTHOR
//    Andy Gokhale, Sumedh Mungee, and Sergio Flores-Gaitan
//
// ============================================================================

#include <api/binding.H>
#include <api/api.H>
#include <api/coolThreadPool.H>
#include <corba/eoa.H>
#include <corba/eorb.H>

#include "cubit.H"
#include "cubit_impl.h"		// server header file
#include "sk_cubit.H"
#include "sk_cubit.C"
#include "ace/OS.h"

ACE_RCSID(MT_Cubit, server, "$Id$")

CORBA_BOA_ptr boa;

int
create_servant (void *arg)
{
  char ** argv = (char **) arg;

  CORBA_Environment env;
  Cubit_ptr cubit;
  Cubit_Impl cubit_impl;

  COOL_ThreadPool*        MyPool_1;
  u_int concurrencyModel = 0;
  
  if (strcmp (argv[1], "tpool") == 0)
    {
      //
      // Defines a policy for using a pool of 20 threads.
      //
      concurrencyModel = THREAD_POOL;
      ACE_DEBUG ((LM_DEBUG, "Using Thread Pool concurrency model\n"));
    }
  else if (strcmp (argv[1], "tpr") == 0)
    {
      // Defines policy to create a thread for each incoming
      // request.
      concurrencyModel = THREAD_PER_REQUEST;
      ACE_DEBUG ((LM_DEBUG, "Using Thread Per Request concurrency model\n"));
    }
  else
    {
      ACE_ERROR ((LM_ERROR, "Concurrency model determination failed.\n"));
      return -1;
    }

  COOL_ThreadPoolControl poolCtl_1 (concurrencyModel, 1, 20, -1);
  COOL_QueueControl      qCtl;
  
  // Defines thread attributes for threads belonging to pools.
  COOL_ThreadAttributes  poolAttr;
  struct sched_param sp;
  poolAttr.init();
  poolAttr.stack_size(8192*2);
  sp.sched_priority = 43;
  poolAttr.sched_attr(&sp);
  //
  // Allocate and initialize first pool
  //
  MyPool_1  = new COOL_ThreadPool;
  MyPool_1->init_pool(poolCtl_1, poolAttr, qCtl);
  
  COOL::EOABindingData bindingData (argv[2]);
  bindingData.threadpool(MyPool_1);
  
  COOL_bind (cubit_impl, cubit, bindingData, env);

  if (env.exception ()) 
    {
      ACE_DEBUG ((LM_ERROR, "Bind failed.\n"));
      return -1;
    }
  
  if (env.exception ()) {
    ACE_DEBUG ((LM_ERROR, "Object Reference Export Failed.\n"));
    return -1;
  }

  ACE_DEBUG ((LM_DEBUG, "Entering boa->run ().\n"));
  boa->run ();
  return 0;
}


int 
main (int argc, char** argv)
{
  CORBA_Environment env;
  int i = 0;

  COOL::EOA::bind(argv[2], env);
  if (env.exception()) {
    ACE_DEBUG ((LM_ERROR, "Impossible to bind the ORB to the description: %s\n", argv[2]));
    return -1;
  }

  CORBA_ORB_ptr orb = CORBA_ORB_init (argc, argv, 0, env);
  if (env.exception ()) {
    ACE_DEBUG ((LM_ERROR, "ORB_init failed..\n"));
    CORBA::SystemException* ex;
    
    ex = CORBA::SystemException::_narrow(env.exception());
    if (ex) {
      CORBA::String_var msg = ex->message();
      fprintf(stderr, "%s.\n", (const char*) msg);
    } else {
      fprintf(stderr, "Unknown user exception.\n");
    }
    return -1;
  }

  boa = orb->OA_init (argc, argv, 0, env);
  if (env.exception ()) 
    {
      ACE_DEBUG ((LM_ERROR, "OA_init failed..\n"));
      return -1;
    }
  
  COOL_Activity_ptr new_activity [2];
  
  for (i = 0; i < 1; i++)
    {
      if (thisCapsule->createActivity ((COOL_ActivityFunc) create_servant,
				       (void *) argv,
                                       new_activity [i]) != C_OK)
        {
	  ACE_ERROR ((LM_ERROR, "%p\n", "activate failed\n"));
        }
    }
  
  CORBA_Long status;
  for (i = 0; i < 1; i++)
    {
      if (new_activity [i]->join (status) != C_OK)
        {
          ACE_ERROR ((LM_ERROR, "%p\n", "join failed\n"));
        }
    }
  
  ACE_DEBUG ((LM_DEBUG, "Cubit server is exiting\n"));
  return 0;
}