-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim_manager.cpp
More file actions
542 lines (503 loc) · 15 KB
/
Copy pathsim_manager.cpp
File metadata and controls
542 lines (503 loc) · 15 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#include "ace/FILE_Addr.h"
#include "ace/FILE_Connector.h"
#include "ace/FILE_IO.h"
#include "log_msg.h"
#include "sim_manager.h"
#include "sim_handler.h"
#include "sim_service.h"
#include "tinyxml.h"
SimManager::SimManager()
{
MY_DEBUG(ACE_TEXT("SimManager::SimManager()\n"));
}
SimManager::~SimManager()
{
fini();
MY_DEBUG(ACE_TEXT("SimManager::~SimManager()\n"));
}
int SimManager::fini( void )
{
MY_DEBUG(ACE_TEXT("SimManager::fini\n"));
_acceptor_map.unbind_all();
deleteAllSimHandlerSet();
deleteAllSimMaps();
deleteAllSimNEs();
return 0;
}
int SimManager::deleteAllSimHandlerSet()
{
if(_handler_map.current_size()>0)
{
for( SimHandler_Map::iterator
iter = _handler_map.begin ();
iter!= _handler_map.end ();
iter++)
{
// MY_DEBUG ("try to delete handler_set, key=%s\n",(*iter).ext_id_.c_str());
SimHandlerSet* handler_set = (*iter).int_id_;
if(handler_set)
{
MY_DEBUG ("delete handler_set, key=%s\n",(*iter).ext_id_.c_str());
delete handler_set;
handler_set = 0;
}
}
}
_handler_map.unbind_all();
return 0;
}
int SimManager::deleteAllSimMaps()
{
if(_map_map.current_size()>0)
{
for( SimMap_Map::iterator
iter = _map_map.begin ();
iter!= _map_map.end ();
iter++)
{
MY_DEBUG ("try to delete sim_map, file=%s\n",(*iter).ext_id_.c_str());
SimMap* sim_map = (*iter).int_id_;
if(sim_map)
{
MY_DEBUG ("delete sim_map, file=%s\n",(*iter).ext_id_.c_str());
delete sim_map;
sim_map = 0;
}
}
}
_map_map.unbind_all();
return 0;
}
int SimManager::deleteAllSimNEs(void)
{
if(_ne_map.current_size()>0)
{
for( SimNE_Map::iterator
iter = _ne_map.begin ();
iter!= _ne_map.end ();
iter++)
{
// MY_DEBUG ("try to delete NE, tid=%s\n",(*iter).ext_id_.c_str());
SimNE* ne = (*iter).int_id_;
if(ne)
{
MY_DEBUG ("delete NE, tid=%s\n",(*iter).ext_id_.c_str());
delete ne;
ne = 0;
}
}
}
_ne_map.unbind_all();
return 0;
}
int SimManager::addSimHandler(const ACE_CString& key, SimHandler *handler)
{
MY_DEBUG(ACE_TEXT("SimManager::addSimHandler, key=%s\n"), key.c_str());
SimHandlerSet* handler_set;
if(_handler_map.find( key, handler_set )==-1)
{
handler_set = new SimHandlerSet;
handler_set->insert(handler);
return _handler_map.bind(key, handler_set);
}
else
{
handler_set->insert(handler);
return 0;
}
}
int SimManager::addSimAcceptor(const ACE_CString& key, SimAcceptor *acceptor)
{
MY_DEBUG(ACE_TEXT("SimManager::addSimAcceptor, key=%s\n"), key.c_str());
return _acceptor_map.bind( key, acceptor );
}
int SimManager::removeSimHandler(const ACE_CString& key, SimHandler *handler)
{
MY_DEBUG(ACE_TEXT("SimManager::removeSimHandler, key=%s\n"), key.c_str());
SimHandlerSet* handler_set;
if(_handler_map.find( key, handler_set )==-1)
{
return -1;
}
else if(handler_set)
{
handler_set->remove(handler);
if(handler_set->is_empty())
{
// MY_DEBUG ("remove handler_set, key=%s\n",key.c_str());
delete handler_set;
handler_set = 0;
_handler_map.unbind(key);
}
}
return 0;
}
int SimManager::removeSimAcceptor( const ACE_CString& key, SimAcceptor *acceptor )
{
MY_DEBUG(ACE_TEXT("SimManager::removeSimAcceptor, key=%s\n"), key.c_str());
if(acceptor)
{
_acceptor_map.unbind( key, acceptor );
}
else
{
_acceptor_map.unbind( key );
}
return 0;
}
SimHandlerSet* SimManager::getSimHandlerSet(const ACE_CString& key)
{
SimHandlerSet* handler_set = 0;
if(_handler_map.find(key, handler_set)==-1)
{
MY_DEBUG("Cannot find handler list for %s\n",key.c_str());
}
return handler_set;
}
int SimManager::findSimHandler(const ACE_CString& key, SimHandler* handler)
{
SimHandlerSet* handler_set = 0;
if(_handler_map.find(key, handler_set)==-1)
{
return -1;
}
if(handler_set)
{
if( handler_set->find(handler) != -1 ) ;
{
return 0;
}
}
return -1;
}
SimAcceptor* SimManager::getSimAccetpor(const ACE_CString& key)
{
SimAcceptor* accetpor = 0;
if(_acceptor_map.find(key, accetpor)==-1)
{
MY_DEBUG("Cannot find accetpor for %s\n",key.c_str());
}
return accetpor;
}
SimMap* SimManager::getSimMap(const ACE_CString& file)
{
SimMap* sim_map = 0;
if(_map_map.find(file, sim_map)==-1)
{
MY_DEBUG("Cannot find sim_map for %s\n",file.c_str());
}
return sim_map;
}
int SimManager::createSimMap(const ACE_CString& file )
{
MY_DEBUG("create sim_map, file=%s\n", file.c_str());
SimMap* sim_map = 0;
if(_map_map.find(file, sim_map)!=-1)
{
if(sim_map)
{
MY_DEBUG("sim_map already exists, file=%s\n",file.c_str());
sim_map->increaseCnt();
}
return 0;
}
ACE_NEW_RETURN(sim_map, SimMap(file), -1);
if( sim_map->init()==-1 )
{
return -1;
}
else
{
_map_map.bind( file, sim_map );
sim_map->increaseCnt();
}
return 0;
}
int SimManager::deleteSimMap(const ACE_CString& file )
{
MY_DEBUG("delete sim_map, file=%s\n",file.c_str());
SimMap* sim_map = 0;
if( _map_map.find(file, sim_map)<0 )
{
MY_DEBUG("Cannot find sim_map, file=%s\n",file.c_str());
return -1;
}
if(sim_map)
{
sim_map->decreaseCnt();
if( sim_map->tryDelete() == 0 )
{
_map_map.unbind( file, sim_map );
}
return 0;
}
else
{
_map_map.unbind( file );
}
return 0;
}
SimNE::SimNE(const ACE_CString& tid, const ACE_CString& type,
const ACE_CString& gne, const ACE_CString& conf,
int port, SimManager* manager, const ACE_CString& tidIpMap )
:_tid(tid),_type(type),_gne(gne),_conf(conf),_port(port),
_sim_map(0),_manager(manager),_tidIpMap(tidIpMap)
{
MY_DEBUG("SimNE::SimNE(), tid=%s\n",_tid.c_str());
}
SimNE::~SimNE()
{
MY_DEBUG("SimNE::~SimNE(), tid=%s\n",_tid.c_str());
}
SimMap* SimNE::getSimMap()
{
if(_sim_map==NULL)
{
_sim_map = this->getManager()->getSimMap(_conf);
}
return _sim_map;
}
int SimManager::createSimNE(const ACE_CString& tid, const ACE_CString& type,
const ACE_CString& gne, const ACE_CString& conf,
int port, const ACE_CString& tidIpMap)
{
MY_DEBUG("create NE, tid=%s\n", tid.c_str());
SimNE* ne = 0;
ACE_NEW_RETURN(ne, SimNE(tid,type,gne,conf,port,this, tidIpMap), -1);
_ne_map.bind( tid, ne );
return 0;
}
int SimManager::deleteSimNE(const ACE_CString& tid )
{
MY_DEBUG("delete NE, tid=%s\n",tid.c_str());
SimNE* ne = 0;
if(_ne_map.find(tid, ne)==-1)
{
MY_DEBUG("Cannot find NE, tid=%s\n",tid.c_str());
return -1;
}
if(ne)
{
_ne_map.unbind( tid, ne );
delete ne;
ne = 0;
return 0;
}
else
{
_ne_map.unbind( tid );
}
return 0;
}
SimNE* SimManager::getSimNE(const ACE_CString& tid)
{
SimNE* ne = 0;
if(_ne_map.find(tid, ne)==-1)
{
MY_DEBUG("Cannot find NE, tid=%s\n",tid.c_str());
}
return ne;
}
int SimManager::getTidAndConfList(const ACE_CString& tid,
std::list<ACE_CString>& tidList, std::list<ACE_CString>& confList)
{
for( SimNE_Map::iterator
iter = _ne_map.begin ();
iter!= _ne_map.end ();
iter++)
{
SimNE* ne = (*iter).int_id_;
if( ne && ne->_gne==tid )
{
tidList.push_back(ne->_tid);
confList.push_back(ne->_conf);
}
}
return 0;
}
int SimManager::startAllNE(const ACE_TCHAR* conf)
{
ACE_FILE_Connector connector;
ACE_FILE_Info fileinfo;
ACE_FILE_IO file;
ACE_CString name = conf;
ACE_FILE_Addr fileaddr(name.c_str());
if (connector.connect (file, fileaddr) == -1)
{
MY_ERROR(ACE_TEXT("cannot open file: %s\n"),name.c_str());
}
else
{
MY_DEBUG(ACE_TEXT("open file: %s\n"),name.c_str());
if (file.get_info (&fileinfo) == -1)
{
MY_ERROR(ACE_TEXT("cannot get fileinfo of %s\n"), name.c_str());
return -1;
}
else
{
// MY_DEBUG(ACE_TEXT("file size = %u\n"),fileinfo.size_);
}
ACE_TCHAR *buf = new ACE_TCHAR[fileinfo.size_];
u_long rbytes;
if ((rbytes = file.recv_n (buf, fileinfo.size_ )) != fileinfo.size_)
{
MY_ERROR(ACE_TEXT("cannot read file %s correctly!\n"), name.c_str());
return -1;
}
TiXmlDocument doc;
doc.Parse( buf );
delete[] buf;
if (true == doc.Error())
{
MY_ERROR(ACE_TEXT("XML parse error!!! file: %s, row: %d, col: %d, error: %s\n"),
name.c_str(),
doc.ErrorRow(),
doc.ErrorCol(),
doc.ErrorDesc());
return -1;
}
for( TiXmlElement *neElement =
doc.FirstChildElement( "CONFIGURATION" )->FirstChildElement("NE");
neElement;
neElement = neElement->NextSiblingElement("NE") )
{
ACE_CString ne_tid, ne_conf, ne_type;
int ne_port=0;
TiXmlElement* tidElement = neElement->FirstChildElement( "tid" );
if(tidElement)
{
ne_tid = tidElement->GetText();
MY_DEBUG(ACE_TEXT("tid is %s\n"), ne_tid.c_str());
}
TiXmlElement* portElement = neElement->FirstChildElement( "port" );
if(portElement)
{
ACE_CString s_port = portElement->GetText();
ne_port = ACE_OS::atoi(s_port.c_str());
MY_DEBUG(ACE_TEXT("port is %d\n"), ne_port);
}
TiXmlElement* confElement = neElement->FirstChildElement( "conf" );
if(confElement)
{
ne_conf = confElement->GetText();
MY_DEBUG(ACE_TEXT("conf is %s\n"), ne_conf.c_str());
}
TiXmlElement* typeElement = neElement->FirstChildElement( "type" );
if(typeElement)
{
ne_type = typeElement->GetText();
MY_DEBUG(ACE_TEXT("type is %s\n"), ne_type.c_str());
}
if( ne_type=="standalone" || ne_type=="gne" )
{
if( ne_tid.empty() )
{
MY_ERROR("missing attribute \"tid\"\n" );
return -1;
}
if( ne_conf.empty() )
{
MY_ERROR("missing attribute \"conf\"\n" );
return -1;
}
if( ne_port==0 )
{
MY_ERROR("missing attribute \"port\"\n" );
return -1;
}
if( ne_type=="standalone" &&
startNE(ne_tid, ne_type, ne_tid, ne_conf, ne_port )==-1 )
{
MY_ERROR("start NE %s failed! port=%d, conf=%s\n",
ne_tid.c_str(), ne_port, ne_conf.c_str() );
return -1;
}
}
else
{
MY_ERROR("NE type error! type = %s\n", ne_type.c_str() );
return -1;
}
if( ne_type=="gne" )
{
ACE_CString tidIpMap;
for( TiXmlElement *rneElement =
neElement->FirstChildElement("rne");
rneElement;
rneElement = rneElement->NextSiblingElement("rne") )
{
ACE_CString rne_tid, rne_conf;
TiXmlElement* rneTidElement = rneElement->FirstChildElement( "tid" );
if(rneTidElement)
{
rne_tid = rneTidElement->GetText();
MY_DEBUG(ACE_TEXT("rne tid is %s\n"), rne_tid.c_str());
}
TiXmlElement* rneConfElement = rneElement->FirstChildElement( "conf" );
if(rneConfElement)
{
rne_conf = rneConfElement->GetText();
MY_DEBUG(ACE_TEXT("rne conf is %s\n"), rne_conf.c_str());
}
if(rne_tid.empty())
{
MY_ERROR("missing attribute \"rnc tid\"\n" );
return -1;
}
if(rne_conf.empty())
{
MY_ERROR("missing attribute \"rnc conf\"\n" );
return -1;
}
if( startNE(rne_tid, "rne", ne_tid, rne_conf, ne_port )==-1 )
{
MY_ERROR("start RNE %s failed! tid=%s, conf=%s\n",
rne_tid.c_str(), rne_conf.c_str() );
return -1;
}
tidIpMap += "\n\"::RNETID=\\\"";
tidIpMap += rne_tid;
tidIpMap += "\\\",RNEIP=0.0.0.1,MODE=DYNAMIC\"";
}
if( startNE(ne_tid, ne_type, ne_tid, ne_conf, ne_port, tidIpMap )==-1 )
{
MY_ERROR("start GNE %s failed! port=%d, conf=%s\n",
ne_tid.c_str(), ne_port, ne_conf.c_str() );
return -1;
}
}
}
}
return 0;
}
int SimManager::startNE( const ACE_CString& tid, const ACE_CString& type,
const ACE_CString& gne, const ACE_CString& conf,
int port, const ACE_CString& tidIpMap)
{
if(type!="rne")
{
ACE_INET_Addr local_addr;
local_addr.set_port_number(port);
SimAcceptor* acceptor;
ACE_NEW_RETURN(acceptor, SimAcceptor(tid, port), -1);
if( acceptor->open(local_addr) == -1 )
{
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("SimService::startNE")),
-1);
}
else
{
MY_NOTICE(ACE_TEXT("NE %s is listening on IP: %s, Port: %d ...\n"),
tid.c_str(),
local_addr.get_host_addr(),
local_addr.get_port_number());
this->addSimAcceptor(tid, acceptor);
}
}
this->createSimMap(conf);
this->createSimNE(tid,type,gne,conf,port,tidIpMap);
return 0;
}