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
90 Register_ModuleInterface( Selector )
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 ModuleInterface( ServiceNet )
108 Machine( default )
109 Parameter( server_count, ParType_Numeric ParType_Const )
110 Parameter( selector_type, ParType_String )
111 Parameter( generator_rate, ParType_Numeric ParType_Const )
112 Parameter( generator_rg, ParType_Numeric ParType_Const )
113 Parameter( sample_count, ParType_Numeric ParType_Const )
114 EndInterface
115
116 Register_ModuleInterface( ServiceNet )
117
118 class ServiceNet : public cCompoundModule
119 {
120 public:
121 ServiceNet(const char *name, cModule *parentmod) :
122 cCompoundModule(name, parentmod) {}
123 protected:
124 virtual void doBuildInside();
125 };
126
127 Define_Module( ServiceNet );
128
129 void ServiceNet::doBuildInside()
130 {
131 cModule *mod = this;
132
133 cPar value, *par;
134 cPar::ExprElem *expr_tab; int k;
135 cFunctionType *functype;
136 const char *type_name;
137 char b1[64], b2[64];
138 cArray machines;
139 bool islocal, do_this_block;
140 int n;
141
142
143
144 cModuleType *modtype = NULL;
145 long sub_nr = 0;
146 long sub_i = 0;
147
148
149 modtype = findModuleType( "Server" );
150 check_modtype( modtype, "Server" );
151 sub_nr = mod->par( "server_count" );
152 check_module_count(sub_nr,"server","ServiceNet");
153 cModule **server_mod = new cModule *[sub_nr];
154
155 for (sub_i=0; sub_i<sub_nr; sub_i++)
156 {
157
158 par = new cPar();
159 *par = mod->machinePar("default");
160 machines.add( par );
161 check_error(); check_memory();
162
163
164 islocal = simulation.netInterface()==NULL ||
165 simulation.netInterface()->isLocalMachineIn( machines );
166 server_mod[sub_i] = modtype->create( "server", mod, islocal);
167 check_error(); check_memory();
168
169 server_mod[sub_i]->setIndex(sub_i, sub_nr);
170
171
172 server_mod[sub_i]->setMachinePar( "default", ((cPar *)machines[0])->stringValue() );
173 check_error(); check_memory();
174
175
176 par = &(server_mod[sub_i]->par("address"));
177 check_param(par, "address");
178 *par = (long)sub_i;
179
180 n = server_mod[sub_i]->params();
181 for(k=0;k<n;k++)
182 if(server_mod[sub_i]->par(k).isInput())
183 {server_mod[sub_i]->par(k).read();check_error();}
184
185
186 server_mod[sub_i]->buildInside();
187 }
188 machines.clear();
189
190
191 par = &mod->par("selector_type");
192 check_param(par,"selector_type");
193 type_name = (const char *)par;
194 type_name = mod->par("selector_type");
195 modtype = findModuleType( type_name );
196 check_modtype( modtype, type_name );
197 cModule *selector_mod;
198
199
200 par = new cPar();
201 *par = mod->machinePar("default");
202 machines.add( par );
203 check_error(); check_memory();
204
205
206 islocal = simulation.netInterface()==NULL ||
207 simulation.netInterface()->isLocalMachineIn( machines );
208 selector_mod = modtype->create( "selector", mod, islocal);
209 check_error(); check_memory();
210
211
212 selector_mod->setMachinePar( "default", ((cPar *)machines[0])->stringValue() );
213 check_error(); check_memory();
214
215
216 par = &(selector_mod->par("server_count"));
217 check_param(par, "server_count");
218 *par = mod->par( "server_count" );
219
220 n = selector_mod->params();
221 for(k=0;k<n;k++)
222 if(selector_mod->par(k).isInput())
223 {selector_mod->par(k).read();check_error();}
224
225
226 n = mod->par( "server_count" );
227 check_gate_count( n, "selector","out","ServiceNet");
228 selector_mod->setGateSize("out", n );
229
230
231 selector_mod->buildInside();
232 machines.clear();
233
234
235 modtype = findModuleType( "Sink" );
236 check_modtype( modtype, "Sink" );
237 cModule *sink_mod;
238
239
240 par = new cPar();
241 *par = mod->machinePar("default");
242 machines.add( par );
243 check_error(); check_memory();
244
245
246 islocal = simulation.netInterface()==NULL ||
247 simulation.netInterface()->isLocalMachineIn( machines );
248 sink_mod = modtype->create( "sink", mod, islocal);
249 check_error(); check_memory();
250
251
252 sink_mod->setMachinePar( "default", ((cPar *)machines[0])->stringValue() );
253 check_error(); check_memory();
254
255
256 par = &(sink_mod->par("sample_count"));
257 check_param(par, "sample_count");
258 *par = mod->par( "sample_count" );
259
260 n = sink_mod->params();
261 for(k=0;k<n;k++)
262 if(sink_mod->par(k).isInput())
263 {sink_mod->par(k).read();check_error();}
264
265
266 n = mod->par( "server_count" );
267 check_gate_count( n, "sink","in","ServiceNet");
268 sink_mod->setGateSize("in", n );
269
270
271 sink_mod->buildInside();
272 machines.clear();
273
274
275 modtype = findModuleType( "Generator" );
276 check_modtype( modtype, "Generator" );
277 cModule *generator_mod;
278
279
280 par = new cPar();
281 *par = mod->machinePar("default");
282 machines.add( par );
283 check_error(); check_memory();
284
285
286 islocal = simulation.netInterface()==NULL ||
287 simulation.netInterface()->isLocalMachineIn( machines );
288 generator_mod = modtype->create( "generator", mod, islocal);
289 check_error(); check_memory();
290
291
292 generator_mod->setMachinePar( "default", ((cPar *)machines[0])->stringValue() );
293 check_error(); check_memory();
294
295
296 par = &(generator_mod->par("rate"));
297 check_param(par, "rate");
298 *par = mod->par( "generator_rate" );
299
300 par = &(generator_mod->par("random_generator"));
301 check_param(par, "random_generator");
302 *par = mod->par( "generator_rg" );
303
304 n = generator_mod->params();
305 for(k=0;k<n;k++)
306 if(generator_mod->par(k).isInput())
307 {generator_mod->par(k).read();check_error();}
308
309
310 generator_mod->buildInside();
311 machines.clear();
312
313
314 cLinkType *link_p;
315 cPar *delay_p, *error_p, *datarate_p;
316 int gateL, gateR;
317 long mod_nr_L, mod_nr_R, gate_nr_L, gate_nr_R;
318 {
319
320 long i_index;
321
322 long i_begin = 0;
323 expr_tab = new cPar::ExprElem[3]; k=0;
324 expr_tab[k++] = mod->par( "server_count" );
325 expr_tab[k++] = 1;
326 expr_tab[k++] = '-';
327 value.cancelRedirection();
328 value.setDoubleValue(expr_tab,k);
329 long i_end = value;
330 check_loop_bounds(i_begin, i_end, "ServiceNet");
331 for (i_index=i_begin; i_index<=i_end; i_index++)
332 {
333
334 do_this_block = true;
335 if (do_this_block)
336 {
337 gate_nr_L = i_index;
338 gateL = selector_mod->findGate( "out", gate_nr_L );
339 check_gate( gateL, "selector", indexedname(b1,"out",gate_nr_L) );
340
341 mod_nr_R = i_index;
342 check_module_index(mod_nr_R,server_mod,"server", "ServiceNet");
343 gateR = server_mod[mod_nr_R]->findGate( "in" );
344 check_gate( gateR, "server", "in" );
345
346 connect (selector_mod, gateL,
347 NULL,
348 server_mod[mod_nr_R], gateR);
349
350 check_error(); check_memory();
351 }
352
353
354 do_this_block = true;
355 if (do_this_block)
356 {
357 mod_nr_L = i_index;
358 check_module_index(mod_nr_L,server_mod,"server", "ServiceNet");
359 gateL = server_mod[mod_nr_L]->findGate( "out" );
360 check_gate( gateL, "server", "out" );
361
362 gate_nr_R = i_index;
363 gateR = sink_mod->findGate( "in", gate_nr_R );
364 check_gate( gateR, "sink", indexedname(b1,"in",gate_nr_R) );
365
366 connect (server_mod[mod_nr_L], gateL,
367 NULL,
368 sink_mod, gateR);
369
370 check_error(); check_memory();
371 }
372
373 }
374
375 }
376
377
378 do_this_block = true;
379 if (do_this_block)
380 {
381 gateL = generator_mod->findGate( "out" );
382 check_gate( gateL, "generator", "out" );
383
384 gateR = selector_mod->findGate( "in" );
385 check_gate( gateR, "selector", "in" );
386
387 connect (generator_mod, gateL,
388 NULL,
389 selector_mod, gateR);
390
391 check_error(); check_memory();
392 }
393
394 mod->checkInternalConnections();
395 delete[] server_mod;
396 check_error(); check_memory();
397 }
398
399 class servicenet : public cNetworkType
400 {
401 public:
402 servicenet(const char *name) : cNetworkType(name) {}
403 servicenet(const servicenet& n) {setName(n.name());operator=(n);}
404 virtual void setupNetwork();
405 };
406
407 Define_Network( servicenet );
408
409 void servicenet::setupNetwork()
410 {
411
412
413 cPar value, *par;
414 cPar::ExprElem *expr_tab; int k;
415 cFunctionType *functype;
416 const char *type_name;
417 char b1[64], b2[64];
418 cArray machines;
419 bool islocal, do_this_block;
420 int n;
421
422
423 par = new cPar();
424 *par = ev.getPhysicalMachineFor("default");
425 machines.add( par );
426 check_error(); check_memory();
427
428
429 cModuleType *modtype;
430 modtype = findModuleType( "ServiceNet" );
431 check_modtype( modtype, "ServiceNet" );
432 cModule *servicenet_mod;
433
434
435 islocal = simulation.netInterface()==NULL ||
436 simulation.netInterface()->isLocalMachineIn( machines );
437 if (!islocal)
438 throw new cException("Local machine `%s' is not among machines specified for this network",
439 simulation.netInterface()->localhost());
440 servicenet_mod = modtype->create( "servicenet", NULL, islocal);
441 check_error(); check_memory();
442
443
444 servicenet_mod->setMachinePar( "default", ev.getPhysicalMachineFor("default") );
445 check_error(); check_memory();
446
447 n = servicenet_mod->params();
448 for(k=0;k<n;k++)
449 if(servicenet_mod->par(k).isInput())
450 {servicenet_mod->par(k).read();check_error();}
451
452
453 servicenet_mod->buildInside();
454 machines.clear();
455
456 check_error(); check_memory();
457 }
458
459