-
Notifications
You must be signed in to change notification settings - Fork 1
/
alh_DB.c
261 lines (231 loc) · 5.94 KB
/
alh_DB.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*************************************************************************\
* Copyright (c) 2002 The University of Chicago, as Operator of Argonne
* National Laboratory.
* Copyright (c) 2002 Deutches Elektronen-Synchrotron in der Helmholtz-
* Gemelnschaft (DESY).
* Copyright (c) 2002 Berliner Speicherring-Gesellschaft fuer Synchrotron-
* Strahlung mbH (BESSY).
* Copyright (c) 2002 Southeastern Universities Research Association, as
* Operator of Thomas Jefferson National Accelerator Facility.
* Copyright (c) 2002 The Regents of the University of California, as
* Operator of Los Alamos National Laboratory.
* This file is distributed subject to a Software License Agreement found
* in the file LICENSE that is included with this distribution.
\*************************************************************************/
/************************DESCRIPTION***********************************
DataBase call routine. Work like independent process.
We have simple Perl call and more usefull C-RPC call.
rpc_gen X-files for RPC is :
struct DBSend {string msg<>;};
program DB_PORT {
version DB_VERS {
void dbSend(DBSend) = 1;
} =1;
}=port;
**********************************************************************/
#define C_CALL
#undef PERL_CALL
/* #define PERL_CALL */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <alh.h>
#ifdef WIN32
#include <process.h>
#else
#include <unistd.h>
#endif
#ifdef HAVE_SYSV_IPC
#include <sys/msg.h>
#endif
#include <errno.h>
#ifdef PERL_CALL
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <arpa/inet.h>
int TCPInit(char *hostname,int port);
int put2TCP(char *hostname,int port,char *string,int len);
#endif
#ifdef C_CALL
#include <rpc/rpc.h>
struct DBSend {
char *msg;
};
typedef struct DBSend DBSend;
#define DB_VERS ((unsigned long)(1))
#define dbSend ((unsigned long)(1))
extern void * dbsend_1();
extern bool_t xdr_DBSend();
static struct timeval TIMEOUT = { 10, 0 };
#endif
#define DEBUG 1
int put2RPC(char *host ,int port,char *msg,int len);
char msg[250];
int main(argc,argv)
int argc;
char *argv[];
{
struct msqid_ds infoBuf;
int DBMsgQId;
int DBMsgQKey;
int port;
int bytes=250;
int socket;
if (argc != 4) {
fprintf(stderr,"usage:%s TCPName TCPport Key\n",argv[0]);
exit(1);
}
if ( !(DBMsgQKey=atoi(argv[3])) || !(port=atoi(argv[2])) ) {
fprintf(stderr,"usage:%s TCPName TCPport Key\n",argv[0]);
exit(1);
}
DBMsgQId = msgget (DBMsgQKey, 0600|IPC_CREAT);
if(DBMsgQId == -1) {perror("msgQ_create"); exit(1);}
else
{
fprintf(stderr,"msgQ with key=%d is OK\n",DBMsgQKey);
if (msgctl(DBMsgQId,IPC_STAT,&infoBuf) != 1)
{
fprintf(stderr,"owner = %d.%d, perms = %04o, max bytes = %d\n",
infoBuf.msg_perm.uid,infoBuf.msg_perm.gid,
infoBuf.msg_perm.mode,infoBuf.msg_qbytes);
fprintf(stderr,"%d msgs = %d bytes on queue\n",
infoBuf.msg_qnum, infoBuf.msg_cbytes);
}
else {perror("msgctl()"); exit(1);}
}
#ifdef PERL_CALL
fprintf(stderr,"Please wait ...\n");
if( (socket=TCPInit(argv[1],port)) <= 0 ) {
perror("can't connect to TCP task");
exit(1);
}
else fprintf(stderr,"TCPInit for %s port=%d is OK\n",argv[1],port);
close(socket);
sleep(2);
#endif
if(DEBUG) fprintf(stderr,"star\n");
while(1)
{
if( msgrcv(DBMsgQId,msg,bytes,0,0) >= 0) /* got a new message */
{
if(DEBUG) fprintf(stderr,"msg=%s\n",msg);
#ifdef C_CALL
put2RPC(argv[1],port,msg,strlen(msg));
#else
put2TCP(argv[1],port,msg,strlen(msg));
#endif
}
else usleep(1000); /* # in microsec */
}
}
#ifdef C_CALL
int put2RPC(char *host ,int port,char *msg,int len)
{
CLIENT *clnt;
void *result_1;
DBSend dbsend_1_arg;
dbsend_1_arg.msg=msg;
clnt = clnt_create(host, port, DB_VERS, "netpath");
if (clnt == (CLIENT *) NULL) {
clnt_pcreateerror(host);
return(-1);
}
result_1 = dbsend_1(&dbsend_1_arg, clnt);
if (result_1 == (void *) NULL) {
clnt_perror(clnt, "call failed");
}
clnt_destroy(clnt);
return (0);
}
void *
dbsend_1(argp, clnt)
DBSend *argp;
CLIENT *clnt;
{
static char clnt_res;
memset((char *)&clnt_res, 0, sizeof (clnt_res));
if (clnt_call(clnt, dbSend,
(xdrproc_t) xdr_DBSend, (caddr_t) argp,
(xdrproc_t) xdr_void, (caddr_t) &clnt_res,
TIMEOUT) != RPC_SUCCESS) {
return (NULL);
}
return ((void *)&clnt_res);
}
bool_t
xdr_DBSend(xdrs, objp)
register XDR *xdrs;
DBSend *objp;
{
register long *buf;
if (!xdr_string(xdrs, &objp->msg, ~0))
return (FALSE);
return (TRUE);
}
#endif
#ifdef PERL_CALL
int TCPInit(char *hostname,int port)
{
int DBSocket;
struct sockaddr_in s_name;
struct hostent *h_ptr;
int true=1;
if ((DBSocket=socket(AF_INET, SOCK_STREAM, 0)) == -1){
perror( "socket");
return(-1);
}
if ((h_ptr = gethostbyname(hostname)) == NULL )
{
perror("gethostbyname");
exit(1);
}
memset(&s_name, 0, sizeof(s_name));
s_name.sin_family = AF_INET;
s_name.sin_port = htons(port);
memcpy(&s_name.sin_addr, h_ptr->h_addr, sizeof (s_name.sin_addr));
true=1;
if(setsockopt(DBSocket,SOL_SOCKET,SO_KEEPALIVE,(char *)&true,sizeof(true))) {
perror("Keepalive option set failed");
return(-1);
}
true=1;
if(setsockopt(DBSocket,IPPROTO_TCP,TCP_NODELAY,(char *)&true,sizeof(true))){
perror("nodelay option set failed");
return(-1);
}
true=200;
if(setsockopt(DBSocket,SOL_SOCKET,SO_SNDBUF,(char *)&true,sizeof(true))){
perror("sendbuf option set failed");
return(-1);
}
if (connect(DBSocket, (struct sockaddr *) &s_name, sizeof (s_name)) ) {
perror("connect");
close(DBSocket);
return(-1);
}
return(DBSocket);
}
int put2TCP(char *hostname,int port,char *string,int len)
{
int DBSock;
int ret;
while(1)
{
if( (DBSock=TCPInit(hostname,port)) <= 0) {
sleep(1);
continue;
}
ret=send(DBSock,string,len,0);
if (ret != strlen(string)) {
fprintf(stderr,"DB_call:Send_byte=%d need%d\n",ret,len);
}
close(DBSock);
usleep(1000);
return(0);
}
}
#endif