JAPAn
Just Another Parity Analyzer
Loading...
Searching...
No Matches
cfSockCli.c File Reference
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <string.h>
#include "cfSockCli.h"
+ Include dependency graph for cfSockCli.c:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int cfSockCliOpen (int crate_number, int keepopen)
 
int cfSockCliSend (int crate_number, struct request *myRequest, struct request *serverReply)
 
int cfSockCliClose ()
 
int cfSockCli (int crate_number, struct request *myRequest, struct request *serverReply)
 
int cfSockCommand (int crate_number, long command_type, long command, long req_param, long req_param_2, char *reply, char *msg)
 
int GreenSockCommand (int crate_number, struct greenRequest *gRequest)
 
void buildRequestInteractive (struct request *myRequest)
 
void handleReplyInteractive (struct request *serverReply)
 

Variables

static int gSocketFd
 
static int gSocketKeepOpen = 0
 

Function Documentation

◆ buildRequestInteractive()

void buildRequestInteractive ( struct request * myRequest)

Definition at line 394 of file cfSockCli.c.

394 {
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}
#define REQUEST_MSG_SIZE
Definition cfSock.h:9
#define MAGIC_COOKIE
Definition cfSock.h:18
#define STD_IN
Definition cfSockCli.h:13
#define FALSE
Definition cfSockCli.h:5
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

References request::command, request::command_type, FALSE, MAGIC_COOKIE, request::magic_cookie, request::message, request::msgLen, request::par1, request::par2, request::reply, REQUEST_MSG_SIZE, and STD_IN.

◆ cfSockCli()

int cfSockCli ( int crate_number,
struct request * myRequest,
struct request * serverReply )

Definition at line 58 of file cfSockCli.c.

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}
#define SOCK_OK
Definition cfSock.h:16
int cfSockCliOpen(int crate_number, int keepopen)
Definition cfSockCli.c:84
int cfSockCliSend(int crate_number, struct request *myRequest, struct request *serverReply)
Definition cfSockCli.c:158
int cfSockCliClose()
Definition cfSockCli.c:234

References cfSockCliClose(), cfSockCliOpen(), cfSockCliSend(), and SOCK_OK.

Referenced by cfSockCommand(), and GreenSockCommand().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ cfSockCliClose()

int cfSockCliClose ( )

Definition at line 234 of file cfSockCli.c.

235{
236 gSocketKeepOpen = 0;
237 close (gSocketFd);
238 /* printf("cfSockCliClose: gSocketKeepOpen=%d\n", gSocketKeepOpen); */
239 return (SOCK_OK);
240}
static int gSocketKeepOpen
Definition cfSockCli.c:25
static int gSocketFd
Definition cfSockCli.c:24

References gSocketFd, gSocketKeepOpen, and SOCK_OK.

Referenced by cfSockCli().

+ Here is the caller graph for this function:

◆ cfSockCliOpen()

int cfSockCliOpen ( int crate_number,
int keepopen )

Definition at line 84 of file cfSockCli.c.

84 {
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};
#define SOCK_ERROR
Definition cfSock.h:13
#define SERVER_PORT_NUM
Definition cfSock.h:4
char * ServerName_QwTSCrate
Definition cfSockCli.h:27
char * ServerName_RightSpect
Definition cfSockCli.h:25
char * ServerName_Injector
Definition cfSockCli.h:26
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

References Crate_CountingHouse, Crate_Injector, Crate_LeftSpect, Crate_Qwvmets, Crate_RightSpect, gSocketFd, gSocketKeepOpen, SERVER_PORT_NUM, ServerName_CountingHouse, ServerName_Injector, ServerName_LeftSpect, ServerName_QwTSCrate, ServerName_RightSpect, SOCK_ERROR, and SOCK_OK.

Referenced by cfSockCli().

+ Here is the caller graph for this function:

◆ cfSockCliSend()

int cfSockCliSend ( int crate_number,
struct request * myRequest,
struct request * serverReply )

Definition at line 158 of file cfSockCli.c.

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};

References gSocketFd, gSocketKeepOpen, request::reply, SOCK_ERROR, and SOCK_OK.

Referenced by cfSockCli().

+ Here is the caller graph for this function:

◆ cfSockCommand()

int cfSockCommand ( int crate_number,
long command_type,
long command,
long req_param,
long req_param_2,
char * reply,
char * msg )

Definition at line 277 of file cfSockCli.c.

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}
int cfSockCli(int crate_number, struct request *myRequest, struct request *serverReply)
Definition cfSockCli.c:58

References cfSockCli(), request::command, request::command_type, FALSE, MAGIC_COOKIE, request::magic_cookie, request::message, request::msgLen, request::par1, request::par2, request::reply, REQUEST_MSG_SIZE, and SOCK_OK.

+ Here is the call graph for this function:

◆ GreenSockCommand()

int GreenSockCommand ( int crate_number,
struct greenRequest * gRequest )

Definition at line 334 of file cfSockCli.c.

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}
char message[GREEN_REQUEST_MSG_SIZE]
Definition GreenSock.h:37
long command_type
Definition GreenSock.h:32
char * reply
Definition GreenSock.h:36

References cfSockCli(), greenRequest::command, request::command, greenRequest::command_type, request::command_type, FALSE, MAGIC_COOKIE, request::magic_cookie, greenRequest::message, request::message, request::msgLen, greenRequest::par1, request::par1, greenRequest::par2, request::par2, greenRequest::reply, request::reply, REQUEST_MSG_SIZE, and SOCK_OK.

Referenced by GreenMonster::SCNCheckStatus(), GreenMonster::SCNCheckValues(), GreenMonster::SCNSetStatus(), and GreenMonster::SCNSetValue().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handleReplyInteractive()

void handleReplyInteractive ( struct request * serverReply)

Definition at line 428 of file cfSockCli.c.

428 {
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}

References request::command, request::command_type, request::message, request::msgLen, request::par1, and request::par2.

Variable Documentation

◆ gSocketFd

int gSocketFd
static

Definition at line 24 of file cfSockCli.c.

Referenced by cfSockCliClose(), cfSockCliOpen(), and cfSockCliSend().

◆ gSocketKeepOpen

int gSocketKeepOpen = 0
static

Definition at line 25 of file cfSockCli.c.

Referenced by cfSockCliClose(), cfSockCliOpen(), and cfSockCliSend().