summaryrefslogtreecommitdiff
path: root/TAO/tests/RTCORBA/Linear_Priority/readers.cpp
blob: a1f5fd719ac5ef33b12b9351a1e8a89f43aae428 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// $Id$

#include "ace/Read_Buffer.h"
#include "ace/Array_Base.h"

typedef ACE_Array_Base<CORBA::Short> Priorities;

int
get_priority_bands (const char *test_type,
                    const char *bands_file,
                    RTCORBA::RTORB_ptr rt_orb,
                    CORBA::PolicyList &policies,
                    CORBA::Environment &ACE_TRY_ENV)
{
  //
  // Read bands from a file.
  //
  FILE* file =
    ACE_OS::fopen (bands_file, "r");

  if (file == 0)
    return -1;

  ACE_Read_Buffer reader (file, 1);

  char *string =
    reader.read (EOF, ' ', '\0');

  // Check for empty bands file.
  if (string == 0)
    {
      ACE_DEBUG ((LM_DEBUG,
                  "\n%s: No bands set!\n\n",
                  test_type));
      return 0;
    }

  CORBA::ULong bands_length =
    (reader.replaced () + 1) / 2;

  RTCORBA::PriorityBands bands;
  bands.length (bands_length);

  ACE_DEBUG ((LM_DEBUG,
              "\n%s: There are %d bands: ",
              test_type,
              bands_length));

  int result = 1;
  char* working_string = string;
  for (CORBA::ULong i = 0; i < bands_length; ++i)
    {
      result = ::sscanf (working_string,
                         "%hd",
                         &bands[i].low);
      if (result == 0 || result == EOF)
        break;

      working_string += ACE_OS::strlen (working_string);
      working_string += 1;

      result = ::sscanf (working_string,
                         "%hd",
                         &bands[i].high);
      if (result == 0 || result == EOF)
        break;

      working_string += ACE_OS::strlen (working_string);
      working_string += 1;

      ACE_DEBUG ((LM_DEBUG,
                  "[%d %d] ",
                  bands[i].low,
                  bands[i].high));
    }

  reader.alloc ()->free (string);

  if (result == 0 || result == EOF)
    return -1;

  ACE_DEBUG ((LM_DEBUG,
              "\n\n"));

  CORBA::Policy_var banded_connection_policy =
    rt_orb->create_priority_banded_connection_policy (bands,
                                                      ACE_TRY_ENV);
  ACE_CHECK_RETURN (-1);

  policies.length (policies.length () + 1);
  policies[policies.length () - 1] =
    banded_connection_policy;

  return 0;
}

int
get_priorities (const char *test_type,
                const char *file_name,
                const char *name,
                Priorities &priorities)
{
  //
  // Read lanes from a file.
  //
  FILE* file =
    ACE_OS::fopen (file_name, "r");

  if (file == 0)
    return -1;

  ACE_Read_Buffer reader (file, 1);

  char *string =
    reader.read (EOF, ' ', '\0');

  // Check for empty lanes file.
  if (string == 0)
    {
      ACE_DEBUG ((LM_DEBUG,
                  "\n%s: No %s set!\n\n",
                  test_type,
                  name));
      return 0;
    }

  size_t length =
    reader.replaced () + 1;

  priorities.size (length);

  ACE_DEBUG ((LM_DEBUG,
              "\n%s: There are %d %s: ",
              test_type,
              length,
              name));

  int result = 1;
  char* working_string = string;
  for (CORBA::ULong i = 0; i < length; ++i)
    {
      result = ::sscanf (working_string,
                         "%hd",
                         &priorities[i]);
      if (result == 0 || result == EOF)
        break;

      working_string += ACE_OS::strlen (working_string);
      working_string += 1;

      ACE_DEBUG ((LM_DEBUG,
                  "[%d] ",
                  priorities[i]));
    }

  reader.alloc ()->free (string);

  if (result == 0 || result == EOF)
    return -1;

  ACE_DEBUG ((LM_DEBUG,
              "\n\n"));

  return 0;
}

int
get_priority_lanes (const char *test_type,
                    const char *lanes_file,
                    RTCORBA::RTORB_ptr rt_orb,
                    CORBA::ULong stacksize,
                    CORBA::ULong static_threads,
                    CORBA::ULong dynamic_threads,
                    CORBA::Boolean allow_request_buffering,
                    CORBA::ULong max_buffered_requests,
                    CORBA::ULong max_request_buffer_size,
                    CORBA::Boolean allow_borrowing,
                    CORBA::PolicyList &policies,
                    CORBA::Environment &ACE_TRY_ENV)
{
  Priorities priorities;
  int result =
    get_priorities (test_type,
                    lanes_file,
                    "lanes",
                    priorities);
  if (result != 0 ||
      priorities.size () == 0)
    return result;

  RTCORBA::ThreadpoolLanes lanes;
  lanes.length (priorities.size ());

  for (CORBA::ULong i = 0;
       i < priorities.size ();
       ++i)
    {
      lanes[i].lane_priority = priorities[i];
      lanes[i].static_threads = static_threads;
      lanes[i].dynamic_threads = dynamic_threads;
    }

  RTCORBA::ThreadpoolId threadpool_id =
    rt_orb->create_threadpool_with_lanes (stacksize,
                                          lanes,
                                          allow_borrowing,
                                          allow_request_buffering,
                                          max_buffered_requests,
                                          max_request_buffer_size,
                                          ACE_TRY_ENV);
  ACE_CHECK_RETURN (-1);

  CORBA::Policy_var threadpool_policy =
    rt_orb->create_threadpool_policy (threadpool_id,
                                      ACE_TRY_ENV);
  ACE_CHECK_RETURN (-1);

  policies.length (policies.length () + 1);
  policies[policies.length () - 1] =
    threadpool_policy;

  return 0;
}