comparison core/client.cpp @ 24:ad5cd8dd99b3 bbb_network

UDP communication in place, pre-alpha
author Giulio Moro <giuliomoro@yahoo.it>
date Fri, 08 May 2015 11:12:13 +0100
parents
children 98aed580452a
comparison
equal deleted inserted replaced
23:182ae9367104 24:ad5cd8dd99b3
1 ///* UDP client in the internet domain */
2
3 #include <ctype.h>
4 #include <sys/time.h>
5 #include <fcntl.h>
6 #include "../include/client.h"
7 #include <unistd.h>
8 #include <rtdk.h>
9
10 #define BUFF_LEN 1024
11 #define MAX_VAR_STRING 10
12
13 struct sockaddr_in outServer, inServer;
14 int outSock, inSock, n, length;
15 socklen_t fromlen;
16 struct sockaddr_in from;
17 char inBuffer[1024];
18 char variableString[MAX_VAR_STRING];
19
20 int setupSockets(int receivePort, int transmitPort, char const*serverName){
21 //setup transmitter
22 printf("receivePort: %d; transmitPort: %d; serverName: %s\n",receivePort, transmitPort, serverName);
23 outSock= socket(AF_INET, SOCK_DGRAM, 0);
24 outServer.sin_port = htons(transmitPort);
25 if (outSock < 0){
26 error("Opening out socket");
27 return -1;
28 }
29 outServer.sin_family = AF_INET;
30 inet_pton(AF_INET,serverName,&outServer.sin_addr);
31
32 //setup receiver
33 inSock=socket(AF_INET, SOCK_DGRAM, 0);
34 if (inSock < 0){
35 return -1;
36 error("Opening in socket");
37 }
38 length = sizeof(inServer);
39 inServer.sin_family=AF_INET;
40 inServer.sin_addr.s_addr=INADDR_ANY;
41 inServer.sin_port=htons(receivePort);
42 if (bind(inSock,(struct sockaddr *)&inServer,length)<0)
43 error("binding");
44 fromlen = sizeof(struct sockaddr_in);
45 return 0;
46 }
47 int sendMessage(networkData message)
48 {
49 unsigned int length;
50 char buffer[BUFF_LEN];
51 length=sizeof(struct sockaddr_in);
52 int k=0;
53 k=sprintf(buffer+k, "%8d;",*message.counter);
54 for(int j=0; j<message.numVariables; j++){
55 k+=sprintf(buffer+k, "%.3f;",*message.variables[j]);
56 if(k>BUFF_LEN - 20) //safety margin
57 continue;
58 }
59 sprintf(buffer+k,"\n");
60 // printf(buffer);
61 n=sendto(outSock,buffer,
62 strlen(buffer),0,(const struct sockaddr *)&outServer,length);
63 if (n < 0) error("Sendto");
64 return 0;
65 }
66 /*
67 int receiveMessage()
68 {
69 int n = recvfrom(inSock,inBuffer,1024,0,(struct sockaddr *)&from,&fromlen);
70 if (n < 0) error("recvfrom");
71 printf("Received a datagram: ");
72 printf(inBuffer);
73 }
74 */
75
76
77 int receiveMessage(networkData message){
78 struct timeval stTimeOut;
79 fd_set stReadFDS;
80 FD_ZERO(&stReadFDS);
81 // Timeout of one second
82 stTimeOut.tv_sec = 0;
83 stTimeOut.tv_usec = 1000;
84 FD_SET(inSock, &stReadFDS);
85
86 int t = select(inSock+1, &stReadFDS, NULL, NULL, &stTimeOut);
87 if (t == -1) {
88 rt_fprintf(stderr, "Call to select() failed");
89 return -1;
90 }
91 else if (t != 0) {
92 if (FD_ISSET(inSock, &stReadFDS)) {
93 // printf("There is data pending to be read..."); // Read data with recv()
94 int n = recvfrom(inSock,inBuffer,1024,0,(struct sockaddr *)&from,&fromlen);
95 if (n < 0){
96 rt_fprintf(stderr,"Error while receiving");
97 return -1;
98 }
99 printf("Received a datagram: ");
100 printf(inBuffer);
101 //the worst parser ever
102 int previousN=0;
103 int currentVariable=0;
104 for(int n=0; inBuffer[n]!=0 && currentVariable<message.numVariables && n-previousN<MAX_VAR_STRING;n++){ //scan the string
105 if(inBuffer[n]==';'||n==0){ // if you find a separator or you are at the end of the string, parse the variable
106 int j=0;
107 inBuffer[n]=0; //set the semicolon to 0 ...
108 while( (variableString[j++]=inBuffer[previousN++]) ); // ... so that this will stop when it gets there
109 rt_printf("variable %d: %s\n", currentVariable, variableString);
110 *(message.variables[currentVariable])=atof(variableString);
111 n++; //increment to step after the semicolon
112 previousN=n;
113 currentVariable++;
114 }
115 }
116 }
117 }
118 return 0;
119 }
120
121 void closeSockets(){
122 close(outSock);
123 close(inSock);
124 }
125 void error(const char *msg)
126 {
127 perror(msg);
128 exit(0);
129 }