JAPAn
Just Another Parity Analyzer
Loading...
Searching...
No Matches
cfSockCli.c
Go to the documentation of this file.
1
2/* cfSockCli - send requests to server over a TCP socket
3 for configuration utility */
4/*
5 DESCRIPTION
6 This file contains the client-side of the VxWorks TCP example code.
7*/
8
9/* includes */
10
11#include <stdio.h>
12#include <unistd.h>
13#include <sys/socket.h>
14#include <netinet/in.h>
15#include <netinet/tcp.h>
16#include <arpa/inet.h>
17
18#include <string.h>
19
20
21#include "cfSockCli.h"
22
23
24static int gSocketFd; /* socket file descriptor */
25static int gSocketKeepOpen = 0; /* indicator to keep the socket open:
26 0 means close it right away;
27 1 means keep it open
28 */
29
30/****************************************************************************
31 *
32 * cfSockCli - send requests to server over a TCP socket for configuration utility
33 *
34 * This routine connects over a TCP socket to a server, and sends a
35 * user-provided message to the server. Optionally, this routine
36 * waits for the server's reply message.
37 *
38 * This routine may be invoked as follows:
39 * -> cfSockCli "remoteSystem"
40 * Message to send:
41 * Hello out there
42 * Would you like a reply (Y or N):
43 * y
44 * value = 0 = 0x0
45 * -> MESSAGE FROM SERVER:
46 * Server received your message
47 *
48 * RETURNS: OK, or ERROR if the message could not be sent to the server.
49*/
50
51
52int cfSockCliOpen (int crate_number, int keepopen);
53int cfSockCliSend (int crate_number, struct request *myRequest,
54 struct request *serverReply);
55int cfSockCliClose();
56
57
59 int crate_number, /* which crate to address */
60 struct request *myRequest, /* request to send to server */
61 struct request *serverReply /* reply from server */
62 )
63{
64 int status;
65
66 // printf("cfSockCli: gSocketKeepOpen=%d\n",gSocketKeepOpen);
67 // if (gSocketKeepOpen==0){
68 if (1==1){
69 status = cfSockCliOpen(crate_number, 0);
70 if (status == SOCK_OK) {
71 status = cfSockCliSend(crate_number, myRequest, serverReply);
72 if (status == SOCK_OK) {
73 status = cfSockCliClose();
74 }
75 }
76 } else {
77 status = cfSockCliSend(crate_number, myRequest, serverReply);
78 }
79 return status;
80}
81
82
83
84int cfSockCliOpen (int crate_number, int keepopen) {
85 struct sockaddr_in serverAddr; /* server's socket address */
86 int sockAddrSize; /* size of socket address structure */
87 char optval;
88 char * servername;
89
90 /* printf("cfSockCliOpen: gSocketKeepOpen=%d; keepopen=%d\n",
91 gSocketKeepOpen, keepopen);
92 */
93
95 /* create client's socket */
96 switch (crate_number) {
98 servername = ServerName_CountingHouse;
99 break;
100 case Crate_LeftSpect:
101 servername = ServerName_LeftSpect;
102 break;
103 case Crate_RightSpect:
104 servername = ServerName_RightSpect;
105 break;
106 case Crate_Injector:
107 servername = ServerName_Injector;
108 break;
109 case Crate_Qwvmets:
110 servername = ServerName_QwTSCrate;
111 break;
112
113 default:
114 return (SOCK_ERROR);
115 }
116
117 if ((gSocketFd = socket (PF_INET, SOCK_STREAM, 0)) == SOCK_ERROR)
118 {
119 perror ("socket");
120 return (SOCK_ERROR);
121 }
122
123 optval = 1;
124 int test = 0;
125 test = setsockopt ( gSocketFd, IPPROTO_TCP, TCP_NODELAY,
126 (char *) &optval,
127 4 ); /* black magic from Chowdhary's code */
128
129
130 test = setsockopt(gSocketFd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
131
132 /* bind not required - port number is dynamic */
133 /* build server socket address */
134
135 sockAddrSize = sizeof (struct sockaddr_in);
136 bzero ((char *) &serverAddr, sockAddrSize);
137 serverAddr.sin_family = PF_INET;
138 serverAddr.sin_port = htons (SERVER_PORT_NUM);
139
140 if ((serverAddr.sin_addr.s_addr = inet_addr (servername)) == SOCK_ERROR)
141 {
142 perror ("unknown server name");
143 close (gSocketFd);
144 return (SOCK_ERROR);
145 }
146
147 /* connect to server */
148 if (connect (gSocketFd, (struct sockaddr *) &serverAddr, sockAddrSize) == SOCK_ERROR)
149 {
150 perror ("connect");
151 close (gSocketFd);
152 return (SOCK_ERROR);
153 }
154 gSocketKeepOpen = keepopen;
155 return (SOCK_OK);
156};
157
159 int crate_number, /* which crate to address */
160 struct request *myRequest, /* request to send to server */
161 struct request *serverReply /* reply from server */
162 )
163{
164 int nWrite;
165 int i, j;
166 int keepopen = gSocketKeepOpen;
167
168 /* printf("cfSockCliSend: gSocketKeepOpen=%d; keepopen=%d\n",
169 gSocketKeepOpen, keepopen);
170 */
171
172 gSocketKeepOpen = 0;
173 /*
174 buildRequestInteractive(myRequest);
175 printf("\n");
176 printf("your command type will be: %d \n",ntohl(myRequest->command_type));
177 printf("your command will be: %d \n",ntohl(myRequest->command));
178 printf("your magic_cookie will be: %d \n",ntohl(myRequest->magic_cookie));
179 printf("your request param_1 will be: %d \n",ntohl(myRequest->par1));
180 printf("your request param_2 will be: %d \n",ntohl(myRequest->par2));
181 */
182
183 /* send request to server */
184 nWrite = send (gSocketFd, (char *) myRequest, sizeof (*myRequest), 0);
185 if ( nWrite != sizeof(*myRequest) )
186 {
187 printf ( "cfSockCli WARN: nWrite = %d, sizeof (*myRequest) = %d\n",
188 nWrite, sizeof (*myRequest) );
189 perror ("Connection aborted on error");
190 close(gSocketFd);
191 return (SOCK_ERROR);
192 }
193
194 if (myRequest->reply) /* if expecting reply, read and display it */
195 {
196 i = recv ( gSocketFd, (char *) serverReply, sizeof(*serverReply), 0 );
197
198 if ( i == SOCK_ERROR )
199 {
200 perror ("Error reading result\n");
201 return (SOCK_ERROR);
202 }
203 /* The reason this while loop exists is that there
204 * is a remote possibility of the above recv returning
205 * less than sizeof(*serverReply) bytes. This is because a recv returns
206 * as soon as there is some data, and will not wait for
207 * all of the requested data to arrive. Since sizeof(*serverReply) bytes
208 * is relatively small compared to the allowed TCP
209 * packet sizes, a partial receive is unlikely. If
210 * the reply size were 2048 bytes instead,
211 * a partial receive would be far more likely.
212 * This loop will keep receiving until all sizeof(*serverReply) bytes
213 * have been received, thus guaranteeing that the
214 * next recv at the top of the loop will start at
215 * the beginning of the next reply.
216 */
217 while ( i < sizeof(*serverReply) ) {
218 j = recv( gSocketFd, ((char *) serverReply)+i, sizeof(*serverReply)-i, 0 );
219 if ( j == SOCK_ERROR )
220 {
221 perror ("Error reading result\n");
222 close(gSocketFd);
223 return (SOCK_ERROR);
224 }
225 i += j;
226 }
227 // handleReplyInteractive(serverReply);
228 }
229 gSocketKeepOpen = keepopen;
230 /* printf("cfSockCliSend: End; gSocketKeepOpen=%d\n", gSocketKeepOpen); */
231 return (SOCK_OK);
232};
233
235{
236 gSocketKeepOpen = 0;
237 close (gSocketFd);
238 /* printf("cfSockCliClose: gSocketKeepOpen=%d\n", gSocketKeepOpen); */
239 return (SOCK_OK);
240}
241
242/*
243
244 main (int argc, char *argv[])
245 {
246 long command = 1, par1 = 1, par2 = 1;
247 char *reply="Y";
248 char *msg = "one";
249 cfSockCommand(Crate_CountingHouse,command,par1,par2,reply,msg);
250 printf ("MESSAGE FROM SERVER: <%s> \n", msg);
251 printf("Server reply command: %d \n",command);
252 printf("Server reply param_1: %d \n",par1);
253 printf("Server reply param_2: %d \n\n",par2);
254 }
255 main (int argc, char *argv[])
256 {
257 struct request myRequest; // request to send to server
258 struct request serverReply; // reply from server
259
260
261 // cfSockCli("129.57.164.13");
262 buildRequestInteractive(&myRequest);
263 printf("\n");
264 printf("your command will be: %d \n",ntohl(myRequest.command));
265 printf("your magic_cookie will be: %d \n",ntohl(myRequest.magic_cookie));
266 printf("your request param_1 will be: %d \n",ntohl(myRequest.par1));
267 printf("your request param_2 will be: %d \n",ntohl(myRequest.par2));
268
269 cfSockCli(Crate_CountingHouse, &myRequest,&serverReply);
270
271 if (myRequest.reply) // if expecting reply, read and display it
272 handleReplyInteractive(&serverReply);
273
274 }
275*/
276
277int cfSockCommand(int crate_number,
278 long command_type, long command,
279 long req_param, long req_param_2,
280 char *reply, char *msg )
281{
282 struct request myRequest; // request to send to server
283 struct request serverReply; // reply from server
284 int mlen;
285 int errFlag;
286 // char *reply = "Y";
287
288 myRequest.command_type = htonl(command_type);
289 myRequest.command = htonl(command);
290 myRequest.magic_cookie = htonl(MAGIC_COOKIE);
291 myRequest.par1 = htonl(req_param);
292 myRequest.par2 = htonl(req_param_2);
293 switch (*reply)
294 {
295 case 'y':
296 case 'Y': myRequest.reply = TRUE;
297 break;
298 default: myRequest.reply = FALSE;
299 break;
300 }
301 mlen = strlen(msg);
302 strncpy(myRequest.message,msg,REQUEST_MSG_SIZE);
303 myRequest.msgLen = htonl(mlen);
304 myRequest.message[mlen] = '\0';
305
306 // printf("\n");
307 // printf ("Message to send: <%s> \n",myRequest.message);
308 // printf("your command type will be: %d \n",ntohl(myRequest.command_type));
309 // printf("your command will be: %d \n",ntohl(myRequest.command));
310 // printf("your request param_1 will be: %d \n",ntohl(myRequest.par1));
311 // printf("your request param_2 will be: %d \n",ntohl(myRequest.par2));
312 // printf("Requested reply? : %s \n",reply);
313 // printf("\n");
314
315 errFlag = cfSockCli(crate_number,&myRequest,&serverReply);
316 if (errFlag != SOCK_OK) return errFlag;
317
318 // if expecting reply, read and display it
319 if (myRequest.reply) {
320 msg = strcpy(serverReply.message,serverReply.message);
321 command_type = htonl(serverReply.command_type);
322 command = htonl(serverReply.command);
323 req_param = htonl(serverReply.par1);
324 req_param_2 = htonl(serverReply.par2);
325 // printf ("MESSAGE FROM SERVER:\n%s\n", msg);
326 // printf("Server reply command: %d \n",command);
327 // printf("Server reply param_1: %d \n",req_param);
328 // printf("Server reply param_2: %d \n\n",req_param_2);
329 // handleReplyInteractive(&serverReply);
330 }
331 return errFlag;
332}
333
334int GreenSockCommand(int crate_number, struct greenRequest *gRequest)
335{
336 struct request myRequest; // request to send to server
337 struct request serverReply; // reply from server
338 // char *reply;
339 int mlen;
340 int errFlag;
341
342 // char *reply = "Y";
343
344 myRequest.command_type = htonl(gRequest->command_type);
345 myRequest.command = htonl(gRequest->command);
346 myRequest.magic_cookie = htonl(MAGIC_COOKIE);
347 myRequest.par1 = htonl(gRequest->par1);
348 myRequest.par2 = htonl(gRequest->par2);
349
350 switch (*(gRequest->reply))
351 {
352 case 'y':
353 case 'Y': myRequest.reply = TRUE;
354 break;
355 default: myRequest.reply = FALSE;
356 break;
357 }
358 mlen = strlen(gRequest->message);
359 strncpy(myRequest.message,gRequest->message,REQUEST_MSG_SIZE);
360 myRequest.msgLen = htonl(mlen);
361 myRequest.message[mlen] = '\0';
362
363 // printf("\n");
364 // printf ("Message to send: <%s> \n",myRequest.message);
365 // printf("your command will be: %d \n",ntohl(myRequest.command));
366 // printf("your request param_1 will be: %d \n",ntohl(myRequest.par1));
367 // printf("your request param_2 will be: %d \n",ntohl(myRequest.par2));
368 // printf("Requested reply? : %s \n",gRequest->reply);
369 // printf("\n");
370
371 errFlag = cfSockCli(crate_number, &myRequest,&serverReply);
372
373 if (errFlag != SOCK_OK) return errFlag;
374
375 // if expecting reply, read and display it
376 if (myRequest.reply) {
377 // gRequest->message = strcpy(serverReply.message,serverReply.message);
378 strcpy(gRequest->message,serverReply.message);
379 gRequest->command_type = htonl(serverReply.command_type);
380 gRequest->command = htonl(serverReply.command);
381 gRequest->par1 = htonl(serverReply.par1);
382 gRequest->par2 = htonl(serverReply.par2);
383
384 // printf ("cfSockCli: MESSAGE FROM SERVER: %s\n", gRequest->message);
385 // printf("Server reply command: %d \n",gRequest->command);
386 // printf("Server reply param_1: %d \n",gRequest->req_param);
387 // printf("Server reply param_2: %d \n\n",gRequest->req_param_2);
388 // handleReplyInteractive(&serverReply);
389 }
390 return errFlag;
391}
392
393
394void buildRequestInteractive(struct request *myRequest) {
395 /* build request, prompting user for message */
396 int mlen; /* length of message */
397 char reply; /* if TRUE, expect reply back */
398
399 printf ("Message to send: \n");
400 mlen = read (STD_IN, myRequest->message, REQUEST_MSG_SIZE);
401 myRequest->msgLen = htonl(mlen);
402 myRequest->message[mlen - 1] = '\0';
403
404 myRequest->command_type = htonl(1);
405 printf("your command type will be: %d \n",ntohl(myRequest->command_type));
406 myRequest->command = htonl(10);
407 printf("your command will be: %d \n",ntohl(myRequest->command));
408 myRequest->magic_cookie = htonl(MAGIC_COOKIE);
409 printf("your magic_cookie will be: %d \n",ntohl(myRequest->magic_cookie));
410 myRequest->par1 = htonl(1);
411 printf("your request param_1 will be: %d \n",ntohl(myRequest->par1));
412 myRequest->par2 = htonl(2);
413 printf("your request param_2 will be: %d \n",ntohl(myRequest->par2));
414
415
416 printf ("Would you like a reply (Y or N): \n");
417 read (STD_IN, &reply, 1);
418 switch (reply)
419 {
420 case 'y':
421 case 'Y': myRequest->reply = TRUE;
422 break;
423 default: myRequest->reply = FALSE;
424 break;
425 }
426}
427
428void handleReplyInteractive(struct request *serverReply) {
429 /* build request, prompting user for message */
430 printf ("MESSAGE FROM SERVER:\n%s\n", serverReply->message);
431 printf("Server reply command type: %d \n",ntohl(serverReply->command_type));
432 printf("Server reply command: %d \n",ntohl(serverReply->command));
433 printf("Server reply param_1: %d \n",ntohl(serverReply->par1));
434 printf("Server reply param_2: %d \n",ntohl(serverReply->par2));
435 printf("Server reply msgLen: %d \n",ntohl(serverReply->msgLen));
436}
437
438
439
440
441
442
443
#define REQUEST_MSG_SIZE
Definition cfSock.h:9
#define SOCK_OK
Definition cfSock.h:16
#define SOCK_ERROR
Definition cfSock.h:13
#define SERVER_PORT_NUM
Definition cfSock.h:4
#define MAGIC_COOKIE
Definition cfSock.h:18
char * ServerName_QwTSCrate
Definition cfSockCli.h:27
char * ServerName_RightSpect
Definition cfSockCli.h:25
char * ServerName_Injector
Definition cfSockCli.h:26
#define STD_IN
Definition cfSockCli.h:13
#define FALSE
Definition cfSockCli.h:5
char * ServerName_CountingHouse
Definition cfSockCli.h:21
char * ServerName_LeftSpect
Definition cfSockCli.h:22
#define Crate_Injector
Definition GreenSock.h:25
#define Crate_RightSpect
Definition GreenSock.h:22
#define Crate_CountingHouse
Definition GreenSock.h:16
#define Crate_LeftSpect
Definition GreenSock.h:19
#define Crate_Qwvmets
Definition GreenSock.h:28
static int gSocketKeepOpen
Definition cfSockCli.c:25
void buildRequestInteractive(struct request *myRequest)
Definition cfSockCli.c:394
int cfSockCommand(int crate_number, long command_type, long command, long req_param, long req_param_2, char *reply, char *msg)
Definition cfSockCli.c:277
int GreenSockCommand(int crate_number, struct greenRequest *gRequest)
Definition cfSockCli.c:334
static int gSocketFd
Definition cfSockCli.c:24
void handleReplyInteractive(struct request *serverReply)
Definition cfSockCli.c:428
int cfSockCliOpen(int crate_number, int keepopen)
Definition cfSockCli.c:84
int cfSockCli(int crate_number, struct request *myRequest, struct request *serverReply)
Definition cfSockCli.c:58
int cfSockCliSend(int crate_number, struct request *myRequest, struct request *serverReply)
Definition cfSockCli.c:158
int cfSockCliClose()
Definition cfSockCli.c:234
long command_type
Definition cfSock.h:28
long par1
Definition cfSock.h:30
long reply
Definition cfSock.h:32
long par2
Definition cfSock.h:31
long msgLen
Definition cfSock.h:34
long command
Definition cfSock.h:29
char message[REQUEST_MSG_SIZE]
Definition cfSock.h:33
long magic_cookie
Definition cfSock.h:27
char message[GREEN_REQUEST_MSG_SIZE]
Definition GreenSock.h:37
long command_type
Definition GreenSock.h:32
char * reply
Definition GreenSock.h:36