annotate core/client.cpp @ 72:d837fb676977

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