home *** CD-ROM | disk | FTP | other *** search
/ ftp.eri.u-tokyo.ac.jp / 2014.03.ftp.eri.u-tokyo.ac.jp.zip / ftp.eri.u-tokyo.ac.jp / pub / seisv / src / 3.02 / seisvd.c < prev    next >
C/C++ Source or Header  |  1999-11-10  |  10KB  |  409 lines

  1. /************************************************************************
  2.  *   SeisView server                            *
  3.  *                                    *
  4.  *   Copyright (C) 1992,1999  K.Koketsu                    *
  5.  *   Earthquake Research Institute, University of Tokyo            * 
  6.  *                                    *
  7.  *      <history>                            *
  8.  *    1999.11.12 (3.02)  Y2K issue for filename convention.
  9.  *    1998.11.04 (3.01)  Remote auxiliary files.            *
  10.  *    1998.10.18 (3.01)  Solaris is supported as well as SunOS.    *
  11.  *               Adjustment for the revision of retrv.c.    *
  12.  *    1997.08.02 (2.02a) 'kea' version.                *
  13.  *    1996.05.28 (2.02)  Fix the bug in making filenames after b10.    *
  14.  *    1994.01.07 (2.01)                        *
  15.  *    1993.06.01 (2.00)                        *
  16.  *    1992.05.08 (1.00)                        *
  17. /************************************************************************/
  18.  
  19. /* References:
  20.    Network Programming (800-1779-10, Revision A, May 9, 1988)
  21.    by Sun Microsystems.
  22.    SunOS 5.3 Network Interfaces Programmer's Guide (p.223-266, 801-5295-10,
  23.    Revision A, November, 1993) by SunSoft.
  24.    Daemons & Dragons (p.81-83, UNIX magazine, AUG, 1992) by Rob Kolstad. */
  25.  
  26. #define  SEISD  (516 + 5000)
  27. #define  DATA   "/usr/local/lib/seisv/data/"    /* data directory      */
  28. #define  ETC    "/usr/local/lib/seisv/etc/"    /* auxiliary directory      */
  29. #define  LOG    "log"    /* name of file for logging              */
  30. #define  MAXSIZE  1000    /* maximum packet size of auxiliary file transfer */
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <fcntl.h>
  35. #include <string.h>
  36. #include <time.h>
  37. #include <ctype.h>
  38. #ifdef BSDI
  39. #include <sys/malloc.h>
  40. #else
  41. #include <malloc.h>
  42. #endif
  43.  
  44. #include <sys/types.h>
  45. #include <sys/ioctl.h>
  46. #include <sys/socket.h>
  47. #include <netinet/in.h>
  48. #include <netdb.h>
  49. #include <sys/wait.h>
  50. #include <signal.h>
  51. #include "seis.h"
  52.  
  53. void  reaper();
  54. void  seis_select(), aux_select();
  55. FILE  *fp;                /* file descriptor of log file */
  56. int   fd;                /* file handle of socket */
  57. char  w$[81];                /* work string */
  58.  
  59. main()
  60. {
  61.     int     i, sock, len, on = 1;
  62.     struct  sockaddr_in  server, client;
  63.     struct sigaction act;
  64.  
  65.     server.sin_family = AF_INET;
  66.     server.sin_port = htons( SEISD );
  67.     server.sin_addr.s_addr = INADDR_ANY;
  68.  
  69.     if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  70.     perror("seisd: socket");
  71.     exit(1);
  72.     }
  73.     setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  74.     if (bind(sock, &server, sizeof (server)) < 0) {
  75.     perror("seisd: bind");
  76.     exit(1);
  77.     }
  78.  
  79.     /* Be a daemon releasing everything. */
  80.     if( fork() )  exit(0);
  81.  
  82.     for(i=0; i<3; i++)  close(i);
  83.     open("/dev/null", O_RDONLY);
  84.     dup(0, 1);  dup(0, 2);
  85.  
  86.     /* This portion cannot run on Solaris, and is maybe unnecessary.
  87.     i = open("/dev/tty", O_RDWR);
  88.     if(i >= 0) {
  89.     ioctl(i, TIOCNOTTY, 0);
  90.     close( i );
  91.     }
  92.     */
  93.  
  94.     /* signal -> sigaction with SA_NOCLDWAIT to avoid
  95.        zombies on Solaris.  See man -s signal. (98-10-17) */
  96. #ifdef BSD
  97.     signal(SIGCHLD, reaper);
  98. #else
  99.     act.sa_handler = reaper;
  100.     act.sa_flags = SA_NOCLDWAIT;
  101.     sigaction(SIGCHLD, &act, NULL);
  102. #endif
  103.  
  104.     /* Listening to connection. */
  105.     listen(sock, 10);
  106.     len = sizeof(client);
  107.  
  108.     for (;;) {
  109.     fd = accept(sock, &client, &len);
  110.     if (fd < 0)  continue;
  111.     if (fork() == 0) {
  112.         signal(SIGCHLD, SIG_DFL);
  113.         seis_select();
  114.         _exit(0);
  115.     }
  116.     close( fd );
  117.     }
  118.  
  119. /* <For debugging> */
  120. /*
  121.     while((fd = accept(sock, &client, &len)) < 0);
  122.     signal(SIGCHLD, SIG_DFL);
  123.     seis_select();
  124.     close( fd );
  125. */
  126. }
  127.  
  128.  
  129. /* reaper --- Reap child processes. */
  130. void reaper()
  131. {
  132.     int  status; /* union wait --> int for Solaris (98-10-16) */
  133.  
  134.     while(wait3(&status,WNOHANG,0) > 0);
  135. }
  136.  
  137. char  hname[17];
  138. char  *iseg[2];
  139. /**************************** variables for retrieval ************************/
  140. char   f2$[6];
  141. char   y1$[5], m1$[3], d1$[3], h1$[3], n1$[3], s1$[5];
  142. char   y2$[5], m2$[3], d2$[3], h2$[3], n2$[3], s2$[5];
  143. char   dst$[9];
  144. char   so1$[4], som1$[3], so2$[4], som2$[3];
  145. char   sa1$[3], sam1$[3], sa2$[3], sam2$[3];
  146. char   sew1, sew2, sns1, sns2;
  147. char   dp1$[6], dp2$[6], mg1$[5], mg2$[5];
  148. char   ynh$[2], ynm$[2];
  149. char   to1$[4], tom1$[3], to2$[4], tom2$[3];
  150. char   ta1$[3], tam1$[3], ta2$[3], tam2$[3];
  151.  
  152. /******************** parameters for retrieving kernel ***********************/
  153. unsigned imd1,ihm1,isc1, imd2,ihm2,isc2;    /* month+day, hour+min, sec */
  154. long     lo1, lo2;                /* longitude            */
  155. long     b180 = 180*256L, b360 = 360*256L;
  156. short    lom1,lom2, la1,lam1,la2,lam2;        /* lonmin, lat,latmin        */
  157. unsigned ih1, ih2;                /* depth            */
  158. int      m1, m2;                /* magnitude            */
  159. short    wrap;                    /* flag of wrap-around        */
  160.  
  161. unsigned iyy1,im1,id1, iyy2,im2,id2;        /* years from and to        */
  162. unsigned jh1,jm1, jh2,jm2;            /* times from and to        */
  163. double   hh1,hh2, rm1,rm2;            /* depth, magnitude        */
  164.  
  165. void seis_select()
  166. {
  167.     int      i, iy, year, itot;
  168.     char     g$[24], s[17];
  169.     unsigned ra;
  170.     long     timer;
  171.     short    nmax;
  172.  
  173.     nmax = 0x7fff;
  174.  
  175.     strcpy(w$, DATA);
  176.     strcat(w$, LOG);
  177.     fp = fopen(w$, "a");
  178.  
  179.     time( &timer );
  180.     strcpy(g$, ctime(&timer)+4);
  181.     g$[12] = 0;
  182.  
  183.     iseg[0] = malloc(0x10000);
  184.     iseg[1] = malloc(0x10000);
  185.  
  186.     read_client( hname );
  187.     fprintf(fp, "[%s(%s):", hname, g$);
  188.     read_client( f2$ );
  189.     if(*f2$ == ':') {
  190.     aux_select();
  191.     fterm();
  192.     return;
  193.     }
  194.  
  195.     read_client( s );  year = atoi( s );
  196.     read_client(y1$);  read_client(m1$);  read_client(d1$);
  197.     read_client(h1$);  read_client(n1$);  read_client(s1$);
  198.     read_client(y2$);  read_client(m2$);  read_client(d2$);
  199.     read_client(h2$);  read_client(n2$);  read_client(s2$);
  200.     read_client(so1$);  read_client(w$);
  201.     strncpy(som1$,w$, 2);  if(w$[2] == 0)  sew1 = 'E';  else  sew1 = w$[2];
  202.     read_client(so2$);  read_client(w$);
  203.     strncpy(som2$,w$, 2);  if(w$[2] == 0)  sew2 = 'E';  else  sew2 = w$[2];
  204.     read_client(sa1$);  read_client(w$);
  205.     strncpy(sam1$,w$, 2);  if(w$[2] == 0)  sns1 = 'N';  else  sns1 = w$[2];
  206.     read_client(sa2$);  read_client(w$);
  207.     strncpy(sam2$,w$, 2);  if(w$[2] == 0)  sns2 = 'N';  else  sns2 = w$[2];
  208.     read_client(dp1$);  read_client(dp2$);
  209.     read_client(mg1$);  read_client(mg2$);
  210.     read_client(ynh$);  read_client(ynm$);
  211.  
  212.     iyy1 = atoi(y1$);    iyy2 = atoi(y2$);
  213.     im1 = atoi(m1$); im2 = atoi(m2$);
  214.     id1 = atoi(d1$); id2 = atoi(d2$);
  215.     jh1 = atoi(h1$); jh2 = atoi(h2$);
  216.     jm1 = atoi(n1$); jm2 = atoi(n2$);
  217.     isc1 = atof(s1$)*10.; isc2 = atof(s2$)*10.;
  218.     imd1 = im1*256 + id1;  imd2 = im2*256 + id2;
  219.     ihm1 = jh1*256 + jm1;  ihm2 = jh2*256 + jm2;
  220.  
  221.     lo1  = atol(so1$);  lo2  = atol(so2$);
  222.     la1  = atoi(sa1$);  la2  = atoi(sa2$);
  223.     lom1 = atoi(som1$); lom2 = atoi(som2$);
  224.     lam1 = atoi(sam1$); lam2 = atoi(sam2$);
  225.     lo1 = lo1*256 + lom1;
  226.     lo2 = lo2*256 + lom2;
  227.     la1 = la1*256 + lam1;
  228.     la2 = la2*256 + lam2;
  229.     if(sew1 == 'W')  lo1 = - lo1;
  230.     if(sew2 == 'W')  lo2 = - lo2;
  231.     if(sns1 == 'S')  la1 = - la1;
  232.     if(sns2 == 'S')  la2 = - la2;
  233.     if(lo1 >= lo2)  lo2 = lo2 + b360;
  234.  
  235.     hh1 = atof(dp1$);  hh2 = atof(dp2$);
  236.     ih1 = hh1*10 + 0.01;  ih2  = hh2*10 + 0.01;
  237.     rm1 = atof(mg1$);  rm2 = atof(mg2$);
  238.     if(rm1 >= 0.)   m1 = rm1*10 + 0.01;
  239.     else  m1 = rm1*10 - 0.01;
  240.     if(rm2 >= 0.)   m2 = rm2*10 + 0.01;
  241.     else  m2 = rm2*10 - 0.01;
  242.  
  243.     if(year == 0) { strcpy(g$, f2$); strcat(g$, ".B"); }
  244.     else {
  245.     strcpy(g$, f2$); /* Add a directory name. */
  246.     strcat(g$, "/");  strcat(g$, f2$);
  247.     /* Y2K for file name convention */
  248.     /*sprintf(g$+strlen(g$), "-%d.B", year-1900);*/
  249.     if     (year < 2000) sprintf(g$+strlen(g$), "-%d.B", year-1900);
  250.     else if(year < 2010) sprintf(g$+strlen(g$),"00%d.B", year-2000);
  251.     else                 sprintf(g$+strlen(g$), "0%d.B", year-2000);
  252.     }
  253.  
  254. j29:
  255.     if(! bload(iseg[0],0,g$)) {
  256.     reply_client(0, g$+strlen(f2$)+1);
  257.     fterm();
  258.     return;
  259.     }
  260.     else {
  261.     reply_client(1, 0);
  262.     read_client( s );
  263.     if(*s == '0') { fterm(); return; }
  264.     }
  265.  
  266.     itot = 0;
  267.     ra   = 0;
  268.     for(;;) {
  269.     ra = retrv(ra, itot, 0, 1);
  270.     if(ra > 0x7fff)  break;
  271.     else {
  272.         if(++itot == nmax) break;
  273.         ra++;
  274.     }
  275.     }
  276.  
  277.     send_client(iseg[1]+4, itot);
  278.     if(ra == 0xffff) { fterm(); return; }
  279.     if(ra == 0xfffe)   itoa(2, g$+strlen(g$), 10);
  280.     else  itoa(0xffff-ra+1, strchr(g$,'.')+2, 10);
  281.     goto j29;
  282. }
  283.  
  284. /* common retrieving kernel and a dummy function */
  285. #include "retrv.c"
  286. void poke(){}
  287.  
  288. void aux_select()
  289. {
  290.     char  s[17], t[6], *seg;
  291.     int   rcod, rtimes, rfract, i, j;
  292.  
  293.     read_client( s );
  294.     if(!(rcod = bload(iseg[1],0,s))) {
  295.     reply_client(0, s);
  296.     return;
  297.     }
  298.     reply_client(1, 0);
  299.     rtimes = rcod / MAXSIZE;
  300.     rfract = rcod % MAXSIZE;
  301.     seg = iseg[1];
  302.     if(rtimes > 0) {
  303.     j = MAXSIZE;
  304.     for(i=0; i<rtimes; i++) {
  305.         sprintf(t, "%5d", j);
  306.         write(fd, t, 6);
  307.         write(fd, seg, j);
  308.         seg += MAXSIZE;
  309.     }
  310.     }
  311.     if(rfract > 0) {
  312.     sprintf(t, "%5d", rfract);
  313.     write(fd, t, 6);
  314.     write(fd, seg, rfract);
  315.     }
  316.     write(fd, "    0", 6);
  317. }
  318.  
  319. read_client( t )
  320. char  *t;
  321. {
  322.     char  s[17]; /* Packets are always 16 bytes. */
  323.     int   i;
  324.  
  325.     i = read(fd, s, 16);
  326.     if(i <= 0)  return( 0 );
  327.     s[16] = 0;
  328.     for(i=0; i<16; i++) {
  329.     if(isgraph(s[i]))  t[i] = s[i];
  330.     else  break;
  331.     }
  332.     t[i] = 0;
  333.     return( 1 );
  334. }
  335.  
  336. send_client(s, n)
  337. char *s;
  338. int   n;
  339. {
  340.     int  i;
  341.     char t[6];
  342.     unsigned  nn;
  343.  
  344.     nn = (unsigned)n*16 + 2;
  345.     sprintf(t, "%5d", n);
  346.     write(fd, t, 5);
  347.     write(fd, s, nn);
  348. }
  349.  
  350. reply_client(c, t)
  351. int   c;
  352. char  *t;
  353. {
  354.     char  s[17]; /* Packets are always 16 bytes. */
  355.     int   i;
  356.  
  357.     *s = c;
  358.     if(c == 0)  strcpy(s+1, t);
  359.     i = write(fd, s, 16);
  360.     if(i <= 0)  return( 0 );
  361.     return( 1 );
  362. }
  363.  
  364. char *itoa(i, s, radix)
  365. int  i, radix;
  366. char *s;
  367. {   /* Assume radix = 10. */
  368.     sprintf(s, "%d", i);
  369.     return( s );
  370. }
  371.  
  372. #include <errno.h>
  373.  
  374. int bload(sg, off, fname)
  375. char *sg, *fname;
  376. unsigned  off;
  377. {
  378.     int  handle, rcod;
  379.     char *s, fn[78];
  380.  
  381.     if(strstr(fname,".B") || strstr(fname,".b"))  strcpy(fn, DATA);
  382.     else  strcpy(fn, ETC);
  383.     strlwr( fname );  strcat(fn, fname);
  384.     fprintf(fp, "%s ", fname);
  385.  
  386.     handle = open(fn, O_RDONLY);
  387.     if(handle == -1)  return( 0 );
  388.     s = sg + off;
  389.     rcod = read(handle, s, 0xffff);
  390.     if(rcod == -1)  rcod = 0;
  391.     close( handle );
  392.     return( rcod );
  393. }
  394.  
  395. char *strlwr( s )
  396. char  *s;
  397. {
  398.     char  *t;
  399.     t = s;
  400.     while(*t != 0)  *(t++) = tolower( (int)*t );
  401.     return( s );
  402. }
  403.  
  404. fterm()
  405. {
  406.     fputs("]\n", fp);
  407.     fclose( fp );
  408. }
  409.