source: trunk/FACT++/dim/src/discpp.cxx@ 20068

Last change on this file since 20068 was 18920, checked in by tbretz, 7 years ago
Updated to v20r20 - This also includes some minor fixes, I requested.
File size: 28.9 KB
Line 
1#define DIMLIB
2#include <dis.hxx>
3#include "tokenstring.hxx"
4//#include <iostream>
5//using namespace std;
6#include <time.h>
7//#include <sys/timeb.h>
8
9DimClientExitHandler *DimServer::itsClientExit = 0;
10DimExitHandler *DimServer::itsExit = 0;
11DimErrorHandler *DimServer::itsSrvError = 0;
12char *DimServer::itsName = 0;
13char *DimServer::clientName = 0;
14char *DimServer::dimDnsNode = 0;
15int DimServer::autoStart = 1;
16//int DimServer::itsNServices = 0;
17
18extern "C" {
19extern void dis_init();
20extern int do_dis_remove_service(unsigned service_id, int do_stop_serving, void **dnsp );
21extern void do_dis_stop_serving_dns(void *dnsp);
22
23static void user_routine( void *tagp, void **buf, int *size, int *first_time)
24{
25// int *tag = (int *)tagp;
26// int id = *tag;
27 DimService *t;
28
29 if(first_time){}
30// t = (DimService *)id_get_ptr(id, SRC_DIS);
31 t = *(DimService **)tagp;
32 if( t->itsServiceHandler ) {
33 t->itsServiceHandler->itsService = t;
34 DimCore::inCallback = 2;
35 t->itsServiceHandler->serviceHandler();
36 DimCore::inCallback = 0;
37 }
38 else
39 {
40 DimCore::inCallback = 2;
41 t->serviceHandler();
42 DimCore::inCallback = 0;
43 }
44 if( t->itsType == DisSTRING)
45 t->itsSize = (int)strlen((char *)t->itsData)+1;
46 *buf = t->itsData;
47 *size = t->itsSize;
48}
49}
50
51void DimService::declareIt(char *name, char *format, DimServiceHandler *handler, DimServerDns *dns)
52{
53// itsTagId = 0;
54 itsDns = dns;
55 itsName = new char[(int)strlen(name)+1];
56 itsDataSize = 0;
57 strcpy( itsName, name);
58 if(handler)
59 itsServiceHandler = handler;
60 else
61 itsServiceHandler = 0;
62// itsTagId = id_get((void *)this, SRC_DIS);
63 dis_init();
64 if(itsDns == 0)
65 {
66 DISABLE_AST
67 itsId = dis_add_service( name, format, NULL, 0,
68// user_routine, itsTagId);
69 user_routine, (dim_long)this);
70 ENABLE_AST
71 DimServer::start();
72 }
73 else
74 {
75 DISABLE_AST
76 itsId = dis_add_service_dns( itsDns->getDnsId(), name, format, NULL, 0,
77// user_routine, itsTagId);
78 user_routine, (dim_long)this);
79 ENABLE_AST
80// itsDns->addServiceId(itsId);
81 DimServer::start(itsDns);
82 }
83}
84
85void DimService::storeIt(void *data, int size)
86{
87 DISABLE_AST
88 if(!itsId)
89 {
90 ENABLE_AST
91 return;
92 }
93 if(!itsDataSize)
94 {
95 itsData = new char[size];
96 itsDataSize = size;
97 }
98 else if(itsDataSize < size)
99 {
100 delete[] (char *)itsData;
101 itsData = new char[size];
102 itsDataSize = size;
103 }
104 memcpy(itsData, data, (size_t)size);
105 itsSize = size;
106 ENABLE_AST
107}
108
109extern "C" {
110static void command_routine( void *tagp, void *buf, int *size)
111{
112// int *tag = (int *)tagp;
113// int id = *tag;
114 DimCommand *t;
115
116// t = (DimCommand *)id_get_ptr(id, SRC_DIS);
117 t = *(DimCommand **)tagp;
118 t->itsData = buf;
119 t->itsSize = *size;
120 t->secs = 0;
121 if( t->itsCommandHandler ) {
122 t->itsCommandHandler->itsCommand = t;
123 DimCore::inCallback = 2;
124 t->itsCommandHandler->commandHandler();
125 DimCore::inCallback = 0;
126 }
127 else
128 {
129 DimCore::inCallback = 2;
130 t->commandHandler();
131 DimCore::inCallback = 0;
132 }
133 t->itsData = 0;
134 t->itsSize = 0;
135}
136}
137
138void DimCommand::declareIt(char *name, char *format, DimCommandHandler *handler, DimServerDns *dns)
139{
140// itsTagId = 0;
141 itsId = 0;
142 itsDns = dns;
143 itsName = new char[(int)strlen(name)+1];
144 strcpy( itsName, name);
145 itsFormat = new char[(int)strlen(format)+1];
146 strcpy( itsFormat, format);
147 currCmnd = 0;
148 if(handler)
149 itsCommandHandler = handler;
150 else
151 itsCommandHandler = 0;
152// itsTagId = id_get((void *)this, SRC_DIS);
153 dis_init();
154 if(!itsDns)
155 {
156 DISABLE_AST
157 itsId = dis_add_cmnd( name, format, command_routine,
158// itsTagId);
159 (dim_long)this);
160 ENABLE_AST
161 DimServer::start();
162 }
163 else
164 {
165 DISABLE_AST
166 itsId = dis_add_cmnd_dns( itsDns->getDnsId(), name, format, command_routine,
167// itsTagId);
168 (dim_long)this);
169 ENABLE_AST
170// itsDns->addServiceId(itsId);
171 DimServer::start(itsDns);
172 }
173}
174
175extern "C" {
176/*
177static void timeout_rout(DimRpc *t)
178{
179 sleep(t->itsTimeout);
180 t->itsKilled = 1;
181}
182*/
183static void rpcin_routine( void *tagp, void *buf, int *size)
184{
185 time_t tt1 = 0, tt2 = 0;
186
187// int *tag = (int *)tagp;
188// int id = *tag;
189 DimRpc *t;
190 int tout, clientId, ids[2];
191// long tid;
192
193// t = (DimRpc *)id_get_ptr(id, SRC_DIS);
194 t = *(DimRpc **)tagp;
195 t->itsDataIn = buf;
196 t->itsSizeIn = *size;
197 clientId = dis_get_conn_id();
198 tout = dis_get_timeout(t->itsIdOut, clientId);
199 t->itsTimeout = tout;
200// tid = 0;
201 if(tout > 0)
202 {
203 tt1 = time((time_t *)0);
204 t->itsKilled = 0;
205// dtq_start_timer(t->itsTimeout,(void(*)(void *))timeout_rout,(void *)t);
206// tid = dim_start_thread((void(*)(void *))timeout_rout,(void *)t);
207 }
208 DimCore::inCallback = 2;
209 t->rpcHandler();
210 DimCore::inCallback = 0;
211 t->itsDataIn = 0;
212 t->itsSizeIn = 0;
213 if(tout > 0)
214 {
215 tt2 = time((time_t *)0);
216 if((tt2 - tt1) > tout)
217 t->itsKilled = 1;
218 }
219 if(!t->itsKilled)
220 {
221// if(tid)
222// {
223// dtq_stop_timer((void *)t);
224// dim_stop_thread(tid);
225// }
226 ids[0] = clientId;
227 ids[1] = 0;
228 dis_selective_update_service(t->itsIdOut, ids);
229 }
230}
231
232}
233
234extern "C" {
235static void rpcout_routine( void *tagp, void **buf, int *size, int *first_time)
236{
237// int *tag = (int *)tagp;
238// int id = *tag;
239 DimRpc *t;
240
241 if(first_time){}
242// t = (DimRpc *)id_get_ptr(id, SRC_DIS);
243 t = *(DimRpc**)tagp;
244 *buf = t->itsDataOut;
245 *size = t->itsSizeOut;
246}
247}
248
249void DimRpc::declareIt(char *name, char *formatin, char *formatout, DimServerDns *dns)
250{
251// itsTagId = 0;
252 itsIdIn = 0;
253 itsIdOut = 0;
254 itsDns = dns;
255 itsName = new char[(int)strlen(name)+1];
256 strcpy( itsName, name);
257 itsNameIn = new char[(int)strlen(name)+1+10];
258 strcpy( itsNameIn, name);
259 strcat(itsNameIn,(char *)"/RpcIn");
260 itsNameOut = new char[(int)strlen(name)+1+10];
261 strcpy( itsNameOut, name);
262 strcat(itsNameOut,(char *)"/RpcOut");
263 itsDataOut = new char[1];
264 itsDataOutSize = itsSizeOut = 1;
265 itsKilled = 0;
266 itsTimeout = 0;
267
268// itsTagId = id_get((void *)this, SRC_DIS);
269 dis_init();
270 if(!itsDns)
271 {
272 DISABLE_AST
273 itsIdIn = dis_add_cmnd( itsNameIn, formatin,
274// rpcin_routine, itsTagId);
275 rpcin_routine, (dim_long)this);
276 itsIdOut = dis_add_service( itsNameOut, formatout, 0,0,
277// rpcout_routine, itsTagId);
278 rpcout_routine, (dim_long)this);
279 ENABLE_AST
280 DimServer::start();
281 }
282 else
283 {
284 DISABLE_AST
285 itsIdIn = dis_add_cmnd_dns( itsDns->getDnsId(), itsNameIn, formatin,
286// rpcin_routine, itsTagId);
287 rpcin_routine, (dim_long)this);
288 itsIdOut = dis_add_service_dns( itsDns->getDnsId(), itsNameOut, formatout, 0,0,
289// rpcout_routine, itsTagId);
290 rpcout_routine, (dim_long)this);
291 ENABLE_AST
292// itsDns->addServiceId(itsIdIn);
293// itsDns->addServiceId(itsIdOut);
294 DimServer::start(itsDns);
295 }
296}
297
298void DimRpc::storeIt(void *data, int size)
299{
300 DISABLE_AST
301 if(!itsIdIn)
302 {
303 ENABLE_AST
304 return;
305 }
306 if(!itsDataOutSize)
307 {
308 itsDataOut = new char[size];
309 itsDataOutSize = size;
310 }
311 else if(itsDataOutSize < size)
312 {
313 delete[] (char *)itsDataOut;
314 itsDataOut = new char[size];
315 itsDataOutSize = size;
316 }
317 memcpy(itsDataOut, data, (size_t)size);
318 itsSizeOut = size;
319 ENABLE_AST
320}
321
322extern "C" {
323static void client_exit_user_routine(int*);
324static void exit_user_routine(int*);
325static void srv_error_user_routine(int, int, char*);
326}
327
328DimServerDns::DimServerDns(const char *node)
329{
330 init(node, 0);
331}
332
333DimServerDns::DimServerDns(const char *node, int port)
334{
335 init(node, port);
336}
337
338DimServerDns::DimServerDns(const char *node, int port, char *name)
339{
340 init(node, port);
341 DimServer::start(this, name);
342}
343
344#define DisDnsIdBlock 100
345
346void DimServerDns::init(const char *node, int port)
347{
348// if(!itsNode)
349// {
350 itsNode = new char[(int)strlen(node)+1];
351 strcpy(itsNode,node);
352// }
353 itsPort = port;
354 autoStart = 1;
355 itsName = 0;
356 itsServiceIdList = new int[DisDnsIdBlock];
357 itsServiceIdListSize = DisDnsIdBlock;
358 itsNServiceIds = 0;
359// itsNServices = 0;
360 itsDnsId = DimServer::addDns(node, port);
361}
362
363void DimServerDns::addServiceId(int id)
364{
365 int *tmp;
366
367 DISABLE_AST
368 if((itsNServiceIds + 2) > itsServiceIdListSize)
369 {
370 tmp = new int[itsServiceIdListSize + DisDnsIdBlock];
371 memcpy(tmp, itsServiceIdList, (size_t)itsServiceIdListSize*sizeof(int));
372 delete itsServiceIdList;
373 itsServiceIdList = tmp;
374 itsServiceIdListSize += DisDnsIdBlock;
375 }
376 itsServiceIdList[itsNServiceIds] = id;
377 itsServiceIdList[itsNServiceIds+1] = 0;
378 itsNServiceIds++;
379 ENABLE_AST
380}
381
382int *DimServerDns::getServiceIdList()
383{
384 int *list;
385 if(itsNServiceIds)
386 list = itsServiceIdList;
387 else
388 list = 0;
389 itsNServiceIds = 0;
390 return list;
391}
392
393DimServerDns::~DimServerDns()
394{
395 if(itsName)
396 {
397 DimServer::stop(this);
398// if(itsName)
399// delete[] itsName;
400 }
401// if(itsNode)
402 delete[] itsNode;
403}
404
405dim_long DimServerDns::getDnsId()
406{
407 return itsDnsId;
408}
409
410void DimServerDns::setName(const char *name)
411{
412 if(!itsName)
413 {
414 itsName = new char[(int)strlen(name)+1];
415 strcpy(itsName,name);
416 }
417}
418
419void DimServerDns::clearName()
420{
421 if(itsName)
422 {
423 delete[] itsName;
424 itsName = 0;
425 }
426}
427
428char *DimServerDns::getName()
429{
430 return itsName;
431}
432
433void DimServerDns::autoStartOn()
434{
435 autoStart = 1;
436}
437
438void DimServerDns::autoStartOff()
439{
440 autoStart = 0;
441}
442
443int DimServerDns::isAutoStart()
444{
445 return autoStart;
446}
447
448DimServer::DimServer()
449{
450 itsClientExit = this;
451 itsExit = this;
452 itsSrvError = this;
453// itsNServices = 0;
454}
455
456DimServer::~DimServer()
457{
458 if(itsName)
459 {
460 dis_stop_serving();
461 delete[] itsName;
462 }
463 if(clientName)
464 delete[] clientName;
465 if(dimDnsNode)
466 delete[] dimDnsNode;
467}
468
469void DimServer::start(const char *name)
470{
471 if(!itsName)
472 {
473 itsName = new char[(int)strlen(name)+1];
474 strcpy(itsName,name);
475 }
476 dis_start_serving(itsName);
477}
478
479void DimServer::start(DimServerDns *dns, const char *name)
480{
481 dim_long dnsid;
482
483 dis_init();
484 {
485 DISABLE_AST
486 dns->setName(name);
487 dnsid = dns->getDnsId();
488 dis_start_serving_dns(dnsid, (char *)name /*, dns->getServiceIdList()*/);
489 ENABLE_AST
490 }
491}
492/*
493void DimServer::threadHandler()
494{
495 int oldNServices;
496
497 while(1)
498 {
499 oldNServices = itsNServices;
500 usleep(100000);
501 if(oldNServices == itsNServices)
502 break;
503 }
504cout << "Starting " << itsNServices << endl;
505 {
506 DISABLE_AST
507 dis_start_serving(itsName);
508 itsNServices = 0;
509 ENABLE_AST
510 }
511
512}
513*/
514void DimServer::start()
515{
516// itsNServices++;
517 if((itsName) && (autoStart))
518 {
519// DimThread::start();
520 dis_start_serving(itsName);
521 }
522}
523
524void DimServer::start(DimServerDns *dns)
525{
526 dim_long dnsid;
527 char *name;
528 int isAuto;
529
530 dis_init();
531 {
532 DISABLE_AST
533// dns->itsNServices++;
534
535 name = dns->getName();
536 dnsid = dns->getDnsId();
537 isAuto = dns->isAutoStart();
538 if((name) && (isAuto))
539 {
540// DimThread::start();
541 dis_start_serving_dns(dnsid, (char *)name /*, dns->getServiceIdList()*/);
542 }
543 ENABLE_AST
544 }
545}
546
547void DimServer::stop()
548{
549 dis_stop_serving();
550 if(itsName)
551 {
552 delete[] itsName;
553 itsName = 0;
554 }
555}
556
557void DimServer::stop(DimServerDns *dns)
558{
559 dis_stop_serving_dns(dns->getDnsId());
560 dns->clearName();
561}
562
563void DimServer::autoStartOn()
564{
565 autoStart = 1;
566}
567
568void DimServer::autoStartOff()
569{
570 autoStart = 0;
571}
572
573int DimServer::getClientId()
574{
575 if(!clientName)
576 clientName = new char[128];
577 clientName[0] = '\0';
578 return dis_get_client(clientName);
579}
580
581char *DimServer::getClientName()
582{
583 if(!clientName)
584 clientName = new char[128];
585 clientName[0] = '\0';
586 dis_get_client(clientName);
587 return(clientName);
588}
589/*
590char *DimServer::getClientServices()
591{
592 int id;
593 if((id = dis_get_conn_id()))
594 return dis_get_client_services(id);
595 return (char *)0;
596}
597
598char *DimServer::getClientServices(int clientId)
599{
600 return dis_get_client_services(clientId);
601}
602*/
603char **DimServer::getClientServices()
604{
605 static TokenString *data = 0;
606 int id, len = 0, index = 0;
607 char *services;
608 static char** list = 0;
609 char *sep;
610
611 if(data)
612 {
613 delete data;
614 data = 0;
615 }
616 if(list)
617 {
618 delete[] list;
619 list = 0;
620 }
621 if((id = dis_get_conn_id()))
622 {
623 services = dis_get_client_services(id);
624 if(services)
625 {
626 data = new TokenString(services,(char *)"\n");
627 len = data->getNTokens();
628 list = new char*[len];
629 while(data->getToken(list[index]))
630 {
631 data->getToken(sep);
632 index++;
633 }
634 }
635 }
636 if(!len)
637 list = new char*[1];
638 list[index] = 0;
639 return list;
640}
641
642void DimServer::setClientExitHandler(int clientId)
643{
644 dis_set_client_exit_handler(clientId, 1);
645}
646
647void DimServer::clearClientExitHandler(int clientId)
648{
649 dis_set_client_exit_handler(clientId, 0);
650}
651
652void DimServer::addClientExitHandler(DimClientExitHandler *handler)
653{
654 if(handler == 0)
655 {
656 dis_add_client_exit_handler(0);
657 DimServer::itsClientExit = 0;
658 }
659 else
660 {
661 DimServer::itsClientExit = handler;
662 dis_add_client_exit_handler(client_exit_user_routine);
663 }
664}
665
666void DimServer::addClientExitHandler()
667{
668 DimServer::itsClientExit = this;
669 dis_add_client_exit_handler(client_exit_user_routine);
670}
671
672void DimServer::addExitHandler(DimExitHandler *handler)
673{
674 if(handler == 0)
675 {
676 dis_add_exit_handler(0);
677 DimServer::itsExit = 0;
678 }
679 else
680 {
681 DimServer::itsExit = handler;
682 dis_add_exit_handler(exit_user_routine);
683 }
684}
685
686void DimServer::addErrorHandler(DimErrorHandler *handler)
687{
688 if(handler == 0)
689 {
690 dis_add_error_handler(0);
691 DimServer::itsSrvError = 0;
692 }
693 else
694 {
695 DimServer::itsSrvError = handler;
696 dis_add_error_handler(srv_error_user_routine);
697 }
698}
699
700int DimServer::setDnsNode(const char *node)
701{
702 return dis_set_dns_node((char *)node);
703}
704
705int DimServer::setDnsNode(const char *node, int port)
706{
707 dis_set_dns_port(port);
708 return dis_set_dns_node((char *)node);
709}
710
711dim_long DimServer::addDns(const char *node, int port)
712{
713 return dis_add_dns((char *)node, port);
714}
715char *DimServer::getDnsNode()
716{
717 if(!dimDnsNode)
718 dimDnsNode = new char[256];
719 if(dis_get_dns_node(dimDnsNode))
720 return dimDnsNode;
721 else
722 return 0;
723}
724
725int DimServer::getDnsPort()
726{
727 return dis_get_dns_port();
728}
729
730void DimServer::setWriteTimeout(int secs)
731{
732 dim_set_write_timeout(secs);
733}
734
735int DimServer::getWriteTimeout()
736{
737 return dim_get_write_timeout();
738}
739
740void DimServer::addExitHandler()
741{
742 DimServer::itsExit = this;
743 dis_add_exit_handler(exit_user_routine);
744}
745
746void DimServer::addErrorHandler()
747{
748 DimServer::itsSrvError = this;
749 dis_add_error_handler(srv_error_user_routine);
750}
751
752int DimServer::inCallback()
753{
754 if(DimCore::inCallback)
755 return 1;
756 return 0;
757}
758
759extern "C" {
760static void client_exit_user_routine(int *idp)
761{
762 int id = *idp;
763
764 id++;
765 DimCore::inCallback = 2;
766 DimServer::itsClientExit->clientExitHandler();
767 DimCore::inCallback = 0;
768}
769
770static void exit_user_routine(int *idp)
771{
772// int id = *idp;
773
774// id++;
775 DimCore::inCallback = 2;
776 DimServer::itsExit->exitHandler(*idp);
777 DimCore::inCallback = 0;
778}
779
780static void srv_error_user_routine(int severity, int code, char *msg)
781{
782
783 DimCore::inCallback = 2;
784 if(DimServer::itsSrvError != 0)
785 DimServer::itsSrvError->errorHandler(severity, code, msg);
786 DimCore::inCallback = 0;
787}
788
789}
790
791
792DimService::DimService()
793{
794// itsTagId = 0;
795 itsId = 0;
796 itsName = 0;
797}
798
799DimService::DimService(const char *name, int &value)
800{
801 itsId = 0;
802 itsName = 0;
803 itsData = &value;
804 itsSize = sizeof(int);
805 itsType = DisINT;
806 declareIt((char *)name, (char *)"L", 0, 0);
807}
808
809DimService::DimService(const char *name, float &value)
810{
811 itsId = 0;
812 itsName = 0;
813 itsData = &value;
814 itsSize = sizeof(float);
815 itsType = DisFLOAT;
816 declareIt((char *)name, (char *)"F", 0, 0);
817}
818
819DimService::DimService(const char *name, double &value)
820{
821 itsId = 0;
822 itsName = 0;
823 itsData = &value;
824 itsSize = sizeof(double);
825 itsType = DisDOUBLE;
826 declareIt((char *)name, (char *)"D", 0, 0);
827}
828
829DimService::DimService(const char *name, longlong &value)
830{
831 itsId = 0;
832 itsName = 0;
833 itsData = &value;
834 itsSize = sizeof(longlong);
835 itsType = DisXLONG;
836 declareIt((char *)name, (char *)"X", 0, 0);
837}
838
839DimService::DimService(const char *name, short &value)
840{
841 itsId = 0;
842 itsName = 0;
843 itsData = &value;
844 itsSize = sizeof(short);
845 itsType = DisSHORT;
846 declareIt((char *)name, (char *)"S", 0, 0);
847}
848
849DimService::DimService(const char *name, char *string)
850{
851 itsId = 0;
852 itsName = 0;
853 itsData = string;
854 itsSize = (int)strlen(string)+1;
855 itsType = DisSTRING;
856 declareIt((char *)name, (char *)"C", 0, 0);
857}
858
859DimService::DimService(const char *name, char *format, void *structure, int size)
860{
861 itsId = 0;
862 itsName = 0;
863 itsData = structure;
864 itsSize = size;
865 itsType = DisPOINTER;
866 declareIt((char *)name, (char *)format, 0, 0);
867}
868
869DimService::DimService(const char *name, char *format, DimServiceHandler *handler)
870{
871 itsId = 0;
872 itsName = 0;
873 itsData = 0;
874 itsSize = 0;
875 itsType = DisPOINTER;
876 declareIt((char *)name, (char *)format, handler, 0);
877}
878
879DimService::DimService(const char *name, const char *format, void *structure, int size)
880{
881 itsId = 0;
882 itsName = 0;
883 itsData = structure;
884 itsSize = size;
885 itsType = DisPOINTER;
886 declareIt((char *)name, (char *)format, 0, 0);
887}
888
889DimService::DimService(const char *name, const char *format, DimServiceHandler *handler)
890{
891 itsId = 0;
892 itsName = 0;
893 itsData = 0;
894 itsSize = 0;
895 itsType = DisPOINTER;
896 declareIt((char *)name, (char *)format, handler, 0);
897}
898
899// with Dns
900
901DimService::DimService(DimServerDns *dns, const char *name, int &value)
902{
903 itsId = 0;
904 itsName = 0;
905 itsData = &value;
906 itsSize = sizeof(int);
907 itsType = DisINT;
908 declareIt((char *)name, (char *)"L", 0, dns);
909}
910
911DimService::DimService(DimServerDns *dns, const char *name, float &value)
912{
913 itsId = 0;
914 itsName = 0;
915 itsData = &value;
916 itsSize = sizeof(float);
917 itsType = DisFLOAT;
918 declareIt((char *)name, (char *)"F", 0, dns);
919}
920
921DimService::DimService(DimServerDns *dns, const char *name, double &value)
922{
923 itsId = 0;
924 itsName = 0;
925 itsData = &value;
926 itsSize = sizeof(double);
927 itsType = DisDOUBLE;
928 declareIt((char *)name, (char *)"D", 0, dns);
929}
930
931DimService::DimService(DimServerDns *dns, const char *name, longlong &value)
932{
933 itsId = 0;
934 itsName = 0;
935 itsData = &value;
936 itsSize = sizeof(longlong);
937 itsType = DisXLONG;
938 declareIt((char *)name, (char *)"X", 0, dns);
939}
940
941DimService::DimService(DimServerDns *dns, const char *name, short &value)
942{
943 itsId = 0;
944 itsName = 0;
945 itsData = &value;
946 itsSize = sizeof(short);
947 itsType = DisSHORT;
948 declareIt((char *)name, (char *)"S", 0, dns);
949}
950
951DimService::DimService(DimServerDns *dns, const char *name, char *string)
952{
953 itsId = 0;
954 itsName = 0;
955 itsData = string;
956 itsSize = (int)strlen(string)+1;
957 itsType = DisSTRING;
958 declareIt((char *)name, (char *)"C", 0, dns);
959}
960
961DimService::DimService(DimServerDns *dns, const char *name, char *format, void *structure, int size)
962{
963 itsId = 0;
964 itsName = 0;
965 itsData = structure;
966 itsSize = size;
967 itsType = DisPOINTER;
968 declareIt((char *)name, (char *)format, 0, dns);
969}
970
971DimService::DimService(DimServerDns *dns, const char *name, char *format, DimServiceHandler *handler)
972{
973 itsId = 0;
974 itsName = 0;
975 itsData = 0;
976 itsSize = 0;
977 itsType = DisPOINTER;
978 declareIt((char *)name, (char *)format, handler, dns);
979}
980
981
982DimService::DimService(DimServerDns *dns, const char *name, const char *format, void *structure, int size)
983{
984 itsId = 0;
985 itsName = 0;
986 itsData = structure;
987 itsSize = size;
988 itsType = DisPOINTER;
989 declareIt((char *)name, (char *)format, 0, dns);
990}
991
992DimService::DimService(DimServerDns *dns, const char *name, const char *format, DimServiceHandler *handler)
993{
994 itsId = 0;
995 itsName = 0;
996 itsData = 0;
997 itsSize = 0;
998 itsType = DisPOINTER;
999 declareIt((char *)name, (char *)format, handler, dns);
1000}
1001
1002
1003DimService::~DimService()
1004{
1005 int ret = 0;
1006 void *dnsp;
1007
1008 DISABLE_AST
1009 if(itsName)
1010 delete[] itsName;
1011 if(itsDataSize)
1012 delete[] (char *)itsData;
1013// if(itsTagId)
1014// id_free(itsTagId, SRC_DIS);
1015 if(itsId)
1016 ret = do_dis_remove_service( itsId, 0, &dnsp );
1017 itsId = 0;
1018 ENABLE_AST
1019 if(ret == -1)
1020 {
1021 do_dis_stop_serving_dns(dnsp);
1022 }
1023}
1024
1025int DimService::updateService()
1026{
1027 if(!itsId)
1028 return 0;
1029 return dis_update_service( itsId );
1030}
1031
1032int DimService::updateService( int &value )
1033{
1034 if(!itsId)
1035 return 0;
1036 if( itsType == DisINT)
1037 {
1038 itsData = &value;
1039 return dis_update_service( itsId );
1040 }
1041 return -1;
1042}
1043
1044int DimService::updateService( float &value )
1045{
1046 if(!itsId)
1047 return 0;
1048 if( itsType == DisFLOAT) {
1049 itsData = &value;
1050 return dis_update_service( itsId );
1051 }
1052 return -1;
1053}
1054
1055int DimService::updateService( double &value )
1056{
1057 if(!itsId)
1058 return 0;
1059 if( itsType == DisDOUBLE) {
1060 itsData = &value;
1061 return dis_update_service( itsId );
1062 }
1063 return -1;
1064}
1065
1066int DimService::updateService( longlong &value )
1067{
1068 if(!itsId)
1069 return 0;
1070 if( itsType == DisXLONG)
1071 {
1072 itsData = &value;
1073 return dis_update_service( itsId );
1074 }
1075 return -1;
1076}
1077
1078int DimService::updateService( short &value )
1079{
1080 if(!itsId)
1081 return 0;
1082 if( itsType == DisSHORT)
1083 {
1084 itsData = &value;
1085 return dis_update_service( itsId );
1086 }
1087 return -1;
1088}
1089
1090int DimService::updateService( char *string )
1091{
1092 if(!itsId)
1093 return 0;
1094 if( itsType == DisSTRING)
1095 {
1096 itsData = string;
1097 itsSize = (int)strlen(string)+1;
1098 return dis_update_service( itsId );
1099 }
1100 return -1;
1101}
1102
1103int DimService::updateService( void *structure, int size )
1104{
1105 if(!itsId)
1106 return 0;
1107 if( itsType == DisPOINTER)
1108 {
1109 itsData = structure;
1110 itsSize = size;
1111 return dis_update_service( itsId );
1112 }
1113 return -1;
1114}
1115
1116int DimService::selectiveUpdateService(int *cids)
1117{
1118 if(!itsId)
1119 return 0;
1120 if( cids == 0)
1121 {
1122 int ids[2];
1123 ids[0] = DimServer::getClientId();
1124 ids[1] = 0;
1125 return dis_selective_update_service( itsId, ids );
1126 }
1127 return dis_selective_update_service( itsId, cids );
1128}
1129
1130int DimService::selectiveUpdateService( int &value, int *cids)
1131{
1132 if(!itsId)
1133 return 0;
1134 if( itsType == DisINT)
1135 {
1136 itsData = &value;
1137 if( cids == 0)
1138 {
1139 int ids[2];
1140 ids[0] = DimServer::getClientId();
1141 ids[1] = 0;
1142 return dis_selective_update_service( itsId, ids );
1143 }
1144 return dis_selective_update_service( itsId, cids );
1145 }
1146 return -1;
1147}
1148
1149int DimService::selectiveUpdateService( float &value, int *cids )
1150{
1151 if(!itsId)
1152 return 0;
1153 if( itsType == DisFLOAT)
1154 {
1155 itsData = &value;
1156 if( cids == 0)
1157 {
1158 int ids[2];
1159 ids[0] = DimServer::getClientId();
1160 ids[1] = 0;
1161 return dis_selective_update_service( itsId, ids );
1162 }
1163 return dis_selective_update_service( itsId, cids );
1164 }
1165 return -1;
1166}
1167
1168int DimService::selectiveUpdateService( double &value, int *cids )
1169{
1170 if(!itsId)
1171 return 0;
1172 if( itsType == DisDOUBLE)
1173 {
1174 itsData = &value;
1175 if( cids == 0)
1176 {
1177 int ids[2];
1178 ids[0] = DimServer::getClientId();
1179 ids[1] = 0;
1180 return dis_selective_update_service( itsId, ids );
1181 }
1182 return dis_selective_update_service( itsId, cids );
1183 }
1184 return -1;
1185}
1186
1187int DimService::selectiveUpdateService( longlong &value, int *cids )
1188{
1189 if(!itsId)
1190 return 0;
1191 if( itsType == DisXLONG)
1192 {
1193 itsData = &value;
1194 if( cids == 0)
1195 {
1196 int ids[2];
1197 ids[0] = DimServer::getClientId();
1198 ids[1] = 0;
1199 return dis_selective_update_service( itsId, ids );
1200 }
1201 return dis_selective_update_service( itsId, cids );
1202 }
1203 return -1;
1204}
1205
1206int DimService::selectiveUpdateService( short &value, int *cids )
1207{
1208 if(!itsId)
1209 return 0;
1210 if( itsType == DisSHORT)
1211 {
1212 itsData = &value;
1213 if( cids == 0)
1214 {
1215 int ids[2];
1216 ids[0] = DimServer::getClientId();
1217 ids[1] = 0;
1218 return dis_selective_update_service( itsId, ids );
1219 }
1220 return dis_selective_update_service( itsId, cids );
1221 }
1222 return -1;
1223}
1224
1225int DimService::selectiveUpdateService( char *string, int *cids )
1226{
1227 if(!itsId)
1228 return 0;
1229 if( itsType == DisSTRING)
1230 {
1231 itsData = string;
1232 itsSize = (int)strlen(string)+1;
1233 if( cids == 0)
1234 {
1235 int ids[2];
1236 ids[0] = DimServer::getClientId();
1237 ids[1] = 0;
1238 return dis_selective_update_service( itsId, ids );
1239 }
1240 return dis_selective_update_service( itsId, cids );
1241 }
1242 return -1;
1243}
1244
1245int DimService::selectiveUpdateService( void *structure, int size, int *cids )
1246{
1247 if(!itsId)
1248 return 0;
1249 if( itsType == DisPOINTER)
1250 {
1251 itsData = structure;
1252 itsSize = size;
1253 if( cids == 0)
1254 {
1255 int ids[2];
1256 ids[0] = DimServer::getClientId();
1257 ids[1] = 0;
1258 return dis_selective_update_service( itsId, ids );
1259 }
1260 return dis_selective_update_service( itsId, cids );
1261 }
1262 return -1;
1263}
1264
1265void DimService::setQuality(int quality)
1266{
1267 if(!itsId)
1268 return;
1269 dis_set_quality( itsId, quality );
1270}
1271
1272void DimService::setTimestamp(int secs, int millisecs)
1273{
1274 if(!itsId)
1275 return;
1276 dis_set_timestamp( itsId, secs, millisecs );
1277}
1278
1279void DimService::setData(void *data, int size)
1280{
1281 storeIt(data, size);
1282}
1283
1284void DimService::setData(int &data)
1285{
1286 storeIt(&data, sizeof(int));
1287}
1288
1289void DimService::setData(float &data)
1290{
1291 storeIt(&data, sizeof(float));
1292}
1293
1294void DimService::setData(double &data)
1295{
1296 storeIt(&data, sizeof(double));
1297}
1298
1299void DimService::setData(longlong &data)
1300{
1301 storeIt(&data, sizeof(longlong));
1302}
1303
1304void DimService::setData(short &data)
1305{
1306 storeIt(&data, sizeof(short));
1307}
1308
1309void DimService::setData(char *data)
1310{
1311 storeIt(data, (int)strlen(data)+1);
1312}
1313
1314char *DimService::getName()
1315{
1316 return itsName;
1317}
1318
1319int DimService::getTimeout(int clientId)
1320{
1321 return dis_get_timeout(itsId, clientId);
1322}
1323
1324int DimService::getNClients()
1325{
1326 return dis_get_n_clients( itsId );
1327}
1328
1329
1330CmndInfo::CmndInfo(void *data, int datasize, int tsecs, int tmillisecs)
1331{
1332 itsData = new char[datasize];
1333 itsDataSize = datasize;
1334 secs = tsecs;
1335 millisecs = tmillisecs;
1336 memcpy(itsData, data, (size_t)datasize);
1337}
1338
1339CmndInfo::~CmndInfo()
1340{
1341 delete[] (char *)itsData;
1342}
1343
1344
1345DimCommand::DimCommand(const char *name, char *format)
1346{
1347 declareIt( (char *)name, (char *)format, 0, 0);
1348}
1349
1350DimCommand::DimCommand(const char *name, char *format, DimCommandHandler *handler)
1351{
1352 declareIt( (char *)name, (char *)format, handler, 0);
1353}
1354
1355DimCommand::DimCommand(DimServerDns *dns, const char *name, char *format)
1356{
1357 declareIt( (char *)name, (char *)format, 0, dns);
1358}
1359
1360DimCommand::DimCommand(DimServerDns *dns, const char *name, char *format, DimCommandHandler *handler)
1361{
1362 declareIt( (char *)name, (char *)format, handler, dns);
1363}
1364
1365
1366DimCommand::DimCommand(const char *name, const char *format)
1367{
1368 declareIt( (char *)name, (char *)format, 0, 0);
1369}
1370
1371DimCommand::DimCommand(const char *name, const char *format, DimCommandHandler *handler)
1372{
1373 declareIt( (char *)name, (char *)format, handler, 0);
1374}
1375
1376DimCommand::DimCommand(DimServerDns *dns, const char *name, const char *format)
1377{
1378 declareIt( (char *)name, (char *)format, 0, dns);
1379}
1380
1381DimCommand::DimCommand(DimServerDns *dns, const char *name, const char *format, DimCommandHandler *handler)
1382{
1383 declareIt( (char *)name, (char *)format, handler, dns);
1384}
1385
1386int DimCommand::getNext()
1387{
1388 CmndInfo *cmndptr;
1389 if(currCmnd)
1390 {
1391 delete currCmnd;
1392 currCmnd = 0;
1393 itsData = 0;
1394 itsSize = 0;
1395 }
1396 if ((cmndptr = (CmndInfo *)itsCmndList.removeHead()))
1397 {
1398 currCmnd = cmndptr;
1399 itsData = currCmnd->itsData;
1400 itsSize = currCmnd->itsDataSize;
1401 secs = currCmnd->secs;
1402 millisecs = currCmnd->millisecs;
1403 return(1);
1404 }
1405 return(0);
1406}
1407
1408int DimCommand::hasNext()
1409{
1410 if ((CmndInfo *)itsCmndList.getHead())
1411 {
1412 return(1);
1413 }
1414 return(0);
1415}
1416
1417void *DimCommand::getData()
1418{
1419 return itsData;
1420}
1421
1422int DimCommand::getInt()
1423{
1424 return *(int *)itsData;
1425}
1426
1427float DimCommand::getFloat()
1428{
1429 return *(float *)itsData;
1430}
1431
1432double DimCommand::getDouble()
1433{
1434 return *(double *)itsData;
1435}
1436
1437longlong DimCommand::getLonglong()
1438{
1439 return *(longlong *)itsData;
1440}
1441
1442short DimCommand::getShort()
1443{
1444 return *(short *)itsData;
1445}
1446
1447char *DimCommand::getString()
1448{
1449 return (char *)itsData;
1450}
1451
1452int DimCommand::getSize()
1453{
1454 return itsSize;
1455}
1456
1457char *DimCommand::getFormat()
1458{
1459 return itsFormat;
1460}
1461
1462int DimCommand::getTimestamp()
1463{
1464
1465 if(secs == 0)
1466 {
1467 DISABLE_AST
1468 if(itsId)
1469 dis_get_timestamp(itsId, &secs, &millisecs);
1470 ENABLE_AST
1471 }
1472 return(secs);
1473}
1474
1475int DimCommand::getTimestampMillisecs()
1476{
1477 return(millisecs);
1478}
1479
1480void DimCommand::commandHandler()
1481{
1482 CmndInfo *cmndptr;
1483 int tsecs, tmillisecs;
1484
1485 tsecs = getTimestamp();
1486 tmillisecs = getTimestampMillisecs();
1487 cmndptr = new CmndInfo(getData(), getSize(), tsecs, tmillisecs);
1488 itsCmndList.add(cmndptr);
1489}
1490
1491char *DimCommand::getName()
1492{
1493 return itsName;
1494}
1495
1496DimCommand::~DimCommand()
1497{
1498 int ret;
1499 void *dnsp;
1500
1501 DISABLE_AST
1502 delete[] itsName;
1503 delete[] itsFormat;
1504// if(itsTagId)
1505// id_free(itsTagId, SRC_DIS);
1506 if(itsId)
1507 ret = do_dis_remove_service( itsId, 0, &dnsp );
1508 itsId = 0;
1509 ENABLE_AST
1510 if(ret == -1)
1511 {
1512 do_dis_stop_serving_dns(dnsp);
1513 }
1514}
1515
1516DimRpc::DimRpc()
1517{
1518}
1519
1520DimRpc::DimRpc(const char *name, const char *formatin, const char *formatout)
1521{
1522 declareIt( (char *)name, (char *)formatin, (char *)formatout, 0);
1523}
1524
1525DimRpc::DimRpc(DimServerDns *dns, const char *name, const char *formatin, const char *formatout)
1526{
1527 declareIt( (char *)name, (char *)formatin, (char *)formatout, dns);
1528}
1529
1530DimRpc::~DimRpc()
1531{
1532 int ret;
1533 void *dnsp;
1534
1535 DISABLE_AST
1536 delete[] itsName;
1537 delete[] itsNameIn;
1538 delete[] itsNameOut;
1539// if(itsTagId)
1540// id_free(itsTagId, SRC_DIS);
1541 if(itsIdIn)
1542 ret = do_dis_remove_service( itsIdIn, 0, &dnsp );
1543 if(itsIdOut)
1544 ret = do_dis_remove_service( itsIdOut, 0, &dnsp );
1545 itsIdIn = 0;
1546 itsIdOut = 0;
1547 ENABLE_AST
1548 if(ret == -1)
1549 {
1550 do_dis_stop_serving_dns(dnsp);
1551 }
1552}
1553
1554void *DimRpc::getData()
1555{
1556 return itsDataIn;
1557}
1558
1559int DimRpc::getInt()
1560{
1561 return *(int *)itsDataIn;
1562}
1563
1564float DimRpc::getFloat()
1565{
1566 return *(float *)itsDataIn;
1567}
1568
1569double DimRpc::getDouble()
1570{
1571 return *(double *)itsDataIn;
1572}
1573
1574longlong DimRpc::getLonglong()
1575{
1576 return *(longlong *)itsDataIn;
1577}
1578
1579short DimRpc::getShort()
1580{
1581 return *(short *)itsDataIn;
1582}
1583
1584char *DimRpc::getString()
1585{
1586 return (char *)itsDataIn;
1587}
1588
1589int DimRpc::getSize()
1590{
1591 return itsSizeIn;
1592}
1593
1594void DimRpc::setData(void *data, int size)
1595{
1596 storeIt(data,size);
1597}
1598
1599void DimRpc::setData(int &data)
1600{
1601 storeIt(&data,sizeof(int));
1602}
1603
1604void DimRpc::setData(float &data)
1605{
1606 storeIt(&data,sizeof(float));
1607}
1608
1609void DimRpc::setData(double &data)
1610{
1611 storeIt(&data,sizeof(double));
1612}
1613
1614void DimRpc::setData(longlong &data)
1615{
1616 storeIt(&data,sizeof(longlong));
1617}
1618
1619void DimRpc::setData(short &data)
1620{
1621 storeIt(&data,sizeof(short));
1622}
1623
1624void DimRpc::setData(char *data)
1625{
1626 storeIt(data,(int)strlen(data)+1);
1627}
1628
1629char *DimRpc::getName()
1630{
1631 return itsName;
1632}
Note: See TracBrowser for help on using the repository browser.