studies/performance/Performance-Code5/servicenet_n.cpp
⇒
download file
1
2
3
4
5
6
7
8
9
10
11 #include <math.h>
12 #include "omnetpp.h"
13
14 #define check_error() \
15 {(void)0;}
16 #define check_memory() \
17 {if (memoryIsLow()) {throw new cException(eNOMEM); }}
18 #define check_module_count(num, mod, parentmod) \
19 {if ((int)num<=0) {throw new cException("Negative or zero module vector size %s[%d] in compound module %s", ===>\<===
20 mod,(int)num,parentmod);}}
21 #define check_gate_count(num, mod, gate, parentmod) \
22 {if ((int)num<0) {throw new cException("Negative gate vector size %s.%s[%d] in compound module %s", ===>\<===
23 mod,gate,(int)num,parentmod);}}
24 #define check_loop_bounds(lower, upper, parentmod) \
25 {if ((int)lower<0) ===>\<===
26 {throw new cException("Bad loop bounds (%d..%d) in compound module %s", ===>\<===
27 (int)lower,(int)upper,parentmod);}}
28 #define check_module_index(index,modvar,modname,parentmod) \
29 {if (index<0 || index>=modvar[0]->size()) {throw new cException("Bad submodule index %s[%d] in compound module %s", ===>\<===
30 modname,(int)index,parentmod);}}
31 #define check_channel_params(delay, err, channel) \
32 {if ((double)delay<0.0) ===>\<===
33 {throw new cException("Negative delay value %lf in channel %s",(double)delay,channel);} ===>\<===
34 if ((double)err<0.0 || (double)err>1.0) ===>\<===
35 {throw new cException("Incorrect error value %lf in channel %s",(double)err,channel);}}
36 #define check_modtype(modtype, modname) \
37 {if ((modtype)==NULL) {throw new cException("Simple module type definition %s not found", ===>\<===
38 modname);}}
39 #define check_function(funcptr, funcname) \
40 {if ((funcptr)==NULL) {throw new cException("Function %s not found", ===>\<===
41 funcname);}}
42 #define check_function_retnull(funcptr, funcname) \
43 {if ((funcptr)==NULL) {throw new cException("Function %s not found", ===>\<===
44 funcname);return NULL;}}
45 #define check_gate(gateindex, modname, gatename) \
46 {if ((int)gateindex==-1) {throw new cException("Gate %s.%s not found",modname,gatename);}}
47 #define check_anc_param(ptr,parname,compoundmod) \
48 {if ((ptr)==NULL) {throw new cException("Unknown ancestor parameter named %s in compound module %s", ===>\<===
49 parname,compoundmod);}}
50 #define check_param(ptr,parname) \
51 {if ((ptr)==NULL) {throw new cException("Unknown parameter named %s", ===>\<===
52 parname);}}
53 #ifndef __cplusplus
54 # error Compile as C++!
55 #endif
56 #ifdef __BORLANDC__
57 # if !defined(__FLAT__) && !defined(__LARGE__)
58 # error Compile as 16-bit LARGE model or 32-bit DPMI!
59 # endif
60 #endif
61
62
63 #ifdef _MSC_VER
64 # pragma warning(disable:4101)
65 #endif
66 #ifdef __BORLANDC__
67 # pragma warn -waus
68 # pragma warn -wuse
69 #endif
70
71
72
73
74 #define NEDC_VERSION 0x0203
75 #if (NEDC_VERSION!=OMNETPP_VERSION)
76 # error Version mismatch! Probably this file was generated by an earlier version of nedc: 'make clean' should help.
77 #endif
78
79
80
81
82
83 ModuleInterface( Selector )
84 Machine( default )
85 Parameter( server_count, ParType_Numeric )
86 Gate( out[], GateDir_Output )
87 Gate( in, GateDir_Input )
88 EndInterface
89 Register_ModuleInterface( Selector )
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 ModuleInterface( ServiceNet )
107 Machine( default )
108 Parameter( server_count, ParType_Numeric ParType_Const )
109 Parameter( selector_type, ParType_String )
110 Parameter( generator_rate, ParType_Numeric ParType_Const )
111 Parameter( generator_rg, ParType_Numeric ParType_Const )
112 Parameter( sample_count, ParType_Numeric ParType_Const )
113 EndInterface
114 Register_ModuleInterface( ServiceNet )
115
116 class ServiceNet : public cCompoundModule
{
117 public:
118 ServiceNet(const char *name, cModule *parentmod) :
119 cCompoundModule(name, parentmod) {}
120 protected:
121 virtual void doBuildInside();
122 };
123
124 Define_Module( ServiceNet );
125
126 void ServiceNet::doBuildInside()
127 {
128 cModule *mod = this;
129
130 cPar value, *par;
131 cPar::ExprElem *expr_tab; int k;
132 cFunctionType *functype;
133 const char *type_name;
134 char b1[64], b2[64];
135 cArray machines;
136 bool islocal, do_this_block;
137 int n;
138
139
140
141 cModuleType *modtype = NULL;
142 long sub_nr = 0;
143 long sub_i = 0;
144
145
146 modtype = findModuleType( "Server" );
147 check_modtype( modtype, "Server" );
148 sub_nr = mod->par( "server_count" );
149 check_module_count(sub_nr,"server","ServiceNet");
150 cModule **server_mod = new cModule *[sub_nr];
151
152 for (sub_i=0; sub_i<sub_nr; sub_i++)
153 {
154
155 par = new cPar();
156 *par = mod->machinePar("default");
157 machines.add( par );
158 check_error(); check_memory();
159
160
161 islocal = simulation.netInterface()==NULL ||
162 simulation.netInterface()->isLocalMachineIn( machines );
163 server_mod[sub_i] = modtype->create( "server", mod, islocal);
164 check_error(); check_memory();
165
166 server_mod[sub_i]->setIndex(sub_i, sub_nr);
167
168
169 server_mod[sub_i]->setMachinePar( "default", ((cPar *)machines[0])->stringValue() );
170 check_error(); check_memory();
171
172
173 par = &(server_mod[sub_i]->par("address"));
174 check_param(par, "address");
175 *par = (long)sub_i;
176
177 n = server_mod[sub_i]->params();
178 for(k=0;k<n;k++)
179 if(server_mod[sub_i]->par(k).isInput())
180 {server_mod[sub_i]->par(k).read();check_error();}
181
182
183 server_mod[sub_i]->buildInside();
184 }
185 machines.clear();
186
187
188 par = &mod->par("selector_type");
189 check_param(par,"selector_type");
190 type_name = (const char *)par;
191 type_name = mod->par("selector_type");
192 modtype = findModuleType( type_name );
193 check_modtype( modtype, type_name );
194 cModule *selector_mod;
195
196
197 par = new cPar();
198 *par = mod->machinePar("default");
199 machines.add( par );
200 check_error(); check_memory();
201
202
203 islocal = simulation.netInterface()==NULL ||
204 simulation.netInterface()->isLocalMachineIn( machines );
205 selector_mod = modtype->create( "selector", mod, islocal);
206 check_error(); check_memory();
207
208
209 selector_mod->setMachinePar( "default", ((cPar *)machines[0])->stringValue() );
210 check_error(); check_memory();
211
212
213 par = &(selector_mod->par("server_count"));
214 check_param(par, "server_count");
215 *par = mod->par( "server_count" );
216
217 n = selector_mod->params();
218 for(k=0;k<n;k++)
219 if(selector_mod->par(k).isInput())
220 {selector_mod->par(k).read();check_error();}
221
222
223 n = mod->par( "server_count" );
224 check_gate_count( n, "selector","out","ServiceNet");
225 selector_mod->setGateSize("out", n );
226
227
228 selector_mod->buildInside();
229 machines.clear();
230
231
232 modtype = findModuleType( "Sink" );
233 check_modtype( modtype, "Sink" );
234 cModule *sink_mod;
235
236
237 par = new cPar();
238 *par = mod->machinePar("default");
239 machines.add( par );
240 check_error(); check_memory();
241
242
243 islocal = simulation.netInterface()==NULL ||
244 simulation.netInterface()->isLocalMachineIn( machines );
245 sink_mod = modtype->create( "sink", mod, islocal);
246 check_error(); check_memory();
247
248
249 sink_mod->setMachinePar( "default", ((cPar *)machines[0])->stringValue() );
250 check_error(); check_memory();
251
252
253 par = &(sink_mod->par("sample_count"));
254 check_param(par, "sample_count");
255 *par = mod->par( "sample_count" );
256
257 n = sink_mod->params();
258 for(k=0;k<n;k++)
259 if(sink_mod->par(k).isInput())
260 {sink_mod->par(k).read();check_error();}
261
262
263 n = mod->par( "server_count" );
264 check_gate_count( n, "sink","in","ServiceNet");
265 sink_mod->setGateSize("in", n );
266
267
268 sink_mod->buildInside();
269 machines.clear();
270
271
272 modtype = findModuleType( "Generator" );
273 check_modtype( modtype, "Generator" );
274 cModule *generator_mod;
275
276
277 par = new cPar();
278 *par = mod->machinePar("default");
279 machines.add( par );
280 check_error(); check_memory();
281
282
283 islocal = simulation.netInterface()==NULL ||
284 simulation.netInterface()->isLocalMachineIn( machines );
285 generator_mod = modtype->create( "generator", mod, islocal);
286 check_error(); check_memory();
287
288
289 generator_mod->setMachinePar( "default", ((cPar *)machines[0])->stringValue() );
290 check_error(); check_memory();
291
292
293 par = &(generator_mod->par("rate"));
294 check_param(par, "rate");
295 *par = mod->par( "generator_rate" );
296
297 par = &(generator_mod->par("random_generator"));
298 check_param(par, "random_generator");
299 *par = mod->par( "generator_rg" );
300
301 n = generator_mod->params();
302 for(k=0;k<n;k++)
303 if(generator_mod->par(k).isInput())
304 {generator_mod->par(k).read();check_error();}
305
306
307 generator_mod->buildInside();
308 machines.clear();
309
310
311 cLinkType *link_p;
312 cPar *delay_p, *error_p, *datarate_p;
313 int gateL, gateR;
314 long mod_nr_L, mod_nr_R, gate_nr_L, gate_nr_R;
315 {
316
317 long i_index;
318
319 long i_begin = 0;
320 expr_tab = new cPar::ExprElem[3]; k=0;
321 expr_tab[k++] = mod->par( "server_count" );
322 expr_tab[k++] = 1;
323 expr_tab[k++] = '-';
324 value.cancelRedirection();
325 value.setDoubleValue(expr_tab,k);
326 long i_end = value;
327 check_loop_bounds(i_begin, i_end, "ServiceNet");
328 for (i_index=i_begin; i_index<=i_end; i_index++)
329 {
330
331 do_this_block = true;
332 if (do_this_block)
333 {
334 gate_nr_L = i_index;
335 gateL = selector_mod->findGate( "out", gate_nr_L );
336 check_gate( gateL, "selector", indexedname(b1,"out",gate_nr_L) );
337
338 mod_nr_R = i_index;
339 check_module_index(mod_nr_R,server_mod,"server", "ServiceNet");
340 gateR = server_mod[mod_nr_R]->findGate( "in" );
341 check_gate( gateR, "server", "in" );
342
343 connect (selector_mod, gateL,
344 NULL,
345 server_mod[mod_nr_R], gateR);
346
347 check_error(); check_memory();
348 }
349
350
351 do_this_block = true;
352 if (do_this_block)
353 {
354 mod_nr_L = i_index;
355 check_module_index(mod_nr_L,server_mod,"server", "ServiceNet");
356 gateL = server_mod[mod_nr_L]->findGate( "out" );
357 check_gate( gateL, "server", "out" );
358
359 gate_nr_R = i_index;
360 gateR = sink_mod->findGate( "in", gate_nr_R );
361 check_gate( gateR, "sink", indexedname(b1,"in",gate_nr_R) );
362
363 connect (server_mod[mod_nr_L], gateL,
364 NULL,
365 sink_mod, gateR);
366
367 check_error(); check_memory();
368 }
369
370 }
371
372 }
373
374
375 do_this_block = true;
376 if (do_this_block)
377 {
378 gateL = generator_mod->findGate( "out" );
379 check_gate( gateL, "generator", "out" );
380
381 gateR = selector_mod->findGate( "in" );
382 check_gate( gateR, "selector", "in" );
383
384 connect (generator_mod, gateL,
385 NULL,
386 selector_mod, gateR);
387
388 check_error(); check_memory();
389 }
390
391 mod->checkInternalConnections();
392 delete[] server_mod;
393 check_error(); check_memory();
394 }
395
396 class servicenet : public cNetworkType
{
397 public:
398 servicenet(const char *name) : cNetworkType(name) {}
399 servicenet(const servicenet& n) {setName(n.name());operator=(n);}
400 virtual void setupNetwork();
401 };
402
403 Define_Network( servicenet );
404
405 void servicenet::setupNetwork()
406 {
407
408
409 cPar value, *par;
410 cPar::ExprElem *expr_tab; int k;
411 cFunctionType *functype;
412 const char *type_name;
413 char b1[64], b2[64];
414 cArray machines;
415 bool islocal, do_this_block;
416 int n;
417
418
419 par = new cPar();
420 *par = ev.getPhysicalMachineFor("default");
421 machines.add( par );
422 check_error(); check_memory();
423
424
425 cModuleType *modtype;
426 modtype = findModuleType( "ServiceNet" );
427 check_modtype( modtype, "ServiceNet" );
428 cModule *servicenet_mod;
429
430
431 islocal = simulation.netInterface()==NULL ||
432 simulation.netInterface()->isLocalMachineIn( machines );
433 if (!islocal)
434 throw new cException("Local machine `%s' is not among machines specified for this network",
435 simulation.netInterface()->localhost());
436 servicenet_mod = modtype->create( "servicenet", NULL, islocal);
437 check_error(); check_memory();
438
439
440 servicenet_mod->setMachinePar( "default", ev.getPhysicalMachineFor("default") );
441 check_error(); check_memory();
442
443 n = servicenet_mod->params();
444 for(k=0;k<n;k++)
445 if(servicenet_mod->par(k).isInput())
446 {servicenet_mod->par(k).read();check_error();}
447
448
449 servicenet_mod->buildInside();
450 machines.clear();
451
452 check_error(); check_memory();
453 }
454
455
456