~ubuntu-branches/ubuntu/utopic/sip-tester/utopic

« back to all changes in this revision

Viewing changes to opentask.cpp

  • Committer: Package Import Robot
  • Author(s): Mark Purcell, Paul Belanger, Mark Purcell
  • Date: 2011-11-03 21:56:17 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20111103215617-nnxicj1oto9e8ix5
Tags: 1:3.2-1
[ Paul Belanger ]
* New Upstream Release (Closes: #623915).
* Switch to dpkg-source 3.0 (quilt) format
* Building with PCAP play.
* Switch back to Debhelper.
* debian/patches/spelling-error-in-binary: Fix lintian warning

[ Mark Purcell ]
* Drop pabs from Uploaders: at his request
* fix debhelper-overrides-need-versioned-build-depends
* fix description-synopsis-starts-with-article

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  This program is free software; you can redistribute it and/or modify
 
3
 *  it under the terms of the GNU General Public License as published by
 
4
 *  the Free Software Foundation; either version 2 of the License, or
 
5
 *  (at your option) any later version.
 
6
 *
 
7
 *  This program is distributed in the hope that it will be useful,
 
8
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
 *  GNU General Public License for more details.
 
11
 *
 
12
 *  You should have received a copy of the GNU General Public License
 
13
 *  along with this program; if not, write to the Free Software
 
14
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
 *
 
16
 *  Author : Richard GAYRAUD - 04 Nov 2003
 
17
 *           Marc LAMBERTON
 
18
 *           Olivier JACQUES
 
19
 *           Herve PELLAN
 
20
 *           David MANSUTTI
 
21
 *           Francois-Xavier Kowalski
 
22
 *           Gerard Lyonnaz
 
23
 *           From Hewlett Packard Company.
 
24
 *           F. Tarek Rogers
 
25
 *           Peter Higginson
 
26
 *           Vincent Luba
 
27
 *           Shriram Natarajan
 
28
 *           Guillaume Teissier from FTR&D
 
29
 *           Clement Chen
 
30
 *           Wolfgang Beck
 
31
 *           Charles P Wright from IBM Research
 
32
 */
 
33
#include "sipp.hpp"
 
34
 
 
35
class opentask *opentask::instance = NULL;
 
36
unsigned long opentask::calls_since_last_rate_change = 0;
 
37
unsigned long opentask::last_rate_change_time = 0;
 
38
 
 
39
void opentask::initialize() {
 
40
  assert(instance == NULL);
 
41
  instance = new opentask();
 
42
}
 
43
 
 
44
opentask::opentask() {
 
45
  setRunning();
 
46
}
 
47
 
 
48
opentask::~opentask() {
 
49
  instance = NULL;
 
50
}
 
51
 
 
52
void opentask::dump() {
 
53
  WARNING("Uniform rate call generation task: %d", rate);
 
54
}
 
55
 
 
56
unsigned int opentask::wake() {
 
57
  if (paused) {
 
58
    return 0;
 
59
  } else if (users >= 0) {
 
60
    /* We need to wait until another call is terminated. */
 
61
    return 0;
 
62
  } else {
 
63
    /* We need to compute when the next call is going to be opened. */
 
64
    return (unsigned long) (last_rate_change_time + ((calls_since_last_rate_change + 1) / (rate/rate_period_ms)));
 
65
  }
 
66
}
 
67
 
 
68
bool opentask::run() {
 
69
  int calls_to_open = 0;
 
70
 
 
71
  if (quitting) {
 
72
    delete this;
 
73
    return false;
 
74
  }
 
75
 
 
76
  if (paused) {
 
77
    setPaused();
 
78
    return true;
 
79
  }
 
80
 
 
81
  long l=0;
 
82
  unsigned long long current_calls = main_scenario->stats->GetStat(CStat::CPT_C_CurrentCall);
 
83
  unsigned long long total_calls = main_scenario->stats->GetStat(CStat::CPT_C_IncomingCallCreated) + main_scenario->stats->GetStat(CStat::CPT_C_OutgoingCallCreated);
 
84
 
 
85
  if (users >= 0) {
 
86
    calls_to_open = ((l = (users - current_calls)) > 0) ? l : 0;
 
87
  } else {
 
88
    calls_to_open = (unsigned int)
 
89
      ((l=(long)floor(((clock_tick - last_rate_change_time) * rate/rate_period_ms)
 
90
                      - calls_since_last_rate_change))>0?l:0);
 
91
  }
 
92
 
 
93
  if (total_calls + calls_to_open > stop_after) {
 
94
    calls_to_open = stop_after - total_calls;
 
95
  }
 
96
 
 
97
  if (open_calls_allowed && (current_calls + calls_to_open > open_calls_allowed)) {
 
98
    calls_to_open = open_calls_allowed - current_calls;
 
99
  }
 
100
 
 
101
  if (calls_to_open < 0) {
 
102
    calls_to_open = 0;
 
103
  }
 
104
 
 
105
  unsigned int start_clock = getmilliseconds();
 
106
 
 
107
 
 
108
  while(calls_to_open--)
 
109
  {
 
110
      /* Associate a user with this call, if we are in users mode. */
 
111
      int userid = 0;
 
112
      if (users >= 0) {
 
113
        userid = freeUsers.back();
 
114
        freeUsers.pop_back();
 
115
      }
 
116
 
 
117
      // adding a new OUTGOING CALL
 
118
      main_scenario->stats->computeStat(CStat::E_CREATE_OUTGOING_CALL);
 
119
      call * call_ptr = call::add_call(userid, is_ipv6, use_remote_sending_addr ? &remote_sending_sockaddr : &remote_sockaddr);
 
120
      if(!call_ptr) {
 
121
        ERROR("Out of memory allocating call!");
 
122
      }
 
123
 
 
124
      calls_since_last_rate_change++;
 
125
 
 
126
      outbound_congestion = false;
 
127
 
 
128
      if (!multisocket) {
 
129
        switch(transport) {
 
130
          case T_UDP:
 
131
            call_ptr->associate_socket(main_socket);
 
132
            main_socket->ss_count++;
 
133
            break;
 
134
          case T_TCP:
 
135
          case T_TLS:
 
136
            call_ptr->associate_socket(tcp_multiplex);
 
137
            tcp_multiplex->ss_count++;
 
138
            break;
 
139
        }
 
140
      }
 
141
      if (getmilliseconds() > start_clock) {
 
142
        break;
 
143
      }
 
144
    }
 
145
 
 
146
    /* We can pause. */
 
147
    if (calls_to_open <= 0) {
 
148
      setPaused();
 
149
    } else {
 
150
      /* Stay running. */
 
151
    }
 
152
 
 
153
    // Quit after asked number of calls is reached
 
154
    if(total_calls >= stop_after) {
 
155
      quitting = 1;
 
156
      return false;
 
157
    }
 
158
 
 
159
    return true;
 
160
}
 
161
 
 
162
void opentask::set_paused(bool new_paused)
 
163
{
 
164
  if (!instance) {
 
165
    /* Doesn't do anything, we must be in server mode. */
 
166
    return;
 
167
  }
 
168
  if (new_paused) {
 
169
    instance->setPaused();
 
170
  } else {
 
171
    instance->setRunning();
 
172
    if (users >= 0) {
 
173
      set_users(users);
 
174
    } else {
 
175
      set_rate(rate);
 
176
    }
 
177
  }
 
178
  paused = new_paused;
 
179
}
 
180
 
 
181
void opentask::set_rate(double new_rate)
 
182
{
 
183
  if (!instance) {
 
184
    /* Doesn't do anything, we must be in server mode. */
 
185
  }
 
186
 
 
187
  rate = new_rate;
 
188
  if(rate < 0) {
 
189
    rate = 0;
 
190
  }
 
191
 
 
192
  last_rate_change_time = getmilliseconds();
 
193
  calls_since_last_rate_change = 0;
 
194
 
 
195
  if(!open_calls_user_setting) {
 
196
 
 
197
    int call_duration_min =  main_scenario->duration;
 
198
 
 
199
    if(duration > call_duration_min) call_duration_min = duration;
 
200
 
 
201
    if(call_duration_min < 1000) call_duration_min = 1000;
 
202
 
 
203
    open_calls_allowed = (int)((3.0 * rate * call_duration_min) / (double)rate_period_ms);
 
204
    if(!open_calls_allowed) {
 
205
      open_calls_allowed = 1;
 
206
    }
 
207
  }
 
208
}
 
209
 
 
210
void opentask::set_users(int new_users)
 
211
{
 
212
  if (!instance) {
 
213
    /* Doesn't do anything, we must be in server mode. */
 
214
    return;
 
215
  }
 
216
 
 
217
  if (new_users < 0) {
 
218
    new_users = 0;
 
219
  }
 
220
  assert(users >= 0);
 
221
 
 
222
  if (users < new_users ) {
 
223
    while (users < new_users) {
 
224
      int userid;
 
225
      if (!retiredUsers.empty()) {
 
226
        userid = retiredUsers.back();
 
227
        retiredUsers.pop_back();
 
228
      } else {
 
229
        userid = users + 1;
 
230
        userVarMap[userid] = new VariableTable(userVariables);
 
231
      }
 
232
      freeUsers.push_front(userid);
 
233
      users++;
 
234
    }
 
235
  }
 
236
 
 
237
  users = open_calls_allowed = new_users;
 
238
 
 
239
  last_rate_change_time = clock_tick;
 
240
  calls_since_last_rate_change = 0;
 
241
 
 
242
  assert(open_calls_user_setting);
 
243
 
 
244
  instance->setRunning();
 
245
}
 
246
 
 
247
void opentask::freeUser(int userId) {
 
248
  if (main_scenario->stats->GetStat(CStat::CPT_C_CurrentCall) > open_calls_allowed) {
 
249
    retiredUsers.push_front(userId);
 
250
  } else {
 
251
    freeUsers.push_front(userId);
 
252
    /* Wake up the call creation thread. */
 
253
    if (instance) {
 
254
      instance->setRunning();
 
255
    }
 
256
  }
 
257
}