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

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