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

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