source: trunk/FACT++/dim/src/webDid/webDid.c@ 17050

Last change on this file since 17050 was 15282, checked in by tbretz, 12 years ago
Updated to v20r7.
File size: 37.7 KB
Line 
1#include <stdio.h>
2#include <ctype.h>
3#include <time.h>
4#include <dim.h>
5#include <dic.h>
6#include <dis.h>
7
8extern int WebDID_Debug;
9
10typedef struct item{
11 struct item *next;
12 DNS_SERVER_INFO server;
13 DNS_SERVICE_INFO *service_ptr;
14 char name[MAX_NAME];
15 int match;
16 int busy;
17 int isSMI;
18}SERVER;
19
20typedef struct nitem{
21 struct nitem *next;
22 SERVER *server_head;
23 char name[MAX_NAME];
24 int match;
25 int busy;
26 int hasSMI;
27}NODE;
28NODE *Node_head = (NODE *)0;
29
30typedef struct sitem{
31 struct sitem *next;
32 char name[MAX_NAME];
33 int sid;
34 void *buffer;
35 int buffer_size;
36 int size;
37 time_t timestamp;
38 void *buffer_str;
39 int buffer_str_size;
40 int str_size;
41 int conn_id;
42 time_t last_subscribed;
43 time_t last_updated;
44 int n_browsers;
45}CURR_SERVICE;
46CURR_SERVICE *Curr_service_head = (CURR_SERVICE *)0;
47
48typedef struct bitem{
49 struct bitem *next;
50 int id;
51 int subscribe;
52 time_t last_subscribed;
53 time_t last_updated;
54 time_t last_polled;
55 int conn_id;
56 int n_services;
57 int n_servers;
58 int n_nodes;
59 CURR_SERVICE *servicep;
60 char *JSONBuffer;
61 int JSONBufferSize;
62 char pattern[256];
63 char curr_command[MAX_NAME];
64 char *service_format_ptr;
65}BROWSER;
66BROWSER *Browser_head = (BROWSER *)0;
67
68char *JSONBuffer = 0;
69int JSONBufferSize = 0;
70char JSONHeader[256] = {'\0'};
71
72int First_time = 1;
73int Curr_view_opt = -1;
74char Curr_view_opt_par[80];
75char Curr_service_name[132];
76char Curr_service_format[256];
77int Curr_service_print_type = 0;
78int N_nodes = 0;
79int N_servers = 0;
80int N_services = 0;
81static char no_link = -1;
82int no_link_int = -1;
83FILE *fptr;
84
85char *Service_content_str;
86char *Curr_service_list = 0;
87char *Curr_client_list = 0;
88int Curr_service_id = 0;
89SERVER *Got_Service_List = 0;
90SERVER *Got_Client_List = 0;
91
92int Timer_q;
93char Title[128];
94
95int did_init(char *local_node, int dns_port)
96{
97 void update_servers();
98 char icon_title[128];
99 char dns_node[128];
100 int ret;
101
102 dim_init();
103 dic_disable_padding();
104 dis_disable_padding();
105
106 ret = dim_get_dns_node(dns_node);
107 if(!ret)
108 {
109 strcpy(dns_node, local_node);
110 dim_set_dns_node(dns_node);
111 }
112 dns_port = dic_get_dns_port();
113 if(dns_port != DNS_PORT)
114 {
115 sprintf(Title,"DIM DNS: %s:%d",dns_node,dns_port);
116 }
117 else
118 {
119 sprintf(Title,"DIM DNS: %s",dns_node);
120 }
121 sprintf(icon_title,"DID %s",dns_node);
122dim_print_date_time();
123printf("webDid Starting up on %s\n\t serving %s\n", local_node, Title);
124 Timer_q = dtq_create();
125 dic_info_service("DIS_DNS/SERVER_INFO",MONITORED,0,0,0,update_servers,0,
126 &no_link,1);
127 return 1;
128}
129
130SERVER *find_server(NODE *nodep, int pid)
131{
132 SERVER *servp;
133 DNS_SERVER_INFO *ptr;
134
135 servp = nodep->server_head;
136 while( (servp = (SERVER *)sll_get_next((SLL *)servp)) )
137 {
138 ptr = &servp->server;
139 if(ptr->pid == pid)
140 {
141 return(servp);
142 }
143 }
144 return ((SERVER *)0);
145}
146
147NODE *find_node(char *node)
148{
149 NODE *nodep;
150
151 nodep = Node_head;
152 while( (nodep = (NODE *)sll_get_next((SLL *)nodep)) )
153 {
154 if(!strcmp(nodep->name,node))
155 {
156 return(nodep);
157 }
158 }
159 return ((NODE *)0);
160}
161
162int find_server_service_pattern(SERVER *servp, char *pattern)
163{
164 DNS_SERVICE_INFO *servicep;
165 int n_services, i;
166 int n_found = 0;
167
168 servicep = servp->service_ptr;
169 n_services = servp->server.n_services;
170 for(i = 0; i < n_services; i++)
171 {
172 if(strstr(servicep->name, pattern))
173 {
174 n_found++;
175 }
176 servicep++;
177 }
178 return(n_found);
179}
180
181int find_service_pattern(NODE *nodep, SERVER *servpp, char *pattern, int *n_servers)
182{
183 SERVER *servp;
184 int ret, n_found = 0;
185 int n_servers_found = 0;
186
187 if(!servpp)
188 {
189 servp = nodep->server_head;
190 while( (servp = (SERVER *)sll_get_next((SLL *)servp)) )
191 {
192 if((ret = find_server_service_pattern(servp, pattern)))
193 {
194 n_found += ret;
195 n_servers_found++;
196 }
197 }
198 }
199 else
200 {
201 if((ret = find_server_service_pattern(servpp, pattern)))
202 {
203 n_found += ret;
204 }
205 }
206 if(n_servers != 0)
207 *n_servers = n_servers_found;
208 return(n_found);
209}
210
211CURR_SERVICE *find_curr_service(char *service)
212{
213 CURR_SERVICE *servicep;
214
215 servicep = Curr_service_head ;
216 while( (servicep = (CURR_SERVICE *)sll_get_next((SLL *)servicep)) )
217 {
218 if(!strcmp(servicep->name,service))
219 {
220 return(servicep);
221 }
222 }
223 return ((CURR_SERVICE *)0);
224}
225
226BROWSER *find_browser(int id)
227{
228 BROWSER *browserp;
229
230 browserp = Browser_head;
231 while( (browserp = (BROWSER *)sll_get_next((SLL *)browserp)) )
232 {
233 if(browserp->id == id)
234 {
235 return(browserp);
236 }
237 }
238 return ((BROWSER *)0);
239}
240/*
241void set_browser_changes(int n_services, int n_servers, int n_nodes)
242{
243 BROWSER *browserp;
244
245 browserp = Browser_head;
246 while( (browserp = (BROWSER *)sll_get_next((SLL *)browserp)) )
247 {
248 if(browserp->n_services != n_services)
249 browserp->n_services_changed = 1;
250 if(browserp->n_servers != n_servers)
251 browserp->n_servers_changed = 1;
252 if(browserp->n_nodes != n_nodes)
253 browserp->n_nodes_changed = 1;
254 }
255}
256*/
257void prepare_browser_tree()
258{
259 BROWSER *browserp;
260 int prepareJSONTree();
261
262 browserp = Browser_head;
263 while( (browserp = (BROWSER *)sll_get_next((SLL *)browserp)) )
264 {
265 if(browserp->pattern[0] != '\0')
266 prepareJSONTree(browserp);
267 }
268}
269/*
270void clear_browser_changes(BROWSER *browserp)
271{
272 browserp->n_services_changed = 0;
273 browserp->n_servers_changed = 0;
274 browserp->n_nodes_changed = 0;
275}
276*/
277void update_servers(int *tag, DNS_DID *buffer, int *size)
278{
279int n_services, service_size;
280SERVER *servp;
281NODE *nodep;
282int j;
283char str[MAX_NAME], sname[MAX_NAME], *ptr;
284int prepareJSONTree();
285int prepareJSONHeader();
286
287 if(tag){}
288 if(!Node_head)
289 {
290 Node_head = (NODE *)malloc(sizeof(NODE));
291 sll_init((SLL *)Node_head);
292 }
293 if(First_time)
294 {
295 First_time = 0;
296 }
297
298 if(!*size)
299 return;
300 if(*(char *)buffer == -1)
301 {
302 N_servers = 0;
303 N_services = 0;
304 return;
305 }
306 buffer->server.n_services = vtohl(buffer->server.n_services);
307 buffer->server.pid = vtohl(buffer->server.pid);
308 n_services = buffer->server.n_services;
309
310 if(n_services == 1)
311 return;
312 strcpy(sname, buffer->server.task);
313 if(n_services > 1)
314 {
315 for(j = 0; j < n_services; j++)
316 {
317 buffer->services[j].type = vtohl(
318 buffer->services[j].type);
319 buffer->services[j].status = vtohl(
320 buffer->services[j].status);
321 buffer->services[j].n_clients = vtohl(
322 buffer->services[j].n_clients);
323 if((int)strlen(sname) == MAX_TASK_NAME-4-1)
324 {
325 strcpy(str,buffer->services[j].name);
326 if( (ptr = strstr(str,"/CLIENT_LIST")) )
327 {
328 *ptr = '\0';
329 strcpy(sname,str);
330 }
331 }
332 }
333 }
334 if (!(nodep = find_node(buffer->server.node)))
335 {
336 if(n_services)
337 {
338 N_nodes++;
339 nodep = (NODE *)malloc(sizeof(NODE));
340 strcpy(nodep->name,buffer->server.node);
341 nodep->hasSMI = 0;
342 nodep->server_head = (SERVER *)malloc(sizeof(SERVER));
343 sll_init((SLL *)nodep->server_head);
344 sll_insert_queue((SLL *)Node_head,(SLL *)nodep);
345 }
346 }
347 if (!(servp = find_server(nodep,buffer->server.pid)))
348 {
349 if(n_services)
350 {
351 servp = (SERVER *)malloc(sizeof(SERVER));
352 strcpy(servp->name,sname);
353 servp->next = 0;
354 servp->busy = 0;
355 servp->server.n_services = 0;
356 servp->service_ptr = 0;
357 servp->isSMI = 0;
358 if(strstr(sname,"_SMI"))
359 {
360 servp->isSMI = 1;
361 nodep->hasSMI = 1;
362 }
363 sll_insert_queue((SLL *)nodep->server_head,(SLL *)servp);
364 }
365 }
366 if(n_services != 0)
367 {
368 if(n_services == servp->server.n_services)
369 {
370 return;
371 }
372 if(servp->server.n_services == 0)
373 N_servers++;
374 if(servp->server.n_services != -1)
375 N_services -= servp->server.n_services;
376 memcpy(&servp->server,&buffer->server,sizeof(DNS_SERVER_INFO));
377 if(servp->service_ptr)
378 {
379 free(servp->service_ptr);
380 servp->service_ptr = 0;
381 }
382 if(n_services != -1)
383 {
384 service_size = n_services*(int)sizeof(DNS_SERVICE_INFO);
385 servp->service_ptr = (DNS_SERVICE_INFO *)malloc((size_t)service_size);
386 memcpy(servp->service_ptr, buffer->services, (size_t)service_size);
387 N_services += n_services;
388 }
389 servp->busy = 1;
390 }
391 else
392 {
393 if(servp)
394 {
395 N_servers--;
396 if(servp->server.n_services != -1)
397 {
398 N_services -= servp->server.n_services;
399 }
400 servp->server.n_services = 0;
401 servp->busy = -1;
402 servp->isSMI = 0;
403 }
404 }
405 if(JSONHeader[0])
406 prepareJSONHeader();
407}
408
409void got_update_services(BROWSER **tag, char *buffer, int *size)
410{
411 BROWSER *browserp;
412
413 if(size){}
414 browserp = (BROWSER *)*tag;
415 if(browserp->service_format_ptr)
416 free(browserp->service_format_ptr);
417 browserp->service_format_ptr = (char *)malloc(strlen(buffer)+1);
418 strcpy(browserp->service_format_ptr, buffer);
419}
420
421char *update_services(char *node, char *server, int pid, int browser)
422{
423 char str[MAX_NAME];
424 NODE *nodep;
425 SERVER *servp;
426 char *ptr;
427 BROWSER *browserp;
428 char *prepareJSONServiceList();
429 BROWSER *create_browser();
430
431 if(!(browserp = find_browser(browser)))
432 browserp = create_browser(browser);
433
434 if(server){}
435 sprintf(str,"%s/SERVICE_LIST",server);
436 dic_info_service(str,ONCE_ONLY,20,0,0,
437 got_update_services,(dim_long)browserp,"None",5);
438 if((nodep = find_node(node)))
439 {
440 if((servp = find_server(nodep, pid)))
441 {
442 ptr = prepareJSONServiceList(servp, node, pid, browserp);
443 }
444 }
445 return ptr;
446}
447
448void get_curr_service_format()
449{
450 char *format;
451 char *dic_get_format();
452/*
453 char str[256], *ptr, *ptr1;
454 int rpc_flag;
455
456 strcpy(str,Curr_service_name);
457 rpc_flag = 0;
458 if( (ptr = strstr(str,"/RpcIn")) )
459 {
460 *ptr = '\0';
461 rpc_flag = 1;
462 }
463 if( (ptr = strstr(str,"/RpcOut")) )
464 {
465 *ptr = '\0';
466 rpc_flag = 2;
467 }
468 strcat(str,"|");
469*/
470 format = dic_get_format(0);
471/*
472 if( (ptr = strstr(Curr_service_list,str)) )
473 {
474 if(!rpc_flag)
475 {
476 ptr += (int)strlen(str);
477 ptr1 = strchr(ptr,'|');
478 }
479 else if(rpc_flag == 1)
480 {
481 ptr += (int)strlen(str);
482 ptr1 = strchr(ptr,',');
483 }
484 else
485 {
486 ptr += (int)strlen(str);
487 ptr = strchr(ptr,',');
488 ptr++;
489 ptr1 = strchr(ptr,'|');
490 }
491 strncpy(Curr_service_format,ptr,(int)(ptr1 - ptr));
492 Curr_service_format[(int)(ptr1-ptr)] = '\0';
493 }
494*/
495 if(format)
496 strcpy(Curr_service_format,format);
497 else
498 Curr_service_format[0] = '\0';
499}
500
501void get_service_format(char *buffer, char *service, char *format)
502{
503 char str[256], *ptr, *ptr1;
504 int rpc_flag;
505
506 strcpy(str, service);
507 rpc_flag = 0;
508 *format = '\0';
509 if( (ptr = strstr(str,"/RpcIn")) )
510 {
511 *ptr = '\0';
512 rpc_flag = 1;
513 }
514 if( (ptr = strstr(str,"/RpcOut")) )
515 {
516 *ptr = '\0';
517 rpc_flag = 2;
518 }
519 strcat(str,"|");
520 if( (ptr = strstr(buffer, str)) )
521 {
522 if(!rpc_flag)
523 {
524 ptr += (int)strlen(str);
525 ptr1 = strchr(ptr,'|');
526 }
527 else if(rpc_flag == 1)
528 {
529 ptr += (int)strlen(str);
530 ptr1 = strchr(ptr,',');
531 }
532 else
533 {
534 ptr += (int)strlen(str);
535 ptr = strchr(ptr,',');
536 ptr++;
537 ptr1 = strchr(ptr,'|');
538 }
539 strncpy(format,ptr,(size_t)(ptr1 - ptr));
540 format[(int)(ptr1-ptr)] = '\0';
541 }
542}
543
544int delete_curr_service(CURR_SERVICE *servicep)
545{
546
547if(WebDID_Debug)
548printf("\nUnsubscribing %s\n\n",servicep->name);
549 dic_release_service(servicep->sid);
550 if(servicep->buffer_size)
551 free(servicep->buffer);
552 if(servicep->buffer_str_size)
553 free(servicep->buffer_str);
554 sll_remove((SLL *)Curr_service_head, (SLL *)servicep);
555 free(servicep);
556 return(1);
557}
558
559int delete_browser(BROWSER *browserp)
560{
561 CURR_SERVICE *servicep;
562
563 if((servicep = browserp->servicep))
564 {
565 servicep->n_browsers--;
566 if(!servicep->n_browsers)
567 delete_curr_service(servicep);
568 }
569 if(browserp->service_format_ptr)
570 free(browserp->service_format_ptr);
571 sll_remove((SLL *)Browser_head, (SLL *)browserp);
572 free(browserp);
573 return(1);
574}
575
576void check_browser( BROWSER *tag)
577{
578 BROWSER *browserp;
579 time_t tsecs;
580
581 browserp = (BROWSER *)tag;
582if(WebDID_Debug)
583printf("\nCheck_browser %d\n",browserp->id);
584 tsecs = time((time_t *)0);
585 if((tsecs - browserp->last_polled) > 20)
586 {
587if(WebDID_Debug)
588printf("\nDeleting browser %d\n\n",browserp->id);
589 delete_browser(browserp);
590 }
591 else
592 dtq_start_timer(10, check_browser, browserp);
593}
594
595BROWSER *create_browser(int id)
596{
597 BROWSER *browserp;
598
599 if(!Browser_head)
600 {
601 Browser_head = (BROWSER *)malloc(sizeof(BROWSER));
602 sll_init((SLL *)Browser_head);
603 }
604 browserp = (BROWSER *)malloc(sizeof(BROWSER));
605 browserp->id = id;
606 browserp->last_subscribed = 0;
607 browserp->last_updated = 0;
608 browserp->last_polled = 0;
609 browserp->n_nodes = 0;
610 browserp->n_servers = 0;
611 browserp->n_services = 0;
612 browserp->servicep = 0;
613 browserp->JSONBuffer = 0;
614 browserp->JSONBufferSize = 0;
615 browserp->pattern[0] = '\0';
616 browserp->service_format_ptr = 0;
617 browserp->curr_command[0] = '\0';
618 sll_insert_queue((SLL *)Browser_head,(SLL *)browserp);
619 dtq_start_timer(10, check_browser, browserp);
620 return browserp;
621}
622
623int update_command_data(char *service, int conn_id, BROWSER *browserp)
624{
625 char format[MAX_NAME];
626 char answer[MAX_NAME*3];
627 extern void sendData();
628
629 if(browserp->service_format_ptr)
630 {
631 get_service_format(browserp->service_format_ptr, service, format);
632if(WebDID_Debug)
633printf("CMD data %s %s\n",service,format);
634 }
635 else
636 {
637 strcpy(format,"?");
638 }
639 strcpy(browserp->curr_command, service);
640 sprintf(answer,"To %s (%s)",service, format);
641 sendData(conn_id, answer, 4);
642 return 1;
643}
644
645int update_service_data(char *service, int conn_id, int subscribe, int req, int browser, int force)
646{
647 CURR_SERVICE *servicep;
648 time_t tsecs;
649 void recv_service_info();
650 extern void sendData();
651 BROWSER *browserp;
652
653 if(req){}
654 if(!Curr_service_head)
655 {
656 Curr_service_head = (CURR_SERVICE *)malloc(sizeof(CURR_SERVICE));
657 sll_init((SLL *)Curr_service_head);
658 }
659 if(!(browserp = find_browser(browser)))
660 browserp = create_browser(browser);
661 if(force == -1)
662 {
663 update_command_data(service, conn_id, browserp);
664 return 1;
665 }
666 if((servicep = browserp->servicep))
667 {
668 servicep->n_browsers--;
669 if(!servicep->n_browsers)
670 delete_curr_service(servicep);
671 }
672 if(!(servicep = find_curr_service(service)))
673 {
674 servicep = (CURR_SERVICE *)malloc(sizeof(CURR_SERVICE));
675 strcpy(servicep->name,service);
676 servicep->conn_id = conn_id;
677 servicep->buffer = 0;
678 servicep->buffer_size = 0;
679 servicep->size = 0;
680 servicep->buffer_str = 0;
681 servicep->buffer_str_size = 0;
682 servicep->str_size = 0;
683 servicep->last_updated = 0;
684 tsecs = time((time_t *)0);
685 browserp->last_subscribed = tsecs;
686 browserp->last_updated = tsecs;
687 servicep->last_subscribed = tsecs;
688 servicep->n_browsers = 0;
689 sll_insert_queue((SLL *)Curr_service_head,(SLL *)servicep);
690 servicep->sid = (int)dic_info_service_stamped( service, MONITORED, subscribe, 0, 0,
691 recv_service_info, servicep, &no_link_int, 4);
692 }
693 else
694 {
695 if(servicep->size)
696 {
697 if((servicep->timestamp > browserp->last_updated) || (force))
698 {
699 sendData(conn_id, servicep->buffer_str, 4);
700 }
701 else
702 {
703 sendData(conn_id, "", 4);
704 }
705 browserp->last_updated = servicep->timestamp;
706 }
707 }
708 if(force)
709 {
710 browserp->servicep = servicep;
711 servicep->n_browsers++;
712 }
713 return 1;
714}
715
716int check_browser_changes(char *service, int conn_id, int subscribe, int req, int browser, int force)
717{
718 CURR_SERVICE *servicep;
719 time_t tsecs;
720 void recv_service_info();
721 extern void sendData();
722 BROWSER *browserp;
723 char answer[256];
724 int service_changed = 0;
725
726 if(req){}
727 if(subscribe){}
728 if(!(browserp = find_browser(browser)))
729 browserp = create_browser(browser);
730 if(!Curr_service_head)
731 {
732 Curr_service_head = (CURR_SERVICE *)malloc(sizeof(CURR_SERVICE));
733 sll_init((SLL *)Curr_service_head);
734 }
735 if(service[0] != '\0')
736 {
737 if((servicep = find_curr_service(service)))
738 {
739 if(servicep->size)
740 {
741 if((servicep->timestamp > browserp->last_updated) || (force))
742 {
743 service_changed = 1;
744 }
745 }
746 }
747 }
748/*
749 sprintf(answer,"%d %d %d %d\n",
750 browserp->n_services_changed, browserp->n_servers_changed,
751 browserp->n_nodes_changed, service_changed);
752*/
753 sprintf(answer,"%d %d %d %d %d %d %d\n",
754 N_services, N_servers, N_nodes, service_changed,
755 browserp->n_services, browserp->n_servers, browserp->n_nodes);
756 sendData(conn_id, answer, 4);
757 tsecs = time((time_t *)0);
758 browserp->last_polled = tsecs;
759 return 1;
760}
761
762int find_services(char *pattern, int conn_id, int browser, int force)
763{
764 void recv_service_info();
765 extern void sendData();
766 BROWSER *browserp;
767 char format[MAX_NAME];
768 int prepareJSONTree();
769 void did_prepare_command();
770
771 if(conn_id){}
772 if(!(browserp = find_browser(browser)))
773 browserp = create_browser(browser);
774 if(force == -1)
775 {
776 if(browserp->service_format_ptr)
777 {
778 get_service_format(browserp->service_format_ptr, browserp->curr_command, format);
779 did_prepare_command(pattern, browserp->curr_command, format);
780 }
781 return 1;
782 }
783 if(conn_id){}
784 if(!(browserp = find_browser(browser)))
785 browserp = create_browser(browser);
786 strcpy(browserp->pattern, pattern);
787 return 1;
788}
789
790void recv_service_info(void **tag, int *buffer, int *size)
791{
792 int conn_id;
793 void print_service_formatted();
794 extern void sendData();
795 CURR_SERVICE *servicep;
796 time_t tsecs;
797
798 servicep = *tag;
799 conn_id = servicep->conn_id;
800 if (servicep->buffer_size < *size)
801 {
802 if(servicep->buffer_size)
803 free(servicep->buffer);
804 servicep->buffer = malloc((size_t)*size);
805 servicep->buffer_size = *size;
806 }
807 memcpy(servicep->buffer, (char *)buffer, (size_t)*size);
808 servicep->size = *size;
809 if (servicep->buffer_str_size < (1024 + (*size)*16))
810 {
811 if(servicep->buffer_str_size)
812 free(servicep->buffer_str);
813 servicep->buffer_str = malloc((size_t)(1024 + (*size)*16));
814 servicep->buffer_str_size = 1024 + (*size)*16;
815 }
816 Service_content_str = servicep->buffer_str;
817 strcpy(Curr_service_name, servicep->name);
818 get_curr_service_format();
819 if((*size == 4 ) && (*buffer == -1))
820 {
821 sprintf(Service_content_str,
822 "Service %s Not Available", Curr_service_name);
823 }
824 else
825 {
826 print_service_formatted(servicep, buffer, *size);
827 }
828 if(servicep->last_updated == 0)
829 {
830 sendData(conn_id, Service_content_str, 4);
831 tsecs = time((time_t *)0);
832 servicep->last_updated = tsecs;
833 }
834}
835
836void print_service_formatted(CURR_SERVICE *servicep, void *buff, int size)
837{
838char type;
839int num, ret;
840char str[256];
841char *ptr;
842void *buffer_ptr;
843char timestr[256], aux[64], sizestr[64];
844int quality = 0, secs = 0, mili = 0;
845int did_write_string(char, int, void **, int);
846time_t tsecs;
847
848 if(size < 1024)
849 sprintf(sizestr,"%d bytes",size);
850 else if (size < 1024*1024)
851 sprintf(sizestr,"%2.2f Kb",(float)size/1024);
852 else
853 sprintf(sizestr,"%2.2f Mb",(float)size/(1024*1024));
854
855 sprintf(Service_content_str,
856 "<FONT FACE=\"consolas\">Service %s (%s) Contents :<br /> <br />", Curr_service_name,
857 Curr_service_format);
858 dic_get_timestamp(0, &secs, &mili);
859 quality = dic_get_quality(0);
860 tsecs = secs;
861 servicep->timestamp = tsecs;
862 my_ctime(&tsecs, timestr, 128);
863 ptr = strrchr(timestr,' ');
864 strcpy(aux, ptr);
865 sprintf(ptr,".%03d",mili);
866 strcat(timestr, aux);
867 timestr[(int)strlen(timestr)-1] = '\0';
868
869 sprintf(str," Timestamp: %s&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Quality: %d&nbsp&nbsp&nbsp Size: %s<br /><br />",
870 timestr, quality, sizestr);
871
872 strcat(Service_content_str,str);
873 ptr = Curr_service_format;
874 buffer_ptr = buff;
875 while(*ptr)
876 {
877 type = *ptr++;
878 if(*ptr == ':')
879 {
880 ptr++;
881 sscanf(ptr, "%d", &num);
882 ret = did_write_string(type, num, &buffer_ptr, size);
883 size -= ret;
884 if( (ptr = strchr(ptr,';')) )
885 ptr++;
886 else
887 break;
888 }
889 else
890 {
891 ret = did_write_string(type, 0, &buffer_ptr, size);
892 size -= ret;
893 break;
894 }
895 }
896 strcat(Service_content_str,"</FONT>");
897}
898
899
900int did_write_string(char type, int num, void **buffer_ptr, int ssize)
901{
902void *ptr;
903int size, psize;
904
905 void print_service_standard();
906 void print_service_char();
907 void print_service_short();
908 void print_service_float();
909 void print_service_double();
910
911 ptr = *buffer_ptr;
912 switch(type)
913 {
914 case 'L':
915 case 'l':
916 strcat(Service_content_str," L");
917 if(!num)
918 size = ssize/(int)sizeof(int);
919 else
920 size = num;
921 psize = size * (int)sizeof(int);
922 print_service_standard(ptr, size);
923 break;
924 case 'I':
925 case 'i':
926 strcat(Service_content_str," I");
927 if(!num)
928 size = ssize/(int)sizeof(int);
929 else
930 size = num;
931 psize = size * (int)sizeof(int);
932 print_service_standard(ptr, size);
933 break;
934 case 'S':
935 case 's':
936 strcat(Service_content_str," S");
937 if(!num)
938 size = ssize/(int)sizeof(short);
939 else
940 size = num;
941 psize = size * (int)sizeof(short);
942 print_service_short(ptr, size);
943 break;
944 case 'F':
945 case 'f':
946 strcat(Service_content_str," F");
947 if(!num)
948 size = ssize/(int)sizeof(float);
949 else
950 size = num;
951 psize = size * (int)sizeof(float);
952 print_service_float(ptr, size);
953 break;
954 case 'D':
955 case 'd':
956 strcat(Service_content_str," D");
957 if(!num)
958 size = ssize/(int)sizeof(double);
959 else
960 size = num;
961 psize = size * (int)sizeof(double);
962 print_service_double(ptr, size);
963 break;
964 case 'X':
965 case 'x':
966 strcat(Service_content_str," X");
967 if(!num)
968 size = ssize/(int)sizeof(longlong);
969 else
970 size = num;
971 psize = size * (int)sizeof(longlong);
972 print_service_standard(ptr, size*2);
973 break;
974 case 'C':
975 case 'c':
976 default:
977 strcat(Service_content_str," C");
978 if(!num)
979 size = ssize;
980 else
981 size = num;
982 psize = size;
983 print_service_char(ptr, size);
984 }
985 ptr = (char *)ptr + psize;
986 *buffer_ptr = ptr;
987 return psize;
988}
989
990void sprintf_html(char *str, int n, int value)
991{
992 char tmp[80];
993 int min, i;
994
995 str[0] = '\0';
996 min = sprintf(tmp,"%d",value);
997 for(i = 0; i < (n-min); i++)
998 {
999 strcat(str,"&nbsp");
1000 }
1001 strcat(str, tmp);
1002}
1003
1004void print_service_standard(int *buff, int size)
1005{
1006int i,j;
1007char *ptr, str[80], tmp[256];
1008int last[4];
1009
1010 ptr = Service_content_str;
1011 ptr += (int)strlen(Service_content_str);
1012 for( i = 0; i < size; i++)
1013 {
1014 strcpy(tmp,"");
1015 if(i%4 == 0)
1016 {
1017 if(i != 0)
1018 {
1019 strcat(tmp,"&nbsp");
1020 }
1021 sprintf_html(str, 7, i);
1022 strcat(tmp,str);
1023 }
1024 if(!(i%4))
1025 strcat(tmp,"H: ");
1026 sprintf(str,"&nbsp&nbsp&nbsp %08X",buff[i]);
1027 strcat(tmp,str);
1028 last[i%4] = buff[i];
1029 if((i%4 == 3) || (i == (size-1)))
1030 {
1031 strcat(tmp,"<br />");
1032 for(j = 0; j <= (i%4); j++)
1033 {
1034 if(j == 0)
1035 strcat(tmp,"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp D: ");
1036 sprintf_html(str, 12, last[j]);
1037 strcat(tmp,str);
1038 }
1039 strcat(tmp,"<br />");
1040 }
1041 strcpy(ptr, tmp);
1042 ptr += (int)strlen(tmp);
1043 }
1044 strcpy(tmp,"<br />");
1045 strcpy(ptr, tmp);
1046}
1047
1048void print_service_longlong(longlong *buff, int size)
1049{
1050int i,j;
1051char *ptr, str[80], tmp[256];
1052longlong last[4];
1053
1054 ptr = Service_content_str;
1055 ptr += (int)strlen(Service_content_str);
1056 for( i = 0; i < size; i++)
1057 {
1058 strcpy(tmp,"");
1059 if(i%4 == 0)
1060 {
1061 if(i != 0)
1062 {
1063 strcat(tmp,"&nbsp");
1064 }
1065 sprintf_html(str, 7, i);
1066 strcat(tmp,str);
1067 }
1068 if(!(i%4))
1069 strcat(tmp,"H: ");
1070 sprintf(str,"&nbsp&nbsp&nbsp %08X",(unsigned)buff[i]);
1071 strcat(tmp,str);
1072 last[i%4] = buff[i];
1073 if((i%4 == 3) || (i == (size-1)))
1074 {
1075 strcat(tmp,"<br />");
1076 for(j = 0; j <= (i%4); j++)
1077 {
1078 if(j == 0)
1079 strcat(tmp,"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp D: ");
1080 sprintf_html(str, 12, (int)last[j]);
1081 strcat(tmp,str);
1082 }
1083 strcat(tmp,"<br />");
1084 }
1085 strcpy(ptr, tmp);
1086 ptr += (int)strlen(tmp);
1087 }
1088 strcpy(tmp,"<br />");
1089 strcpy(ptr, tmp);
1090}
1091
1092void print_service_short(short *buff, int size)
1093{
1094int i,j;
1095char *ptr, str[80], tmp[256];
1096short last[8];
1097
1098 ptr = Service_content_str;
1099 ptr += (int)strlen(Service_content_str);
1100 for( i = 0; i < size; i++)
1101 {
1102 strcpy(tmp,"");
1103 if(i%8 == 0)
1104 {
1105 if(i != 0)
1106 {
1107 strcat(tmp,"&nbsp");
1108 }
1109 sprintf_html(str, 7, i);
1110 strcat(tmp,str);
1111 }
1112 if(!(i%8))
1113 strcat(tmp,"H: ");
1114 sprintf(str,"&nbsp %04X",buff[i]);
1115 strcat(tmp,str);
1116 last[i%8] = buff[i];
1117 if((i%8 == 7) || (i == (size-1)))
1118 {
1119 strcat(tmp,"<br />");
1120 for(j = 0; j <= (i%8); j++)
1121 {
1122 if(j == 0)
1123 strcat(tmp,"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp D: ");
1124 sprintf_html(str, 6, last[j]);
1125 strcat(tmp,str);
1126 }
1127 strcat(tmp,"<br />");
1128 }
1129 strcpy(ptr, tmp);
1130 ptr += (int)strlen(tmp);
1131 }
1132 strcpy(tmp,"<br />");
1133 strcpy(ptr, tmp);
1134}
1135
1136void print_service_char(char *buff, int size)
1137{
1138int i,j;
1139char *asc, *ptr, str[80], tmp[256];
1140
1141 asc = (char *)buff;
1142 ptr = Service_content_str;
1143 ptr += (int)strlen(Service_content_str);
1144 for( i = 0; i < size; i++)
1145 {
1146 strcpy(tmp,"");
1147 if(i%16 == 0)
1148 {
1149 if(i != 0)
1150 {
1151 strcat(tmp,"&nbsp");
1152 }
1153 sprintf_html(str, 7, i);
1154 strcat(tmp,str);
1155 }
1156 if(!(i%16))
1157 strcat(tmp,"H: ");
1158 sprintf(str,"%02X",buff[i]);
1159/* strcat(tmp,str);
1160*/
1161 strcat(tmp," ");
1162 strcat(tmp,&str[(int)strlen(str)-2]);
1163 /*
1164 if(i%4 == 3)
1165 strcat(tmp," ");
1166 */
1167 if((i%16 == 15) || (i == (size-1)))
1168 {
1169 if(i%16 != 15)
1170 {
1171 for(j = 1; j < 16 - (i%16); j++)
1172 strcat(tmp,"&nbsp&nbsp ");
1173 }
1174 strcat(tmp,"&nbsp&nbsp&nbsp '");
1175 for(j = 0; j <= (i%16) ; j++)
1176 {
1177 if(isprint(asc[j]))
1178 {
1179 sprintf(str,"%c",asc[j]);
1180 strcat(tmp,str);
1181 }
1182 else
1183 {
1184 sprintf(str,".");
1185 strcat(tmp,str);
1186 }
1187 }
1188 strcat(tmp,"'<br />");
1189 asc = (char *)&buff[i+1];
1190 }
1191 strcpy(ptr, tmp);
1192 ptr += (int)strlen(tmp);
1193 }
1194 strcpy(tmp,"<br />");
1195 strcpy(ptr, tmp);
1196}
1197
1198void print_service_float(float *buff, int size)
1199{
1200int i;
1201char *ptr, str[80], tmp[256];
1202
1203 ptr = Service_content_str;
1204 ptr += (int)strlen(Service_content_str);
1205 for( i = 0; i < size; i++)
1206 {
1207 strcpy(tmp,"");
1208 if(i%4 == 0)
1209 {
1210 if(i != 0)
1211 {
1212 strcat(tmp,"&nbsp");
1213 }
1214 sprintf_html(str, 7, i);
1215 strcat(tmp,str);
1216 }
1217 sprintf(str,"%12.3G",*(buff++));
1218 strcat(tmp,str);
1219 if((i%4 == 3) || (i == size-1))
1220 {
1221 strcat(tmp,"<br />");
1222 }
1223 strcpy(ptr, tmp);
1224 ptr += (int)strlen(tmp);
1225 }
1226 strcpy(tmp,"<br />");
1227 strcpy(ptr, tmp);
1228 ptr += (int)strlen(tmp);
1229}
1230
1231void print_service_double(double *buff, int size)
1232{
1233int i;
1234char *ptr, str[80], tmp[256];
1235
1236 ptr = Service_content_str;
1237 ptr += (int)strlen(Service_content_str);
1238 for( i = 0; i < size; i++)
1239 {
1240 strcpy(tmp,"");
1241 if(i%4 == 0)
1242 {
1243 if(i != 0)
1244 {
1245 strcat(tmp,"&nbsp");
1246 }
1247 sprintf_html(str, 7, i);
1248 strcat(tmp,str);
1249 }
1250 sprintf(str,"%12.3G",*(buff++));
1251 strcat(tmp,str);
1252 if((i%4 == 3) || (i == size-1))
1253 {
1254 strcat(tmp,"<br />");
1255 }
1256 strcpy(ptr, tmp);
1257 ptr += (int)strlen(tmp);
1258 }
1259 strcpy(tmp,"<br />");
1260 strcpy(ptr, tmp);
1261 ptr += (int)strlen(tmp);
1262}
1263
1264char *addJSONStart(char *ptr)
1265{
1266 char *ptro;
1267
1268 strcat(ptr,"{\n");
1269 ptro = ptr + (int)strlen(ptr);
1270 return ptro;
1271}
1272
1273char *addJSONEnd(char *ptr)
1274{
1275 char *ptro;
1276
1277 strcat(ptr,"}\n");
1278 ptro = ptr + (int)strlen(ptr);
1279 return ptro;
1280}
1281
1282char *addJSONNodeStart(char *ptr, char *node)
1283{
1284 char *ptro;
1285
1286 sprintf(ptr,"%s: [\n", node);
1287 ptro = ptr + (int)strlen(ptr);
1288 return ptro;
1289}
1290
1291char *addJSONNodeEnd(char *ptr)
1292{
1293 char *ptro;
1294
1295 strcat(ptr,"]\n");
1296 ptro = ptr + (int)strlen(ptr);
1297 return ptro;
1298}
1299
1300char *addJSONChildStart(char *ptr, char *child, int sep)
1301{
1302 char *ptro;
1303
1304 if(sep)
1305 sprintf(ptr,"{ %s, ", child);
1306 else
1307 sprintf(ptr,"{ %s", child);
1308 ptro = ptr + (int)strlen(ptr);
1309 return ptro;
1310}
1311
1312char *addJSONChildEnd(char *ptr, int sep)
1313{
1314 char *ptro;
1315
1316 if(sep)
1317 strcat(ptr," },\n");
1318 else
1319 strcat(ptr," }\n");
1320 ptro = ptr + (int)strlen(ptr);
1321 return ptro;
1322}
1323
1324char *getJSONBuffer(char *node, int browser)
1325{
1326 BROWSER *browserp;
1327 int prepareJSONTree();
1328
1329 if(browser)
1330 {
1331 if((browserp = find_browser(browser)))
1332 {
1333 if(browserp->pattern[0] != '\0')
1334 {
1335 prepareJSONTree(node, browserp);
1336 return(browserp->JSONBuffer);
1337 }
1338 browserp->n_services = 0;
1339 browserp->n_servers = 0;
1340 browserp->n_nodes = 0;
1341 }
1342 }
1343 prepareJSONTree(node, 0);
1344 return(JSONBuffer);
1345}
1346
1347char *getJSONHeader()
1348{
1349 int prepareJSONHeader();
1350
1351 if(JSONHeader[0] == '\0')
1352 prepareJSONHeader();
1353 return(JSONHeader);
1354}
1355
1356int getNodeLabel(char *name, char *label)
1357{
1358 int i;
1359 extern int web_get_node_name();
1360
1361 web_get_node_name(name, label);
1362 for(i = 0; i < ((int)strlen(label) + 1); i++)
1363 {
1364 label[i] = (char)tolower((int)label[i]);
1365 if(label[i] == '.')
1366 {
1367 label[i] = '\0';
1368 break;
1369 }
1370 }
1371 return 1;
1372}
1373
1374int prepareJSONTree(char *node, BROWSER *browserp)
1375{
1376 char *ptr;
1377 NODE *nodep;
1378 SERVER *servp;
1379 char str[256], aux[128];
1380 int selective = 0;
1381 int n_nodes, tot_n_nodes;
1382 int n_servers, tot_n_servers;
1383 int ret, n_found = 0;
1384
1385 if(browserp)
1386 {
1387 if(browserp->pattern[0] != '\0')
1388 selective = 1;
1389 else
1390 return(0);
1391 }
1392 if(!selective)
1393 {
1394 if(JSONBufferSize == 0)
1395 {
1396 JSONBuffer = malloc((size_t)(N_nodes*128+N_servers*128));
1397 }
1398 else if (JSONBufferSize < N_nodes*128+N_servers*128)
1399 {
1400 free(JSONBuffer);
1401 JSONBuffer = malloc((size_t)(N_nodes*128+N_servers*128));
1402 }
1403 ptr = JSONBuffer;
1404 }
1405 else
1406 {
1407 if(browserp->JSONBufferSize == 0)
1408 {
1409 browserp->JSONBuffer = malloc((size_t)(N_nodes*128+N_servers*128));
1410 }
1411 else if (browserp->JSONBufferSize < N_nodes*128+N_servers*128)
1412 {
1413 free(browserp->JSONBuffer);
1414 browserp->JSONBuffer = malloc((size_t)(N_nodes*128+N_servers*128));
1415 }
1416 ptr = browserp->JSONBuffer;
1417 }
1418 *ptr = '\0';
1419 if(!strcmp(node, "src"))
1420 {
1421 ptr = addJSONStart(ptr);
1422 ptr = addJSONNodeStart(ptr,"children");
1423 sprintf(str,"text: \"%s\", id: \"Nodes\", expanded: false", Title);
1424 ptr = addJSONChildStart(ptr,str,1);
1425 ptr = addJSONNodeStart(ptr,"children");
1426 nodep = Node_head;
1427 tot_n_nodes = 0;
1428 while( (nodep = (NODE *)sll_get_next((SLL *)nodep)) )
1429 {
1430 nodep->match = 1;
1431 if(selective)
1432 {
1433 if(!(ret = find_service_pattern(nodep, 0, browserp->pattern, &tot_n_servers)))
1434 {
1435 nodep->match = 0;
1436 continue;
1437 }
1438 else
1439 {
1440 n_found += ret;
1441 }
1442 }
1443 tot_n_nodes++;
1444 }
1445 n_nodes = 0;
1446 nodep = Node_head;
1447 while( (nodep = (NODE *)sll_get_next((SLL *)nodep)) )
1448 {
1449 if(!nodep->match)
1450 continue;
1451 getNodeLabel(nodep->name, aux);
1452 sprintf(str,"text: \"%s\", id: \"%s\", qtip: \"%s\"",
1453 aux, nodep->name, nodep->name);
1454 ptr = addJSONChildStart(ptr,str,0);
1455 n_nodes++;
1456if(WebDID_Debug)
1457 printf("adding %s %d %d\n",nodep->name, n_nodes, tot_n_nodes);
1458 if(n_nodes < tot_n_nodes)
1459 ptr = addJSONChildEnd(ptr,1);
1460 else
1461 ptr = addJSONChildEnd(ptr,0);
1462 }
1463 ptr = addJSONNodeEnd(ptr);
1464 ptr = addJSONChildEnd(ptr,0);
1465 ptr = addJSONNodeEnd(ptr);
1466 ptr = addJSONEnd(ptr);
1467 if(selective)
1468 {
1469 browserp->n_services = n_found;
1470 browserp->n_servers = tot_n_servers;
1471 browserp->n_nodes = tot_n_nodes;
1472 }
1473 }
1474 else
1475 {
1476 if((nodep = find_node(node)))
1477 {
1478 ptr = addJSONStart(ptr);
1479 ptr = addJSONNodeStart(ptr,"children");
1480 servp = nodep->server_head;
1481 tot_n_servers = 0;
1482 while( (servp = (SERVER *)sll_get_next((SLL *)servp)) )
1483 {
1484 servp->match = 1;
1485 if(servp->busy != 1)
1486 {
1487 servp->match = 0;
1488 continue;
1489 }
1490 if(selective)
1491 {
1492 if(!(ret = find_service_pattern(nodep, servp, browserp->pattern, 0)))
1493 {
1494 servp->match = 0;
1495 continue;
1496 }
1497 else
1498 {
1499 n_found += ret;
1500 }
1501 }
1502 tot_n_servers++;
1503 }
1504 n_servers = 0;
1505 servp = nodep->server_head;
1506 while( (servp = (SERVER *)sll_get_next((SLL *)servp)) )
1507 {
1508 if(!servp->match)
1509 continue;
1510 sprintf(str,"text: \"%s\", id: \"%d\", leaf: true, icon: \"server.png\"",servp->name, servp->server.pid);
1511 ptr = addJSONChildStart(ptr,str,0);
1512 n_servers++;
1513 if(n_servers < tot_n_servers)
1514 ptr = addJSONChildEnd(ptr,1);
1515 else
1516 ptr = addJSONChildEnd(ptr,0);
1517 }
1518 ptr = addJSONNodeEnd(ptr);
1519 ptr = addJSONEnd(ptr);
1520 }
1521 }
1522 /*
1523if(!selective)
1524printf(" Nodes&Servers %s\n",JSONBuffer);
1525else
1526printf(" Nodes&Servers %s\n",browserp->JSONBuffer);
1527 */
1528 return(1);
1529}
1530
1531int prepareJSONHeader()
1532{
1533 char *ptr;
1534 char str[128];
1535
1536 ptr = JSONHeader;
1537 *ptr = '\0';
1538 ptr = addJSONStart(ptr);
1539 ptr = addJSONNodeStart(ptr,"items");
1540 sprintf(str,"text: \"%s\"",Title);
1541 ptr = addJSONChildStart(ptr,str,0);
1542 ptr = addJSONChildEnd(ptr,1);
1543 sprintf(str,"text: \"%d Servers Known - %d Services Available\"",N_servers, N_services);
1544 ptr = addJSONChildStart(ptr,str,0);
1545 ptr = addJSONChildEnd(ptr,0);
1546 ptr = addJSONNodeEnd(ptr);
1547 ptr = addJSONEnd(ptr);
1548if(WebDID_Debug)
1549printf(" Header %s\n",JSONHeader);
1550 return(1);
1551}
1552
1553char *JSONServices = 0;
1554int JSONServicesSize = 0;
1555char *prepareJSONServiceList(SERVER *servp, char *node, int pid, BROWSER *browserp)
1556{
1557 DNS_SERVICE_INFO *servicep;
1558 char *ptr;
1559 int n_services, i;
1560 char str[256], type_str[256];
1561 int selective = 0;
1562 int n_found = 0, n;
1563
1564 servicep = servp->service_ptr;
1565 n_services = servp->server.n_services;
1566 if(JSONServicesSize == 0)
1567 {
1568 JSONServices = malloc((size_t)(n_services*256));
1569 }
1570 else if (JSONServicesSize < n_services*256)
1571 {
1572 free(JSONServices);
1573 JSONServices = malloc((size_t)(n_services*256));
1574 }
1575 if(browserp)
1576 {
1577 if(browserp->pattern[0] != '\0')
1578 selective = 1;
1579 }
1580 n_found = n_services;
1581 if(selective)
1582 {
1583 n_found = find_server_service_pattern(servp, browserp->pattern);
1584 }
1585 ptr = JSONServices;
1586 *ptr = '\0';
1587 ptr = addJSONStart(ptr);
1588 ptr = addJSONNodeStart(ptr,"children");
1589 if(selective)
1590 sprintf(str,"text: \"%s (%d/%d services, pid %d)\"",servp->name, n_found, n_services, servp->server.pid);
1591 else
1592 sprintf(str,"text: \"%s (%d services, pid %d)\"",servp->name, n_services, servp->server.pid);
1593 ptr = addJSONChildStart(ptr,str,1);
1594 ptr = addJSONNodeStart(ptr,"children");
1595 servicep = servp->service_ptr;
1596 n = 0;
1597 for(i = 0; i < n_services; i++)
1598 {
1599/*
1600printf("Service type = %d\n",servicep->type);
1601*/
1602 if((!selective) || (strstr(servicep->name, browserp->pattern)))
1603 {
1604 if(servicep->type == 1)
1605 {
1606 sprintf(type_str,"%d@%s|%s|CMD", pid, node, servicep->name);
1607 sprintf(str,"text: \"%s\", id: \"%s\", leaf: true, icon: \"leaf_cmd.gif\"",servicep->name, type_str);
1608 }
1609 else
1610 {
1611 sprintf(type_str,"%d@%s|%s", pid, node, servicep->name);
1612 sprintf(str,"text: \"%s\", id: \"%s\", leaf: true",servicep->name, type_str);
1613 }
1614 ptr = addJSONChildStart(ptr,str,0);
1615 n++;
1616 if(n < n_found)
1617 ptr = addJSONChildEnd(ptr,1);
1618 else
1619 ptr = addJSONChildEnd(ptr,0);
1620 }
1621 servicep++;
1622 }
1623 ptr = addJSONNodeEnd(ptr);
1624 ptr = addJSONChildEnd(ptr,0);
1625 ptr = addJSONNodeEnd(ptr);
1626 ptr = addJSONEnd(ptr);
1627 return JSONServices;
1628}
1629
1630int matchString( char *wzString, char *wzPattern )
1631{
1632 switch (*wzPattern){
1633 case '\0':
1634 return !*wzString;
1635 case '*':
1636 return matchString(wzString, wzPattern+1) ||
1637 ( *wzString && matchString(wzString+1, wzPattern) );
1638 case '?':
1639 return *wzString &&
1640 matchString(wzString+1, wzPattern+1);
1641 default:
1642 return (*wzPattern == *wzString) &&
1643 matchString(wzString+1, wzPattern+1);
1644 }
1645}
1646
1647int get_type_size(char type)
1648{
1649 int size;
1650
1651 switch(type)
1652 {
1653 case 'L':
1654 case 'l':
1655 size = sizeof(long);
1656 break;
1657 case 'I':
1658 case 'i':
1659 size = sizeof(int);
1660 break;
1661 case 'S':
1662 case 's':
1663 size = sizeof(short);
1664 break;
1665 case 'F':
1666 case 'f':
1667 size = sizeof(float);
1668 break;
1669 case 'D':
1670 case 'd':
1671 size = sizeof(double);
1672 break;
1673 case 'C':
1674 case 'c':
1675 default:
1676 size = 1;
1677 }
1678 return(size);
1679}
1680
1681void did_prepare_command(char *str, char *service, char *format)
1682{
1683char type;
1684int num;
1685int size, full_size = 0;
1686char *ptr;
1687static int last_size = 0;
1688static void *last_buffer = 0;
1689void *buffer_ptr;
1690char *str_ptr;
1691void did_read_string(char, int, void **, char **);
1692
1693 str_ptr = str;
1694 ptr = format;
1695 while(*ptr)
1696 {
1697 type = *ptr++;
1698 if(*ptr == ':')
1699 {
1700 ptr++;
1701 size = get_type_size(type);
1702 sscanf(ptr, "%d", &num);
1703 full_size += size * num;
1704 if( (ptr = strchr(ptr,';')) )
1705 ptr++;
1706 else
1707 break;
1708 }
1709 }
1710
1711 full_size += 256;
1712 if(full_size > last_size)
1713 {
1714 if(last_size)
1715 free(last_buffer);
1716 last_buffer = malloc((size_t)full_size);
1717 last_size = full_size;
1718 }
1719 memset(last_buffer, 0, (size_t)last_size);
1720 buffer_ptr = last_buffer;
1721 ptr = format;
1722 while(*ptr)
1723 {
1724 type = *ptr++;
1725 if(*ptr == ':')
1726 {
1727 ptr++;
1728 sscanf(ptr, "%d", &num);
1729 did_read_string(type, num, &buffer_ptr, &str_ptr);
1730 if(!str_ptr)
1731 break;
1732 if( (ptr = strchr(ptr,';')) )
1733 ptr++;
1734 else
1735 break;
1736 }
1737 else
1738 {
1739 did_read_string(type, 0, &buffer_ptr, &str_ptr);
1740 break;
1741 }
1742 }
1743 full_size = (int) ((char *)buffer_ptr - (char *)last_buffer);
1744 dic_cmnd_service(service,last_buffer,full_size);
1745}
1746
1747int read_str_int(char *str)
1748{
1749 int i;
1750 if((str[0] == '0') && (str[1] == 'x'))
1751 sscanf(str+2,"%x",&i);
1752 else
1753 sscanf(str,"%d",&i);
1754 return(i);
1755}
1756
1757int read_str_char(char *str, char *cc)
1758{
1759 int num;
1760
1761 if(str[0] == '\'')
1762 *cc = str[1];
1763 else if(str[0] == '\"')
1764 return(0);
1765 else if((str[0] == '0') && (str[1] == 'x'))
1766 {
1767 sscanf(str+2,"%x",&num);
1768 if(num <= 0xff)
1769 *cc = (char)num;
1770 else
1771 return(-1);
1772 }
1773 else if(isalpha(str[0]))
1774 return(-1);
1775 else
1776 {
1777 sscanf(str,"%d",&num);
1778 if(num <= 0xff)
1779 *cc = (char)num;
1780 else
1781 return(-1);
1782 }
1783 return(1);
1784}
1785
1786void did_read_string(char type, int num, void **buffer_ptr, char **str_ptr)
1787{
1788int i, ret = 0;
1789float ff;
1790double dd;
1791void *ptr;
1792char *strp, *ptr1;
1793char cc;
1794 short s;
1795
1796 strp = *str_ptr;
1797 ptr = *buffer_ptr;
1798 if(!num)
1799 num = 1000000;
1800 switch(type)
1801 {
1802 case 'L':
1803 case 'l':
1804 case 'I':
1805 case 'i':
1806 for(i = 0; i<num; i++)
1807 {
1808 *(int *)ptr = read_str_int(strp);
1809 ptr = (int *)ptr +1;
1810 if( (strp = strchr(strp,' ')) )
1811 strp++;
1812 else
1813 break;
1814 }
1815 break;
1816 case 'S':
1817 case 's':
1818 for(i = 0; i<num; i++)
1819 {
1820 s = (short)read_str_int(strp);
1821 *((short *)ptr) = s;
1822 ptr = (short *)ptr +1;
1823 if( (strp = strchr(strp,' ')) )
1824 strp++;
1825 else
1826 break;
1827 }
1828 break;
1829 case 'F':
1830 case 'f':
1831 for(i = 0; i<num; i++)
1832 {
1833 sscanf(strp,"%f",&ff);
1834 *(float *)ptr = ff;
1835 ptr = (float *)ptr +1;
1836 if( (strp = strchr(strp,' ')) )
1837 strp++;
1838 else
1839 break;
1840 }
1841 break;
1842 case 'D':
1843 case 'd':
1844 for(i = 0; i<num; i++)
1845 {
1846 sscanf(strp,"%f",&ff);
1847 dd = (double)ff;
1848 *(double *)ptr = dd;
1849 ptr = (double *)ptr +1;
1850 if( (strp = strchr(strp,' ')) )
1851 strp++;
1852 else
1853 break;
1854 }
1855 break;
1856 case 'C':
1857 case 'c':
1858 default:
1859 for(i = 0; i<num; i++)
1860 {
1861 if((ret = read_str_char(strp, &cc)) <= 0)
1862 break;
1863 *(char *)ptr = cc;
1864 ptr = (char *)ptr +1;
1865 if( (strp = strchr(strp,' ')) )
1866 strp++;
1867 else
1868 break;
1869 }
1870 if(ret <= 0)
1871 {
1872 if(!ret)
1873 {
1874 strp++;
1875 }
1876 num = (int)strlen(strp)+1;
1877 strncpy((char *)ptr,strp,(size_t)num);
1878 if( (ptr1 = (char *)strchr((char *)ptr,'\"')) )
1879 {
1880 num--;
1881 *ptr1 = '\0';
1882 }
1883 ptr = (char *)ptr + num;
1884 if( (strp = strchr(strp,' ')) )
1885 strp++;
1886 else
1887 break;
1888 }
1889 }
1890 *buffer_ptr = ptr;
1891 *str_ptr = strp;
1892}
Note: See TracBrowser for help on using the repository browser.