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 >
Wrap
C/C++ Source or Header
|
1999-11-10
|
10KB
|
409 lines
/************************************************************************
* SeisView server *
* *
* Copyright (C) 1992,1999 K.Koketsu *
* Earthquake Research Institute, University of Tokyo *
* *
* <history> *
* 1999.11.12 (3.02) Y2K issue for filename convention.
* 1998.11.04 (3.01) Remote auxiliary files. *
* 1998.10.18 (3.01) Solaris is supported as well as SunOS. *
* Adjustment for the revision of retrv.c. *
* 1997.08.02 (2.02a) 'kea' version. *
* 1996.05.28 (2.02) Fix the bug in making filenames after b10. *
* 1994.01.07 (2.01) *
* 1993.06.01 (2.00) *
* 1992.05.08 (1.00) *
/************************************************************************/
/* References:
Network Programming (800-1779-10, Revision A, May 9, 1988)
by Sun Microsystems.
SunOS 5.3 Network Interfaces Programmer's Guide (p.223-266, 801-5295-10,
Revision A, November, 1993) by SunSoft.
Daemons & Dragons (p.81-83, UNIX magazine, AUG, 1992) by Rob Kolstad. */
#define SEISD (516 + 5000)
#define DATA "/usr/local/lib/seisv/data/" /* data directory */
#define ETC "/usr/local/lib/seisv/etc/" /* auxiliary directory */
#define LOG "log" /* name of file for logging */
#define MAXSIZE 1000 /* maximum packet size of auxiliary file transfer */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#ifdef BSDI
#include <sys/malloc.h>
#else
#include <malloc.h>
#endif
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/wait.h>
#include <signal.h>
#include "seis.h"
void reaper();
void seis_select(), aux_select();
FILE *fp; /* file descriptor of log file */
int fd; /* file handle of socket */
char w$[81]; /* work string */
main()
{
int i, sock, len, on = 1;
struct sockaddr_in server, client;
struct sigaction act;
server.sin_family = AF_INET;
server.sin_port = htons( SEISD );
server.sin_addr.s_addr = INADDR_ANY;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("seisd: socket");
exit(1);
}
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if (bind(sock, &server, sizeof (server)) < 0) {
perror("seisd: bind");
exit(1);
}
/* Be a daemon releasing everything. */
if( fork() ) exit(0);
for(i=0; i<3; i++) close(i);
open("/dev/null", O_RDONLY);
dup(0, 1); dup(0, 2);
/* This portion cannot run on Solaris, and is maybe unnecessary.
i = open("/dev/tty", O_RDWR);
if(i >= 0) {
ioctl(i, TIOCNOTTY, 0);
close( i );
}
*/
/* signal -> sigaction with SA_NOCLDWAIT to avoid
zombies on Solaris. See man -s signal. (98-10-17) */
#ifdef BSD
signal(SIGCHLD, reaper);
#else
act.sa_handler = reaper;
act.sa_flags = SA_NOCLDWAIT;
sigaction(SIGCHLD, &act, NULL);
#endif
/* Listening to connection. */
listen(sock, 10);
len = sizeof(client);
for (;;) {
fd = accept(sock, &client, &len);
if (fd < 0) continue;
if (fork() == 0) {
signal(SIGCHLD, SIG_DFL);
seis_select();
_exit(0);
}
close( fd );
}
/* <For debugging> */
/*
while((fd = accept(sock, &client, &len)) < 0);
signal(SIGCHLD, SIG_DFL);
seis_select();
close( fd );
*/
}
/* reaper --- Reap child processes. */
void reaper()
{
int status; /* union wait --> int for Solaris (98-10-16) */
while(wait3(&status,WNOHANG,0) > 0);
}
char hname[17];
char *iseg[2];
/**************************** variables for retrieval ************************/
char f2$[6];
char y1$[5], m1$[3], d1$[3], h1$[3], n1$[3], s1$[5];
char y2$[5], m2$[3], d2$[3], h2$[3], n2$[3], s2$[5];
char dst$[9];
char so1$[4], som1$[3], so2$[4], som2$[3];
char sa1$[3], sam1$[3], sa2$[3], sam2$[3];
char sew1, sew2, sns1, sns2;
char dp1$[6], dp2$[6], mg1$[5], mg2$[5];
char ynh$[2], ynm$[2];
char to1$[4], tom1$[3], to2$[4], tom2$[3];
char ta1$[3], tam1$[3], ta2$[3], tam2$[3];
/******************** parameters for retrieving kernel ***********************/
unsigned imd1,ihm1,isc1, imd2,ihm2,isc2; /* month+day, hour+min, sec */
long lo1, lo2; /* longitude */
long b180 = 180*256L, b360 = 360*256L;
short lom1,lom2, la1,lam1,la2,lam2; /* lonmin, lat,latmin */
unsigned ih1, ih2; /* depth */
int m1, m2; /* magnitude */
short wrap; /* flag of wrap-around */
unsigned iyy1,im1,id1, iyy2,im2,id2; /* years from and to */
unsigned jh1,jm1, jh2,jm2; /* times from and to */
double hh1,hh2, rm1,rm2; /* depth, magnitude */
void seis_select()
{
int i, iy, year, itot;
char g$[24], s[17];
unsigned ra;
long timer;
short nmax;
nmax = 0x7fff;
strcpy(w$, DATA);
strcat(w$, LOG);
fp = fopen(w$, "a");
time( &timer );
strcpy(g$, ctime(&timer)+4);
g$[12] = 0;
iseg[0] = malloc(0x10000);
iseg[1] = malloc(0x10000);
read_client( hname );
fprintf(fp, "[%s(%s):", hname, g$);
read_client( f2$ );
if(*f2$ == ':') {
aux_select();
fterm();
return;
}
read_client( s ); year = atoi( s );
read_client(y1$); read_client(m1$); read_client(d1$);
read_client(h1$); read_client(n1$); read_client(s1$);
read_client(y2$); read_client(m2$); read_client(d2$);
read_client(h2$); read_client(n2$); read_client(s2$);
read_client(so1$); read_client(w$);
strncpy(som1$,w$, 2); if(w$[2] == 0) sew1 = 'E'; else sew1 = w$[2];
read_client(so2$); read_client(w$);
strncpy(som2$,w$, 2); if(w$[2] == 0) sew2 = 'E'; else sew2 = w$[2];
read_client(sa1$); read_client(w$);
strncpy(sam1$,w$, 2); if(w$[2] == 0) sns1 = 'N'; else sns1 = w$[2];
read_client(sa2$); read_client(w$);
strncpy(sam2$,w$, 2); if(w$[2] == 0) sns2 = 'N'; else sns2 = w$[2];
read_client(dp1$); read_client(dp2$);
read_client(mg1$); read_client(mg2$);
read_client(ynh$); read_client(ynm$);
iyy1 = atoi(y1$); iyy2 = atoi(y2$);
im1 = atoi(m1$); im2 = atoi(m2$);
id1 = atoi(d1$); id2 = atoi(d2$);
jh1 = atoi(h1$); jh2 = atoi(h2$);
jm1 = atoi(n1$); jm2 = atoi(n2$);
isc1 = atof(s1$)*10.; isc2 = atof(s2$)*10.;
imd1 = im1*256 + id1; imd2 = im2*256 + id2;
ihm1 = jh1*256 + jm1; ihm2 = jh2*256 + jm2;
lo1 = atol(so1$); lo2 = atol(so2$);
la1 = atoi(sa1$); la2 = atoi(sa2$);
lom1 = atoi(som1$); lom2 = atoi(som2$);
lam1 = atoi(sam1$); lam2 = atoi(sam2$);
lo1 = lo1*256 + lom1;
lo2 = lo2*256 + lom2;
la1 = la1*256 + lam1;
la2 = la2*256 + lam2;
if(sew1 == 'W') lo1 = - lo1;
if(sew2 == 'W') lo2 = - lo2;
if(sns1 == 'S') la1 = - la1;
if(sns2 == 'S') la2 = - la2;
if(lo1 >= lo2) lo2 = lo2 + b360;
hh1 = atof(dp1$); hh2 = atof(dp2$);
ih1 = hh1*10 + 0.01; ih2 = hh2*10 + 0.01;
rm1 = atof(mg1$); rm2 = atof(mg2$);
if(rm1 >= 0.) m1 = rm1*10 + 0.01;
else m1 = rm1*10 - 0.01;
if(rm2 >= 0.) m2 = rm2*10 + 0.01;
else m2 = rm2*10 - 0.01;
if(year == 0) { strcpy(g$, f2$); strcat(g$, ".B"); }
else {
strcpy(g$, f2$); /* Add a directory name. */
strcat(g$, "/"); strcat(g$, f2$);
/* Y2K for file name convention */
/*sprintf(g$+strlen(g$), "-%d.B", year-1900);*/
if (year < 2000) sprintf(g$+strlen(g$), "-%d.B", year-1900);
else if(year < 2010) sprintf(g$+strlen(g$),"00%d.B", year-2000);
else sprintf(g$+strlen(g$), "0%d.B", year-2000);
}
j29:
if(! bload(iseg[0],0,g$)) {
reply_client(0, g$+strlen(f2$)+1);
fterm();
return;
}
else {
reply_client(1, 0);
read_client( s );
if(*s == '0') { fterm(); return; }
}
itot = 0;
ra = 0;
for(;;) {
ra = retrv(ra, itot, 0, 1);
if(ra > 0x7fff) break;
else {
if(++itot == nmax) break;
ra++;
}
}
send_client(iseg[1]+4, itot);
if(ra == 0xffff) { fterm(); return; }
if(ra == 0xfffe) itoa(2, g$+strlen(g$), 10);
else itoa(0xffff-ra+1, strchr(g$,'.')+2, 10);
goto j29;
}
/* common retrieving kernel and a dummy function */
#include "retrv.c"
void poke(){}
void aux_select()
{
char s[17], t[6], *seg;
int rcod, rtimes, rfract, i, j;
read_client( s );
if(!(rcod = bload(iseg[1],0,s))) {
reply_client(0, s);
return;
}
reply_client(1, 0);
rtimes = rcod / MAXSIZE;
rfract = rcod % MAXSIZE;
seg = iseg[1];
if(rtimes > 0) {
j = MAXSIZE;
for(i=0; i<rtimes; i++) {
sprintf(t, "%5d", j);
write(fd, t, 6);
write(fd, seg, j);
seg += MAXSIZE;
}
}
if(rfract > 0) {
sprintf(t, "%5d", rfract);
write(fd, t, 6);
write(fd, seg, rfract);
}
write(fd, " 0", 6);
}
read_client( t )
char *t;
{
char s[17]; /* Packets are always 16 bytes. */
int i;
i = read(fd, s, 16);
if(i <= 0) return( 0 );
s[16] = 0;
for(i=0; i<16; i++) {
if(isgraph(s[i])) t[i] = s[i];
else break;
}
t[i] = 0;
return( 1 );
}
send_client(s, n)
char *s;
int n;
{
int i;
char t[6];
unsigned nn;
nn = (unsigned)n*16 + 2;
sprintf(t, "%5d", n);
write(fd, t, 5);
write(fd, s, nn);
}
reply_client(c, t)
int c;
char *t;
{
char s[17]; /* Packets are always 16 bytes. */
int i;
*s = c;
if(c == 0) strcpy(s+1, t);
i = write(fd, s, 16);
if(i <= 0) return( 0 );
return( 1 );
}
char *itoa(i, s, radix)
int i, radix;
char *s;
{ /* Assume radix = 10. */
sprintf(s, "%d", i);
return( s );
}
#include <errno.h>
int bload(sg, off, fname)
char *sg, *fname;
unsigned off;
{
int handle, rcod;
char *s, fn[78];
if(strstr(fname,".B") || strstr(fname,".b")) strcpy(fn, DATA);
else strcpy(fn, ETC);
strlwr( fname ); strcat(fn, fname);
fprintf(fp, "%s ", fname);
handle = open(fn, O_RDONLY);
if(handle == -1) return( 0 );
s = sg + off;
rcod = read(handle, s, 0xffff);
if(rcod == -1) rcod = 0;
close( handle );
return( rcod );
}
char *strlwr( s )
char *s;
{
char *t;
t = s;
while(*t != 0) *(t++) = tolower( (int)*t );
return( s );
}
fterm()
{
fputs("]\n", fp);
fclose( fp );
}