00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "typedefs.h"
00015 #include "f_his_hist.h"
00016 #include "f_his_toupper.h"
00017 #include "f_his_swpbas.h"
00018 #include "f_stccomm.h"
00019 #include "f_swaplw.h"
00020 #include "f_ut_compress.h"
00021 #include "portnum_def.h"
00022 #include <stdio.h>
00023 #include <errno.h>
00024 #include <ctype.h>
00025 #include <string.h>
00026 #include <stdlib.h>
00027 static CHARS c_gl_serv_access[64];
00028 static CHARS c_gl_serv_base[32];
00029 static INTS4 l_gl_serv_port;
00030 static INTS4 l_gl_serv_chan;
00031 static INTS4 l_gl_serv_verb;
00032 struct s_tcpcomm *ps_tcpserv;
00033 s_his_comm s_his_comm_serv;
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 INTS4 f_his_getbas(CHARS *pc_server, INTS4 l_port, CHARS *pc_base, CHARS *pc_access,INTS4 **p_buffer)
00083 {
00084 s_his_comm s_his_comm_cli;
00085 s_his_head *ps_his_head;
00086 INTS4 i_j,i_l,l_status,l_chan,l_swap,l_buffer,l_histos,l_size;
00087 INTS4 *pl_all,*pl_l;
00088 s_compress *ps_compress;
00089 struct s_tcpcomm *ps_tcpcli;
00090
00091 ps_tcpcli = (struct s_tcpcomm *) malloc (sizeof( struct s_tcpcomm));
00092
00093
00094 s_his_comm_cli.lu_endian=1;
00095 s_his_comm_cli.lu_size=0;
00096 s_his_comm_cli.lu_histos=0;
00097 s_his_comm_cli.lu_action=COMM__GETBASE;
00098 s_his_comm_cli.l_status=COMM__SUCCESS;
00099 strcpy(s_his_comm_cli.c_histo,"*");
00100 strcpy(s_his_comm_cli.c_access,pc_access);
00101 strcpy(s_his_comm_cli.c_base,pc_base);
00102 l_status = f_stc_connectserver (pc_server,l_port, &l_chan, ps_tcpcli);
00103 if(l_status != STC__SUCCESS) {l_status=COMM__NOSERVER;goto g_return;}
00104 l_status = f_stc_write(&s_his_comm_cli,sizeof(s_his_comm),l_chan);
00105 if(l_status != STC__SUCCESS) {l_status=COMM__ERROR;goto g_return;}
00106 l_status = f_stc_read (&s_his_comm_cli,sizeof(s_his_comm),l_chan,-1);
00107 if(l_status != STC__SUCCESS) {l_status=COMM__ERROR;goto g_return;}
00108 l_swap = (s_his_comm_cli.lu_endian > 1);
00109 if(l_swap) l_status = f_swaplw((INTS4 *)&s_his_comm_cli,5,NULL);
00110 if(s_his_comm_cli.l_status != COMM__SUCCESS) {l_status=s_his_comm_cli.l_status;goto g_return;}
00111
00112
00113 pl_all = (INTS4 *)malloc(s_his_comm_cli.lu_size);
00114 l_buffer = s_his_comm_cli.lu_size;
00115
00116 pl_l=pl_all;
00117 i_l=l_buffer/16384;
00118 l_buffer=l_buffer%16384;
00119 for(i_j=0;i_j<i_l;i_j++){l_status = f_stc_read (pl_l,16384,l_chan,-1);pl_l+=4096;}
00120 if(l_buffer > 0) l_status = f_stc_read (pl_l,l_buffer,l_chan,-1);
00121 if(l_status != STC__SUCCESS) {l_status=COMM__ERROR;goto g_return;}
00122
00123 ps_compress = (s_compress *) pl_all;
00124 if(l_swap == 1)
00125 {
00126 f_swaplw((INTS4 *)pl_all,sizeof(s_compress)/4,NULL);
00127 pl_l=pl_all+(sizeof(s_compress)/4);
00128 f_swaplw((INTS4 *)pl_l,ps_compress->l_masks,NULL);
00129 }
00130 *p_buffer = (INTS4 *)malloc(ps_compress->l_full_bytes);
00131 f_ut_compr_unpack((INTU1 *)pl_all,(INTU1 *) *p_buffer,ps_compress->l_full_bytes);
00132 if(l_swap == 1) l_status = f_his_swpbas((s_head*) *p_buffer);
00133
00134 l_status=COMM__SUCCESS;
00135 g_return:
00136 f_stc_discclient (l_chan);
00137 f_stc_close (ps_tcpcli);
00138 free(pl_all);
00139 free(ps_tcpcli);
00140 return(l_status);
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189 INTS4 f_his_getdir(CHARS *pc_server, INTS4 l_port, CHARS *pc_base, CHARS *pc_access, CHARS *pc_histo
00190 ,INTS4 **p_buffer, INTS4 *pl_histos)
00191 {
00192 s_his_comm s_his_comm_cli;
00193 s_his_head *ps_his_head;
00194 INTS4 i_j,i_l,l_status,l_chan,l_swap,l_buffer,l_histos,l_size;
00195 INTS4 *pl_all,*pl_l;
00196 struct s_tcpcomm *ps_tcpcli;
00197
00198 ps_tcpcli = (struct s_tcpcomm *) malloc (sizeof( struct s_tcpcomm));
00199
00200
00201 s_his_comm_cli.lu_endian=1;
00202 s_his_comm_cli.lu_size=0;
00203 s_his_comm_cli.lu_histos=0;
00204 s_his_comm_cli.lu_action=COMM__GETDIR;
00205 s_his_comm_cli.l_status=COMM__SUCCESS;
00206 strcpy(s_his_comm_cli.c_histo,pc_histo);
00207 strcpy(s_his_comm_cli.c_access,pc_access);
00208 strcpy(s_his_comm_cli.c_base,pc_base);
00209 l_status = f_stc_connectserver (pc_server,l_port, &l_chan, ps_tcpcli);
00210 if(l_status != STC__SUCCESS) {l_status=COMM__NOSERVER;goto g_return;}
00211 l_status = f_stc_write(&s_his_comm_cli,sizeof(s_his_comm),l_chan);
00212 if(l_status != STC__SUCCESS) {l_status=COMM__ERROR;goto g_return;}
00213 l_status = f_stc_read (&s_his_comm_cli,sizeof(s_his_comm),l_chan,-1);
00214 if(l_status != STC__SUCCESS) {l_status=COMM__ERROR;goto g_return;}
00215 l_swap = (s_his_comm_cli.lu_endian > 1);
00216 if(l_swap) l_status = f_swaplw((INTS4 *)&s_his_comm_cli,5,NULL);
00217 if(s_his_comm_cli.l_status != COMM__SUCCESS) {l_status=s_his_comm_cli.l_status;goto g_return;}
00218
00219 l_histos=s_his_comm_cli.lu_histos;
00220 l_size=sizeof(s_his_head)*l_histos;
00221
00222 if(*p_buffer == 0)
00223 {
00224 pl_all = (INTS4 *)malloc(l_size);
00225 *p_buffer = pl_all;
00226 }
00227 else
00228 {
00229 if(*pl_histos < l_histos) {l_status=COMM__ERROR;goto g_return;}
00230 pl_all = *p_buffer;
00231 }
00232 *pl_histos = l_histos;
00233 l_buffer = s_his_comm_cli.lu_size;
00234
00235 pl_l=pl_all;
00236 i_l=l_buffer/16384;
00237 l_buffer=l_buffer%16384;
00238 for(i_j=0;i_j<i_l;i_j++){l_status = f_stc_read (pl_l,16384,l_chan,-1);pl_l+=4096;}
00239 if(l_buffer > 0) l_status = f_stc_read (pl_l,l_buffer,l_chan,-1);
00240 if(l_status != STC__SUCCESS) {l_status=COMM__ERROR;goto g_return;}
00241
00242 ps_his_head = (s_his_head *)pl_all;
00243 for(i_j=0;i_j<s_his_comm_cli.lu_histos;i_j++)
00244 {
00245 if(l_swap) l_status = f_swaplw((INTS4 *)ps_his_head,16,NULL);
00246 ps_his_head++;
00247
00248 }
00249 l_status=COMM__SUCCESS;
00250 g_return:
00251 f_stc_discclient (l_chan);
00252 f_stc_close (ps_tcpcli);
00253 free(ps_tcpcli);
00254 return(l_status);
00255 }
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311 INTS4 f_his_gethis(CHARS *pc_server, INTS4 l_port, CHARS *pc_base, CHARS *pc_access, CHARS *pc_histo
00312 , s_his_head **p_head, INTS4 **p_buffer, INTS4 *pl_size)
00313 {
00314 s_his_comm s_his_comm_cli;
00315 s_his_head *ps_his_head;
00316 INTS4 i_j,i_l,l_status,l_chan,l_swap,l_buffer,l_histos,l_size;
00317 INTS4 *pl_all,*pl_head,*pl_l;
00318 struct s_tcpcomm *ps_tcpcli;
00319
00320 ps_tcpcli = (struct s_tcpcomm *) malloc (sizeof( struct s_tcpcomm));
00321
00322
00323 s_his_comm_cli.lu_endian=1;
00324 s_his_comm_cli.lu_size=0;
00325 s_his_comm_cli.lu_histos=0;
00326 s_his_comm_cli.lu_action=COMM__GETHIST;
00327 s_his_comm_cli.l_status=COMM__SUCCESS;
00328 strcpy(s_his_comm_cli.c_histo,pc_histo);
00329 strcpy(s_his_comm_cli.c_access,pc_access);
00330 strcpy(s_his_comm_cli.c_base,pc_base);
00331 l_status = f_stc_connectserver (pc_server,l_port, &l_chan, ps_tcpcli);
00332 if(l_status != STC__SUCCESS) {l_status=COMM__NOSERVER;goto g_return;}
00333 l_status = f_stc_write(&s_his_comm_cli,sizeof(s_his_comm),l_chan);
00334 if(l_status != STC__SUCCESS) {l_status=COMM__ERROR;goto g_return;}
00335 l_status = f_stc_read (&s_his_comm_cli,sizeof(s_his_comm),l_chan,-1);
00336 if(l_status != STC__SUCCESS) {l_status=COMM__ERROR;goto g_return;}
00337 l_swap = (s_his_comm_cli.lu_endian > 1);
00338 if(l_swap) l_status = f_swaplw((INTS4 *)&s_his_comm_cli,5,NULL);
00339 if(s_his_comm_cli.l_status != COMM__SUCCESS) {l_status=s_his_comm_cli.l_status;goto g_return;}
00340
00341 l_size=s_his_comm_cli.lu_size-sizeof(s_his_head);
00342
00343 if(*p_buffer == 0)
00344 {
00345 pl_all = (INTS4 *)malloc(l_size);
00346 *p_buffer = pl_all;
00347 }
00348 else
00349 {
00350 if(*pl_size < l_size) {l_status=COMM__ERROR;goto g_return;}
00351 pl_all = *p_buffer;
00352 }
00353
00354 if(*p_head == 0)
00355 {
00356 ps_his_head = (s_his_head *)malloc(sizeof(s_his_head));
00357 *p_head = ps_his_head;
00358 }
00359 else ps_his_head = *p_head;
00360
00361 l_status = f_stc_read (ps_his_head,sizeof(s_his_head),l_chan,-1);
00362 if(l_swap) l_status = f_swaplw((INTS4 *)ps_his_head,16,NULL);
00363
00364 *pl_size = l_size;
00365 l_buffer = l_size;
00366
00367
00368 pl_l=pl_all;
00369 i_l=l_buffer/16384;
00370 l_buffer=l_buffer%16384;
00371 for(i_j=0;i_j<i_l;i_j++){l_status = f_stc_read (pl_l,16384,l_chan,-1);pl_l+=4096;}
00372 if(l_buffer > 0) l_status = f_stc_read (pl_l,l_buffer,l_chan,-1);
00373 if(l_status != STC__SUCCESS) {l_status=COMM__ERROR;goto g_return;}
00374
00375 if(l_swap) l_status = f_swaplw((INTS4 *)pl_all,l_size/4,NULL);
00376
00377 l_status=COMM__SUCCESS;
00378 g_return:
00379 f_stc_discclient (l_chan);
00380 f_stc_close (ps_tcpcli);
00381 free(ps_tcpcli);
00382 return(l_status);
00383 }
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418 INTS4 f_his_server(CHARS *pc_base, CHARS *pc_access, INTS4 *pl_port)
00419 {
00420 INTS4 *pl_l,i_j,i_l,ll,l_buffer,l_status;
00421
00422 ps_tcpserv = (struct s_tcpcomm *) malloc (sizeof( struct s_tcpcomm));
00423 l_gl_serv_port=PORT__HIST_SERV;
00424 strcpy(c_gl_serv_access,pc_access);
00425 strcpy(c_gl_serv_base,pc_base);
00426 f_his_toupper(c_gl_serv_access,sizeof(c_gl_serv_access));
00427 f_his_toupper(c_gl_serv_base,sizeof(c_gl_serv_base));
00428
00429 while((l_status = f_stc_createserver (&l_gl_serv_port, ps_tcpserv)) != STC__SUCCESS)
00430 {
00431 l_gl_serv_port++;
00432 if(l_gl_serv_port > PORT__HIST_SERV + 20)
00433 {
00434 printf("Error creating histogram server\n");
00435 return(COMM__ERROR);
00436 }
00437 }
00438 *pl_port=l_gl_serv_port;
00439 return(0);
00440 }
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478 INTS4 f_his_wait(INTS4 *pl_action, CHARS *pc_histo)
00479 {
00480 INTS4 *pl_l,i_j,i_l,ll,l_buffer,l_status;
00481
00482 if(l_gl_serv_verb == 1) {printf("Waiting for client on port %d [%s] \n",l_gl_serv_port,c_gl_serv_access);}
00483 l_status = f_stc_acceptclient (ps_tcpserv, &l_gl_serv_chan);
00484 if(l_status != STC__SUCCESS) { printf("error accepting client\n"); return(COMM__ERROR);}
00485 if(l_gl_serv_verb == 1) {printf("Client connected. \n");}
00486 l_status = f_stc_read (&s_his_comm_serv,sizeof(s_his_comm),l_gl_serv_chan,-1);
00487 if(l_status != STC__SUCCESS) { printf("error reading client\n"); f_stc_discclient(l_gl_serv_chan);return(COMM__ERROR); }
00488
00489 f_his_toupper(s_his_comm_serv.c_access,sizeof(s_his_comm_serv.c_access));
00490 f_his_toupper(s_his_comm_serv.c_base,sizeof(s_his_comm_serv.c_base));
00491 if((strlen(c_gl_serv_access) != 0) &
00492 (strcmp(s_his_comm_serv.c_access,c_gl_serv_access) != 0))
00493 {
00494 printf("Client connected with wrong password [%s]. \n",s_his_comm_serv.c_access);
00495 s_his_comm_serv.lu_endian=1;
00496 s_his_comm_serv.lu_size=0;
00497 s_his_comm_serv.lu_histos=0;
00498 s_his_comm_serv.l_status=COMM__NOACCESS;
00499 l_status = f_stc_write (&s_his_comm_serv,sizeof(s_his_comm), l_gl_serv_chan);
00500 f_stc_discclient (l_gl_serv_chan);
00501 return(COMM__NOACCESS);
00502 }
00503
00504 if(strcmp(s_his_comm_serv.c_base,c_gl_serv_base) != 0)
00505 {
00506 printf("Client connected for wrong base [%s]. \n",s_his_comm_serv.c_base);
00507 s_his_comm_serv.lu_endian=1;
00508 s_his_comm_serv.lu_size=0;
00509 s_his_comm_serv.lu_histos=0;
00510 s_his_comm_serv.l_status=COMM__NOBASE;
00511 l_status = f_stc_write (&s_his_comm_serv,sizeof(s_his_comm), l_gl_serv_chan);
00512 f_stc_discclient (l_gl_serv_chan);
00513 return(COMM__NOBASE);
00514 }
00515 if(s_his_comm_serv.lu_endian > 100) l_status = f_swaplw((INTS4 *)&s_his_comm_serv,5,NULL);
00516 if(l_gl_serv_verb == 1) {printf("%d: %d %d %d %d %d \"%s\" \"%s\" \"%s\"\n",l_status
00517 ,s_his_comm_serv.lu_endian
00518 ,s_his_comm_serv.lu_action
00519 ,s_his_comm_serv.lu_histos
00520 ,s_his_comm_serv.lu_size
00521 ,s_his_comm_serv.l_status
00522 ,s_his_comm_serv.c_access
00523 ,s_his_comm_serv.c_base
00524 ,s_his_comm_serv.c_histo);}
00525 *pl_action=s_his_comm_serv.lu_action;
00526 strcpy(pc_histo,s_his_comm_serv.c_histo);
00527 return(COMM__SUCCESS);
00528 }
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563 INTS4 f_his_senddir(s_his_head *ps_head, INTS4 l_histos)
00564 {
00565 INTS4 *pl_l,i_j,i_l,ll,l_buffer,l_status;
00566
00567 if(l_gl_serv_verb == 1) {printf("Send directory %s. \n",s_his_comm_serv.c_histo);}
00568 s_his_comm_serv.lu_histos=l_histos;
00569 s_his_comm_serv.lu_endian=1;
00570 s_his_comm_serv.lu_action=COMM__PUTDIR;
00571 s_his_comm_serv.lu_size=sizeof(s_his_head)*s_his_comm_serv.lu_histos;
00572 s_his_comm_serv.l_status=COMM__SUCCESS;
00573 l_status = f_stc_write (&s_his_comm_serv,sizeof(s_his_comm), l_gl_serv_chan);
00574 if(l_status != STC__SUCCESS)
00575 { printf("error writing comm to client\n"); f_stc_discclient(l_gl_serv_chan);return(COMM__ERROR);}
00576
00577
00578 pl_l=(INTS4 *)ps_head;
00579 l_buffer = s_his_comm_serv.lu_size;
00580 i_l=l_buffer/16384;
00581 l_buffer=l_buffer%16384;
00582 for(i_j=0;i_j<i_l;i_j++){l_status = f_stc_write (pl_l,16384,l_gl_serv_chan);pl_l+=4096;}
00583 if(l_buffer > 0) l_status = f_stc_write (pl_l,l_buffer,l_gl_serv_chan);
00584
00585 if(l_status != STC__SUCCESS)
00586 { printf("error writing directory to client\n"); f_stc_discclient(l_gl_serv_chan);return(COMM__ERROR);}
00587 f_stc_discclient (l_gl_serv_chan);
00588 return(COMM__SUCCESS);
00589 }
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626 INTS4 f_his_sendhis(s_his_head *ps_head, INTS4 l_histos, CHARS *pc_histo, INTS4 *pl_data)
00627 {
00628 s_his_head *ps_his_head;
00629 INTS4 *pl_l,i_j,i_l,ll,l_buffer,l_status;
00630
00631 s_his_comm_serv.lu_endian=1;
00632 s_his_comm_serv.lu_histos=0;
00633 s_his_comm_serv.lu_action=COMM__PUTHIST;
00634 ps_his_head = ps_head;
00635 for(ll=0;ll<l_histos;ll++)
00636 {
00637 pl_l = pl_data;
00638 if(strcmp(ps_his_head->c_name,pc_histo) == 0)
00639 {
00640 s_his_comm_serv.lu_histos=1;
00641 s_his_comm_serv.l_status=COMM__SUCCESS;
00642 s_his_comm_serv.lu_size=sizeof(s_his_head)+ps_his_head->l_bins_1 * ps_his_head->l_bins_2 * 4;
00643 l_status = f_stc_write (&s_his_comm_serv,sizeof(s_his_comm), l_gl_serv_chan);
00644 if(l_status != STC__SUCCESS)
00645 { printf("error writing comm to client\n"); f_stc_discclient(l_gl_serv_chan); return(COMM__ERROR);}
00646 if(l_gl_serv_verb == 1) {printf("%-32s %8d\n",ps_his_head->c_name,s_his_comm_serv.lu_size-sizeof(s_his_head));}
00647 l_status = f_stc_write (ps_his_head,sizeof(s_his_head), l_gl_serv_chan);
00648 if(l_status != STC__SUCCESS)
00649 { printf("error writing header to client\n"); f_stc_discclient(l_gl_serv_chan); return(COMM__ERROR); }
00650
00651
00652 l_buffer = s_his_comm_serv.lu_size-sizeof(s_his_head);
00653 i_l=l_buffer/16384;
00654 l_buffer=l_buffer%16384;
00655 for(i_j=0;i_j<i_l;i_j++){l_status = f_stc_write (pl_l,16384,l_gl_serv_chan);pl_l+=4096;}
00656 if(l_buffer > 0) l_status = f_stc_write (pl_l,l_buffer,l_gl_serv_chan);
00657 if(l_status != STC__SUCCESS)
00658 { printf("error writing data to client\n"); f_stc_discclient(l_gl_serv_chan); return(COMM__ERROR); }
00659 break;
00660 }
00661 ps_his_head++;
00662 }
00663 if(s_his_comm_serv.lu_histos == 0)
00664 {
00665 if(l_gl_serv_verb == 1) {printf("Histogram %s not found\n",pc_histo);}
00666 s_his_comm_serv.l_status=COMM__NOHIST;
00667 s_his_comm_serv.lu_size=0;
00668 l_status = f_stc_write (&s_his_comm_serv,sizeof(s_his_comm), l_gl_serv_chan);
00669 if(l_status != STC__SUCCESS)
00670 { printf("error writing comm to client\n"); f_stc_discclient(l_gl_serv_chan); return(COMM__ERROR); }
00671 f_stc_discclient (l_gl_serv_chan);
00672 return(COMM__NOHIST);
00673 }
00674 f_stc_discclient (l_gl_serv_chan);
00675 return(COMM__SUCCESS);
00676 }
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706 INTS4 f_his_close()
00707 {
00708 printf("Exit histogram server\n");
00709 f_stc_close (ps_tcpserv);
00710 free(ps_tcpserv);
00711 return(COMM__SUCCESS);
00712 }