This repository has been archived by the owner on Jul 8, 2022. It is now read-only.
/
utils.cpp
3288 lines (2800 loc) · 79.5 KB
/
utils.cpp
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//+===================================================================================================================
//
// file : Tango_utils.cpp
//
// description : C++ source for all the utilities used by Tango device server and mainly for the Tango class
//
// project : TANGO
//
// author(s) : A.Gotz + E.Taurel
//
// Copyright (C) : 2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015
// European Synchrotron Radiation Facility
// BP 220, Grenoble 38043
// FRANCE
//
// This file is part of Tango.
//
// Tango is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Tango is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along with Tango.
// If not, see <http://www.gnu.org/licenses/>.
//
//
//-==================================================================================================================
#if HAVE_CONFIG_H
#include <ac_config.h>
#endif
#include <tango.h>
#include <omniORB4/internal/orbOptions.h>
#include <stdlib.h>
#include <dserversignal.h>
#include <dserverclass.h>
#include <eventsupplier.h>
#ifndef _TG_WINDOWS_
#include <unistd.h>
#include <assert.h>
#include <sys/time.h>
#include <netdb.h>
#include <sys/socket.h>
#else
#include <sys/timeb.h>
#include <process.h>
#include <coutbuf.h>
#include <ntservice.h>
#include <ws2tcpip.h>
#endif /* _TG_WINDOWS_ */
#include <omniORB4/omniInterceptors.h>
omni_thread::key_t key_py_data;
namespace Tango
{
Util *Util::_instance = NULL;
int Util::_tracelevel = 0;
bool Util::_UseDb = true;
bool Util::_FileDb = false;
bool Util::_daemon = false;
long Util::_sleep_between_connect = 60;
bool Util::_constructed = false;
#ifdef _TG_WINDOWS_
bool Util::_win = false;
bool Util::_service = false;
#endif
//
// A global key used for per thread specific storage. This is used to retrieve
// client host and stores it in the device blackbox. This global is referenced
// in blackbox.cpp
//
omni_thread::key_t key;
//+-------------------------------------------------------------------------------------------------------------------
//
// method :
// Util::init()
//
// description :
// static method to create/retrieve the Tango object. This method is the only one which enables a user to
// create the object
//
// arguments :
// in :
// - argc : The command line argument number
// - argv : The command line argument
//
//------------------------------------------------------------------------------------------------------------------
Util *Util::init(int argc,char *argv[])
{
if (_instance == NULL)
{
_instance = new Util(argc,argv);
}
return _instance;
}
#ifdef _TG_WINDOWS_
Util *Util::init(HINSTANCE hi,int nCmd)
{
if (_instance == NULL)
{
_instance = new Util(hi,nCmd);
}
return _instance;
}
#endif
//+------------------------------------------------------------------------------------------------------------------
//
// method :
// Util::instance()
//
// description :
// static method to retrieve the Tango object once it has been initialised
//
//-------------------------------------------------------------------------------------------------------------------
Util *Util::instance(bool exit)
{
if (_instance == NULL)
{
if (exit == true)
Util::print_err_message("Tango is not initialised !!!\nExiting");
else
{
Except::throw_exception((const char*)"API_UtilSingletonNotCreated",
(const char*)"Util singleton not created",
(const char*)"Util::instance");
}
}
return _instance;
}
//+------------------------------------------------------------------------------------------------------------------
//
// method :
// Util::Util()
//
// description :
// Constructor of the Tango class.
//
//-----------------------------------------------------------------------------
#ifdef _TG_WINDOWS
Util::Util(int argc,char *argv[]):cl_list_ptr(NULL),mon("Windows startup"),ext(new UtilExt),
heartbeat_th(NULL),heartbeat_th_id(0),poll_mon("utils_poll"),poll_on(false),ser_model(BY_DEVICE),
only_one("process"),nd_event_supplier(NULL),py_interp(NULL),py_ds(false),py_dbg(false),
db_cache(NULL),inter(NULL),svr_starting(true),svr_stopping(false),poll_pool_size(ULONG_MAX),
conf_needs_db_upd(false),ev_loop_func(NULL),shutdown_server(false),_dummy_thread(false),
zmq_event_supplier(NULL),endpoint_specified(false),user_pub_hwm(-1),wattr_nan_allowed(false),
polling_bef_9_def(false)
# ifndef TANGO_HAS_LOG4TANGO
,cout_tmp(cout.rdbuf())
# endif
#else
Util::Util(int argc,char *argv[]):cl_list_ptr(NULL),ext(new UtilExt),
heartbeat_th(NULL),heartbeat_th_id(0),poll_mon("utils_poll"),poll_on(false),ser_model(BY_DEVICE),
only_one("process"),nd_event_supplier(NULL),py_interp(NULL),py_ds(false),py_dbg(false),
db_cache(NULL),inter(NULL),svr_starting(true),svr_stopping(false),poll_pool_size(ULONG_MAX),
conf_needs_db_upd(false),ev_loop_func(NULL),shutdown_server(false),_dummy_thread(false),
zmq_event_supplier(NULL),endpoint_specified(false),user_pub_hwm(-1),wattr_nan_allowed(false),
polling_bef_9_def(false)
# ifndef TANGO_HAS_LOG4TANGO
,cout_tmp(cout.rdbuf())
# endif
#endif
{
shared_data.cmd_pending=false;
shared_data.trigger=false;
cr_py_lock = new CreatePyLock();
//
// Do the job
//
effective_job(argc,argv);
_constructed = true;
//
// For Windows, install a console handler to ignore LOGOFF event
//
#ifdef _TG_WINDOWS_
install_cons_handler();
#endif
}
void Util::effective_job(int argc,char *argv[])
{
try
{
//
// Check server option
//
if (argc < 2)
{
print_usage(argv[0]);
}
//
// Manage command line option (personal name and -v option)
//
check_args(argc,argv);
//
// Create the signal object
// It is necessary to create this object before the ORB is initialised.
// Otherwise, threads created by thread started by the ORB_init will not have
// the correct signal mask (set by the DServerSignal object) and the device
// server signal feature will not work
//
DServerSignal::instance();
//
// Check if the user specified a endPoint on the command line or using one
// env. variable
// If true, extract the IP address from the end point and store it
// for future use in the ZMQ publiher(s)
//
check_end_point_specified(argc,argv);
//
// Destroy the ORB created as a client (in case there is one)
// Also destroy database objsect stored in the ApiUtil object. This is needed in case of CS running TAC
// because the TAC device is stored in the db object and it references the destroyed ORB
//
ApiUtil *au = Tango::ApiUtil::instance();
CORBA::ORB_ptr orb_clnt = au->get_orb();
if (CORBA::is_nil(orb_clnt) == false)
{
orb_clnt->destroy();
CORBA::release(orb_clnt);
au->set_orb(CORBA::ORB::_nil());
size_t nb_db = au->get_db_vect().size();
for (size_t ctr = 0;ctr < nb_db;ctr++)
delete au->get_db_vect()[ctr];
au->get_db_vect().clear();
}
//
// Initialise CORBA ORB
//
#ifdef _TG_WINDOWS_
WORD rel = MAKEWORD(2,2);
WSADATA dat;
int err = WSAStartup(rel,&dat);
if (err != 0)
{
std::stringstream ss;
ss << "WSAStartup function failed with error " << err;
print_err_message(ss.str());
}
if (LOBYTE(dat.wVersion) != 2 || HIBYTE(dat.wVersion) != 2)
{
WSACleanup();
print_err_message("Could not find a usable version of Winsock.dll");
}
#endif
//
// Starting with omniORB 4.2, we need to add the throwTransientOnTimeout option for compatibility
//
bool omni_42_compat = false;
CORBA::ULong omni_vers_hex = omniORB::versionHex();
if (omni_vers_hex > 0x04020000)
omni_42_compat = true;
if (get_endpoint_specified() == true)
{
const char *options[][2] = {
{"clientCallTimeOutPeriod",CLNT_TIMEOUT_STR},
{"serverCallTimeOutPeriod","5000"},
{"maxServerThreadPoolSize","100"},
{"threadPerConnectionUpperLimit","55"},
{"threadPerConnectionLowerLimit","50"},
{"supportCurrent","0"},
{"verifyObjectExistsAndType","0"},
{"maxGIOPConnectionPerServer",MAX_GIOP_PER_SERVER},
{"giopMaxMsgSize",MAX_TRANSFER_SIZE},
#ifndef _TG_WINDOWS_
{"endPoint","giop:unix:"},
#endif
{"throwTransientOnTimeOut","1"},
{0,0}
};
if (omni_42_compat == false)
{
int nb_opt = sizeof(options) / sizeof (char *[2]);
options[nb_opt - 2][0] = NULL;
options[nb_opt - 2][1] = NULL;
}
orb = CORBA::ORB_init(argc,argv,"omniORB4",options);
}
else
{
const char *options[][2] = {
{"endPointPublish","all(addr)"},
{"clientCallTimeOutPeriod",CLNT_TIMEOUT_STR},
{"serverCallTimeOutPeriod","5000"},
{"maxServerThreadPoolSize","100"},
{"threadPerConnectionUpperLimit","55"},
{"threadPerConnectionLowerLimit","50"},
{"supportCurrent","0"},
{"verifyObjectExistsAndType","0"},
{"maxGIOPConnectionPerServer",MAX_GIOP_PER_SERVER},
{"giopMaxMsgSize",MAX_TRANSFER_SIZE},
#ifndef _TG_WINDOWS_
{"endPoint","giop:tcp::"},
{"endPoint","giop:unix:"},
#endif
{"throwTransientOnTimeOut","1"},
{0,0}
};
if (omni_42_compat == false)
{
int nb_opt = sizeof(options) / sizeof (char *[2]);
options[nb_opt - 2][0] = NULL;
options[nb_opt - 2][1] = NULL;
}
orb = CORBA::ORB_init(argc,argv,"omniORB4",options);
}
#ifndef TANGO_HAS_LOG4TANGO
//
// Initialize trace output (For Windows, the stdc++ lib also implements the
// new iostreams where the xx_with_assign classes are not defined. Therefore,
// to copy streams, I have used the advices in the C++ report of June 1997
//
trace_output = InitialOutput;
file_stream = NULL;
cout_tmp.copyfmt(cout);
cout_tmp.clear(cout.rdstate());
#endif // TANGO_HAS_LOG4TANGO
//
// Init host name
//
init_host_name();
//
// Connect to the database
//
if (_UseDb == true)
{
connect_db();
//
// Display help message if requested by user. This is done after the process is
// connected to the database becaue a call to the database server is done in the
// display_help_message() method
//
if (display_help == true)
{
display_help_message();
}
}
else
{
db = NULL;
ApiUtil *au = ApiUtil::instance();
au->in_server(true);
}
//
// Create the server CORBA objects
//
create_CORBA_objects();
#ifdef TANGO_HAS_LOG4TANGO
//
// Initialize logging stuffs
//
Logging::init(ds_name, (int)_tracelevel, ((!_FileDb) && _UseDb), *db, this);
#endif
cout4 << "Connected to database" << std::endl;
if (get_db_cache() == NULL)
cout4 << "DbServerCache unavailable, will call db..." << std::endl;
//
// Check if the server is not already running somewhere else
//
if ((_UseDb == true) && (_FileDb == false))
server_already_running();
//
// Get process PID and Tango version
//
misc_init();
//
// Automatically create the EventSupplier objects
//
// In the future this could be created only when the
// first event is fired ...
//
create_notifd_event_supplier();
create_zmq_event_supplier();
//
// Create the heartbeat thread and start it
//
heartbeat_th = new PollThread(shared_data,poll_mon,true);
heartbeat_th->start();
heartbeat_th_id = heartbeat_th->id();
cout4 << "Heartbeat thread Id = " << heartbeat_th_id << std::endl;
cout4 << "Tango object singleton constructed" << std::endl;
}
catch (CORBA::Exception &)
{
throw;
}
}
//+-------------------------------------------------------------------------------------------------------------------
//
// method :
// Util::create_CORBA_objects()
//
// description :
// Create some CORBA objects needed later-on
//
//-------------------------------------------------------------------------------------------------------------------
void Util::create_CORBA_objects()
{
//
// Install an omniORB interceptors to store client name in blackbox and allocate a key for per thread specific storage
//
omni::omniInterceptors *intercep = omniORB::getInterceptors();
intercep->serverReceiveRequest.add(get_client_addr);
intercep->createThread.add(create_PyPerThData);
key = omni_thread::allocate_key();
key_py_data = omni_thread::allocate_key();
//
// Get some CORBA object references
//
CORBA::Object_var poaObj = orb->resolve_initial_references("RootPOA");
PortableServer::POA_var root_poa = PortableServer::POA::_narrow(poaObj);
//
// If the database is not used, we must used the omniINSPOA poa in both cases of database on file or nodb
// remember that when you have database on file you have _UseDb == true and _FileDb == true
//
PortableServer::POA_var nodb_poa;
if ((_UseDb == false) || (_FileDb == true))
{
CORBA::Object_var poaInsObj = orb->resolve_initial_references("omniINSPOA");
nodb_poa = PortableServer::POA::_narrow(poaInsObj);
}
//
// Store POA. This is the same test but inverted
//
if ((_UseDb == true) && (_FileDb == false))
_poa = PortableServer::POA::_duplicate(root_poa);
else if ((_UseDb == false) || (_FileDb == true))
_poa = PortableServer::POA::_duplicate(nodb_poa);
}
#ifdef _TG_WINDOWS_
//+-----------------------------------------------------------------------------------------------------------------
//
// method :
// Util::Util()
//
// description :
// Constructor of the Tango class when used in a non-console Windows device server. On top of the UNIX way of
// building a Util singleton, for Windows non-console mode, it is necessary to :
// - Build a UNIX like argc,argv from the command line
// - Initialise the ORB
// - Create a debug output window if verbose mode is requested and change cout so that it prints into this
// window
//
// arguments :
// in :
// - hInst : The application instance
// - nCmdShow : The display window flag
//
//-------------------------------------------------------------------------------------------------------------------
Util::Util(HINSTANCE hInst,int nCmdShow):cl_list_ptr(NULL),mon("Windows startup"),ext(new UtilExt),
heartbeat_th(NULL),heartbeat_th_id(0),poll_mon("utils_poll"),poll_on(false),ser_model(BY_DEVICE),
only_one("process"),nd_event_supplier(NULL),py_interp(NULL),py_ds(false),py_dbg(false),
db_cache(NULL),inter(NULL),svr_starting(true),svr_stopping(false),poll_pool_size(ULONG_MAX),
conf_needs_db_upd(false),ev_loop_func(NULL),shutdown_server(false),_dummy_thread(false),
zmq_event_supplier(NULL),endpoint_specified(false),user_pub_hwm(-1),wattr_nan_allowed(false),
polling_bef_9_def(false)
#ifndef TANGO_HAS_LOG4TANGO
,cout_tmp(cout.rdbuf())
#endif
{
//
// This method should be called from a Windows graphic program
//
_win = true;
//
// Some more init
//
shared_data.cmd_pending = false;
shared_data.trigger = false;
cr_py_lock = new CreatePyLock();
//
// Build UNIX like command argument(s)
//
build_argc_argv();
//
// Really do the job now
//
effective_job(argc,argv);
//
// Store the nCmdShow parameter an mark the object has being completely
// constructed. Usefull, in case one of the method previously called
// failed, the orb variable (type ORB_var) is alaready destroyed therefore
// releasing the orb pointer.
//
nCmd = nCmdShow;
_constructed = true;
}
#endif
//+------------------------------------------------------------------------------------------------------------------
//
// method :
// Util::ckeck_args()
//
// description :
// Check the command line arguments. The first one is mandatory and is the server personal name. A -v option
// is authorized with an optional argument. The other option should be ORBacus option
//
// arguments :
// in :
// - argc : The command line argument number
// - argv : The command line argument
//
//-------------------------------------------------------------------------------------------------------------------
void Util::check_args(int argc,char *argv[])
{
//
// Check command line argument
//
std::string first_arg(argv[1]);
display_help = false;
if ((argc == 2) && (_UseDb == true))
{
if ((first_arg == "-?") || (first_arg == "-help") || (first_arg == "-h"))
{
display_help = true;
}
}
if ((display_help == false) && (argv[1][0] == '-'))
{
print_usage(argv[0]);
}
ds_instance_name = argv[1];
char *tmp;
#ifdef _TG_WINDOWS_
if ((tmp = strrchr(argv[0],'\\')) == 0)
{
if ((tmp = strrchr(argv[0],'/')) == 0)
{
ds_exec_name = argv[0];
}
else
{
tmp++;
ds_exec_name = tmp;
}
}
else
{
tmp++;
ds_exec_name = tmp;
}
#else
if ((tmp = strrchr(argv[0],'/')) == 0)
ds_exec_name = argv[0];
else
{
tmp++;
ds_exec_name = tmp;
}
#endif
//
// For Windows only. Remove the .exe after the executable name
//
#ifdef _TG_WINDOWS_
std::string::size_type pos;
if ((pos = ds_exec_name.find('.')) != std::string::npos)
{
ds_exec_name.erase(pos,ds_exec_name.size());
}
#endif /* _TG_WINDOWS_ */
if (argc > 2)
{
long ind = 2;
std::string dlist;
while (ind < argc)
{
if (argv[ind][0] == '-')
{
switch (argv[ind][1])
{
//
// The verbose option
//
case 'v':
if (strlen(argv[ind]) == 2)
{
if ((argc - 1)> ind)
{
if (argv[ind + 1][0] == '-')
set_trace_level(4);
else
{
std::cerr << "Unknown option " << argv[ind] << std::endl;
print_usage(argv[0]);
}
}
else
set_trace_level(4);
ind++;
}
else
{
long level = atol(&(argv[ind][2]));
if (level == 0)
{
std::cerr << "Unknown option " << argv[ind] << std::endl;
print_usage(argv[0]);
}
else
{
set_trace_level(level);
ind++;
}
}
break;
//
// Device server without database
//
case 'n':
if (strcmp(argv[ind],"-nodb") != 0)
{
std::cerr << "Unknown option " << argv[ind] << std::endl;
print_usage(argv[0]);
}
else
{
if ( _FileDb )
print_usage(argv[0]);
_UseDb = false;
ind++;
check_orb_endpoint(argc,argv);
}
break;
case 'f':
if (strncmp(argv[ind],"-file=",6) != 0)
{
std::cerr << "Unknown option " << argv[ind] << std::endl;
print_usage(argv[0]);
}
else
{
if ( !_UseDb )
print_usage(argv[0]);
Tango::Util::_FileDb = true;
database_file_name = argv[ind];
database_file_name.erase(0,6);
#ifdef _TG_WINDOWS_
replace(database_file_name.begin(),
database_file_name.end(),
'\\','/');
#endif
cout4 << "File name = <" << database_file_name << ">" << std::endl;
ind++;
//
// Try to find the ORB endPoint option
//
check_orb_endpoint(argc,argv);
}
break;
//
// Device list (for device server without database)
//
case 'd':
if (strcmp(argv[ind],"-dbg")==0)
{
ind++;
break;
}
if (strcmp(argv[ind],"-dlist") != 0)
{
std::cerr << "Unknown option " << argv[ind] << std::endl;
print_usage(argv[0]);
}
else
{
if (_UseDb == true)
print_usage(argv[0]);
ind++;
if (ind == argc)
print_usage(argv[0]);
else
{
dlist = argv[ind];
//
// Extract each device name
//
std::string::size_type start = 0;
std::string str;
std::string::size_type pos;
//vector<std::string> &list = get_cmd_line_name_list();
std::vector<std::string> dev_list;
while ((pos = dlist.find(',',start)) != std::string::npos)
{
str = dlist.substr(start,pos - start);
start = pos + 1;
dev_list.push_back(str);
}
if (start != dlist.size())
{
str = dlist.substr(start);
dev_list.push_back(str);
}
validate_sort(dev_list);
}
}
break;
default:
ind++;
break;
}
}
else
{
if (strncmp(argv[ind - 1],"-v",2) == 0)
{
print_usage(argv[0]);
}
ind++;
}
}
}
//
// Build server name
//
// long ctr;
// for (ctr = 0;ctr < ds_exec_name.size();ctr++)
// ds_exec_name[ctr] = std::tolower(ds_exec_name[ctr]);
// for (ctr = 0;ctr < ds_instance_name.size();ctr++)
// ds_instance_name[ctr] = std::tolower(ds_instance_name[ctr]);
ds_name = ds_exec_name;
ds_name.append("/");
ds_name.append(ds_instance_name);
//
// Check that the server name is not too long
//
if (ds_name.size() > MaxServerNameLength)
{
TangoSys_OMemStream o;
o << "The device server name is too long! Max length is " << MaxServerNameLength << " characters" << std::ends;
print_err_message(o.str(),Tango::INFO);
}
//
// If the server is the database server (DS started with _UseDb set to false but without the
// nodb option), we need its port
//
if ((_UseDb == false) && (svr_port_num.empty() == true))
{
check_orb_endpoint(argc,argv);
}
}
//+-----------------------------------------------------------------------------------------------------------------
//
// method :
// Util::validate_sort
//
// description :
// Validate and sort the list of devices passed by the user to the DS in the command line
//
// arguments :
// in :
// - dev_list : Vector with device name list
//
//-------------------------------------------------------------------------------------------------------------------
void Util::validate_sort(std::vector<std::string> &dev_list)
{
unsigned long i,j;
std::vector<std::string> dev_no_class;
//
// First, create a vector with device name in lower case letters without class name
//
for (i = 0;i < dev_list.size();i++)
{
std::string tmp(dev_list[i]);
std::transform(tmp.begin(),tmp.end(),tmp.begin(),::tolower);
std::string::size_type pos = tmp.find("::");
if (pos == std::string::npos)
{
dev_no_class.push_back(tmp);
}
else
{
std::string tmp_no_class = tmp.substr(pos + 2);
dev_no_class.push_back(tmp_no_class);
}
}
//
// Check that the same device name is not used twice and check that their syntax is correct (2 / characters)
//
for (i = 0;i < dev_no_class.size();i++)
{
int nb_char = count(dev_no_class[i].begin(),dev_no_class[i].end(),'/');
if (nb_char != 2)
{
std::stringstream ss;
ss << "Device name " << dev_list[i] << " does not have the correct syntax (domain/family/member or ClassName::domain/family/member)";
print_err_message(ss.str());
}
for (j = 0;j < dev_no_class.size();j++)
{
if (i == j)
continue;
else
{
if (dev_no_class[i] == dev_no_class[j])
{
print_err_message("Each device must have different name",Tango::INFO);
}
}
}
}
//
// Sort this device name list and store them in a map according to the class name. Command line device name
// syntax is "ClassName::device_name" eg: MyClass::my/device/name
//
std::map<std::string,std::vector<std::string> > &the_map = get_cmd_line_name_list();
std::string::size_type pos;
for (i = 0;i < dev_list.size();i++)
{
pos = dev_list[i].find("::");
if (pos == std::string::npos)
{
std::map<std::string,std::vector<std::string> >::iterator ite = the_map.find(NoClass);
if (ite == the_map.end())
{
std::vector<std::string> v_s;
v_s.push_back(dev_list[i]);
the_map.insert(std::pair<std::string,std::vector<std::string> >(NoClass,v_s));
}
else
{
ite->second.push_back(dev_list[i]);
}
}
else
{
std::string cl_name = dev_list[i].substr(0,pos);
std::transform(cl_name.begin(),cl_name.end(),cl_name.begin(),::tolower);
std::map<std::string,std::vector<std::string> >::iterator ite = the_map.find(cl_name);
if (ite == the_map.end())
{
std::vector<std::string> v_s;
std::string d_name = dev_list[i].substr(pos + 2);
v_s.push_back(d_name);
the_map.insert(std::pair<std::string,std::vector<std::string> >(cl_name,v_s));
}
else
{
std::string d_name = dev_list[i].substr(pos + 2);
ite->second.push_back(d_name);
}
}
}
}
//+-----------------------------------------------------------------------------------------------------------------
//
// method :
// Util::display_help_message()
//
// description :
// Display Tango device server process command line option and the list of instances defined for the
// device server (retrieved from DB)
//
//-------------------------------------------------------------------------------------------------------------------
void Util::display_help_message()
{